public void finishWrite(Buffer buf) { Debug.Assert(_writeEventArgs != null); if (_fd == null) { buf.b.position(buf.size()); // Assume all the data was sent for at-most-once semantics. _writeEventArgs = null; return; } if (!_incoming && _state < StateConnected) { if (_writeEventArgs.SocketError != SocketError.Success) { SocketException ex = new SocketException((int)_writeEventArgs.SocketError); if (Network.connectionRefused(ex)) { throw new Ice.ConnectionRefusedException(ex); } else { throw new Ice.ConnectFailedException(ex); } } return; } int ret; try { if (_writeEventArgs.SocketError != SocketError.Success) { throw new SocketException((int)_writeEventArgs.SocketError); } ret = _writeEventArgs.BytesTransferred; } catch (SocketException ex) { if (Network.connectionLost(ex)) { throw new Ice.ConnectionLostException(ex); } else { throw new Ice.SocketException(ex); } } if (ret == 0) { throw new Ice.ConnectionLostException(); } Debug.Assert(ret > 0); Debug.Assert(ret == buf.b.limit()); buf.b.position(buf.b.position() + ret); }
public int connect(Buffer readBuffer, Buffer writeBuffer, ref bool moreData) { if (_state == StateNeedConnect) { _state = StateConnectPending; return(SocketOperation.Connect); } else if (_state <= StateConnectPending) { Debug.Assert(_writeEventArgs != null); if (_writeEventArgs.SocketError != SocketError.Success) { SocketException ex = new SocketException((int)_writeEventArgs.SocketError); if (Network.connectionRefused(ex)) { throw new Ice.ConnectionRefusedException(ex); } else { throw new Ice.ConnectFailedException(ex); } } Debug.Assert(_fd != null); _desc = Network.fdToString(_fd, _proxy, _addr); _state = _proxy != null ? StateProxyWrite : StateConnected; } if (_state == StateProxyWrite) { Debug.Assert(_proxy != null); Debug.Assert(_addr != null); _proxy.beginWrite(_addr, writeBuffer); return(SocketOperation.Write); } else if (_state == StateProxyRead) { Debug.Assert(_proxy != null); _proxy.beginRead(readBuffer); return(SocketOperation.Read); } else if (_state == StateProxyConnected) { Debug.Assert(_proxy != null); _proxy.finish(readBuffer, writeBuffer); readBuffer.clear(); writeBuffer.clear(); _state = StateConnected; } Debug.Assert(_state == StateConnected); return(SocketOperation.None); }
public int initialize() { try { if (_state == StateNeedConnect) { _state = StateConnectPending; return(SocketOperation.Connect); } else if (_state <= StateConnectPending) { #if ICE_SOCKET_ASYNC_API if (_writeEventArgs.SocketError != SocketError.Success) { SocketException ex = new SocketException((int)_writeEventArgs.SocketError); if (Network.connectionRefused(ex)) { throw new Ice.ConnectionRefusedException(ex); } else { throw new Ice.ConnectFailedException(ex); } } #else Network.doFinishConnectAsync(_fd, _writeResult); _writeResult = null; #endif _desc = Network.fdToString(_fd, _proxy, _addr); if (_proxy != null) { _state = StateProxyConnectRequest; // Send proxy connect request return(SocketOperation.Write); } _state = StateConnected; } else if (_state == StateProxyConnectRequest) { _state = StateProxyConnectRequestPending; // Wait for proxy response return(SocketOperation.Read); } else if (_state == StateProxyConnectRequestPending) { _state = StateConnected; } } catch (Ice.LocalException ex) { if (_traceLevels.network >= 2) { System.Text.StringBuilder s = new System.Text.StringBuilder(); s.Append("failed to establish tcp connection\n"); s.Append(Network.fdToString(_fd, _proxy, _addr)); s.Append("\n"); s.Append(ex.ToString()); _logger.trace(_traceLevels.networkCat, s.ToString()); } throw; } Debug.Assert(_state == StateConnected); if (_traceLevels.network >= 1) { string s = "tcp connection established\n" + _desc; _logger.trace(_traceLevels.networkCat, s); } return(SocketOperation.None); }
public void finishRead(Buffer buf) { if (_fd == null) { return; } int ret; try { if (_readEventArgs.SocketError != SocketError.Success) { throw new SocketException((int)_readEventArgs.SocketError); } ret = _readEventArgs.BytesTransferred; if (_state != StateConnected) { _peerAddr = _readEventArgs.RemoteEndPoint; } } catch (SocketException ex) { if (Network.recvTruncated(ex)) { // The message was truncated and the whole buffer is filled. We ignore // this error here, it will be detected at the connection level when // the Ice message size is checked against the buffer size. ret = buf.size(); } else { if (Network.connectionLost(ex)) { throw new Ice.ConnectionLostException(ex); } if (Network.connectionRefused(ex)) { throw new Ice.ConnectionRefusedException(ex); } else { throw new Ice.SocketException(ex); } } } if (ret == 0) { throw new Ice.ConnectionLostException(); } Debug.Assert(ret > 0); if (_state == StateNeedConnect) { Debug.Assert(_incoming); // // If we must connect, then we connect to the first peer that // sends us a packet. // bool connected = !_fd.ConnectAsync(_readEventArgs); Debug.Assert(connected); _state = StateConnected; // We're connected now if (_instance.traceLevel() >= 1) { string s = "connected " + protocol() + " socket\n" + ToString(); _instance.logger().trace(_instance.traceCategory(), s); } } buf.resize(ret, true); buf.b.position(ret); }
public void finishWrite(Buffer buf) { if (_fd == null) { buf.b.position(buf.size()); // Assume all the data was sent for at-most-once semantics. #if ICE_SOCKET_ASYNC_API _writeEventArgs = null; #else _writeResult = null; #endif return; } if (!_incoming && _state < StateConnected) { #if ICE_SOCKET_ASYNC_API if (_writeEventArgs.SocketError != SocketError.Success) { SocketException ex = new SocketException((int)_writeEventArgs.SocketError); if (Network.connectionRefused(ex)) { throw new Ice.ConnectionRefusedException(ex); } else { throw new Ice.ConnectFailedException(ex); } } #else Debug.Assert(_writeResult != null); Network.doFinishConnectAsync(_fd, _writeResult); _writeResult = null; #endif return; } int ret; try { #if ICE_SOCKET_ASYNC_API if (_writeEventArgs.SocketError != SocketError.Success) { throw new SocketException((int)_writeEventArgs.SocketError); } ret = _writeEventArgs.BytesTransferred; #else if (_state == StateConnected) { ret = _fd.EndSend(_writeResult); } else { ret = _fd.EndSendTo(_writeResult); } _writeResult = null; #endif } catch (SocketException ex) { if (Network.connectionLost(ex)) { throw new Ice.ConnectionLostException(ex); } else { throw new Ice.SocketException(ex); } } if (ret == 0) { throw new Ice.ConnectionLostException(); } Debug.Assert(ret > 0); if (_traceLevels.network >= 3) { string s = "sent " + ret + " bytes via udp\n" + ToString(); _logger.trace(_traceLevels.networkCat, s); } if (_stats != null) { #pragma warning disable 618 _stats.bytesSent(type(), ret); #pragma warning restore 618 } Debug.Assert(ret == buf.b.limit()); buf.b.position(buf.b.position() + ret); }
public void finishRead(Buffer buf) { if (_fd == null) { return; } int ret; try { #if ICE_SOCKET_ASYNC_API if (_readEventArgs.SocketError != SocketError.Success) { throw new SocketException((int)_readEventArgs.SocketError); } ret = _readEventArgs.BytesTransferred; if (_state != StateConnected) { _peerAddr = _readEventArgs.RemoteEndPoint; } #else Debug.Assert(_readResult != null); if (_state == StateConnected) { ret = _fd.EndReceive(_readResult); } else { EndPoint peerAddr = _peerAddr; if (_addr.AddressFamily == AddressFamily.InterNetwork) { peerAddr = new IPEndPoint(IPAddress.Any, 0); } else { Debug.Assert(_addr.AddressFamily == AddressFamily.InterNetworkV6); peerAddr = new IPEndPoint(IPAddress.IPv6Any, 0); } ret = _fd.EndReceiveFrom(_readResult, ref peerAddr); _peerAddr = (IPEndPoint)peerAddr; } _readResult = null; #endif } catch (SocketException ex) { if (Network.recvTruncated(ex)) { // The message was truncated and the whole buffer is filled. We ignore // this error here, it will be detected at the connection level when // the Ice message size is checked against the buffer size. ret = buf.size(); } else { if (Network.connectionLost(ex)) { throw new Ice.ConnectionLostException(ex); } if (Network.connectionRefused(ex)) { throw new Ice.ConnectionRefusedException(ex); } else { throw new Ice.SocketException(ex); } } } if (ret == 0) { throw new Ice.ConnectionLostException(); } Debug.Assert(ret > 0); if (_state == StateNeedConnect) { Debug.Assert(_incoming); // // If we must connect, then we connect to the first peer that // sends us a packet. // #if ICE_SOCKET_ASYNC_API bool connected = !_fd.ConnectAsync(_readEventArgs); #else bool connected = Network.doConnect(_fd, _peerAddr); #endif Debug.Assert(connected); _state = StateConnected; // We're connected now if (_traceLevels.network >= 1) { string s = "connected udp socket\n" + ToString(); _logger.trace(_traceLevels.networkCat, s); } } if (_traceLevels.network >= 3) { string s = "received " + ret + " bytes via udp\n" + ToString(); _logger.trace(_traceLevels.networkCat, s); } if (_stats != null) { #pragma warning disable 618 _stats.bytesReceived(type(), ret); #pragma warning restore 618 } buf.resize(ret, true); buf.b.position(ret); }