| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | using System.Diagnostics; |
| | 4 | | using System.Net.Sockets; |
| | 5 | | using System.Security.Cryptography; |
| | 6 | | using System.Text; |
| | 7 | |
|
| | 8 | | namespace Ice.Internal; |
| | 9 | |
|
| | 10 | | internal sealed class WSTransceiver : Transceiver |
| | 11 | | { |
| 0 | 12 | | public Socket fd() => _delegate.fd(); |
| | 13 | |
|
| | 14 | | public int initialize(Buffer readBuffer, Buffer writeBuffer, ref bool hasMoreData) |
| | 15 | | { |
| | 16 | | // |
| | 17 | | // Delegate logs exceptions that occur during initialize(), so there's no need to trap them here. |
| | 18 | | // |
| 1 | 19 | | if (_state == StateInitializeDelegate) |
| | 20 | | { |
| 1 | 21 | | int op = _delegate.initialize(readBuffer, writeBuffer, ref hasMoreData); |
| 1 | 22 | | if (op != 0) |
| | 23 | | { |
| 1 | 24 | | return op; |
| | 25 | | } |
| 1 | 26 | | _state = StateConnected; |
| | 27 | | } |
| | 28 | |
|
| | 29 | | try |
| | 30 | | { |
| 1 | 31 | | if (_state == StateConnected) |
| | 32 | | { |
| | 33 | | // |
| | 34 | | // We don't know how much we'll need to read. |
| | 35 | | // |
| 1 | 36 | | _readBuffer.resize(1024, true); |
| 1 | 37 | | _readBuffer.b.position(0); |
| 1 | 38 | | _readBufferPos = 0; |
| | 39 | |
|
| | 40 | | // |
| | 41 | | // The server waits for the client's upgrade request, the |
| | 42 | | // client sends the upgrade request. |
| | 43 | | // |
| 1 | 44 | | _state = StateUpgradeRequestPending; |
| 1 | 45 | | if (!_incoming) |
| | 46 | | { |
| | 47 | | // |
| | 48 | | // Compose the upgrade request. |
| | 49 | | // |
| 1 | 50 | | var @out = new StringBuilder(); |
| 1 | 51 | | @out.Append("GET " + _resource + " HTTP/1.1\r\n"); |
| 1 | 52 | | @out.Append("Host: " + _host + "\r\n"); |
| 1 | 53 | | @out.Append("Upgrade: websocket\r\n"); |
| 1 | 54 | | @out.Append("Connection: Upgrade\r\n"); |
| 1 | 55 | | @out.Append("Sec-WebSocket-Protocol: " + _iceProtocol + "\r\n"); |
| 1 | 56 | | @out.Append("Sec-WebSocket-Version: 13\r\n"); |
| 1 | 57 | | @out.Append("Sec-WebSocket-Key: "); |
| | 58 | |
|
| | 59 | | // |
| | 60 | | // The value for Sec-WebSocket-Key is a 16-byte random number, |
| | 61 | | // encoded with Base64. |
| | 62 | | // |
| 1 | 63 | | byte[] key = new byte[16]; |
| 1 | 64 | | _rand.NextBytes(key); |
| 1 | 65 | | _key = System.Convert.ToBase64String(key); |
| 1 | 66 | | @out.Append(_key + "\r\n\r\n"); // EOM |
| | 67 | |
|
| 1 | 68 | | byte[] bytes = _utf8.GetBytes(@out.ToString()); |
| 1 | 69 | | _writeBuffer.resize(bytes.Length, false); |
| 1 | 70 | | _writeBuffer.b.position(0); |
| 1 | 71 | | _writeBuffer.b.put(bytes); |
| 1 | 72 | | _writeBuffer.b.flip(); |
| | 73 | | } |
| | 74 | | } |
| | 75 | |
|
| | 76 | | // |
| | 77 | | // Try to write the client's upgrade request. |
| | 78 | | // |
| 1 | 79 | | if (_state == StateUpgradeRequestPending && !_incoming) |
| | 80 | | { |
| 1 | 81 | | if (_writeBuffer.b.hasRemaining()) |
| | 82 | | { |
| 1 | 83 | | int s = _delegate.write(_writeBuffer); |
| 1 | 84 | | if (s != 0) |
| | 85 | | { |
| 1 | 86 | | return s; |
| | 87 | | } |
| | 88 | | } |
| | 89 | | Debug.Assert(!_writeBuffer.b.hasRemaining()); |
| 1 | 90 | | _state = StateUpgradeResponsePending; |
| | 91 | |
|
| 1 | 92 | | if (_instance.traceLevel() >= 1) |
| | 93 | | { |
| 1 | 94 | | _instance.logger().trace( |
| 1 | 95 | | _instance.traceCategory(), |
| 1 | 96 | | "sent " + protocol() + " connection HTTP upgrade request\n" + ToString()); |
| | 97 | | } |
| | 98 | | } |
| | 99 | |
|
| | 100 | | while (true) |
| | 101 | | { |
| 1 | 102 | | if (_readBuffer.b.hasRemaining()) |
| | 103 | | { |
| 1 | 104 | | int s = _delegate.read(_readBuffer, ref hasMoreData); |
| 1 | 105 | | if (s == SocketOperation.Write || _readBuffer.b.position() == 0) |
| | 106 | | { |
| 1 | 107 | | return s; |
| | 108 | | } |
| | 109 | | } |
| | 110 | |
|
| | 111 | | // |
| | 112 | | // Try to read the client's upgrade request or the server's response. |
| | 113 | | // |
| 1 | 114 | | if ((_state == StateUpgradeRequestPending && _incoming) || |
| 1 | 115 | | (_state == StateUpgradeResponsePending && !_incoming)) |
| | 116 | | { |
| | 117 | | // |
| | 118 | | // Check if we have enough data for a complete message. |
| | 119 | | // |
| 1 | 120 | | int p = _parser.isCompleteMessage(_readBuffer.b, 0, _readBuffer.b.position()); |
| 1 | 121 | | if (p == -1) |
| | 122 | | { |
| 0 | 123 | | if (_readBuffer.b.hasRemaining()) |
| | 124 | | { |
| 0 | 125 | | return SocketOperation.Read; |
| | 126 | | } |
| | 127 | |
|
| | 128 | | // |
| | 129 | | // Enlarge the buffer and try to read more. |
| | 130 | | // |
| 0 | 131 | | int oldSize = _readBuffer.b.position(); |
| 0 | 132 | | if (oldSize + 1024 > _instance.messageSizeMax()) |
| | 133 | | { |
| 0 | 134 | | Ex.throwMemoryLimitException( |
| 0 | 135 | | requested: oldSize + 1024, |
| 0 | 136 | | maximum: _instance.messageSizeMax()); |
| | 137 | | } |
| 0 | 138 | | _readBuffer.resize(oldSize + 1024, true); |
| 0 | 139 | | _readBuffer.b.position(oldSize); |
| 0 | 140 | | continue; // Try again to read the response/request |
| | 141 | | } |
| | 142 | |
|
| | 143 | | // |
| | 144 | | // Set _readBufferPos at the end of the response/request message. |
| | 145 | | // |
| 1 | 146 | | _readBufferPos = p; |
| | 147 | | } |
| | 148 | |
|
| | 149 | | // |
| | 150 | | // We're done, the client's upgrade request or server's response is read. |
| | 151 | | // |
| | 152 | | break; |
| | 153 | | } |
| | 154 | |
|
| | 155 | | try |
| | 156 | | { |
| | 157 | | // |
| | 158 | | // Parse the client's upgrade request. |
| | 159 | | // |
| 1 | 160 | | if (_state == StateUpgradeRequestPending && _incoming) |
| | 161 | | { |
| 1 | 162 | | if (_parser.parse(_readBuffer.b, 0, _readBufferPos)) |
| | 163 | | { |
| 1 | 164 | | handleRequest(_writeBuffer); |
| 1 | 165 | | _state = StateUpgradeResponsePending; |
| | 166 | | } |
| | 167 | | else |
| | 168 | | { |
| 0 | 169 | | throw new Ice.ProtocolException("incomplete request message"); |
| | 170 | | } |
| | 171 | | } |
| | 172 | |
|
| 1 | 173 | | if (_state == StateUpgradeResponsePending) |
| | 174 | | { |
| 1 | 175 | | if (_incoming) |
| | 176 | | { |
| 1 | 177 | | if (_writeBuffer.b.hasRemaining()) |
| | 178 | | { |
| 1 | 179 | | int s = _delegate.write(_writeBuffer); |
| 1 | 180 | | if (s != 0) |
| | 181 | | { |
| 1 | 182 | | return s; |
| | 183 | | } |
| | 184 | | } |
| | 185 | | } |
| | 186 | | else |
| | 187 | | { |
| | 188 | | // |
| | 189 | | // Parse the server's response |
| | 190 | | // |
| 1 | 191 | | if (_parser.parse(_readBuffer.b, 0, _readBufferPos)) |
| | 192 | | { |
| 1 | 193 | | handleResponse(); |
| | 194 | | } |
| | 195 | | else |
| | 196 | | { |
| 0 | 197 | | throw new Ice.ProtocolException("incomplete response message"); |
| | 198 | | } |
| | 199 | | } |
| | 200 | | } |
| 1 | 201 | | } |
| 0 | 202 | | catch (WebSocketException ex) |
| | 203 | | { |
| 0 | 204 | | throw new Ice.ProtocolException(ex.Message); |
| | 205 | | } |
| | 206 | |
|
| 1 | 207 | | _state = StateOpened; |
| 1 | 208 | | _nextState = StateOpened; |
| | 209 | |
|
| 1 | 210 | | hasMoreData = _readBufferPos < _readBuffer.b.position(); |
| 1 | 211 | | } |
| 1 | 212 | | catch (Ice.LocalException ex) |
| | 213 | | { |
| 1 | 214 | | if (_instance.traceLevel() >= 2) |
| | 215 | | { |
| 1 | 216 | | _instance.logger().trace( |
| 1 | 217 | | _instance.traceCategory(), |
| 1 | 218 | | protocol() + " connection HTTP upgrade request failed\n" + ToString() + "\n" + ex); |
| | 219 | | } |
| 1 | 220 | | throw; |
| | 221 | | } |
| | 222 | |
|
| 1 | 223 | | if (_instance.traceLevel() >= 1) |
| | 224 | | { |
| 1 | 225 | | if (_incoming) |
| | 226 | | { |
| 1 | 227 | | _instance.logger().trace( |
| 1 | 228 | | _instance.traceCategory(), |
| 1 | 229 | | "accepted " + protocol() + " connection HTTP upgrade request\n" + ToString()); |
| | 230 | | } |
| | 231 | | else |
| | 232 | | { |
| 1 | 233 | | _instance.logger().trace( |
| 1 | 234 | | _instance.traceCategory(), |
| 1 | 235 | | protocol() + " connection HTTP upgrade request accepted\n" + ToString()); |
| | 236 | | } |
| | 237 | | } |
| | 238 | |
|
| 1 | 239 | | return SocketOperation.None; |
| 1 | 240 | | } |
| | 241 | |
|
| | 242 | | public int closing(bool initiator, Ice.LocalException reason) |
| | 243 | | { |
| 1 | 244 | | if (_instance.traceLevel() >= 1) |
| | 245 | | { |
| 1 | 246 | | _instance.logger().trace( |
| 1 | 247 | | _instance.traceCategory(), |
| 1 | 248 | | "gracefully closing " + protocol() + " connection\n" + ToString()); |
| | 249 | | } |
| | 250 | |
|
| 1 | 251 | | int s = _nextState == StateOpened ? _state : _nextState; |
| | 252 | |
|
| 1 | 253 | | if (s == StateClosingRequestPending && _closingInitiator) |
| | 254 | | { |
| | 255 | | // |
| | 256 | | // If we initiated a close connection but also received a |
| | 257 | | // close connection, we assume we didn't initiated the |
| | 258 | | // connection and we send the close frame now. This is to |
| | 259 | | // ensure that if both peers close the connection at the same |
| | 260 | | // time we don't hang having both peer waiting for the close |
| | 261 | | // frame of the other. |
| | 262 | | // |
| | 263 | | Debug.Assert(!initiator); |
| 1 | 264 | | _closingInitiator = false; |
| 1 | 265 | | return SocketOperation.Write; |
| | 266 | | } |
| 1 | 267 | | else if (s >= StateClosingRequestPending) |
| | 268 | | { |
| 0 | 269 | | return SocketOperation.None; |
| | 270 | | } |
| | 271 | |
|
| 1 | 272 | | _closingInitiator = initiator; |
| 1 | 273 | | if (reason is Ice.CloseConnectionException) |
| | 274 | | { |
| 1 | 275 | | _closingReason = CLOSURE_NORMAL; |
| | 276 | | } |
| 1 | 277 | | else if (reason is Ice.ObjectAdapterDeactivatedException || |
| 1 | 278 | | reason is Ice.ObjectAdapterDestroyedException || |
| 1 | 279 | | reason is Ice.CommunicatorDestroyedException) |
| | 280 | | { |
| 1 | 281 | | _closingReason = CLOSURE_SHUTDOWN; |
| | 282 | | } |
| 1 | 283 | | else if (reason is Ice.ProtocolException) |
| | 284 | | { |
| 0 | 285 | | _closingReason = CLOSURE_PROTOCOL_ERROR; |
| | 286 | | } |
| 1 | 287 | | if (_state == StateOpened) |
| | 288 | | { |
| 1 | 289 | | _state = StateClosingRequestPending; |
| 1 | 290 | | return initiator ? SocketOperation.Read : SocketOperation.Write; |
| | 291 | | } |
| | 292 | | else |
| | 293 | | { |
| 0 | 294 | | _nextState = StateClosingRequestPending; |
| 0 | 295 | | return SocketOperation.None; |
| | 296 | | } |
| | 297 | | } |
| | 298 | |
|
| | 299 | | public void close() |
| | 300 | | { |
| 1 | 301 | | _delegate.close(); |
| 1 | 302 | | _state = StateClosed; |
| | 303 | |
|
| | 304 | | // |
| | 305 | | // Clear the buffers now instead of waiting for destruction. |
| | 306 | | // |
| 1 | 307 | | if (!_readPending) |
| | 308 | | { |
| 1 | 309 | | _readBuffer.clear(); |
| | 310 | | } |
| 1 | 311 | | if (!_writePending) |
| | 312 | | { |
| 1 | 313 | | _writeBuffer.clear(); |
| | 314 | | } |
| 1 | 315 | | } |
| | 316 | |
|
| | 317 | | public EndpointI bind() |
| | 318 | | { |
| | 319 | | Debug.Assert(false); |
| 0 | 320 | | return null; |
| | 321 | | } |
| | 322 | |
|
| 1 | 323 | | public void destroy() => _delegate.destroy(); |
| | 324 | |
|
| | 325 | | public int write(Buffer buf) |
| | 326 | | { |
| 1 | 327 | | if (_writePending) |
| | 328 | | { |
| 0 | 329 | | return SocketOperation.Write; |
| | 330 | | } |
| | 331 | |
|
| 1 | 332 | | if (_state < StateOpened) |
| | 333 | | { |
| 1 | 334 | | if (_state < StateConnected) |
| | 335 | | { |
| 1 | 336 | | return _delegate.write(buf); |
| | 337 | | } |
| | 338 | | else |
| | 339 | | { |
| 1 | 340 | | return _delegate.write(_writeBuffer); |
| | 341 | | } |
| | 342 | | } |
| | 343 | |
|
| 1 | 344 | | int s = SocketOperation.None; |
| | 345 | | do |
| | 346 | | { |
| 1 | 347 | | if (preWrite(buf)) |
| | 348 | | { |
| 1 | 349 | | if (_writeState == WriteStateFlush) |
| | 350 | | { |
| | 351 | | // |
| | 352 | | // Invoke write() even though there's nothing to write. |
| | 353 | | // |
| | 354 | | Debug.Assert(!buf.b.hasRemaining()); |
| 0 | 355 | | s = _delegate.write(buf); |
| | 356 | | } |
| | 357 | |
|
| 1 | 358 | | if (s == SocketOperation.None && _writeBuffer.b.hasRemaining()) |
| | 359 | | { |
| 1 | 360 | | s = _delegate.write(_writeBuffer); |
| | 361 | | } |
| 1 | 362 | | else if (s == SocketOperation.None && _incoming && !buf.empty() && _writeState == WriteStatePayload) |
| | 363 | | { |
| 1 | 364 | | s = _delegate.write(buf); |
| | 365 | | } |
| | 366 | | } |
| | 367 | | } |
| 1 | 368 | | while (postWrite(buf, s)); |
| | 369 | |
|
| 1 | 370 | | if (s != SocketOperation.None) |
| | 371 | | { |
| 1 | 372 | | return s; |
| | 373 | | } |
| 1 | 374 | | if (_state == StateClosingResponsePending && !_closingInitiator) |
| | 375 | | { |
| 1 | 376 | | return SocketOperation.Read; |
| | 377 | | } |
| 1 | 378 | | return SocketOperation.None; |
| | 379 | | } |
| | 380 | |
|
| | 381 | | public int read(Buffer buf, ref bool hasMoreData) |
| | 382 | | { |
| 1 | 383 | | if (_readPending) |
| | 384 | | { |
| 0 | 385 | | return SocketOperation.Read; |
| | 386 | | } |
| | 387 | |
|
| 1 | 388 | | if (_state < StateOpened) |
| | 389 | | { |
| 1 | 390 | | if (_state < StateConnected) |
| | 391 | | { |
| 1 | 392 | | return _delegate.read(buf, ref hasMoreData); |
| | 393 | | } |
| | 394 | | else |
| | 395 | | { |
| 1 | 396 | | if (_delegate.read(_readBuffer, ref hasMoreData) == SocketOperation.Write) |
| | 397 | | { |
| 0 | 398 | | return SocketOperation.Write; |
| | 399 | | } |
| | 400 | | else |
| | 401 | | { |
| 1 | 402 | | return SocketOperation.None; |
| | 403 | | } |
| | 404 | | } |
| | 405 | | } |
| | 406 | |
|
| 1 | 407 | | if (!buf.b.hasRemaining()) |
| | 408 | | { |
| 1 | 409 | | hasMoreData |= _readBufferPos < _readBuffer.b.position(); |
| 1 | 410 | | return SocketOperation.None; |
| | 411 | | } |
| | 412 | |
|
| | 413 | | int s; |
| | 414 | | do |
| | 415 | | { |
| 1 | 416 | | if (preRead(buf)) |
| | 417 | | { |
| 1 | 418 | | if (_readState == ReadStatePayload) |
| | 419 | | { |
| | 420 | | // |
| | 421 | | // If the payload length is smaller than what remains to be read, we read |
| | 422 | | // no more than the payload length. The remaining of the buffer will be |
| | 423 | | // sent over in another frame. |
| | 424 | | // |
| 1 | 425 | | int readSz = _readPayloadLength - (buf.b.position() - _readStart); |
| 1 | 426 | | if (buf.b.remaining() > readSz) |
| | 427 | | { |
| 0 | 428 | | int size = buf.size(); |
| 0 | 429 | | buf.resize(buf.b.position() + readSz, true); |
| 0 | 430 | | s = _delegate.read(buf, ref hasMoreData); |
| 0 | 431 | | buf.resize(size, true); |
| | 432 | | } |
| | 433 | | else |
| | 434 | | { |
| 1 | 435 | | s = _delegate.read(buf, ref hasMoreData); |
| | 436 | | } |
| | 437 | | } |
| | 438 | | else |
| | 439 | | { |
| 1 | 440 | | s = _delegate.read(_readBuffer, ref hasMoreData); |
| | 441 | | } |
| | 442 | |
|
| 1 | 443 | | if (s == SocketOperation.Write) |
| | 444 | | { |
| 0 | 445 | | postRead(buf); |
| 0 | 446 | | return s; |
| | 447 | | } |
| | 448 | | } |
| | 449 | | } |
| 1 | 450 | | while (postRead(buf)); |
| | 451 | |
|
| 1 | 452 | | if (!buf.b.hasRemaining()) |
| | 453 | | { |
| 1 | 454 | | hasMoreData |= _readBufferPos < _readBuffer.b.position(); |
| 1 | 455 | | s = SocketOperation.None; |
| | 456 | | } |
| | 457 | | else |
| | 458 | | { |
| 1 | 459 | | hasMoreData = false; |
| 1 | 460 | | s = SocketOperation.Read; |
| | 461 | | } |
| | 462 | |
|
| 1 | 463 | | if (((_state == StateClosingRequestPending && !_closingInitiator) || |
| 1 | 464 | | (_state == StateClosingResponsePending && _closingInitiator) || |
| 1 | 465 | | _state == StatePingPending || |
| 1 | 466 | | _state == StatePongPending) && |
| 1 | 467 | | _writeState == WriteStateHeader) |
| | 468 | | { |
| | 469 | | // We have things to write, ask to be notified when writes are ready. |
| 1 | 470 | | s |= SocketOperation.Write; |
| | 471 | | } |
| | 472 | |
|
| 1 | 473 | | return s; |
| | 474 | | } |
| | 475 | |
|
| | 476 | | public bool startRead(Buffer buf, AsyncCallback callback, object state) |
| | 477 | | { |
| 1 | 478 | | _readPending = true; |
| 1 | 479 | | if (_state < StateOpened) |
| | 480 | | { |
| 1 | 481 | | _finishRead = true; |
| 1 | 482 | | if (_state < StateConnected) |
| | 483 | | { |
| 1 | 484 | | return _delegate.startRead(buf, callback, state); |
| | 485 | | } |
| | 486 | | else |
| | 487 | | { |
| 1 | 488 | | return _delegate.startRead(_readBuffer, callback, state); |
| | 489 | | } |
| | 490 | | } |
| | 491 | |
|
| 1 | 492 | | if (preRead(buf)) |
| | 493 | | { |
| 1 | 494 | | _finishRead = true; |
| 1 | 495 | | if (_readState == ReadStatePayload) |
| | 496 | | { |
| | 497 | | // |
| | 498 | | // If the payload length is smaller than what remains to be read, we read |
| | 499 | | // no more than the payload length. The remaining of the buffer will be |
| | 500 | | // sent over in another frame. |
| | 501 | | // |
| 1 | 502 | | int readSz = _readPayloadLength - (buf.b.position() - _readStart); |
| 1 | 503 | | if (buf.b.remaining() > readSz) |
| | 504 | | { |
| 0 | 505 | | int size = buf.size(); |
| 0 | 506 | | buf.resize(buf.b.position() + readSz, true); |
| 0 | 507 | | bool completedSynchronously = _delegate.startRead(buf, callback, state); |
| 0 | 508 | | buf.resize(size, true); |
| 0 | 509 | | return completedSynchronously; |
| | 510 | | } |
| | 511 | | else |
| | 512 | | { |
| 1 | 513 | | return _delegate.startRead(buf, callback, state); |
| | 514 | | } |
| | 515 | | } |
| | 516 | | else |
| | 517 | | { |
| 1 | 518 | | return _delegate.startRead(_readBuffer, callback, state); |
| | 519 | | } |
| | 520 | | } |
| | 521 | | else |
| | 522 | | { |
| 1 | 523 | | return true; |
| | 524 | | } |
| | 525 | | } |
| | 526 | |
|
| | 527 | | public void finishRead(Buffer buf) |
| | 528 | | { |
| | 529 | | Debug.Assert(_readPending); |
| 1 | 530 | | _readPending = false; |
| | 531 | |
|
| 1 | 532 | | if (_state < StateOpened) |
| | 533 | | { |
| | 534 | | Debug.Assert(_finishRead); |
| 1 | 535 | | _finishRead = false; |
| 1 | 536 | | if (_state < StateConnected) |
| | 537 | | { |
| 1 | 538 | | _delegate.finishRead(buf); |
| | 539 | | } |
| | 540 | | else |
| | 541 | | { |
| 1 | 542 | | _delegate.finishRead(_readBuffer); |
| | 543 | | } |
| 1 | 544 | | return; |
| | 545 | | } |
| | 546 | |
|
| 1 | 547 | | if (!_finishRead) |
| | 548 | | { |
| | 549 | | // Nothing to do. |
| | 550 | | } |
| 1 | 551 | | else if (_readState == ReadStatePayload) |
| | 552 | | { |
| | 553 | | Debug.Assert(_finishRead); |
| 1 | 554 | | _finishRead = false; |
| 1 | 555 | | _delegate.finishRead(buf); |
| | 556 | | } |
| | 557 | | else |
| | 558 | | { |
| | 559 | | Debug.Assert(_finishRead); |
| 1 | 560 | | _finishRead = false; |
| 1 | 561 | | _delegate.finishRead(_readBuffer); |
| | 562 | | } |
| | 563 | |
|
| 1 | 564 | | if (_state == StateClosed) |
| | 565 | | { |
| 0 | 566 | | _readBuffer.clear(); |
| 0 | 567 | | return; |
| | 568 | | } |
| | 569 | |
|
| 1 | 570 | | postRead(buf); |
| 1 | 571 | | } |
| | 572 | |
|
| | 573 | | public bool startWrite(Buffer buf, AsyncCallback callback, object state, out bool messageWritten) |
| | 574 | | { |
| 1 | 575 | | _writePending = true; |
| 1 | 576 | | if (_state < StateOpened) |
| | 577 | | { |
| 1 | 578 | | if (_state < StateConnected) |
| | 579 | | { |
| 1 | 580 | | return _delegate.startWrite(buf, callback, state, out messageWritten); |
| | 581 | | } |
| | 582 | | else |
| | 583 | | { |
| 1 | 584 | | return _delegate.startWrite(_writeBuffer, callback, state, out messageWritten); |
| | 585 | | } |
| | 586 | | } |
| | 587 | |
|
| 1 | 588 | | if (preWrite(buf)) |
| | 589 | | { |
| 1 | 590 | | if (_writeBuffer.b.hasRemaining()) |
| | 591 | | { |
| 1 | 592 | | return _delegate.startWrite(_writeBuffer, callback, state, out messageWritten); |
| | 593 | | } |
| | 594 | | else |
| | 595 | | { |
| | 596 | | Debug.Assert(_incoming); |
| 1 | 597 | | return _delegate.startWrite(buf, callback, state, out messageWritten); |
| | 598 | | } |
| | 599 | | } |
| | 600 | | else |
| | 601 | | { |
| 0 | 602 | | messageWritten = true; |
| 0 | 603 | | return false; |
| | 604 | | } |
| | 605 | | } |
| | 606 | |
|
| | 607 | | public void finishWrite(Buffer buf) |
| | 608 | | { |
| 1 | 609 | | _writePending = false; |
| | 610 | |
|
| 1 | 611 | | if (_state < StateOpened) |
| | 612 | | { |
| 1 | 613 | | if (_state < StateConnected) |
| | 614 | | { |
| 1 | 615 | | _delegate.finishWrite(buf); |
| | 616 | | } |
| | 617 | | else |
| | 618 | | { |
| 1 | 619 | | _delegate.finishWrite(_writeBuffer); |
| | 620 | | } |
| 1 | 621 | | return; |
| | 622 | | } |
| | 623 | |
|
| 1 | 624 | | if (_writeBuffer.b.hasRemaining()) |
| | 625 | | { |
| 1 | 626 | | _delegate.finishWrite(_writeBuffer); |
| | 627 | | } |
| 1 | 628 | | else if (!buf.empty() && buf.b.hasRemaining()) |
| | 629 | | { |
| | 630 | | Debug.Assert(_incoming); |
| 1 | 631 | | _delegate.finishWrite(buf); |
| | 632 | | } |
| | 633 | |
|
| 1 | 634 | | if (_state == StateClosed) |
| | 635 | | { |
| 0 | 636 | | _writeBuffer.clear(); |
| 0 | 637 | | return; |
| | 638 | | } |
| | 639 | |
|
| 1 | 640 | | postWrite(buf, SocketOperation.None); |
| 1 | 641 | | } |
| | 642 | |
|
| 1 | 643 | | public string protocol() => _instance.protocol(); |
| | 644 | |
|
| | 645 | | public ConnectionInfo getInfo(bool incoming, string adapterName, string connectionId) => |
| 1 | 646 | | new WSConnectionInfo(_delegate.getInfo(incoming, adapterName, connectionId), _parser.getHeaders()); |
| | 647 | |
|
| 1 | 648 | | public void checkSendSize(Buffer buf) => _delegate.checkSendSize(buf); |
| | 649 | |
|
| 1 | 650 | | public void setBufferSize(int rcvSize, int sndSize) => _delegate.setBufferSize(rcvSize, sndSize); |
| | 651 | |
|
| 1 | 652 | | public override string ToString() => _delegate.ToString(); |
| | 653 | |
|
| 0 | 654 | | public string toDetailedString() => _delegate.toDetailedString(); |
| | 655 | |
|
| 1 | 656 | | internal |
| 1 | 657 | | WSTransceiver(ProtocolInstance instance, Transceiver del, string host, string resource) |
| | 658 | | { |
| 1 | 659 | | init(instance, del); |
| 1 | 660 | | _host = host; |
| 1 | 661 | | _resource = resource; |
| 1 | 662 | | _incoming = false; |
| | 663 | |
|
| | 664 | | // |
| | 665 | | // Use a 16KB write buffer size. We use 16KB for the write |
| | 666 | | // buffer size because all the data needs to be copied to the |
| | 667 | | // write buffer for the purpose of masking. A 16KB buffer |
| | 668 | | // appears to be a good compromise to reduce the number of |
| | 669 | | // socket write calls and not consume too much memory. |
| | 670 | | // |
| 1 | 671 | | _writeBufferSize = 16 * 1024; |
| | 672 | |
|
| | 673 | | // |
| | 674 | | // Write and read buffer size must be large enough to hold the frame header! |
| | 675 | | // |
| | 676 | | Debug.Assert(_writeBufferSize > 256); |
| | 677 | | Debug.Assert(_readBufferSize > 256); |
| 1 | 678 | | } |
| | 679 | |
|
| 1 | 680 | | internal WSTransceiver(ProtocolInstance instance, Transceiver del) |
| | 681 | | { |
| 1 | 682 | | init(instance, del); |
| 1 | 683 | | _host = ""; |
| 1 | 684 | | _resource = ""; |
| 1 | 685 | | _incoming = true; |
| | 686 | |
|
| | 687 | | // |
| | 688 | | // Write and read buffer size must be large enough to hold the frame header! |
| | 689 | | // |
| | 690 | | Debug.Assert(_writeBufferSize > 256); |
| | 691 | | Debug.Assert(_readBufferSize > 256); |
| 1 | 692 | | } |
| | 693 | |
|
| | 694 | | private void init(ProtocolInstance instance, Transceiver del) |
| | 695 | | { |
| 1 | 696 | | _instance = instance; |
| 1 | 697 | | _delegate = del; |
| 1 | 698 | | _state = StateInitializeDelegate; |
| 1 | 699 | | _parser = new HttpParser(); |
| 1 | 700 | | _readState = ReadStateOpcode; |
| 1 | 701 | | _readBuffer = new Buffer(ByteBuffer.ByteOrder.BigEndian); // Network byte order |
| 1 | 702 | | _readBufferSize = 1024; |
| 1 | 703 | | _readLastFrame = true; |
| 1 | 704 | | _readOpCode = 0; |
| 1 | 705 | | _readHeaderLength = 0; |
| 1 | 706 | | _readPayloadLength = 0; |
| 1 | 707 | | _writeState = WriteStateHeader; |
| 1 | 708 | | _writeBuffer = new Buffer(ByteBuffer.ByteOrder.BigEndian); // Network byte order |
| 1 | 709 | | _writeBufferSize = 1024; |
| 1 | 710 | | _readPending = false; |
| 1 | 711 | | _finishRead = false; |
| 1 | 712 | | _writePending = false; |
| 1 | 713 | | _readMask = new byte[4]; |
| 1 | 714 | | _writeMask = new byte[4]; |
| 1 | 715 | | _key = ""; |
| 1 | 716 | | _pingPayload = []; |
| 1 | 717 | | _rand = new Random(); |
| 1 | 718 | | } |
| | 719 | |
|
| | 720 | | private void handleRequest(Buffer responseBuffer) |
| | 721 | | { |
| | 722 | | // |
| | 723 | | // HTTP/1.1 |
| | 724 | | // |
| 1 | 725 | | if (_parser.versionMajor() != 1 || _parser.versionMinor() != 1) |
| | 726 | | { |
| 0 | 727 | | throw new WebSocketException("unsupported HTTP version"); |
| | 728 | | } |
| | 729 | |
|
| | 730 | | // |
| | 731 | | // "An |Upgrade| header field containing the value 'websocket', |
| | 732 | | // treated as an ASCII case-insensitive value." |
| | 733 | | // |
| 1 | 734 | | string val = _parser.getHeader("Upgrade", true); |
| 1 | 735 | | if (val == null) |
| | 736 | | { |
| 0 | 737 | | throw new WebSocketException("missing value for Upgrade field"); |
| | 738 | | } |
| 1 | 739 | | else if (val != "websocket") |
| | 740 | | { |
| 0 | 741 | | throw new WebSocketException("invalid value `" + val + "' for Upgrade field"); |
| | 742 | | } |
| | 743 | |
|
| | 744 | | // |
| | 745 | | // "A |Connection| header field that includes the token 'Upgrade', |
| | 746 | | // treated as an ASCII case-insensitive value. |
| | 747 | | // |
| 1 | 748 | | val = _parser.getHeader("Connection", true); |
| 1 | 749 | | if (val == null) |
| | 750 | | { |
| 0 | 751 | | throw new WebSocketException("missing value for Connection field"); |
| | 752 | | } |
| 1 | 753 | | else if (!val.Contains("upgrade", StringComparison.Ordinal)) |
| | 754 | | { |
| 0 | 755 | | throw new WebSocketException("invalid value `" + val + "' for Connection field"); |
| | 756 | | } |
| | 757 | |
|
| | 758 | | // |
| | 759 | | // "A |Sec-WebSocket-Version| header field, with a value of 13." |
| | 760 | | // |
| 1 | 761 | | val = _parser.getHeader("Sec-WebSocket-Version", false); |
| 1 | 762 | | if (val == null) |
| | 763 | | { |
| 0 | 764 | | throw new WebSocketException("missing value for WebSocket version"); |
| | 765 | | } |
| 1 | 766 | | else if (val != "13") |
| | 767 | | { |
| 0 | 768 | | throw new WebSocketException("unsupported WebSocket version `" + val + "'"); |
| | 769 | | } |
| | 770 | |
|
| | 771 | | // |
| | 772 | | // "Optionally, a |Sec-WebSocket-Protocol| header field, with a list |
| | 773 | | // of values indicating which protocols the client would like to |
| | 774 | | // speak, ordered by preference." |
| | 775 | | // |
| 1 | 776 | | bool addProtocol = false; |
| 1 | 777 | | val = _parser.getHeader("Sec-WebSocket-Protocol", true); |
| 1 | 778 | | if (val != null) |
| | 779 | | { |
| 1 | 780 | | string[] protocols = Ice.UtilInternal.StringUtil.splitString(val, ",") ?? |
| 1 | 781 | | throw new WebSocketException("invalid value `" + val + "' for WebSocket protocol"); |
| 1 | 782 | | foreach (string p in protocols) |
| | 783 | | { |
| 1 | 784 | | if (!p.Trim().Equals(_iceProtocol, StringComparison.Ordinal)) |
| | 785 | | { |
| 0 | 786 | | throw new WebSocketException("unknown value `" + p + "' for WebSocket protocol"); |
| | 787 | | } |
| 1 | 788 | | addProtocol = true; |
| | 789 | | } |
| | 790 | | } |
| | 791 | |
|
| | 792 | | // |
| | 793 | | // "A |Sec-WebSocket-Key| header field with a base64-encoded |
| | 794 | | // value that, when decoded, is 16 bytes in length." |
| | 795 | | // |
| 1 | 796 | | string key = _parser.getHeader("Sec-WebSocket-Key", false) ?? |
| 1 | 797 | | throw new WebSocketException("missing value for WebSocket key"); |
| 1 | 798 | | byte[] decodedKey = Convert.FromBase64String(key); |
| 1 | 799 | | if (decodedKey.Length != 16) |
| | 800 | | { |
| 0 | 801 | | throw new WebSocketException("invalid value `" + key + "' for WebSocket key"); |
| | 802 | | } |
| | 803 | |
|
| | 804 | | // |
| | 805 | | // Retain the target resource. |
| | 806 | | // |
| 1 | 807 | | _resource = _parser.uri(); |
| | 808 | |
|
| | 809 | | // |
| | 810 | | // Compose the response. |
| | 811 | | // |
| 1 | 812 | | var @out = new StringBuilder(); |
| 1 | 813 | | @out.Append("HTTP/1.1 101 Switching Protocols\r\n"); |
| 1 | 814 | | @out.Append("Upgrade: websocket\r\n"); |
| 1 | 815 | | @out.Append("Connection: Upgrade\r\n"); |
| 1 | 816 | | if (addProtocol) |
| | 817 | | { |
| 1 | 818 | | @out.Append("Sec-WebSocket-Protocol: " + _iceProtocol + "\r\n"); |
| | 819 | | } |
| | 820 | |
|
| | 821 | | // |
| | 822 | | // The response includes: |
| | 823 | | // |
| | 824 | | // "A |Sec-WebSocket-Accept| header field. The value of this |
| | 825 | | // header field is constructed by concatenating /key/, defined |
| | 826 | | // above in step 4 in Section 4.2.2, with the string "258EAFA5- |
| | 827 | | // E914-47DA-95CA-C5AB0DC85B11", taking the SHA-1 hash of this |
| | 828 | | // concatenated value to obtain a 20-byte value and base64- |
| | 829 | | // encoding (see Section 4 of [RFC4648]) this 20-byte hash. |
| | 830 | | // |
| 1 | 831 | | @out.Append("Sec-WebSocket-Accept: "); |
| 1 | 832 | | string input = key + _wsUUID; |
| | 833 | | #pragma warning disable CA5350 // SHA1 is used for compatibility with the WebSocket protocol |
| 1 | 834 | | using var sha1 = SHA1.Create(); |
| 1 | 835 | | byte[] hash = sha1.ComputeHash(_utf8.GetBytes(input)); |
| | 836 | | #pragma warning restore CA5350 |
| 1 | 837 | | @out.Append(Convert.ToBase64String(hash) + "\r\n" + "\r\n"); // EOM |
| | 838 | |
|
| 1 | 839 | | byte[] bytes = _utf8.GetBytes(@out.ToString()); |
| | 840 | | Debug.Assert(bytes.Length == @out.Length); |
| 1 | 841 | | responseBuffer.resize(bytes.Length, false); |
| 1 | 842 | | responseBuffer.b.position(0); |
| 1 | 843 | | responseBuffer.b.put(bytes); |
| 1 | 844 | | responseBuffer.b.flip(); |
| 1 | 845 | | } |
| | 846 | |
|
| | 847 | | private void handleResponse() |
| | 848 | | { |
| | 849 | | string val; |
| | 850 | |
|
| | 851 | | // |
| | 852 | | // HTTP/1.1 |
| | 853 | | // |
| 1 | 854 | | if (_parser.versionMajor() != 1 || _parser.versionMinor() != 1) |
| | 855 | | { |
| 0 | 856 | | throw new WebSocketException("unsupported HTTP version"); |
| | 857 | | } |
| | 858 | |
|
| | 859 | | // |
| | 860 | | // "If the status code received from the server is not 101, the |
| | 861 | | // client handles the response per HTTP [RFC2616] procedures. In |
| | 862 | | // particular, the client might perform authentication if it |
| | 863 | | // receives a 401 status code; the server might redirect the client |
| | 864 | | // using a 3xx status code (but clients are not required to follow |
| | 865 | | // them), etc." |
| | 866 | | // |
| 1 | 867 | | if (_parser.status() != 101) |
| | 868 | | { |
| 0 | 869 | | var @out = new StringBuilder("unexpected status value " + _parser.status()); |
| 0 | 870 | | if (_parser.reason().Length > 0) |
| | 871 | | { |
| 0 | 872 | | @out.Append(":\n" + _parser.reason()); |
| | 873 | | } |
| 0 | 874 | | throw new WebSocketException(@out.ToString()); |
| | 875 | | } |
| | 876 | |
|
| | 877 | | // |
| | 878 | | // "If the response lacks an |Upgrade| header field or the |Upgrade| |
| | 879 | | // header field contains a value that is not an ASCII case- |
| | 880 | | // insensitive match for the value "websocket", the client MUST |
| | 881 | | // _Fail the WebSocket Connection_." |
| | 882 | | // |
| 1 | 883 | | val = _parser.getHeader("Upgrade", true); |
| 1 | 884 | | if (val == null) |
| | 885 | | { |
| 0 | 886 | | throw new WebSocketException("missing value for Upgrade field"); |
| | 887 | | } |
| 1 | 888 | | else if (val != "websocket") |
| | 889 | | { |
| 0 | 890 | | throw new WebSocketException("invalid value `" + val + "' for Upgrade field"); |
| | 891 | | } |
| | 892 | |
|
| | 893 | | // |
| | 894 | | // "If the response lacks a |Connection| header field or the |
| | 895 | | // |Connection| header field doesn't contain a token that is an |
| | 896 | | // ASCII case-insensitive match for the value "Upgrade", the client |
| | 897 | | // MUST _Fail the WebSocket Connection_." |
| | 898 | | // |
| 1 | 899 | | val = _parser.getHeader("Connection", true); |
| 1 | 900 | | if (val == null) |
| | 901 | | { |
| 0 | 902 | | throw new WebSocketException("missing value for Connection field"); |
| | 903 | | } |
| 1 | 904 | | else if (!val.Contains("upgrade", StringComparison.Ordinal)) |
| | 905 | | { |
| 0 | 906 | | throw new WebSocketException("invalid value `" + val + "' for Connection field"); |
| | 907 | | } |
| | 908 | |
|
| | 909 | | // |
| | 910 | | // "If the response includes a |Sec-WebSocket-Protocol| header field |
| | 911 | | // and this header field indicates the use of a subprotocol that was |
| | 912 | | // not present in the client's handshake (the server has indicated a |
| | 913 | | // subprotocol not requested by the client), the client MUST _Fail |
| | 914 | | // the WebSocket Connection_." |
| | 915 | | // |
| 1 | 916 | | val = _parser.getHeader("Sec-WebSocket-Protocol", true); |
| 1 | 917 | | if (val != null && !val.Equals(_iceProtocol, StringComparison.Ordinal)) |
| | 918 | | { |
| 0 | 919 | | throw new WebSocketException("invalid value `" + val + "' for WebSocket protocol"); |
| | 920 | | } |
| | 921 | |
|
| | 922 | | // |
| | 923 | | // "If the response lacks a |Sec-WebSocket-Accept| header field or |
| | 924 | | // the |Sec-WebSocket-Accept| contains a value other than the |
| | 925 | | // base64-encoded SHA-1 of the concatenation of the |Sec-WebSocket- |
| | 926 | | // Key| (as a string, not base64-decoded) with the string "258EAFA5- |
| | 927 | | // E914-47DA-95CA-C5AB0DC85B11" but ignoring any leading and |
| | 928 | | // trailing whitespace, the client MUST _Fail the WebSocket |
| | 929 | | // Connection_." |
| | 930 | | // |
| 1 | 931 | | val = _parser.getHeader("Sec-WebSocket-Accept", false); |
| 1 | 932 | | if (val == null) |
| | 933 | | { |
| 0 | 934 | | throw new WebSocketException("missing value for Sec-WebSocket-Accept"); |
| | 935 | | } |
| | 936 | |
|
| 1 | 937 | | string input = _key + _wsUUID; |
| | 938 | | #pragma warning disable CA5350 // SHA1 is used for compatibility with the WebSocket protocol |
| 1 | 939 | | using var sha1 = SHA1.Create(); |
| 1 | 940 | | byte[] hash = sha1.ComputeHash(_utf8.GetBytes(input)); |
| | 941 | | #pragma warning restore CA5350 |
| 1 | 942 | | if (!val.Equals(Convert.ToBase64String(hash), StringComparison.Ordinal)) |
| | 943 | | { |
| 0 | 944 | | throw new WebSocketException("invalid value `" + val + "' for Sec-WebSocket-Accept"); |
| | 945 | | } |
| 1 | 946 | | } |
| | 947 | |
|
| | 948 | | private bool preRead(Buffer buf) |
| | 949 | | { |
| | 950 | | while (true) |
| | 951 | | { |
| 1 | 952 | | if (_readState == ReadStateOpcode) |
| | 953 | | { |
| | 954 | | // |
| | 955 | | // Is there enough data available to read the opcode? |
| | 956 | | // |
| 1 | 957 | | if (!readBuffered(2)) |
| | 958 | | { |
| 1 | 959 | | return true; |
| | 960 | | } |
| | 961 | |
|
| | 962 | | // |
| | 963 | | // Most-significant bit indicates whether this is the |
| | 964 | | // last frame. Least-significant four bits hold the |
| | 965 | | // opcode. |
| | 966 | | // |
| 1 | 967 | | int ch = _readBuffer.b.get(_readBufferPos++); |
| 1 | 968 | | _readOpCode = ch & 0xf; |
| | 969 | |
|
| | 970 | | // |
| | 971 | | // Remember if last frame if we're going to read a data or |
| | 972 | | // continuation frame, this is only for protocol |
| | 973 | | // correctness checking purpose. |
| | 974 | | // |
| 1 | 975 | | if (_readOpCode == OP_DATA) |
| | 976 | | { |
| 1 | 977 | | if (!_readLastFrame) |
| | 978 | | { |
| 0 | 979 | | throw new Ice.ProtocolException("invalid data frame, no FIN on previous frame"); |
| | 980 | | } |
| 1 | 981 | | _readLastFrame = (ch & FLAG_FINAL) == FLAG_FINAL; |
| | 982 | | } |
| 1 | 983 | | else if (_readOpCode == OP_CONT) |
| | 984 | | { |
| 0 | 985 | | if (_readLastFrame) |
| | 986 | | { |
| 0 | 987 | | throw new Ice.ProtocolException("invalid continuation frame, previous frame FIN set"); |
| | 988 | | } |
| 0 | 989 | | _readLastFrame = (ch & FLAG_FINAL) == FLAG_FINAL; |
| | 990 | | } |
| | 991 | |
|
| 1 | 992 | | ch = _readBuffer.b.get(_readBufferPos++); |
| | 993 | |
|
| | 994 | | // |
| | 995 | | // Check the MASK bit. Messages sent by a client must be masked; |
| | 996 | | // messages sent by a server must not be masked. |
| | 997 | | // |
| 1 | 998 | | bool masked = (ch & FLAG_MASKED) == FLAG_MASKED; |
| 1 | 999 | | if (masked != _incoming) |
| | 1000 | | { |
| 0 | 1001 | | throw new Ice.ProtocolException("invalid masking"); |
| | 1002 | | } |
| | 1003 | |
|
| | 1004 | | // |
| | 1005 | | // Extract the payload length, which can have the following values: |
| | 1006 | | // |
| | 1007 | | // 0-125: The payload length |
| | 1008 | | // 126: The subsequent two bytes contain the payload length |
| | 1009 | | // 127: The subsequent eight bytes contain the payload length |
| | 1010 | | // |
| 1 | 1011 | | _readPayloadLength = ch & 0x7f; |
| 1 | 1012 | | if (_readPayloadLength < 126) |
| | 1013 | | { |
| 1 | 1014 | | _readHeaderLength = 0; |
| | 1015 | | } |
| 1 | 1016 | | else if (_readPayloadLength == 126) |
| | 1017 | | { |
| 1 | 1018 | | _readHeaderLength = 2; // Need to read a 16-bit payload length. |
| | 1019 | | } |
| | 1020 | | else |
| | 1021 | | { |
| 1 | 1022 | | _readHeaderLength = 8; // Need to read a 64-bit payload length. |
| | 1023 | | } |
| 1 | 1024 | | if (masked) |
| | 1025 | | { |
| 1 | 1026 | | _readHeaderLength += 4; // Need to read a 32-bit mask. |
| | 1027 | | } |
| | 1028 | |
|
| 1 | 1029 | | _readState = ReadStateHeader; |
| | 1030 | | } |
| | 1031 | |
|
| 1 | 1032 | | if (_readState == ReadStateHeader) |
| | 1033 | | { |
| | 1034 | | // |
| | 1035 | | // Is there enough data available to read the header? |
| | 1036 | | // |
| 1 | 1037 | | if (_readHeaderLength > 0 && !readBuffered(_readHeaderLength)) |
| | 1038 | | { |
| 1 | 1039 | | return true; |
| | 1040 | | } |
| | 1041 | |
|
| 1 | 1042 | | if (_readPayloadLength == 126) |
| | 1043 | | { |
| 1 | 1044 | | _readPayloadLength = _readBuffer.b.getShort(_readBufferPos); // Uses network byte order. |
| 1 | 1045 | | if (_readPayloadLength < 0) |
| | 1046 | | { |
| 0 | 1047 | | _readPayloadLength += 65536; |
| | 1048 | | } |
| 1 | 1049 | | _readBufferPos += 2; |
| | 1050 | | } |
| 1 | 1051 | | else if (_readPayloadLength == 127) |
| | 1052 | | { |
| 1 | 1053 | | long l = _readBuffer.b.getLong(_readBufferPos); // Uses network byte order. |
| 1 | 1054 | | _readBufferPos += 8; |
| 1 | 1055 | | if (l < 0 || l > int.MaxValue) |
| | 1056 | | { |
| 0 | 1057 | | throw new Ice.ProtocolException("invalid WebSocket payload length: " + l); |
| | 1058 | | } |
| 1 | 1059 | | _readPayloadLength = (int)l; |
| | 1060 | | } |
| | 1061 | |
|
| | 1062 | | // |
| | 1063 | | // Read the mask if this is an incoming connection. |
| | 1064 | | // |
| 1 | 1065 | | if (_incoming) |
| | 1066 | | { |
| | 1067 | | // |
| | 1068 | | // We must have needed to read the mask. |
| | 1069 | | // |
| | 1070 | | Debug.Assert(_readBuffer.b.position() - _readBufferPos >= 4); |
| 1 | 1071 | | for (int i = 0; i < 4; ++i) |
| | 1072 | | { |
| 1 | 1073 | | _readMask[i] = _readBuffer.b.get(_readBufferPos++); // Copy the mask. |
| | 1074 | | } |
| | 1075 | | } |
| | 1076 | |
|
| 1 | 1077 | | switch (_readOpCode) |
| | 1078 | | { |
| | 1079 | | case OP_TEXT: // Text frame |
| | 1080 | | { |
| 0 | 1081 | | throw new Ice.ProtocolException("text frames not supported"); |
| | 1082 | | } |
| | 1083 | | case OP_DATA: // Data frame |
| | 1084 | | case OP_CONT: // Continuation frame |
| | 1085 | | { |
| 1 | 1086 | | if (_instance.traceLevel() >= 2) |
| | 1087 | | { |
| 1 | 1088 | | _instance.logger().trace( |
| 1 | 1089 | | _instance.traceCategory(), "received " + protocol() + |
| 1 | 1090 | | (_readOpCode == OP_DATA ? " data" : " continuation") + |
| 1 | 1091 | | " frame with payload length of " + _readPayloadLength + |
| 1 | 1092 | | " bytes\n" + ToString()); |
| | 1093 | | } |
| | 1094 | |
|
| 1 | 1095 | | if (_readPayloadLength <= 0) |
| | 1096 | | { |
| 0 | 1097 | | throw new Ice.ProtocolException("payload length is 0"); |
| | 1098 | | } |
| 1 | 1099 | | _readState = ReadStatePayload; |
| | 1100 | | Debug.Assert(buf.b.hasRemaining()); |
| 1 | 1101 | | _readFrameStart = buf.b.position(); |
| 1 | 1102 | | break; |
| | 1103 | | } |
| | 1104 | | case OP_CLOSE: // Connection close |
| | 1105 | | { |
| 1 | 1106 | | if (_instance.traceLevel() >= 2) |
| | 1107 | | { |
| 1 | 1108 | | _instance.logger().trace( |
| 1 | 1109 | | _instance.traceCategory(), |
| 1 | 1110 | | "received " + protocol() + " connection close frame\n" + ToString()); |
| | 1111 | | } |
| | 1112 | |
|
| 1 | 1113 | | _readState = ReadStateControlFrame; |
| 1 | 1114 | | int s = _nextState == StateOpened ? _state : _nextState; |
| 1 | 1115 | | if (s == StateClosingRequestPending) |
| | 1116 | | { |
| | 1117 | | // |
| | 1118 | | // If we receive a close frame while we were actually |
| | 1119 | | // waiting to send one, change the role and send a |
| | 1120 | | // close frame response. |
| | 1121 | | // |
| 1 | 1122 | | if (!_closingInitiator) |
| | 1123 | | { |
| 0 | 1124 | | _closingInitiator = true; |
| | 1125 | | } |
| 1 | 1126 | | if (_state == StateClosingRequestPending) |
| | 1127 | | { |
| 1 | 1128 | | _state = StateClosingResponsePending; |
| | 1129 | | } |
| | 1130 | | else |
| | 1131 | | { |
| 0 | 1132 | | _nextState = StateClosingResponsePending; |
| | 1133 | | } |
| 1 | 1134 | | return false; // No longer interested in reading |
| | 1135 | | } |
| | 1136 | | else |
| | 1137 | | { |
| 1 | 1138 | | throw new Ice.ConnectionLostException(); |
| | 1139 | | } |
| | 1140 | | } |
| | 1141 | | case OP_PING: |
| | 1142 | | { |
| 0 | 1143 | | if (_instance.traceLevel() >= 2) |
| | 1144 | | { |
| 0 | 1145 | | _instance.logger().trace( |
| 0 | 1146 | | _instance.traceCategory(), |
| 0 | 1147 | | "received " + protocol() + " connection ping frame\n" + ToString()); |
| | 1148 | | } |
| 0 | 1149 | | _readState = ReadStateControlFrame; |
| 0 | 1150 | | break; |
| | 1151 | | } |
| | 1152 | | case OP_PONG: // Pong |
| | 1153 | | { |
| 0 | 1154 | | if (_instance.traceLevel() >= 2) |
| | 1155 | | { |
| 0 | 1156 | | _instance.logger().trace( |
| 0 | 1157 | | _instance.traceCategory(), |
| 0 | 1158 | | "received " + protocol() + " connection pong frame\n" + ToString()); |
| | 1159 | | } |
| 0 | 1160 | | _readState = ReadStateControlFrame; |
| 0 | 1161 | | break; |
| | 1162 | | } |
| | 1163 | | default: |
| | 1164 | | { |
| 0 | 1165 | | throw new Ice.ProtocolException("unsupported opcode: " + _readOpCode); |
| | 1166 | | } |
| | 1167 | | } |
| | 1168 | | } |
| | 1169 | |
|
| 1 | 1170 | | if (_readState == ReadStateControlFrame) |
| | 1171 | | { |
| 1 | 1172 | | if (_readPayloadLength > 0 && !readBuffered(_readPayloadLength)) |
| | 1173 | | { |
| 0 | 1174 | | return true; |
| | 1175 | | } |
| | 1176 | |
|
| 1 | 1177 | | if (_readPayloadLength > 0 && _readOpCode == OP_PING) |
| | 1178 | | { |
| 0 | 1179 | | _pingPayload = new byte[_readPayloadLength]; |
| 0 | 1180 | | System.Buffer.BlockCopy( |
| 0 | 1181 | | _readBuffer.b.rawBytes(), |
| 0 | 1182 | | _readBufferPos, |
| 0 | 1183 | | _pingPayload, |
| 0 | 1184 | | 0, |
| 0 | 1185 | | _readPayloadLength); |
| | 1186 | | } |
| | 1187 | |
|
| 1 | 1188 | | _readBufferPos += _readPayloadLength; |
| 1 | 1189 | | _readPayloadLength = 0; |
| | 1190 | |
|
| 1 | 1191 | | if (_readOpCode == OP_PING) |
| | 1192 | | { |
| 0 | 1193 | | if (_state == StateOpened) |
| | 1194 | | { |
| 0 | 1195 | | _state = StatePongPending; // Send pong frame now |
| | 1196 | | } |
| 0 | 1197 | | else if (_nextState < StatePongPending) |
| | 1198 | | { |
| 0 | 1199 | | _nextState = StatePongPending; // Send pong frame next |
| | 1200 | | } |
| | 1201 | | } |
| | 1202 | |
|
| | 1203 | | // |
| | 1204 | | // We've read the payload of the PING/PONG frame, we're ready |
| | 1205 | | // to read a new frame. |
| | 1206 | | // |
| 1 | 1207 | | _readState = ReadStateOpcode; |
| | 1208 | | } |
| | 1209 | |
|
| 1 | 1210 | | if (_readState == ReadStatePayload) |
| | 1211 | | { |
| | 1212 | | // |
| | 1213 | | // This must be assigned before the check for the buffer. If the buffer is empty |
| | 1214 | | // or already read, postRead will return false. |
| | 1215 | | // |
| 1 | 1216 | | _readStart = buf.b.position(); |
| | 1217 | |
|
| 1 | 1218 | | if (buf.empty() || !buf.b.hasRemaining()) |
| | 1219 | | { |
| 0 | 1220 | | return false; |
| | 1221 | | } |
| | 1222 | |
|
| 1 | 1223 | | int n = Math.Min(_readBuffer.b.position() - _readBufferPos, buf.b.remaining()); |
| 1 | 1224 | | if (n > _readPayloadLength) |
| | 1225 | | { |
| 0 | 1226 | | n = _readPayloadLength; |
| | 1227 | | } |
| 1 | 1228 | | if (n > 0) |
| | 1229 | | { |
| 1 | 1230 | | System.Buffer.BlockCopy( |
| 1 | 1231 | | _readBuffer.b.rawBytes(), |
| 1 | 1232 | | _readBufferPos, |
| 1 | 1233 | | buf.b.rawBytes(), |
| 1 | 1234 | | buf.b.position(), |
| 1 | 1235 | | n); |
| 1 | 1236 | | buf.b.position(buf.b.position() + n); |
| 1 | 1237 | | _readBufferPos += n; |
| | 1238 | | } |
| | 1239 | |
|
| | 1240 | | // |
| | 1241 | | // Continue reading if we didn't read the full message, otherwise give back |
| | 1242 | | // the control to the connection |
| | 1243 | | // |
| 1 | 1244 | | return buf.b.hasRemaining() && n < _readPayloadLength; |
| | 1245 | | } |
| | 1246 | | } |
| | 1247 | | } |
| | 1248 | |
|
| | 1249 | | private bool postRead(Buffer buf) |
| | 1250 | | { |
| 1 | 1251 | | if (_readState != ReadStatePayload) |
| | 1252 | | { |
| 1 | 1253 | | return _readStart < _readBuffer.b.position(); // Returns true if data was read. |
| | 1254 | | } |
| | 1255 | |
|
| 1 | 1256 | | if (_readStart == buf.b.position()) |
| | 1257 | | { |
| 1 | 1258 | | return false; // Nothing was read or nothing to read. |
| | 1259 | | } |
| | 1260 | | Debug.Assert(_readStart < buf.b.position()); |
| | 1261 | |
|
| 1 | 1262 | | if (_incoming) |
| | 1263 | | { |
| | 1264 | | // |
| | 1265 | | // Unmask the data we just read. |
| | 1266 | | // |
| 1 | 1267 | | int pos = buf.b.position(); |
| 1 | 1268 | | byte[] arr = buf.b.rawBytes(); |
| 1 | 1269 | | for (int n = _readStart; n < pos; ++n) |
| | 1270 | | { |
| 1 | 1271 | | arr[n] = (byte)(arr[n] ^ _readMask[(n - _readFrameStart) % 4]); |
| | 1272 | | } |
| | 1273 | | } |
| | 1274 | |
|
| 1 | 1275 | | _readPayloadLength -= buf.b.position() - _readStart; |
| 1 | 1276 | | _readStart = buf.b.position(); |
| 1 | 1277 | | if (_readPayloadLength == 0) |
| | 1278 | | { |
| | 1279 | | // |
| | 1280 | | // We've read the complete payload, we're ready to read a new frame. |
| | 1281 | | // |
| 1 | 1282 | | _readState = ReadStateOpcode; |
| | 1283 | | } |
| 1 | 1284 | | return buf.b.hasRemaining(); |
| | 1285 | | } |
| | 1286 | |
|
| | 1287 | | private bool preWrite(Buffer buf) |
| | 1288 | | { |
| 1 | 1289 | | if (_writeState == WriteStateHeader) |
| | 1290 | | { |
| 1 | 1291 | | if (_state == StateOpened) |
| | 1292 | | { |
| 1 | 1293 | | if (buf.empty() || !buf.b.hasRemaining()) |
| | 1294 | | { |
| 1 | 1295 | | return false; |
| | 1296 | | } |
| | 1297 | |
|
| | 1298 | | Debug.Assert(buf.b.position() == 0); |
| 1 | 1299 | | prepareWriteHeader((byte)OP_DATA, buf.size()); |
| | 1300 | |
|
| 1 | 1301 | | _writeState = WriteStatePayload; |
| | 1302 | | } |
| 1 | 1303 | | else if (_state == StatePingPending) |
| | 1304 | | { |
| 0 | 1305 | | prepareWriteHeader((byte)OP_PING, 0); // Don't send any payload |
| | 1306 | |
|
| 0 | 1307 | | _writeState = WriteStateControlFrame; |
| 0 | 1308 | | _writeBuffer.b.flip(); |
| | 1309 | | } |
| 1 | 1310 | | else if (_state == StatePongPending) |
| | 1311 | | { |
| 0 | 1312 | | prepareWriteHeader((byte)OP_PONG, _pingPayload.Length); |
| 0 | 1313 | | if (_pingPayload.Length > _writeBuffer.b.remaining()) |
| | 1314 | | { |
| 0 | 1315 | | int pos = _writeBuffer.b.position(); |
| 0 | 1316 | | _writeBuffer.resize(pos + _pingPayload.Length, false); |
| 0 | 1317 | | _writeBuffer.b.position(pos); |
| | 1318 | | } |
| 0 | 1319 | | _writeBuffer.b.put(_pingPayload); |
| 0 | 1320 | | _pingPayload = []; |
| | 1321 | |
|
| 0 | 1322 | | _writeState = WriteStateControlFrame; |
| 0 | 1323 | | _writeBuffer.b.flip(); |
| | 1324 | | } |
| 1 | 1325 | | else if ((_state == StateClosingRequestPending && !_closingInitiator) || |
| 1 | 1326 | | (_state == StateClosingResponsePending && _closingInitiator)) |
| | 1327 | | { |
| 1 | 1328 | | prepareWriteHeader((byte)OP_CLOSE, 2); |
| | 1329 | |
|
| | 1330 | | // Write closing reason |
| 1 | 1331 | | _writeBuffer.b.putShort((short)_closingReason); |
| | 1332 | |
|
| 1 | 1333 | | if (!_incoming) |
| | 1334 | | { |
| | 1335 | | byte b; |
| 1 | 1336 | | int pos = _writeBuffer.b.position() - 2; |
| 1 | 1337 | | b = (byte)(_writeBuffer.b.get(pos) ^ _writeMask[0]); |
| 1 | 1338 | | _writeBuffer.b.put(pos, b); |
| 1 | 1339 | | pos++; |
| 1 | 1340 | | b = (byte)(_writeBuffer.b.get(pos) ^ _writeMask[1]); |
| 1 | 1341 | | _writeBuffer.b.put(pos, b); |
| | 1342 | | } |
| | 1343 | |
|
| 1 | 1344 | | _writeState = WriteStateControlFrame; |
| 1 | 1345 | | _writeBuffer.b.flip(); |
| | 1346 | | } |
| | 1347 | | else |
| | 1348 | | { |
| | 1349 | | Debug.Assert(_state != StateClosed); |
| 1 | 1350 | | return false; // Nothing to write in this state |
| | 1351 | | } |
| | 1352 | |
|
| 1 | 1353 | | _writePayloadLength = 0; |
| | 1354 | | } |
| | 1355 | |
|
| 1 | 1356 | | if (_writeState == WriteStatePayload) |
| | 1357 | | { |
| | 1358 | | // |
| | 1359 | | // For an outgoing connection, each message must be masked with a random |
| | 1360 | | // 32-bit value, so we copy the entire message into the internal buffer |
| | 1361 | | // for writing. For incoming connections, we just copy the start of the |
| | 1362 | | // message in the internal buffer after the hedaer. If the message is |
| | 1363 | | // larger, the reminder is sent directly from the message buffer to avoid |
| | 1364 | | // copying. |
| | 1365 | | // |
| 1 | 1366 | | if (!_incoming && (_writePayloadLength == 0 || !_writeBuffer.b.hasRemaining())) |
| | 1367 | | { |
| 1 | 1368 | | if (!_writeBuffer.b.hasRemaining()) |
| | 1369 | | { |
| 1 | 1370 | | _writeBuffer.b.position(0); |
| | 1371 | | } |
| | 1372 | |
|
| 1 | 1373 | | int n = buf.b.position(); |
| 1 | 1374 | | int sz = buf.size(); |
| 1 | 1375 | | int pos = _writeBuffer.b.position(); |
| 1 | 1376 | | int count = Math.Min(sz - n, _writeBuffer.b.remaining()); |
| 1 | 1377 | | byte[] src = buf.b.rawBytes(); |
| 1 | 1378 | | byte[] dest = _writeBuffer.b.rawBytes(); |
| 1 | 1379 | | for (int i = 0; i < count; ++i, ++n, ++pos) |
| | 1380 | | { |
| 1 | 1381 | | dest[pos] = (byte)(src[n] ^ _writeMask[n % 4]); |
| | 1382 | | } |
| 1 | 1383 | | _writeBuffer.b.position(pos); |
| 1 | 1384 | | _writePayloadLength = n; |
| | 1385 | |
|
| 1 | 1386 | | _writeBuffer.b.flip(); |
| | 1387 | | } |
| 1 | 1388 | | else if (_writePayloadLength == 0) |
| | 1389 | | { |
| | 1390 | | Debug.Assert(_incoming); |
| 1 | 1391 | | if (_writeBuffer.b.hasRemaining()) |
| | 1392 | | { |
| | 1393 | | Debug.Assert(buf.b.position() == 0); |
| 1 | 1394 | | int n = Math.Min(_writeBuffer.b.remaining(), buf.b.remaining()); |
| 1 | 1395 | | int pos = _writeBuffer.b.position(); |
| 1 | 1396 | | System.Buffer.BlockCopy(buf.b.rawBytes(), 0, _writeBuffer.b.rawBytes(), pos, n); |
| 1 | 1397 | | _writeBuffer.b.position(pos + n); |
| 1 | 1398 | | _writePayloadLength = n; |
| | 1399 | | } |
| 1 | 1400 | | _writeBuffer.b.flip(); |
| | 1401 | | } |
| 1 | 1402 | | return true; |
| | 1403 | | } |
| 1 | 1404 | | else if (_writeState == WriteStateControlFrame) |
| | 1405 | | { |
| 1 | 1406 | | return _writeBuffer.b.hasRemaining(); |
| | 1407 | | } |
| | 1408 | | else |
| | 1409 | | { |
| | 1410 | | Debug.Assert(_writeState == WriteStateFlush); |
| 0 | 1411 | | return true; |
| | 1412 | | } |
| | 1413 | | } |
| | 1414 | |
|
| | 1415 | | private bool postWrite(Buffer buf, int status) |
| | 1416 | | { |
| 1 | 1417 | | if (_state > StateOpened && _writeState == WriteStateControlFrame) |
| | 1418 | | { |
| 1 | 1419 | | if (!_writeBuffer.b.hasRemaining()) |
| | 1420 | | { |
| 1 | 1421 | | if (_state == StatePingPending) |
| | 1422 | | { |
| 0 | 1423 | | if (_instance.traceLevel() >= 2) |
| | 1424 | | { |
| 0 | 1425 | | _instance.logger().trace( |
| 0 | 1426 | | _instance.traceCategory(), |
| 0 | 1427 | | "sent " + protocol() + " connection ping frame\n" + ToString()); |
| | 1428 | | } |
| | 1429 | | } |
| 1 | 1430 | | else if (_state == StatePongPending) |
| | 1431 | | { |
| 0 | 1432 | | if (_instance.traceLevel() >= 2) |
| | 1433 | | { |
| 0 | 1434 | | _instance.logger().trace( |
| 0 | 1435 | | _instance.traceCategory(), |
| 0 | 1436 | | "sent " + protocol() + " connection pong frame\n" + ToString()); |
| | 1437 | | } |
| | 1438 | | } |
| 1 | 1439 | | else if ((_state == StateClosingRequestPending && !_closingInitiator) || |
| 1 | 1440 | | (_state == StateClosingResponsePending && _closingInitiator)) |
| | 1441 | | { |
| 1 | 1442 | | if (_instance.traceLevel() >= 2) |
| | 1443 | | { |
| 1 | 1444 | | _instance.logger().trace( |
| 1 | 1445 | | _instance.traceCategory(), |
| 1 | 1446 | | "sent " + protocol() + " connection close frame\n" + ToString()); |
| | 1447 | | } |
| | 1448 | |
|
| 1 | 1449 | | if (_state == StateClosingRequestPending && !_closingInitiator) |
| | 1450 | | { |
| 1 | 1451 | | _writeState = WriteStateHeader; |
| 1 | 1452 | | _state = StateClosingResponsePending; |
| 1 | 1453 | | return false; |
| | 1454 | | } |
| | 1455 | | else |
| | 1456 | | { |
| 1 | 1457 | | throw new Ice.ConnectionLostException(); |
| | 1458 | | } |
| | 1459 | | } |
| 0 | 1460 | | else if (_state == StateClosed) |
| | 1461 | | { |
| 0 | 1462 | | return false; |
| | 1463 | | } |
| | 1464 | |
|
| 0 | 1465 | | _state = _nextState; |
| 0 | 1466 | | _nextState = StateOpened; |
| 0 | 1467 | | _writeState = WriteStateHeader; |
| | 1468 | | } |
| | 1469 | | else |
| | 1470 | | { |
| 1 | 1471 | | return status == SocketOperation.None; |
| | 1472 | | } |
| | 1473 | | } |
| | 1474 | |
|
| 1 | 1475 | | if ((!_incoming || buf.b.position() == 0) && _writePayloadLength > 0) |
| | 1476 | | { |
| 1 | 1477 | | if (!_writeBuffer.b.hasRemaining()) |
| | 1478 | | { |
| 1 | 1479 | | buf.b.position(_writePayloadLength); |
| | 1480 | | } |
| | 1481 | | } |
| | 1482 | |
|
| 1 | 1483 | | if (status == SocketOperation.Write && !buf.b.hasRemaining() && !_writeBuffer.b.hasRemaining()) |
| | 1484 | | { |
| | 1485 | | // |
| | 1486 | | // Our buffers are empty but the delegate needs another call to write(). |
| | 1487 | | // |
| 0 | 1488 | | _writeState = WriteStateFlush; |
| 0 | 1489 | | return false; |
| | 1490 | | } |
| 1 | 1491 | | else if (!buf.b.hasRemaining()) |
| | 1492 | | { |
| 1 | 1493 | | _writeState = WriteStateHeader; |
| 1 | 1494 | | if (_state == StatePingPending || |
| 1 | 1495 | | _state == StatePongPending || |
| 1 | 1496 | | (_state == StateClosingRequestPending && !_closingInitiator) || |
| 1 | 1497 | | (_state == StateClosingResponsePending && _closingInitiator)) |
| | 1498 | | { |
| 1 | 1499 | | return true; |
| | 1500 | | } |
| | 1501 | | } |
| 1 | 1502 | | else if (_state == StateOpened) |
| | 1503 | | { |
| 1 | 1504 | | return status == SocketOperation.None; |
| | 1505 | | } |
| | 1506 | |
|
| 1 | 1507 | | return false; |
| | 1508 | | } |
| | 1509 | |
|
| | 1510 | | private bool readBuffered(int sz) |
| | 1511 | | { |
| 1 | 1512 | | if (_readBufferPos == _readBuffer.b.position()) |
| | 1513 | | { |
| 1 | 1514 | | _readBuffer.resize(_readBufferSize, true); |
| 1 | 1515 | | _readBufferPos = 0; |
| 1 | 1516 | | _readBuffer.b.position(0); |
| | 1517 | | } |
| | 1518 | | else |
| | 1519 | | { |
| 1 | 1520 | | int available = _readBuffer.b.position() - _readBufferPos; |
| 1 | 1521 | | if (available < sz) |
| | 1522 | | { |
| 1 | 1523 | | if (_readBufferPos > 0) |
| | 1524 | | { |
| 1 | 1525 | | _readBuffer.b.limit(_readBuffer.b.position()); |
| 1 | 1526 | | _readBuffer.b.position(_readBufferPos); |
| 1 | 1527 | | _readBuffer.b.compact(); |
| | 1528 | | Debug.Assert(_readBuffer.b.position() == available); |
| | 1529 | | } |
| 1 | 1530 | | _readBuffer.resize(Math.Max(_readBufferSize, sz), true); |
| 1 | 1531 | | _readBufferPos = 0; |
| 1 | 1532 | | _readBuffer.b.position(available); |
| | 1533 | | } |
| | 1534 | | } |
| | 1535 | |
|
| 1 | 1536 | | _readStart = _readBuffer.b.position(); |
| 1 | 1537 | | if (_readBufferPos + sz > _readBuffer.b.position()) |
| | 1538 | | { |
| 1 | 1539 | | return false; // Not enough read. |
| | 1540 | | } |
| | 1541 | | Debug.Assert(_readBuffer.b.position() > _readBufferPos); |
| 1 | 1542 | | return true; |
| | 1543 | | } |
| | 1544 | |
|
| | 1545 | | private void prepareWriteHeader(byte opCode, int payloadLength) |
| | 1546 | | { |
| | 1547 | | // |
| | 1548 | | // We need to prepare the frame header. |
| | 1549 | | // |
| 1 | 1550 | | _writeBuffer.resize(_writeBufferSize, false); |
| 1 | 1551 | | _writeBuffer.b.limit(_writeBufferSize); |
| 1 | 1552 | | _writeBuffer.b.position(0); |
| | 1553 | |
|
| | 1554 | | // |
| | 1555 | | // Set the opcode - this is the one and only data frame. |
| | 1556 | | // |
| 1 | 1557 | | _writeBuffer.b.put((byte)(opCode | FLAG_FINAL)); |
| | 1558 | |
|
| | 1559 | | // |
| | 1560 | | // Set the payload length. |
| | 1561 | | // |
| 1 | 1562 | | if (payloadLength <= 125) |
| | 1563 | | { |
| 1 | 1564 | | _writeBuffer.b.put((byte)payloadLength); |
| | 1565 | | } |
| 1 | 1566 | | else if (payloadLength > 125 && payloadLength <= 65535) |
| | 1567 | | { |
| | 1568 | | // |
| | 1569 | | // Use an extra 16 bits to encode the payload length. |
| | 1570 | | // |
| 1 | 1571 | | _writeBuffer.b.put(126); |
| 1 | 1572 | | _writeBuffer.b.putShort((short)payloadLength); |
| | 1573 | | } |
| 1 | 1574 | | else if (payloadLength > 65535) |
| | 1575 | | { |
| | 1576 | | // |
| | 1577 | | // Use an extra 64 bits to encode the payload length. |
| | 1578 | | // |
| 1 | 1579 | | _writeBuffer.b.put(127); |
| 1 | 1580 | | _writeBuffer.b.putLong(payloadLength); |
| | 1581 | | } |
| | 1582 | |
|
| 1 | 1583 | | if (!_incoming) |
| | 1584 | | { |
| | 1585 | | // |
| | 1586 | | // Add a random 32-bit mask to every outgoing frame, copy the payload data, |
| | 1587 | | // and apply the mask. |
| | 1588 | | // |
| 1 | 1589 | | _writeBuffer.b.put(1, (byte)(_writeBuffer.b.get(1) | FLAG_MASKED)); |
| 1 | 1590 | | _rand.NextBytes(_writeMask); |
| 1 | 1591 | | _writeBuffer.b.put(_writeMask); |
| | 1592 | | } |
| 1 | 1593 | | } |
| | 1594 | |
|
| | 1595 | | private ProtocolInstance _instance; |
| | 1596 | | private Transceiver _delegate; |
| | 1597 | | private readonly string _host; |
| | 1598 | | private string _resource; |
| | 1599 | | private readonly bool _incoming; |
| | 1600 | |
|
| | 1601 | | private const int StateInitializeDelegate = 0; |
| | 1602 | | private const int StateConnected = 1; |
| | 1603 | | private const int StateUpgradeRequestPending = 2; |
| | 1604 | | private const int StateUpgradeResponsePending = 3; |
| | 1605 | | private const int StateOpened = 4; |
| | 1606 | | private const int StatePingPending = 5; |
| | 1607 | | private const int StatePongPending = 6; |
| | 1608 | | private const int StateClosingRequestPending = 7; |
| | 1609 | | private const int StateClosingResponsePending = 8; |
| | 1610 | | private const int StateClosed = 9; |
| | 1611 | |
|
| | 1612 | | private int _state; |
| | 1613 | | private int _nextState; |
| | 1614 | |
|
| | 1615 | | private HttpParser _parser; |
| | 1616 | | private string _key; |
| | 1617 | |
|
| | 1618 | | private const int ReadStateOpcode = 0; |
| | 1619 | | private const int ReadStateHeader = 1; |
| | 1620 | | private const int ReadStateControlFrame = 2; |
| | 1621 | | private const int ReadStatePayload = 3; |
| | 1622 | |
|
| | 1623 | | private int _readState; |
| | 1624 | | private Buffer _readBuffer; |
| | 1625 | | private int _readBufferPos; |
| | 1626 | | private int _readBufferSize; |
| | 1627 | |
|
| | 1628 | | private bool _readLastFrame; |
| | 1629 | | private int _readOpCode; |
| | 1630 | | private int _readHeaderLength; |
| | 1631 | | private int _readPayloadLength; |
| | 1632 | | private int _readStart; |
| | 1633 | | private int _readFrameStart; |
| | 1634 | | private byte[] _readMask; |
| | 1635 | |
|
| | 1636 | | private const int WriteStateHeader = 0; |
| | 1637 | | private const int WriteStatePayload = 1; |
| | 1638 | | private const int WriteStateControlFrame = 2; |
| | 1639 | | private const int WriteStateFlush = 3; |
| | 1640 | |
|
| | 1641 | | private int _writeState; |
| | 1642 | | private Buffer _writeBuffer; |
| | 1643 | | private int _writeBufferSize; |
| | 1644 | | private byte[] _writeMask; |
| | 1645 | | private int _writePayloadLength; |
| | 1646 | |
|
| | 1647 | | private bool _closingInitiator; |
| | 1648 | | private int _closingReason; |
| | 1649 | |
|
| | 1650 | | private bool _readPending; |
| | 1651 | | private bool _finishRead; |
| | 1652 | | private bool _writePending; |
| | 1653 | |
|
| | 1654 | | private byte[] _pingPayload; |
| | 1655 | |
|
| | 1656 | | private Random _rand; |
| | 1657 | |
|
| | 1658 | | // |
| | 1659 | | // WebSocket opcodes |
| | 1660 | | // |
| | 1661 | | private const int OP_CONT = 0x0; // Continuation frame |
| | 1662 | | private const int OP_TEXT = 0x1; // Text frame |
| | 1663 | | private const int OP_DATA = 0x2; // Data frame |
| | 1664 | | // private const int OP_RES_0x3 = 0x3; // Reserved |
| | 1665 | | // private const int OP_RES_0x4 = 0x4; // Reserved |
| | 1666 | | // private const int OP_RES_0x5 = 0x5; // Reserved |
| | 1667 | | // private const int OP_RES_0x6 = 0x6; // Reserved |
| | 1668 | | // private const int OP_RES_0x7 = 0x7; // Reserved |
| | 1669 | | private const int OP_CLOSE = 0x8; // Connection close |
| | 1670 | | private const int OP_PING = 0x9; // Ping |
| | 1671 | | private const int OP_PONG = 0xA; // Pong |
| | 1672 | | // private const int OP_RES_0xB = 0xB; // Reserved |
| | 1673 | | // private const int OP_RES_0xC = 0xC; // Reserved |
| | 1674 | | // private const int OP_RES_0xD = 0xD; // Reserved |
| | 1675 | | // private const int OP_RES_0xE = 0xE; // Reserved |
| | 1676 | | // private const int OP_RES_0xF = 0xF; // Reserved |
| | 1677 | | private const int FLAG_FINAL = 0x80; // Last frame |
| | 1678 | | private const int FLAG_MASKED = 0x80; // Payload is masked |
| | 1679 | |
|
| | 1680 | | private const int CLOSURE_NORMAL = 1000; |
| | 1681 | | private const int CLOSURE_SHUTDOWN = 1001; |
| | 1682 | | private const int CLOSURE_PROTOCOL_ERROR = 1002; |
| | 1683 | |
|
| | 1684 | | private const string _iceProtocol = "ice.zeroc.com"; |
| | 1685 | | private const string _wsUUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; |
| | 1686 | |
|
| 1 | 1687 | | private static readonly UTF8Encoding _utf8 = new UTF8Encoding(false, true); |
| | 1688 | | } |