| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | using System.Diagnostics; |
| | | 4 | | using System.Net; |
| | | 5 | | using System.Net.Sockets; |
| | | 6 | | |
| | | 7 | | namespace Ice.Internal; |
| | | 8 | | |
| | | 9 | | #pragma warning disable CA1001 // _readEventArgs and _writeEventArgs are disposed by destroy. |
| | | 10 | | internal sealed class StreamSocket |
| | | 11 | | #pragma warning restore CA1001 |
| | | 12 | | { |
| | 1 | 13 | | public StreamSocket( |
| | 1 | 14 | | ProtocolInstance instance, |
| | 1 | 15 | | NetworkProxy proxy, |
| | 1 | 16 | | EndPoint addr, |
| | 1 | 17 | | EndPoint sourceAddr, |
| | 1 | 18 | | TcpBufSize bufSize) |
| | | 19 | | { |
| | 1 | 20 | | _instance = instance; |
| | 1 | 21 | | _proxy = proxy; |
| | 1 | 22 | | _addr = addr; |
| | 1 | 23 | | _sourceAddr = sourceAddr; |
| | 1 | 24 | | _fd = Network.createSocket(false, (_proxy != null ? _proxy.getAddress() : _addr).AddressFamily); |
| | 1 | 25 | | _state = StateNeedConnect; |
| | | 26 | | |
| | 1 | 27 | | init(bufSize); |
| | 1 | 28 | | } |
| | | 29 | | |
| | 1 | 30 | | public StreamSocket(ProtocolInstance instance, Socket fd, TcpBufSize bufSize) |
| | | 31 | | { |
| | 1 | 32 | | _instance = instance; |
| | 1 | 33 | | _fd = fd; |
| | 1 | 34 | | _state = StateConnected; |
| | | 35 | | try |
| | | 36 | | { |
| | 1 | 37 | | _desc = Network.fdToString(_fd); |
| | 1 | 38 | | } |
| | 0 | 39 | | catch (System.Exception) |
| | | 40 | | { |
| | 0 | 41 | | Network.closeSocketNoThrow(_fd); |
| | 0 | 42 | | throw; |
| | | 43 | | } |
| | 1 | 44 | | init(bufSize); |
| | 1 | 45 | | } |
| | | 46 | | |
| | | 47 | | public int connect(Buffer readBuffer, Buffer writeBuffer, ref bool moreData) |
| | | 48 | | { |
| | 1 | 49 | | if (_state == StateNeedConnect) |
| | | 50 | | { |
| | 1 | 51 | | _state = StateConnectPending; |
| | 1 | 52 | | return SocketOperation.Connect; |
| | | 53 | | } |
| | 1 | 54 | | else if (_state <= StateConnectPending) |
| | | 55 | | { |
| | 1 | 56 | | if (_writeEventArgs.SocketError != SocketError.Success) |
| | | 57 | | { |
| | 1 | 58 | | var ex = new System.Net.Sockets.SocketException((int)_writeEventArgs.SocketError); |
| | 1 | 59 | | if (Network.connectionRefused(ex)) |
| | | 60 | | { |
| | 1 | 61 | | throw new Ice.ConnectionRefusedException(_addr, ex); |
| | | 62 | | } |
| | | 63 | | else |
| | | 64 | | { |
| | 0 | 65 | | throw new Ice.ConnectFailedException(_addr, ex); |
| | | 66 | | } |
| | | 67 | | } |
| | 1 | 68 | | _desc = Network.fdToString(_fd, _proxy, _addr); |
| | 1 | 69 | | _state = _proxy != null ? StateProxyWrite : StateConnected; |
| | | 70 | | } |
| | | 71 | | |
| | 1 | 72 | | if (_state == StateProxyWrite) |
| | | 73 | | { |
| | 1 | 74 | | _proxy.beginWrite(_addr, writeBuffer); |
| | 1 | 75 | | return SocketOperation.Write; |
| | | 76 | | } |
| | 1 | 77 | | else if (_state == StateProxyRead) |
| | | 78 | | { |
| | 1 | 79 | | _proxy.beginRead(readBuffer); |
| | 1 | 80 | | return SocketOperation.Read; |
| | | 81 | | } |
| | 1 | 82 | | else if (_state == StateProxyConnected) |
| | | 83 | | { |
| | 1 | 84 | | _proxy.finish(readBuffer, writeBuffer); |
| | | 85 | | |
| | 1 | 86 | | readBuffer.clear(); |
| | 1 | 87 | | writeBuffer.clear(); |
| | | 88 | | |
| | 1 | 89 | | _state = StateConnected; |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | Debug.Assert(_state == StateConnected); |
| | 1 | 93 | | return SocketOperation.None; |
| | | 94 | | } |
| | | 95 | | |
| | 1 | 96 | | public Socket fd() => _fd; |
| | | 97 | | |
| | 1 | 98 | | public void setBufferSize(int rcvSize, int sndSize) => Network.setTcpBufSize(_fd, rcvSize, sndSize, _instance); |
| | | 99 | | |
| | | 100 | | public int read(Buffer buf) |
| | | 101 | | { |
| | 1 | 102 | | if (_state == StateProxyRead) |
| | | 103 | | { |
| | | 104 | | while (true) |
| | | 105 | | { |
| | 1 | 106 | | int ret = read(buf.b); |
| | 1 | 107 | | if (ret == 0) |
| | | 108 | | { |
| | 1 | 109 | | return SocketOperation.Read; |
| | | 110 | | } |
| | | 111 | | |
| | 1 | 112 | | _state = toState(_proxy.endRead(buf)); |
| | 1 | 113 | | if (_state != StateProxyRead) |
| | | 114 | | { |
| | 1 | 115 | | return SocketOperation.None; |
| | | 116 | | } |
| | | 117 | | } |
| | | 118 | | } |
| | 1 | 119 | | read(buf.b); |
| | 1 | 120 | | return buf.b.hasRemaining() ? SocketOperation.Read : SocketOperation.None; |
| | | 121 | | } |
| | | 122 | | |
| | | 123 | | public int write(Buffer buf) |
| | | 124 | | { |
| | 1 | 125 | | if (_state == StateProxyWrite) |
| | | 126 | | { |
| | | 127 | | while (true) |
| | | 128 | | { |
| | 0 | 129 | | int ret = write(buf.b); |
| | 0 | 130 | | if (ret == 0) |
| | | 131 | | { |
| | 0 | 132 | | return SocketOperation.Write; |
| | | 133 | | } |
| | 0 | 134 | | _state = toState(_proxy.endWrite(buf)); |
| | 0 | 135 | | if (_state != StateProxyWrite) |
| | | 136 | | { |
| | 0 | 137 | | return SocketOperation.None; |
| | | 138 | | } |
| | | 139 | | } |
| | | 140 | | } |
| | 1 | 141 | | write(buf.b); |
| | 1 | 142 | | return buf.b.hasRemaining() ? SocketOperation.Write : SocketOperation.None; |
| | | 143 | | } |
| | | 144 | | |
| | | 145 | | public bool startRead(Buffer buf, AsyncCallback callback, object state) |
| | | 146 | | { |
| | | 147 | | Debug.Assert(_fd != null && _readEventArgs != null); |
| | | 148 | | |
| | | 149 | | try |
| | | 150 | | { |
| | 1 | 151 | | _readCallback = callback; |
| | 1 | 152 | | _readEventArgs.UserToken = state; |
| | 1 | 153 | | _readEventArgs.SetBuffer(buf.b.rawBytes(), buf.b.position(), buf.b.remaining()); |
| | 1 | 154 | | return !_fd.ReceiveAsync(_readEventArgs); |
| | | 155 | | } |
| | 0 | 156 | | catch (System.Net.Sockets.SocketException ex) |
| | | 157 | | { |
| | 0 | 158 | | if (Network.connectionLost(ex)) |
| | | 159 | | { |
| | 0 | 160 | | throw new Ice.ConnectionLostException(_addr, ex); |
| | | 161 | | } |
| | 0 | 162 | | throw new Ice.SocketException(ex); |
| | | 163 | | } |
| | 1 | 164 | | } |
| | | 165 | | |
| | | 166 | | public void finishRead(Buffer buf) |
| | | 167 | | { |
| | 1 | 168 | | if (_fd == null) // Transceiver was closed |
| | | 169 | | { |
| | 0 | 170 | | return; |
| | | 171 | | } |
| | | 172 | | |
| | | 173 | | Debug.Assert(_fd != null && _readEventArgs != null); |
| | | 174 | | try |
| | | 175 | | { |
| | 1 | 176 | | if (_readEventArgs.SocketError != SocketError.Success) |
| | | 177 | | { |
| | 1 | 178 | | throw new System.Net.Sockets.SocketException((int)_readEventArgs.SocketError); |
| | | 179 | | } |
| | 1 | 180 | | int ret = _readEventArgs.BytesTransferred; |
| | 1 | 181 | | _readEventArgs.SetBuffer(null, 0, 0); |
| | | 182 | | |
| | 1 | 183 | | if (ret == 0) |
| | | 184 | | { |
| | 1 | 185 | | throw new Ice.ConnectionLostException(_addr); |
| | | 186 | | } |
| | | 187 | | |
| | | 188 | | Debug.Assert(ret > 0); |
| | 1 | 189 | | buf.b.position(buf.b.position() + ret); |
| | | 190 | | |
| | 1 | 191 | | if (_state == StateProxyRead) |
| | | 192 | | { |
| | 1 | 193 | | _state = toState(_proxy.endRead(buf)); |
| | | 194 | | } |
| | 1 | 195 | | } |
| | 1 | 196 | | catch (System.Net.Sockets.SocketException ex) |
| | | 197 | | { |
| | 1 | 198 | | if (Network.connectionLost(ex)) |
| | | 199 | | { |
| | 1 | 200 | | throw new Ice.ConnectionLostException(_addr, ex); |
| | | 201 | | } |
| | 0 | 202 | | throw new Ice.SocketException(ex); |
| | | 203 | | } |
| | 0 | 204 | | catch (ObjectDisposedException ex) |
| | | 205 | | { |
| | 0 | 206 | | throw new Ice.ConnectionLostException(_addr, ex); |
| | | 207 | | } |
| | 1 | 208 | | } |
| | | 209 | | |
| | | 210 | | public bool startWrite(Buffer buf, AsyncCallback callback, object state, out bool messageWritten) |
| | | 211 | | { |
| | | 212 | | Debug.Assert(_fd != null && _writeEventArgs != null); |
| | 1 | 213 | | if (_state == StateConnectPending) |
| | | 214 | | { |
| | 1 | 215 | | messageWritten = false; |
| | 1 | 216 | | _writeCallback = callback; |
| | | 217 | | try |
| | | 218 | | { |
| | 1 | 219 | | EndPoint addr = _proxy != null ? _proxy.getAddress() : _addr; |
| | 1 | 220 | | if (_sourceAddr != null) |
| | | 221 | | { |
| | 0 | 222 | | Network.doBind(_fd, _sourceAddr); |
| | | 223 | | } |
| | 1 | 224 | | _writeEventArgs.RemoteEndPoint = addr; |
| | 1 | 225 | | _writeEventArgs.UserToken = state; |
| | 1 | 226 | | return !_fd.ConnectAsync(_writeEventArgs); |
| | | 227 | | } |
| | 0 | 228 | | catch (System.Exception ex) |
| | | 229 | | { |
| | 0 | 230 | | throw new Ice.SocketException(ex); |
| | | 231 | | } |
| | | 232 | | } |
| | | 233 | | |
| | | 234 | | try |
| | | 235 | | { |
| | 1 | 236 | | _writeCallback = callback; |
| | 1 | 237 | | _writeEventArgs.UserToken = state; |
| | 1 | 238 | | _writeEventArgs.SetBuffer(buf.b.rawBytes(), buf.b.position(), buf.b.remaining()); |
| | 1 | 239 | | bool completedSynchronously = !_fd.SendAsync(_writeEventArgs); |
| | 1 | 240 | | messageWritten = true; |
| | 1 | 241 | | return completedSynchronously; |
| | | 242 | | } |
| | 0 | 243 | | catch (System.Net.Sockets.SocketException ex) |
| | | 244 | | { |
| | 0 | 245 | | if (Network.connectionLost(ex)) |
| | | 246 | | { |
| | 0 | 247 | | throw new Ice.ConnectionLostException(_addr, ex); |
| | | 248 | | } |
| | 0 | 249 | | throw new Ice.SocketException(ex); |
| | | 250 | | } |
| | 0 | 251 | | catch (ObjectDisposedException ex) |
| | | 252 | | { |
| | 0 | 253 | | throw new Ice.ConnectionLostException(_addr, ex); |
| | | 254 | | } |
| | 1 | 255 | | } |
| | | 256 | | |
| | | 257 | | public void finishWrite(Buffer buf) |
| | | 258 | | { |
| | 1 | 259 | | if (_fd == null) // Transceiver was closed |
| | | 260 | | { |
| | 0 | 261 | | buf.b.position(buf.b.limit()); // Assume all the data was sent for at-most-once semantics. |
| | 0 | 262 | | return; |
| | | 263 | | } |
| | | 264 | | |
| | | 265 | | Debug.Assert(_fd != null && _writeEventArgs != null); |
| | | 266 | | |
| | 1 | 267 | | if (_state < StateConnected && _state != StateProxyWrite) |
| | | 268 | | { |
| | 1 | 269 | | return; |
| | | 270 | | } |
| | | 271 | | |
| | | 272 | | try |
| | | 273 | | { |
| | 1 | 274 | | if (_writeEventArgs.SocketError != SocketError.Success) |
| | | 275 | | { |
| | 1 | 276 | | throw new System.Net.Sockets.SocketException((int)_writeEventArgs.SocketError); |
| | | 277 | | } |
| | 1 | 278 | | int ret = _writeEventArgs.BytesTransferred; |
| | 1 | 279 | | _writeEventArgs.SetBuffer(null, 0, 0); |
| | 1 | 280 | | if (ret == 0) |
| | | 281 | | { |
| | 0 | 282 | | throw new Ice.ConnectionLostException(_addr); |
| | | 283 | | } |
| | | 284 | | |
| | | 285 | | Debug.Assert(ret > 0); |
| | 1 | 286 | | buf.b.position(buf.b.position() + ret); |
| | | 287 | | |
| | 1 | 288 | | if (_state == StateProxyWrite) |
| | | 289 | | { |
| | 1 | 290 | | _state = toState(_proxy.endWrite(buf)); |
| | | 291 | | } |
| | 1 | 292 | | } |
| | 1 | 293 | | catch (System.Net.Sockets.SocketException ex) |
| | | 294 | | { |
| | 1 | 295 | | if (Network.connectionLost(ex)) |
| | | 296 | | { |
| | 1 | 297 | | throw new Ice.ConnectionLostException(_addr, ex); |
| | | 298 | | } |
| | | 299 | | |
| | 0 | 300 | | throw new Ice.SocketException(ex); |
| | | 301 | | } |
| | 0 | 302 | | catch (ObjectDisposedException ex) |
| | | 303 | | { |
| | 0 | 304 | | throw new Ice.ConnectionLostException(_addr, ex); |
| | | 305 | | } |
| | 1 | 306 | | } |
| | | 307 | | |
| | | 308 | | public void close() |
| | | 309 | | { |
| | | 310 | | Debug.Assert(_fd != null); |
| | | 311 | | try |
| | | 312 | | { |
| | 1 | 313 | | Network.closeSocket(_fd); |
| | 1 | 314 | | } |
| | | 315 | | finally |
| | | 316 | | { |
| | 1 | 317 | | _fd = null; |
| | 1 | 318 | | } |
| | 1 | 319 | | } |
| | | 320 | | |
| | | 321 | | public void destroy() |
| | | 322 | | { |
| | | 323 | | Debug.Assert(_readEventArgs != null && _writeEventArgs != null); |
| | 1 | 324 | | _readEventArgs.Dispose(); |
| | 1 | 325 | | _writeEventArgs.Dispose(); |
| | 1 | 326 | | } |
| | | 327 | | |
| | 1 | 328 | | public override string ToString() => _desc; |
| | | 329 | | |
| | | 330 | | private int read(ByteBuffer buf) |
| | | 331 | | { |
| | | 332 | | Debug.Assert(_fd != null); |
| | 1 | 333 | | int read = 0; |
| | 1 | 334 | | while (buf.hasRemaining()) |
| | | 335 | | { |
| | | 336 | | try |
| | | 337 | | { |
| | 1 | 338 | | int ret = _fd.Receive(buf.rawBytes(), buf.position(), buf.remaining(), SocketFlags.None); |
| | 1 | 339 | | if (ret == 0) |
| | | 340 | | { |
| | 1 | 341 | | throw new Ice.ConnectionLostException(_addr); |
| | | 342 | | } |
| | 1 | 343 | | read += ret; |
| | 1 | 344 | | buf.position(buf.position() + ret); |
| | 1 | 345 | | } |
| | 1 | 346 | | catch (System.Net.Sockets.SocketException ex) |
| | | 347 | | { |
| | 1 | 348 | | if (Network.wouldBlock(ex)) |
| | | 349 | | { |
| | 1 | 350 | | return read; |
| | | 351 | | } |
| | 1 | 352 | | else if (Network.interrupted(ex)) |
| | | 353 | | { |
| | 0 | 354 | | continue; |
| | | 355 | | } |
| | 1 | 356 | | else if (Network.connectionLost(ex)) |
| | | 357 | | { |
| | 1 | 358 | | throw new ConnectionLostException(_addr, ex); |
| | | 359 | | } |
| | | 360 | | |
| | 0 | 361 | | throw new SocketException(ex); |
| | | 362 | | } |
| | | 363 | | } |
| | 1 | 364 | | return read; |
| | 1 | 365 | | } |
| | | 366 | | |
| | | 367 | | private int write(ByteBuffer buf) |
| | | 368 | | { |
| | | 369 | | Debug.Assert(_fd != null); |
| | 1 | 370 | | int sent = 0; |
| | 1 | 371 | | while (buf.hasRemaining()) |
| | | 372 | | { |
| | | 373 | | try |
| | | 374 | | { |
| | 1 | 375 | | int ret = _fd.Send(buf.rawBytes(), buf.position(), buf.remaining(), SocketFlags.None); |
| | | 376 | | Debug.Assert(ret > 0); |
| | | 377 | | |
| | 1 | 378 | | sent += ret; |
| | 1 | 379 | | buf.position(buf.position() + ret); |
| | 1 | 380 | | } |
| | 1 | 381 | | catch (System.Net.Sockets.SocketException ex) |
| | | 382 | | { |
| | 1 | 383 | | if (Network.wouldBlock(ex)) |
| | | 384 | | { |
| | 1 | 385 | | return sent; |
| | | 386 | | } |
| | 1 | 387 | | else if (Network.connectionLost(ex)) |
| | | 388 | | { |
| | 1 | 389 | | throw new Ice.ConnectionLostException(_addr, ex); |
| | | 390 | | } |
| | 0 | 391 | | throw new Ice.SocketException(ex); |
| | | 392 | | } |
| | | 393 | | } |
| | 1 | 394 | | return sent; |
| | 1 | 395 | | } |
| | | 396 | | |
| | | 397 | | private void ioCompleted(object sender, SocketAsyncEventArgs e) |
| | | 398 | | { |
| | 1 | 399 | | switch (e.LastOperation) |
| | | 400 | | { |
| | | 401 | | case SocketAsyncOperation.Receive: |
| | 1 | 402 | | _readCallback(e.UserToken); |
| | 1 | 403 | | break; |
| | | 404 | | case SocketAsyncOperation.Send: |
| | | 405 | | case SocketAsyncOperation.Connect: |
| | 1 | 406 | | _writeCallback(e.UserToken); |
| | 1 | 407 | | break; |
| | | 408 | | default: |
| | 0 | 409 | | throw new ArgumentException("The last operation completed on the socket was not a receive or send"); |
| | | 410 | | } |
| | | 411 | | } |
| | | 412 | | |
| | | 413 | | private void init(TcpBufSize bufSize) |
| | | 414 | | { |
| | 1 | 415 | | Network.setBlock(_fd, false); |
| | 1 | 416 | | Network.setTcpBufSize(_fd, bufSize.rcvSize, bufSize.sndSize, _instance); |
| | | 417 | | |
| | 1 | 418 | | _readEventArgs = new SocketAsyncEventArgs(); |
| | 1 | 419 | | _readEventArgs.Completed += new EventHandler<SocketAsyncEventArgs>(ioCompleted); |
| | | 420 | | |
| | 1 | 421 | | _writeEventArgs = new SocketAsyncEventArgs(); |
| | 1 | 422 | | _writeEventArgs.Completed += new EventHandler<SocketAsyncEventArgs>(ioCompleted); |
| | 1 | 423 | | } |
| | | 424 | | |
| | | 425 | | private int toState(int operation) |
| | | 426 | | { |
| | 1 | 427 | | return operation switch |
| | 1 | 428 | | { |
| | 1 | 429 | | SocketOperation.Read => StateProxyRead, |
| | 0 | 430 | | SocketOperation.Write => StateProxyWrite, |
| | 1 | 431 | | _ => StateProxyConnected, |
| | 1 | 432 | | }; |
| | | 433 | | } |
| | | 434 | | |
| | | 435 | | private readonly ProtocolInstance _instance; |
| | | 436 | | private readonly NetworkProxy _proxy; |
| | | 437 | | private readonly EndPoint _addr; |
| | | 438 | | private readonly EndPoint _sourceAddr; |
| | | 439 | | |
| | | 440 | | private Socket _fd; |
| | | 441 | | private int _state; |
| | | 442 | | private string _desc; |
| | | 443 | | |
| | | 444 | | private SocketAsyncEventArgs _writeEventArgs; |
| | | 445 | | private SocketAsyncEventArgs _readEventArgs; |
| | | 446 | | private AsyncCallback _writeCallback; |
| | | 447 | | private AsyncCallback _readCallback; |
| | | 448 | | |
| | | 449 | | private const int StateNeedConnect = 0; |
| | | 450 | | private const int StateConnectPending = 1; |
| | | 451 | | private const int StateProxyWrite = 2; |
| | | 452 | | private const int StateProxyRead = 3; |
| | | 453 | | private const int StateProxyConnected = 4; |
| | | 454 | | private const int StateConnected = 5; |
| | | 455 | | } |