public bool startWrite(Buffer buf, AsyncCallback callback, object state, out bool completed) { if (!_incoming && _state < StateConnected) { Debug.Assert(_addr != null); completed = false; if (_sourceAddr != null) { _fd.Bind(_sourceAddr); } _writeEventArgs.UserToken = state; return(!_fd.ConnectAsync(_writeEventArgs)); } Debug.Assert(_fd != null); // The caller is supposed to check the send size before by calling checkSendSize Debug.Assert(Math.Min(_maxPacketSize, _sndSize - _udpOverhead) >= buf.size()); Debug.Assert(buf.b.position() == 0); bool completedSynchronously; try { _writeCallback = callback; if (_state == StateConnected) { _writeEventArgs.UserToken = state; _writeEventArgs.SetBuffer(buf.b.rawBytes(), 0, buf.b.limit()); completedSynchronously = !_fd.SendAsync(_writeEventArgs); } else { if (_peerAddr == null) { throw new Ice.SocketException(); } _writeEventArgs.RemoteEndPoint = _peerAddr; _writeEventArgs.UserToken = state; _writeEventArgs.SetBuffer(buf.b.rawBytes(), 0, buf.b.limit()); completedSynchronously = !_fd.SendToAsync(_writeEventArgs); } } catch (SocketException ex) { if (Network.connectionLost(ex)) { throw new Ice.ConnectionLostException(ex); } else { throw new Ice.SocketException(ex); } } completed = true; return(completedSynchronously); }
public bool startWrite(Buffer buf, AsyncCallback callback, object state, out bool completed) { #if ICE_SOCKET_ASYNC_API Debug.Assert(_fd != null && _writeEventArgs != null); #else Debug.Assert(_fd != null && _writeResult == null); #endif if (_state == StateConnectPending) { completed = false; _writeCallback = callback; try { EndPoint addr = _proxy != null?_proxy.getAddress() : _addr; #if ICE_SOCKET_ASYNC_API _writeEventArgs.RemoteEndPoint = addr; _writeEventArgs.UserToken = state; return(!_fd.ConnectAsync(_writeEventArgs)); #else _writeResult = Network.doConnectAsync(_fd, addr, _sourceAddr, callback, state); return(_writeResult.CompletedSynchronously); #endif } catch (Exception ex) { throw new Ice.SocketException(ex); } } int packetSize = getSendPacketSize(buf.b.remaining()); try { _writeCallback = callback; #if ICE_SOCKET_ASYNC_API _writeEventArgs.UserToken = state; _writeEventArgs.SetBuffer(buf.b.rawBytes(), buf.b.position(), packetSize); bool completedSynchronously = !_fd.SendAsync(_writeEventArgs); #else _writeResult = _fd.BeginSend(buf.b.rawBytes(), buf.b.position(), packetSize, SocketFlags.None, writeCompleted, state); bool completedSynchronously = _writeResult.CompletedSynchronously; #endif completed = packetSize == buf.b.remaining(); return(completedSynchronously); } catch (SocketException ex) { if (Network.connectionLost(ex)) { throw new Ice.ConnectionLostException(ex); } throw new Ice.SocketException(ex); } catch (ObjectDisposedException ex) { throw new Ice.ConnectionLostException(ex); } }
public bool startRead(Buffer buf, AsyncCallback callback, object state) { #if ICE_SOCKET_ASYNC_API Debug.Assert(_fd != null && _readEventArgs != null); #else Debug.Assert(_fd != null && _readResult == null); #endif int packetSize = getRecvPacketSize(buf.b.remaining()); try { _readCallback = callback; #if ICE_SOCKET_ASYNC_API _readEventArgs.UserToken = state; _readEventArgs.SetBuffer(buf.b.rawBytes(), buf.b.position(), packetSize); return(!_fd.ReceiveAsync(_readEventArgs)); #else _readResult = _fd.BeginReceive(buf.b.rawBytes(), buf.b.position(), packetSize, SocketFlags.None, readCompleted, state); return(_readResult.CompletedSynchronously); #endif } catch (SocketException ex) { if (Network.connectionLost(ex)) { throw new Ice.ConnectionLostException(ex); } throw new Ice.SocketException(ex); } }
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); }
private int write(ByteBuffer buf) { Debug.Assert(_fd != null); if (AssemblyUtil.isMono) { // // Mono on Android and iOS don't support the use of synchronous socket // operations on a non-blocking socket. Returning 0 here forces the caller to schedule // an asynchronous operation. // return(0); } int packetSize = buf.remaining(); if (AssemblyUtil.isWindows) { // // On Windows, limiting the buffer size is important to prevent // poor throughput performances when transfering large amount of // data. See Microsoft KB article KB823764. // if (_maxSendPacketSize > 0 && packetSize > _maxSendPacketSize / 2) { packetSize = _maxSendPacketSize / 2; } } int sent = 0; while (buf.hasRemaining()) { try { int ret = _fd.Send(buf.rawBytes(), buf.position(), packetSize, SocketFlags.None); Debug.Assert(ret > 0); sent += ret; buf.position(buf.position() + ret); if (packetSize > buf.remaining()) { packetSize = buf.remaining(); } } catch (SocketException ex) { if (Network.wouldBlock(ex)) { return(sent); } else if (Network.connectionLost(ex)) { throw new Ice.ConnectionLostException(ex); } throw new Ice.SocketException(ex); } } return(sent); }
public void finishRead(Buffer buf) { if (_fd == null) // Transceiver was closed { #if !ICE_SOCKET_ASYNC_API _readResult = null; #endif return; } #if ICE_SOCKET_ASYNC_API Debug.Assert(_fd != null && _readEventArgs != null); #else Debug.Assert(_fd != null && _readResult != null); #endif try { #if ICE_SOCKET_ASYNC_API if (_readEventArgs.SocketError != SocketError.Success) { throw new SocketException((int)_readEventArgs.SocketError); } int ret = _readEventArgs.BytesTransferred; _readEventArgs.SetBuffer(null, 0, 0); #else int ret = _fd.EndReceive(_readResult); _readResult = null; #endif if (ret == 0) { throw new Ice.ConnectionLostException(); } Debug.Assert(ret > 0); buf.b.position(buf.b.position() + ret); if (_state == StateProxyRead) { _state = toState(_proxy.endRead(buf)); } } catch (SocketException ex) { if (Network.connectionLost(ex)) { throw new Ice.ConnectionLostException(ex); } throw new Ice.SocketException(ex); } catch (ObjectDisposedException ex) { throw new Ice.ConnectionLostException(ex); } }
public void finishWrite(Buffer buf) { if (_fd == null) // Transceiver was closed { if (buf.size() - buf.b.position() < _maxSendPacketSize) { buf.b.position(buf.b.limit()); // Assume all the data was sent for at-most-once semantics. } return; } Debug.Assert(_fd != null && _writeEventArgs != null); if (_state < StateConnected && _state != StateProxyWrite) { return; } try { if (_writeEventArgs.SocketError != SocketError.Success) { throw new SocketException((int)_writeEventArgs.SocketError); } int ret = _writeEventArgs.BytesTransferred; _writeEventArgs.SetBuffer(null, 0, 0); if (ret == 0) { throw new Ice.ConnectionLostException(); } Debug.Assert(ret > 0); buf.b.position(buf.b.position() + ret); if (_state == StateProxyWrite) { Debug.Assert(_proxy != null); _state = toState(_proxy.endWrite(buf)); } } catch (SocketException ex) { if (Network.connectionLost(ex)) { throw new Ice.ConnectionLostException(ex); } throw new Ice.SocketException(ex); } catch (ObjectDisposedException ex) { throw new Ice.ConnectionLostException(ex); } }
public bool startWrite(Buffer buf, AsyncCallback callback, object state, out bool completed) { Debug.Assert(_fd != null && _writeEventArgs != null); if (_state == StateConnectPending) { completed = false; _writeCallback = callback; try { EndPoint addr; if (_proxy != null) { addr = _proxy.getAddress(); } else { Debug.Assert(_addr != null); addr = _addr; } _writeEventArgs.RemoteEndPoint = addr; _writeEventArgs.UserToken = state; return(!_fd.ConnectAsync(_writeEventArgs)); } catch (Exception ex) { throw new Ice.SocketException(ex); } } int packetSize = getSendPacketSize(buf.b.remaining()); try { _writeCallback = callback; _writeEventArgs.UserToken = state; _writeEventArgs.SetBuffer(buf.b.rawBytes(), buf.b.position(), packetSize); bool completedSynchronously = !_fd.SendAsync(_writeEventArgs); completed = packetSize == buf.b.remaining(); return(completedSynchronously); } catch (SocketException ex) { if (Network.connectionLost(ex)) { throw new Ice.ConnectionLostException(ex); } throw new Ice.SocketException(ex); } catch (ObjectDisposedException ex) { throw new Ice.ConnectionLostException(ex); } }
private int write(ByteBuffer buf) { Debug.Assert(_fd != null); int packetSize = buf.remaining(); if (AssemblyUtil.platform_ == AssemblyUtil.Platform.Windows) { // // On Windows, limiting the buffer size is important to prevent // poor throughput performances when transfering large amount of // data. See Microsoft KB article KB823764. // if (_maxSendPacketSize > 0 && packetSize > _maxSendPacketSize / 2) { packetSize = _maxSendPacketSize / 2; } } int sent = 0; while (buf.hasRemaining()) { try { int ret = _fd.Send(buf.rawBytes(), buf.position(), packetSize, SocketFlags.None); Debug.Assert(ret > 0); sent += ret; buf.position(buf.position() + ret); if (packetSize > buf.remaining()) { packetSize = buf.remaining(); } } catch (SocketException ex) { if (Network.wouldBlock(ex)) { return(sent); } else if (Network.connectionLost(ex)) { throw new Ice.ConnectionLostException(ex); } throw new Ice.SocketException(ex); } } return(sent); }
public bool startRead(Buffer buf, AsyncCallback callback, object state) { Debug.Assert(_fd != null); Debug.Assert(buf.b.position() == 0); int packetSize = Math.Min(_maxPacketSize, _rcvSize - _udpOverhead); buf.resize(packetSize, true); buf.b.position(0); try { // TODO: Workaround for https://github.com/dotnet/corefx/issues/31182 if (_state == StateConnected || AssemblyUtil.isMacOS && _fd.AddressFamily == AddressFamily.InterNetworkV6 && _fd.DualMode) { _readCallback = callback; _readEventArgs.UserToken = state; _readEventArgs.SetBuffer(buf.b.rawBytes(), buf.b.position(), packetSize); return(!_fd.ReceiveAsync(_readEventArgs)); } else { Debug.Assert(_incoming); _readCallback = callback; _readEventArgs.UserToken = state; _readEventArgs.SetBuffer(buf.b.rawBytes(), 0, buf.b.limit()); return(!_fd.ReceiveFromAsync(_readEventArgs)); } } catch (SocketException ex) { if (Network.recvTruncated(ex)) { // Nothing todo return(true); } else { if (Network.connectionLost(ex)) { throw new Ice.ConnectionLostException(ex); } else { throw new Ice.SocketException(ex); } } } }
public bool startRead(Buffer buf, AsyncCallback callback, object state) { Debug.Assert(buf.b.position() == 0); int packetSize = Math.Min(_maxPacketSize, _rcvSize - _udpOverhead); buf.resize(packetSize, true); buf.b.position(0); try { if (_state == StateConnected) { _readCallback = callback; _readEventArgs.UserToken = state; _readEventArgs.SetBuffer(buf.b.rawBytes(), buf.b.position(), packetSize); return(!_fd.ReceiveAsync(_readEventArgs)); } else { Debug.Assert(_incoming); _readCallback = callback; _readEventArgs.UserToken = state; _readEventArgs.SetBuffer(buf.b.rawBytes(), 0, buf.b.limit()); return(!_fd.ReceiveFromAsync(_readEventArgs)); } } catch (SocketException ex) { if (Network.recvTruncated(ex)) { // Nothing todo return(true); } else { if (Network.connectionLost(ex)) { throw new Ice.ConnectionLostException(ex); } else { throw new Ice.SocketException(ex); } } } }
private int read(ByteBuffer buf) { Debug.Assert(_fd != null); #if COMPACT || SILVERLIGHT // // Silverlight and the Compact .NET Framework don't // support the use of synchronous socket operations on a // non-blocking socket. Returning 0 here forces the caller // to schedule an asynchronous operation. // return(0); #else int read = 0; while (buf.hasRemaining()) { try { int ret = _fd.Receive(buf.rawBytes(), buf.position(), buf.remaining(), SocketFlags.None); if (ret == 0) { throw new Ice.ConnectionLostException(); } read += ret; buf.position(buf.position() + ret); } catch (SocketException ex) { if (Network.wouldBlock(ex)) { return(read); } else if (Network.interrupted(ex)) { continue; } else if (Network.connectionLost(ex)) { throw new Ice.ConnectionLostException(ex); } throw new Ice.SocketException(ex); } } return(read); #endif }
private int read(ByteBuffer buf) { Debug.Assert(_fd != null); if (AssemblyUtil.isMono) { // // Mono on Android and iOS don't support the use of synchronous socket // operations on a non-blocking socket. Returning 0 here forces the caller to schedule // an asynchronous operation. // return(0); } int read = 0; while (buf.hasRemaining()) { try { int ret = _fd.Receive(buf.rawBytes(), buf.position(), buf.remaining(), SocketFlags.None); if (ret == 0) { throw new Ice.ConnectionLostException(); } read += ret; buf.position(buf.position() + ret); } catch (SocketException ex) { if (Network.wouldBlock(ex)) { return(read); } else if (Network.interrupted(ex)) { continue; } else if (Network.connectionLost(ex)) { throw new Ice.ConnectionLostException(ex); } throw new Ice.SocketException(ex); } } return(read); }
public void finishRead(Buffer buf) { if (_fd == null) // Transceiver was closed { return; } Debug.Assert(_fd != null && _readEventArgs != null); try { if (_readEventArgs.SocketError != SocketError.Success) { throw new SocketException((int)_readEventArgs.SocketError); } int ret = _readEventArgs.BytesTransferred; _readEventArgs.SetBuffer(null, 0, 0); if (ret == 0) { throw new Ice.ConnectionLostException(); } Debug.Assert(ret > 0); buf.b.position(buf.b.position() + ret); if (_state == StateProxyRead) { Debug.Assert(_proxy != null); _state = toState(_proxy.endRead(buf)); } } catch (SocketException ex) { if (Network.connectionLost(ex)) { throw new Ice.ConnectionLostException(ex); } throw new Ice.SocketException(ex); } catch (ObjectDisposedException ex) { throw new Ice.ConnectionLostException(ex); } }
public bool startRead(Buffer buf, AsyncCallback callback, object state) { Debug.Assert(_fd != null && _readEventArgs != null); int packetSize = getRecvPacketSize(buf.b.remaining()); try { _readCallback = callback; _readEventArgs.UserToken = state; _readEventArgs.SetBuffer(buf.b.rawBytes(), buf.b.position(), packetSize); return !_fd.ReceiveAsync(_readEventArgs); } catch(SocketException ex) { if(Network.connectionLost(ex)) { throw new Ice.ConnectionLostException(ex); } throw new Ice.SocketException(ex); } }
private int read(ByteBuffer buf) { Debug.Assert(_fd != null); int read = 0; while (buf.hasRemaining()) { try { int ret = _fd.Receive(buf.rawBytes(), buf.position(), buf.remaining(), SocketFlags.None); if (ret == 0) { throw new Ice.ConnectionLostException(); } read += ret; buf.position(buf.position() + ret); } catch (SocketException ex) { if (Network.wouldBlock(ex)) { return(read); } else if (Network.interrupted(ex)) { continue; } else if (Network.connectionLost(ex)) { throw new Ice.ConnectionLostException(ex); } throw new Ice.SocketException(ex); } } return(read); }
public bool write(Buffer buf) { #if COMPACT || SILVERLIGHT // // Silverlight and the Compact .NET Frameworks don't support the use of synchronous socket // operations on a non-blocking socket. Returning false here forces the caller to schedule // an asynchronous operation. // return(false); #else int packetSize = buf.b.remaining(); if (AssemblyUtil.platform_ == AssemblyUtil.Platform.Windows) { // // On Windows, limiting the buffer size is important to prevent // poor throughput performance when transferring large amounts of // data. See Microsoft KB article KB823764. // if (_maxSendPacketSize > 0 && packetSize > _maxSendPacketSize / 2) { packetSize = _maxSendPacketSize / 2; } } while (buf.b.hasRemaining()) { try { Debug.Assert(_fd != null); int ret; try { ret = _fd.Send(buf.b.rawBytes(), buf.b.position(), packetSize, SocketFlags.None); } catch (SocketException e) { if (Network.wouldBlock(e)) { return(false); } throw; } Debug.Assert(ret > 0); if (_traceLevels.network >= 3) { string s = "sent " + ret + " of " + packetSize + " bytes via tcp\n" + ToString(); _logger.trace(_traceLevels.networkCat, s); } if (_stats != null) { #pragma warning disable 618 _stats.bytesSent(type(), ret); #pragma warning restore 618 } buf.b.position(buf.b.position() + ret); if (packetSize > buf.b.remaining()) { packetSize = buf.b.remaining(); } } catch (SocketException ex) { if (Network.connectionLost(ex)) { throw new Ice.ConnectionLostException(ex); } throw new Ice.SocketException(ex); } } return(true); // No more data to send. #endif }
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 bool startWrite(Buffer buf, AsyncCallback callback, object state, out bool completed) { #if ICE_SOCKET_ASYNC_API Debug.Assert(_fd != null && _writeEventArgs != null); #else Debug.Assert(_fd != null && _writeResult == null); #endif if (_state < StateConnected) { completed = false; if (_state == StateConnectPending) { _writeCallback = callback; try { EndPoint addr = _proxy != null?_proxy.getAddress() : _addr; #if ICE_SOCKET_ASYNC_API _writeEventArgs.RemoteEndPoint = addr; _writeEventArgs.UserToken = state; return(!_fd.ConnectAsync(_writeEventArgs)); #else _writeResult = Network.doConnectAsync(_fd, addr, callback, state); return(_writeResult.CompletedSynchronously); #endif } catch (Exception ex) { throw new Ice.SocketException(ex); } } else if (_state == StateProxyConnectRequest) { _proxy.beginWriteConnectRequest(_addr, buf); } } // // We limit the packet size for beginWrite to ensure connection timeouts are based // on a fixed packet size. // int packetSize = buf.b.remaining(); if (_maxSendPacketSize > 0 && packetSize > _maxSendPacketSize) { packetSize = _maxSendPacketSize; } try { _writeCallback = callback; #if ICE_SOCKET_ASYNC_API _writeEventArgs.UserToken = state; _writeEventArgs.SetBuffer(buf.b.rawBytes(), buf.b.position(), packetSize); bool completedSynchronously = !_fd.SendAsync(_writeEventArgs); #else _writeResult = _fd.BeginSend(buf.b.rawBytes(), buf.b.position(), packetSize, SocketFlags.None, writeCompleted, state); bool completedSynchronously = _writeResult.CompletedSynchronously; #endif completed = packetSize == buf.b.remaining(); return(completedSynchronously); } catch (SocketException ex) { if (Network.connectionLost(ex)) { throw new Ice.ConnectionLostException(ex); } throw new Ice.SocketException(ex); } catch (ObjectDisposedException ex) { throw new Ice.ConnectionLostException(ex); } }
public bool read(Buffer buf) { #if COMPACT || SILVERLIGHT // // Silverlight and the Compact .NET Framework don't support the use of synchronous socket // operations on a non-blocking socket. Returning false here forces the // caller to schedule an asynchronous operation. // return(false); #else Debug.Assert(buf.b.position() == 0); Debug.Assert(_fd != null); int packetSize = System.Math.Min(_maxPacketSize, _rcvSize - _udpOverhead); buf.resize(packetSize, true); buf.b.position(0); int ret = 0; while (true) { try { EndPoint peerAddr = _peerAddr; if (peerAddr == null) { if (_addr.AddressFamily == AddressFamily.InterNetwork) { peerAddr = new IPEndPoint(IPAddress.Any, 0); } else { Debug.Assert(_addr.AddressFamily == AddressFamily.InterNetworkV6); peerAddr = new IPEndPoint(IPAddress.IPv6Any, 0); } } if (_state == StateConnected) { ret = _fd.Receive(buf.b.rawBytes(), 0, buf.b.limit(), SocketFlags.None); } else { ret = _fd.ReceiveFrom(buf.b.rawBytes(), 0, buf.b.limit(), SocketFlags.None, ref peerAddr); _peerAddr = (IPEndPoint)peerAddr; } break; } catch (SocketException e) { if (Network.recvTruncated(e)) { // 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(); break; } if (Network.interrupted(e)) { continue; } if (Network.wouldBlock(e)) { return(false); } if (Network.connectionLost(e)) { throw new Ice.ConnectionLostException(); } else { throw new Ice.SocketException(e); } } catch (System.Exception e) { throw new Ice.SyscallException(e); } } if (ret == 0) { throw new Ice.ConnectionLostException(); } if (_state == StateNeedConnect) { Debug.Assert(_incoming); // // If we must connect, then we connect to the first peer that sends us a packet. // bool connected = Network.doConnect(_fd, _peerAddr); 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); return(true); #endif }
public bool startRead(Buffer buf, AsyncCallback callback, object state) { Debug.Assert(buf.b.position() == 0); int packetSize = System.Math.Min(_maxPacketSize, _rcvSize - _udpOverhead); buf.resize(packetSize, true); buf.b.position(0); try { if (_state == StateConnected) { _readCallback = callback; #if ICE_SOCKET_ASYNC_API _readEventArgs.UserToken = state; _readEventArgs.SetBuffer(buf.b.rawBytes(), buf.b.position(), packetSize); return(!_fd.ReceiveAsync(_readEventArgs)); #else _readResult = _fd.BeginReceive(buf.b.rawBytes(), 0, buf.b.limit(), SocketFlags.None, readCompleted, state); return(_readResult.CompletedSynchronously); #endif } else { Debug.Assert(_incoming); _readCallback = callback; #if ICE_SOCKET_ASYNC_API _readEventArgs.UserToken = state; _readEventArgs.SetBuffer(buf.b.rawBytes(), 0, buf.b.limit()); return(!_fd.ReceiveFromAsync(_readEventArgs)); #else EndPoint peerAddr = _peerAddr; if (peerAddr == null) { if (_addr.AddressFamily == AddressFamily.InterNetwork) { peerAddr = new IPEndPoint(IPAddress.Any, 0); } else { Debug.Assert(_addr.AddressFamily == AddressFamily.InterNetworkV6); peerAddr = new IPEndPoint(IPAddress.IPv6Any, 0); } } _readResult = _fd.BeginReceiveFrom(buf.b.rawBytes(), 0, buf.b.limit(), SocketFlags.None, ref peerAddr, readCompleted, state); return(_readResult.CompletedSynchronously); #endif } } catch (SocketException ex) { if (Network.recvTruncated(ex)) { // Nothing todo return(true); } else { if (Network.connectionLost(ex)) { throw new Ice.ConnectionLostException(ex); } else { throw new Ice.SocketException(ex); } } } }
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); }
public bool read(Buffer buf) { #if COMPACT || SILVERLIGHT // // Silverlight and the Compact .NET Framework don't support the use of synchronous socket // operations on a non-blocking socket. // return(false); #else int remaining = buf.b.remaining(); int position = buf.b.position(); while (buf.b.hasRemaining()) { try { Debug.Assert(_fd != null); int ret; try { // // Try to receive first. Much of the time, this will work and we // avoid the cost of calling Poll(). // ret = _fd.Receive(buf.b.rawBytes(), position, remaining, SocketFlags.None); if (ret == 0) { throw new Ice.ConnectionLostException(); } } catch (SocketException e) { if (Network.wouldBlock(e)) { return(false); } throw; } Debug.Assert(ret > 0); if (_traceLevels.network >= 3) { string s = "received " + ret + " of " + remaining + " bytes via tcp\n" + ToString(); _logger.trace(_traceLevels.networkCat, s); } if (_stats != null) { #pragma warning disable 618 _stats.bytesReceived(type(), ret); #pragma warning restore 618 } remaining -= ret; buf.b.position(position += ret); } catch (SocketException ex) { // // On Mono, calling shutdownReadWrite() followed by read() causes Socket.Receive() to // raise a socket exception with the "interrupted" error code. We need to check the // socket's Connected status before checking for the interrupted case. // if (!_fd.Connected) { throw new Ice.ConnectionLostException(ex); } if (Network.interrupted(ex)) { continue; } if (Network.connectionLost(ex)) { throw new Ice.ConnectionLostException(ex); } throw new Ice.SocketException(ex); } } return(true); #endif }
public void finishRead(Buffer buf) { if (_fd == null) // Transceiver was closed { #if ICE_SOCKET_ASYNC_API _readEventArgs = null; #else _readResult = null; #endif return; } #if ICE_SOCKET_ASYNC_API Debug.Assert(_fd != null && _readEventArgs != null); #else Debug.Assert(_fd != null && _readResult != null); #endif try { #if ICE_SOCKET_ASYNC_API if (_readEventArgs.SocketError != SocketError.Success) { throw new SocketException((int)_readEventArgs.SocketError); } int ret = _readEventArgs.BytesTransferred; #else int ret = _fd.EndReceive(_readResult); _readResult = null; #endif if (ret == 0) { throw new Ice.ConnectionLostException(); } Debug.Assert(ret > 0); if (_traceLevels.network >= 3) { int packetSize = buf.b.remaining(); if (_maxReceivePacketSize > 0 && packetSize > _maxReceivePacketSize) { packetSize = _maxReceivePacketSize; } string s = "received " + ret + " of " + packetSize + " bytes via tcp\n" + ToString(); _logger.trace(_traceLevels.networkCat, s); } if (_stats != null) { #pragma warning disable 618 _stats.bytesReceived(type(), ret); #pragma warning restore 618 } buf.b.position(buf.b.position() + ret); if (_state == StateProxyConnectRequestPending) { _proxy.endReadConnectRequestResponse(buf); } } catch (SocketException ex) { if (Network.connectionLost(ex)) { throw new Ice.ConnectionLostException(ex); } throw new Ice.SocketException(ex); } catch (ObjectDisposedException ex) { throw new Ice.ConnectionLostException(ex); } }
public bool write(Buffer buf) { #if COMPACT || SILVERLIGHT // // Silverlight and the Compact .NET Framework don't support the use of synchronous socket // operations on a non-blocking socket. Returning false here forces the // caller to schedule an asynchronous operation. // return(false); #else Debug.Assert(buf.b.position() == 0); Debug.Assert(_fd != null && _state >= StateConnected); // The caller is supposed to check the send size before by calling checkSendSize Debug.Assert(System.Math.Min(_maxPacketSize, _sndSize - _udpOverhead) >= buf.size()); int ret = 0; while (true) { try { if (_state == StateConnected) { ret = _fd.Send(buf.b.rawBytes(), 0, buf.size(), SocketFlags.None); } else { if (_peerAddr == null) { throw new Ice.SocketException(); } ret = _fd.SendTo(buf.b.rawBytes(), 0, buf.size(), SocketFlags.None, _peerAddr); } break; } catch (SocketException ex) { if (Network.interrupted(ex)) { continue; } if (Network.wouldBlock(ex)) { return(false); } if (Network.connectionLost(ex)) { throw new Ice.ConnectionLostException(ex); } else { throw new Ice.SocketException(ex); } } catch (System.Exception e) { throw new Ice.SyscallException(e); } } 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()); return(true); #endif }
public void finishWrite(Buffer buf) { if (_fd == null) // Transceiver was closed { if (buf.size() - buf.b.position() < _maxSendPacketSize) { 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 ICE_SOCKET_ASYNC_API Debug.Assert(_fd != null && _writeEventArgs != null); #else Debug.Assert(_fd != null && _writeResult != null); #endif if (_state < StateConnected && _state != StateProxyConnectRequest) { return; } try { #if ICE_SOCKET_ASYNC_API if (_writeEventArgs.SocketError != SocketError.Success) { throw new SocketException((int)_writeEventArgs.SocketError); } int ret = _writeEventArgs.BytesTransferred; #else int ret = _fd.EndSend(_writeResult); _writeResult = null; #endif if (ret == 0) { throw new Ice.ConnectionLostException(); } Debug.Assert(ret > 0); if (_traceLevels.network >= 3) { int packetSize = buf.b.remaining(); if (_maxSendPacketSize > 0 && packetSize > _maxSendPacketSize) { packetSize = _maxSendPacketSize; } string s = "sent " + ret + " of " + packetSize + " bytes via tcp\n" + ToString(); _logger.trace(_traceLevels.networkCat, s); } if (_stats != null) { #pragma warning disable 618 _stats.bytesSent(type(), ret); #pragma warning restore 618 } buf.b.position(buf.b.position() + ret); if (_state == StateProxyConnectRequest) { _proxy.endWriteConnectRequest(buf); } } catch (SocketException ex) { if (Network.connectionLost(ex)) { throw new Ice.ConnectionLostException(ex); } throw new Ice.SocketException(ex); } catch (ObjectDisposedException ex) { throw new Ice.ConnectionLostException(ex); } }
public int write(Buffer buf) { if (!buf.b.hasRemaining()) { return(SocketOperation.None); } Debug.Assert(buf.b.position() == 0); Debug.Assert(_fd != null && _state >= StateConnected); // The caller is supposed to check the send size before by calling checkSendSize Debug.Assert(Math.Min(_maxPacketSize, _sndSize - _udpOverhead) >= buf.size()); int ret = 0; while (true) { try { if (_state == StateConnected) { ret = _fd.Send(buf.b.rawBytes(), 0, buf.size(), SocketFlags.None); } else { if (_peerAddr == null) { throw new Ice.SocketException(); } ret = _fd.SendTo(buf.b.rawBytes(), 0, buf.size(), SocketFlags.None, _peerAddr); } break; } catch (SocketException ex) { if (Network.interrupted(ex)) { continue; } if (Network.wouldBlock(ex)) { return(SocketOperation.Write); } if (Network.connectionLost(ex)) { throw new Ice.ConnectionLostException(ex); } else { throw new Ice.SocketException(ex); } } catch (Exception e) { throw new Ice.SyscallException(e); } } Debug.Assert(ret > 0); Debug.Assert(ret == buf.b.limit()); buf.b.position(buf.b.limit()); return(SocketOperation.None); }
public int read(Buffer buf, ref bool hasMoreData) { if (!buf.b.hasRemaining()) { return(SocketOperation.None); } Debug.Assert(buf.b.position() == 0); Debug.Assert(_fd != null); int packetSize = Math.Min(_maxPacketSize, _rcvSize - _udpOverhead); buf.resize(packetSize, true); buf.b.position(0); int ret = 0; while (true) { try { EndPoint peerAddr = _peerAddr; if (peerAddr == null) { if (_addr.AddressFamily == AddressFamily.InterNetwork) { peerAddr = new IPEndPoint(IPAddress.Any, 0); } else { Debug.Assert(_addr.AddressFamily == AddressFamily.InterNetworkV6); peerAddr = new IPEndPoint(IPAddress.IPv6Any, 0); } } if (_state == StateConnected) { ret = _fd.Receive(buf.b.rawBytes(), 0, buf.b.limit(), SocketFlags.None); } else { ret = _fd.ReceiveFrom(buf.b.rawBytes(), 0, buf.b.limit(), SocketFlags.None, ref peerAddr); _peerAddr = (IPEndPoint)peerAddr; } break; } catch (SocketException e) { if (Network.recvTruncated(e)) { // 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(); break; } if (Network.interrupted(e)) { continue; } if (Network.wouldBlock(e)) { return(SocketOperation.Read); } if (Network.connectionLost(e)) { throw new Ice.ConnectionLostException(); } else { throw new Ice.SocketException(e); } } catch (Exception e) { throw new Ice.SyscallException(e); } } if (ret == 0) { throw new Ice.ConnectionLostException(); } if (_state == StateNeedConnect) { Debug.Assert(_incoming); // // If we must connect, then we connect to the first peer that sends us a packet. // bool connected = Network.doConnect(_fd, _peerAddr, null); 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); return(SocketOperation.None); }
public bool startWrite(Buffer buf, AsyncCallback callback, object state, out bool completed) { if (!_incoming && _state < StateConnected) { Debug.Assert(_addr != null); completed = false; #if ICE_SOCKET_ASYNC_API _writeEventArgs.UserToken = state; return(!_fd.ConnectAsync(_writeEventArgs)); #else _writeResult = Network.doConnectAsync(_fd, _addr, callback, state); return(_writeResult.CompletedSynchronously); #endif } Debug.Assert(_fd != null); // The caller is supposed to check the send size before by calling checkSendSize Debug.Assert(System.Math.Min(_maxPacketSize, _sndSize - _udpOverhead) >= buf.size()); Debug.Assert(buf.b.position() == 0); bool completedSynchronously; try { _writeCallback = callback; if (_state == StateConnected) { #if ICE_SOCKET_ASYNC_API _writeEventArgs.UserToken = state; _writeEventArgs.SetBuffer(buf.b.rawBytes(), 0, buf.b.limit()); completedSynchronously = !_fd.SendAsync(_writeEventArgs); #else _writeResult = _fd.BeginSend(buf.b.rawBytes(), 0, buf.b.limit(), SocketFlags.None, writeCompleted, state); completedSynchronously = _writeResult.CompletedSynchronously; #endif } else { if (_peerAddr == null) { throw new Ice.SocketException(); } #if ICE_SOCKET_ASYNC_API _writeEventArgs.RemoteEndPoint = _peerAddr; _writeEventArgs.UserToken = state; _writeEventArgs.SetBuffer(buf.b.rawBytes(), 0, buf.b.limit()); completedSynchronously = !_fd.SendToAsync(_writeEventArgs); #else _writeResult = _fd.BeginSendTo(buf.b.rawBytes(), 0, buf.b.limit(), SocketFlags.None, _peerAddr, writeCompleted, state); completedSynchronously = _writeResult.CompletedSynchronously; #endif } } catch (SocketException ex) { if (Network.connectionLost(ex)) { throw new Ice.ConnectionLostException(ex); } else { throw new Ice.SocketException(ex); } } completed = true; return(completedSynchronously); }