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