public bool StartRead(ref ArraySegment <byte> buffer, ref int offset, AsyncCallback callback, object state) { Debug.Assert(_fd != null); Debug.Assert(offset == 0, $"offset: {offset}\n{Environment.StackTrace}"); int packetSize = Math.Min(MaxPacketSize, _rcvSize - UdpOverhead); Debug.Assert(buffer.Count == 0); buffer = new byte[packetSize]; try { // TODO: Workaround for https://github.com/dotnet/corefx/issues/31182 _readCallback = callback; _readEventArgs.UserToken = state; _readEventArgs.SetBuffer(buffer.Array, 0, packetSize); if (_state == StateConnected || (AssemblyUtil.IsMacOS && _fd.AddressFamily == AddressFamily.InterNetworkV6 && _fd.DualMode)) { return(!_fd.ReceiveAsync(_readEventArgs)); } else { Debug.Assert(_incoming); return(!_fd.ReceiveFromAsync(_readEventArgs)); } } catch (System.Net.Sockets.SocketException ex) { if (Network.RecvTruncated(ex)) { // Nothing to do return(true); } else { if (Network.ConnectionLost(ex)) { throw new ConnectionLostException(ex); } else { throw new TransportException(ex); } } } }
public void FinishRead(ref ArraySegment <byte> buffer, ref int offset) { if (_fd == null) { return; } int ret; try { if (_readEventArgs.SocketError != SocketError.Success) { throw new System.Net.Sockets.SocketException((int)_readEventArgs.SocketError); } ret = _readEventArgs.BytesTransferred; // TODO: Workaround for https://github.com/dotnet/corefx/issues/31182 if (_state != StateConnected && !(AssemblyUtil.IsMacOS && _fd.AddressFamily == AddressFamily.InterNetworkV6 && _fd.DualMode)) { _peerAddr = _readEventArgs.RemoteEndPoint; } } catch (System.Net.Sockets.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 = buffer.Count; } else { if (Network.ConnectionLost(ex)) { throw new ConnectionLostException(ex); } else if (Network.ConnectionRefused(ex)) { throw new ConnectionRefusedException(ex); } else { throw new TransportException(ex); } } } if (ret == 0) { throw new 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 (_communicator.TraceLevels.Network >= 1) { _communicator.Logger.Trace(_communicator.TraceLevels.NetworkCat, $"connected {Transport} socket\n{this}"); } } buffer = buffer.Slice(0, ret); offset = ret; }
public int Read(ref ArraySegment <byte> buffer, ref int offset) { if (buffer.Count - offset == 0) { return(SocketOperation.None); } Debug.Assert(offset == 0); Debug.Assert(_fd != null); int packetSize = Math.Min(MaxPacketSize, _rcvSize - UdpOverhead); Debug.Assert(buffer.Count == 0); buffer = new byte[packetSize]; int ret; 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); } } // TODO: Workaround for https://github.com/dotnet/corefx/issues/31182 if (_state == StateConnected || (AssemblyUtil.IsMacOS && _fd.AddressFamily == AddressFamily.InterNetworkV6 && _fd.DualMode)) { ret = _fd.Receive(buffer.Array, 0, packetSize, SocketFlags.None); } else { ret = _fd.ReceiveFrom(buffer.Array, 0, packetSize, SocketFlags.None, ref peerAddr); _peerAddr = (IPEndPoint)peerAddr; } break; } catch (System.Net.Sockets.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 = buffer.Count; break; } if (Network.Interrupted(e)) { continue; } if (Network.WouldBlock(e)) { return(SocketOperation.Read); } if (Network.ConnectionLost(e)) { throw new ConnectionLostException(); } else { throw new TransportException(e); } } } if (ret == 0) { throw new ConnectionLostException(); } if (_state == StateNeedConnect) { Debug.Assert(_incoming); // // If we must connect, then we connect to the first peer that sends us a packet. // Debug.Assert(_peerAddr != null); bool connected = Network.DoConnect(_fd, _peerAddr, null); Debug.Assert(connected); _state = StateConnected; // We're connected now if (_communicator.TraceLevels.Network >= 1) { _communicator.Logger.Trace(_communicator.TraceLevels.NetworkCat, $"connected {Transport} socket\n{this}"); } } buffer = buffer.Slice(0, ret); offset = ret; return(SocketOperation.None); }