WebSocketWriter.smali 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. .class final Lokhttp3/internal/ws/WebSocketWriter;
  2. .super Ljava/lang/Object;
  3. .source ""
  4. # annotations
  5. .annotation system Ldalvik/annotation/MemberClasses;
  6. value = {
  7. Lokhttp3/internal/ws/WebSocketWriter$FrameSink;
  8. }
  9. .end annotation
  10. # instance fields
  11. .field activeWriter:Z
  12. .field final buffer:Lokio/Buffer;
  13. .field final frameSink:Lokhttp3/internal/ws/WebSocketWriter$FrameSink;
  14. .field final isClient:Z
  15. .field final maskBuffer:[B
  16. .field final maskKey:[B
  17. .field final random:Ljava/util/Random;
  18. .field final sink:Lokio/BufferedSink;
  19. .field writerClosed:Z
  20. # direct methods
  21. .method constructor <init>(ZLokio/BufferedSink;Ljava/util/Random;)V
  22. .locals 1
  23. invoke-direct {p0}, Ljava/lang/Object;-><init>()V
  24. new-instance v0, Lokio/Buffer;
  25. invoke-direct {v0}, Lokio/Buffer;-><init>()V
  26. iput-object v0, p0, Lokhttp3/internal/ws/WebSocketWriter;->buffer:Lokio/Buffer;
  27. new-instance v0, Lokhttp3/internal/ws/WebSocketWriter$FrameSink;
  28. invoke-direct {v0, p0}, Lokhttp3/internal/ws/WebSocketWriter$FrameSink;-><init>(Lokhttp3/internal/ws/WebSocketWriter;)V
  29. iput-object v0, p0, Lokhttp3/internal/ws/WebSocketWriter;->frameSink:Lokhttp3/internal/ws/WebSocketWriter$FrameSink;
  30. if-eqz p2, :cond_3
  31. if-eqz p3, :cond_2
  32. iput-boolean p1, p0, Lokhttp3/internal/ws/WebSocketWriter;->isClient:Z
  33. iput-object p2, p0, Lokhttp3/internal/ws/WebSocketWriter;->sink:Lokio/BufferedSink;
  34. iput-object p3, p0, Lokhttp3/internal/ws/WebSocketWriter;->random:Ljava/util/Random;
  35. const/4 p2, 0x0
  36. if-eqz p1, :cond_0
  37. const/4 p3, 0x4
  38. new-array p3, p3, [B
  39. goto :goto_0
  40. :cond_0
  41. move-object p3, p2
  42. :goto_0
  43. iput-object p3, p0, Lokhttp3/internal/ws/WebSocketWriter;->maskKey:[B
  44. if-eqz p1, :cond_1
  45. const/16 p1, 0x2000
  46. new-array p2, p1, [B
  47. :cond_1
  48. iput-object p2, p0, Lokhttp3/internal/ws/WebSocketWriter;->maskBuffer:[B
  49. return-void
  50. :cond_2
  51. new-instance p1, Ljava/lang/NullPointerException;
  52. const-string p2, "random == null"
  53. invoke-direct {p1, p2}, Ljava/lang/NullPointerException;-><init>(Ljava/lang/String;)V
  54. throw p1
  55. :cond_3
  56. new-instance p1, Ljava/lang/NullPointerException;
  57. const-string p2, "sink == null"
  58. invoke-direct {p1, p2}, Ljava/lang/NullPointerException;-><init>(Ljava/lang/String;)V
  59. throw p1
  60. .end method
  61. .method private writeControlFrame(ILokio/ByteString;)V
  62. .locals 7
  63. .annotation system Ldalvik/annotation/Throws;
  64. value = {
  65. Ljava/io/IOException;
  66. }
  67. .end annotation
  68. iget-boolean v0, p0, Lokhttp3/internal/ws/WebSocketWriter;->writerClosed:Z
  69. if-nez v0, :cond_2
  70. invoke-virtual {p2}, Lokio/ByteString;->size()I
  71. move-result v0
  72. int-to-long v1, v0
  73. const-wide/16 v3, 0x7d
  74. cmp-long v5, v1, v3
  75. if-gtz v5, :cond_1
  76. or-int/lit16 p1, p1, 0x80
  77. iget-object v1, p0, Lokhttp3/internal/ws/WebSocketWriter;->sink:Lokio/BufferedSink;
  78. invoke-interface {v1, p1}, Lokio/BufferedSink;->writeByte(I)Lokio/BufferedSink;
  79. iget-boolean p1, p0, Lokhttp3/internal/ws/WebSocketWriter;->isClient:Z
  80. if-eqz p1, :cond_0
  81. or-int/lit16 p1, v0, 0x80
  82. iget-object v0, p0, Lokhttp3/internal/ws/WebSocketWriter;->sink:Lokio/BufferedSink;
  83. invoke-interface {v0, p1}, Lokio/BufferedSink;->writeByte(I)Lokio/BufferedSink;
  84. iget-object p1, p0, Lokhttp3/internal/ws/WebSocketWriter;->random:Ljava/util/Random;
  85. iget-object v0, p0, Lokhttp3/internal/ws/WebSocketWriter;->maskKey:[B
  86. invoke-virtual {p1, v0}, Ljava/util/Random;->nextBytes([B)V
  87. iget-object p1, p0, Lokhttp3/internal/ws/WebSocketWriter;->sink:Lokio/BufferedSink;
  88. iget-object v0, p0, Lokhttp3/internal/ws/WebSocketWriter;->maskKey:[B
  89. invoke-interface {p1, v0}, Lokio/BufferedSink;->write([B)Lokio/BufferedSink;
  90. invoke-virtual {p2}, Lokio/ByteString;->toByteArray()[B
  91. move-result-object p1
  92. array-length p2, p1
  93. int-to-long v2, p2
  94. iget-object v4, p0, Lokhttp3/internal/ws/WebSocketWriter;->maskKey:[B
  95. const-wide/16 v5, 0x0
  96. move-object v1, p1
  97. invoke-static/range {v1 .. v6}, Lokhttp3/internal/ws/WebSocketProtocol;->toggleMask([BJ[BJ)V
  98. iget-object p2, p0, Lokhttp3/internal/ws/WebSocketWriter;->sink:Lokio/BufferedSink;
  99. invoke-interface {p2, p1}, Lokio/BufferedSink;->write([B)Lokio/BufferedSink;
  100. goto :goto_0
  101. :cond_0
  102. iget-object p1, p0, Lokhttp3/internal/ws/WebSocketWriter;->sink:Lokio/BufferedSink;
  103. invoke-interface {p1, v0}, Lokio/BufferedSink;->writeByte(I)Lokio/BufferedSink;
  104. iget-object p1, p0, Lokhttp3/internal/ws/WebSocketWriter;->sink:Lokio/BufferedSink;
  105. invoke-interface {p1, p2}, Lokio/BufferedSink;->write(Lokio/ByteString;)Lokio/BufferedSink;
  106. :goto_0
  107. iget-object p1, p0, Lokhttp3/internal/ws/WebSocketWriter;->sink:Lokio/BufferedSink;
  108. invoke-interface {p1}, Lokio/BufferedSink;->flush()V
  109. return-void
  110. :cond_1
  111. new-instance p1, Ljava/lang/IllegalArgumentException;
  112. const-string p2, "Payload size must be less than or equal to 125"
  113. invoke-direct {p1, p2}, Ljava/lang/IllegalArgumentException;-><init>(Ljava/lang/String;)V
  114. throw p1
  115. :cond_2
  116. new-instance p1, Ljava/io/IOException;
  117. const-string p2, "closed"
  118. invoke-direct {p1, p2}, Ljava/io/IOException;-><init>(Ljava/lang/String;)V
  119. throw p1
  120. .end method
  121. # virtual methods
  122. .method newMessageSink(IJ)Lokio/Sink;
  123. .locals 2
  124. iget-boolean v0, p0, Lokhttp3/internal/ws/WebSocketWriter;->activeWriter:Z
  125. if-nez v0, :cond_0
  126. const/4 v0, 0x1
  127. iput-boolean v0, p0, Lokhttp3/internal/ws/WebSocketWriter;->activeWriter:Z
  128. iget-object v1, p0, Lokhttp3/internal/ws/WebSocketWriter;->frameSink:Lokhttp3/internal/ws/WebSocketWriter$FrameSink;
  129. iput p1, v1, Lokhttp3/internal/ws/WebSocketWriter$FrameSink;->formatOpcode:I
  130. iput-wide p2, v1, Lokhttp3/internal/ws/WebSocketWriter$FrameSink;->contentLength:J
  131. iput-boolean v0, v1, Lokhttp3/internal/ws/WebSocketWriter$FrameSink;->isFirstFrame:Z
  132. const/4 p1, 0x0
  133. iput-boolean p1, v1, Lokhttp3/internal/ws/WebSocketWriter$FrameSink;->closed:Z
  134. return-object v1
  135. :cond_0
  136. new-instance p1, Ljava/lang/IllegalStateException;
  137. const-string p2, "Another message writer is active. Did you call close()?"
  138. invoke-direct {p1, p2}, Ljava/lang/IllegalStateException;-><init>(Ljava/lang/String;)V
  139. throw p1
  140. .end method
  141. .method writeClose(ILokio/ByteString;)V
  142. .locals 1
  143. .annotation system Ldalvik/annotation/Throws;
  144. value = {
  145. Ljava/io/IOException;
  146. }
  147. .end annotation
  148. sget-object v0, Lokio/ByteString;->EMPTY:Lokio/ByteString;
  149. if-nez p1, :cond_0
  150. if-eqz p2, :cond_3
  151. :cond_0
  152. if-eqz p1, :cond_1
  153. invoke-static {p1}, Lokhttp3/internal/ws/WebSocketProtocol;->validateCloseCode(I)V
  154. :cond_1
  155. new-instance v0, Lokio/Buffer;
  156. invoke-direct {v0}, Lokio/Buffer;-><init>()V
  157. invoke-virtual {v0, p1}, Lokio/Buffer;->writeShort(I)Lokio/Buffer;
  158. if-eqz p2, :cond_2
  159. invoke-virtual {v0, p2}, Lokio/Buffer;->write(Lokio/ByteString;)Lokio/Buffer;
  160. :cond_2
  161. invoke-virtual {v0}, Lokio/Buffer;->readByteString()Lokio/ByteString;
  162. move-result-object v0
  163. :cond_3
  164. const/16 p1, 0x8
  165. const/4 p2, 0x1
  166. :try_start_0
  167. invoke-direct {p0, p1, v0}, Lokhttp3/internal/ws/WebSocketWriter;->writeControlFrame(ILokio/ByteString;)V
  168. :try_end_0
  169. .catchall {:try_start_0 .. :try_end_0} :catchall_0
  170. iput-boolean p2, p0, Lokhttp3/internal/ws/WebSocketWriter;->writerClosed:Z
  171. return-void
  172. :catchall_0
  173. move-exception p1
  174. iput-boolean p2, p0, Lokhttp3/internal/ws/WebSocketWriter;->writerClosed:Z
  175. throw p1
  176. .end method
  177. .method writeMessageFrame(IJZZ)V
  178. .locals 9
  179. .annotation system Ldalvik/annotation/Throws;
  180. value = {
  181. Ljava/io/IOException;
  182. }
  183. .end annotation
  184. iget-boolean v0, p0, Lokhttp3/internal/ws/WebSocketWriter;->writerClosed:Z
  185. if-nez v0, :cond_8
  186. const/4 v0, 0x0
  187. if-eqz p4, :cond_0
  188. goto :goto_0
  189. :cond_0
  190. const/4 p1, 0x0
  191. :goto_0
  192. if-eqz p5, :cond_1
  193. or-int/lit16 p1, p1, 0x80
  194. :cond_1
  195. iget-object p4, p0, Lokhttp3/internal/ws/WebSocketWriter;->sink:Lokio/BufferedSink;
  196. invoke-interface {p4, p1}, Lokio/BufferedSink;->writeByte(I)Lokio/BufferedSink;
  197. iget-boolean p1, p0, Lokhttp3/internal/ws/WebSocketWriter;->isClient:Z
  198. if-eqz p1, :cond_2
  199. const/16 p1, 0x80
  200. goto :goto_1
  201. :cond_2
  202. const/4 p1, 0x0
  203. :goto_1
  204. const-wide/16 p4, 0x7d
  205. cmp-long v1, p2, p4
  206. if-gtz v1, :cond_3
  207. long-to-int p4, p2
  208. or-int/2addr p1, p4
  209. iget-object p4, p0, Lokhttp3/internal/ws/WebSocketWriter;->sink:Lokio/BufferedSink;
  210. invoke-interface {p4, p1}, Lokio/BufferedSink;->writeByte(I)Lokio/BufferedSink;
  211. goto :goto_2
  212. :cond_3
  213. const-wide/32 p4, 0xffff
  214. cmp-long v1, p2, p4
  215. if-gtz v1, :cond_4
  216. or-int/lit8 p1, p1, 0x7e
  217. iget-object p4, p0, Lokhttp3/internal/ws/WebSocketWriter;->sink:Lokio/BufferedSink;
  218. invoke-interface {p4, p1}, Lokio/BufferedSink;->writeByte(I)Lokio/BufferedSink;
  219. iget-object p1, p0, Lokhttp3/internal/ws/WebSocketWriter;->sink:Lokio/BufferedSink;
  220. long-to-int p4, p2
  221. invoke-interface {p1, p4}, Lokio/BufferedSink;->writeShort(I)Lokio/BufferedSink;
  222. goto :goto_2
  223. :cond_4
  224. or-int/lit8 p1, p1, 0x7f
  225. iget-object p4, p0, Lokhttp3/internal/ws/WebSocketWriter;->sink:Lokio/BufferedSink;
  226. invoke-interface {p4, p1}, Lokio/BufferedSink;->writeByte(I)Lokio/BufferedSink;
  227. iget-object p1, p0, Lokhttp3/internal/ws/WebSocketWriter;->sink:Lokio/BufferedSink;
  228. invoke-interface {p1, p2, p3}, Lokio/BufferedSink;->writeLong(J)Lokio/BufferedSink;
  229. :goto_2
  230. iget-boolean p1, p0, Lokhttp3/internal/ws/WebSocketWriter;->isClient:Z
  231. if-eqz p1, :cond_6
  232. iget-object p1, p0, Lokhttp3/internal/ws/WebSocketWriter;->random:Ljava/util/Random;
  233. iget-object p4, p0, Lokhttp3/internal/ws/WebSocketWriter;->maskKey:[B
  234. invoke-virtual {p1, p4}, Ljava/util/Random;->nextBytes([B)V
  235. iget-object p1, p0, Lokhttp3/internal/ws/WebSocketWriter;->sink:Lokio/BufferedSink;
  236. iget-object p4, p0, Lokhttp3/internal/ws/WebSocketWriter;->maskKey:[B
  237. invoke-interface {p1, p4}, Lokio/BufferedSink;->write([B)Lokio/BufferedSink;
  238. const-wide/16 p4, 0x0
  239. :goto_3
  240. cmp-long p1, p4, p2
  241. if-gez p1, :cond_7
  242. iget-object p1, p0, Lokhttp3/internal/ws/WebSocketWriter;->maskBuffer:[B
  243. array-length p1, p1
  244. int-to-long v1, p1
  245. invoke-static {p2, p3, v1, v2}, Ljava/lang/Math;->min(JJ)J
  246. move-result-wide v1
  247. long-to-int p1, v1
  248. iget-object v1, p0, Lokhttp3/internal/ws/WebSocketWriter;->buffer:Lokio/Buffer;
  249. iget-object v2, p0, Lokhttp3/internal/ws/WebSocketWriter;->maskBuffer:[B
  250. invoke-virtual {v1, v2, v0, p1}, Lokio/Buffer;->read([BII)I
  251. move-result p1
  252. const/4 v1, -0x1
  253. if-eq p1, v1, :cond_5
  254. iget-object v1, p0, Lokhttp3/internal/ws/WebSocketWriter;->maskBuffer:[B
  255. int-to-long v7, p1
  256. iget-object v4, p0, Lokhttp3/internal/ws/WebSocketWriter;->maskKey:[B
  257. move-wide v2, v7
  258. move-wide v5, p4
  259. invoke-static/range {v1 .. v6}, Lokhttp3/internal/ws/WebSocketProtocol;->toggleMask([BJ[BJ)V
  260. iget-object v1, p0, Lokhttp3/internal/ws/WebSocketWriter;->sink:Lokio/BufferedSink;
  261. iget-object v2, p0, Lokhttp3/internal/ws/WebSocketWriter;->maskBuffer:[B
  262. invoke-interface {v1, v2, v0, p1}, Lokio/BufferedSink;->write([BII)Lokio/BufferedSink;
  263. add-long/2addr p4, v7
  264. goto :goto_3
  265. :cond_5
  266. new-instance p1, Ljava/lang/AssertionError;
  267. invoke-direct {p1}, Ljava/lang/AssertionError;-><init>()V
  268. throw p1
  269. :cond_6
  270. iget-object p1, p0, Lokhttp3/internal/ws/WebSocketWriter;->sink:Lokio/BufferedSink;
  271. iget-object p4, p0, Lokhttp3/internal/ws/WebSocketWriter;->buffer:Lokio/Buffer;
  272. invoke-interface {p1, p4, p2, p3}, Lokio/Sink;->write(Lokio/Buffer;J)V
  273. :cond_7
  274. iget-object p1, p0, Lokhttp3/internal/ws/WebSocketWriter;->sink:Lokio/BufferedSink;
  275. invoke-interface {p1}, Lokio/BufferedSink;->emit()Lokio/BufferedSink;
  276. return-void
  277. :cond_8
  278. new-instance p1, Ljava/io/IOException;
  279. const-string p2, "closed"
  280. invoke-direct {p1, p2}, Ljava/io/IOException;-><init>(Ljava/lang/String;)V
  281. goto :goto_5
  282. :goto_4
  283. throw p1
  284. :goto_5
  285. goto :goto_4
  286. .end method
  287. .method writePing(Lokio/ByteString;)V
  288. .locals 1
  289. .annotation system Ldalvik/annotation/Throws;
  290. value = {
  291. Ljava/io/IOException;
  292. }
  293. .end annotation
  294. const/16 v0, 0x9
  295. invoke-direct {p0, v0, p1}, Lokhttp3/internal/ws/WebSocketWriter;->writeControlFrame(ILokio/ByteString;)V
  296. return-void
  297. .end method
  298. .method writePong(Lokio/ByteString;)V
  299. .locals 1
  300. .annotation system Ldalvik/annotation/Throws;
  301. value = {
  302. Ljava/io/IOException;
  303. }
  304. .end annotation
  305. const/16 v0, 0xa
  306. invoke-direct {p0, v0, p1}, Lokhttp3/internal/ws/WebSocketWriter;->writeControlFrame(ILokio/ByteString;)V
  307. return-void
  308. .end method