internal void connected(TcpSocket fan) { IPEndPoint endPoint = m_dotnet.RemoteEndPoint as IPEndPoint; m_remoteAddr = IpAddrPeer.make(endPoint.Address); m_remotePort = endPoint.Port; m_in = SysInStream.make(new NetworkStream(m_dotnet), getInBufferSize(fan)); m_out = SysOutStream.make(new NetworkStream(m_dotnet), getOutBufferSize(fan)); }
////////////////////////////////////////////////////////////////////////// // End Points ////////////////////////////////////////////////////////////////////////// public IpAddr localAddr(TcpListener fan) { if (!m_bound) { return(null); } IPEndPoint pt = m_dotnet.LocalEndpoint as IPEndPoint; if (pt == null) { return(null); } return(IpAddrPeer.make(pt.Address)); }
////////////////////////////////////////////////////////////////////////// // End Points ////////////////////////////////////////////////////////////////////////// public IpAddr localAddr(TcpSocket fan) { if (!m_dotnet.IsBound) { return(null); } IPEndPoint pt = m_dotnet.LocalEndPoint as IPEndPoint; if (pt == null) { return(null); } return(IpAddrPeer.make(pt.Address)); }
public UdpSocket connect(UdpSocket fan, IpAddr addr, long port) { try { if (m_dotnet == null) { m_dotnet = createSocket(); } m_dotnet.Connect(addr.m_peer.m_dotnet, (int)port); IPEndPoint endPoint = m_dotnet.RemoteEndPoint as IPEndPoint; m_remoteAddr = IpAddrPeer.make(endPoint.Address); m_remotePort = endPoint.Port; return(fan); } catch (SocketException e) { throw IOErr.make(e).val; } }
public UdpPacket receive(UdpSocket fan, UdpPacket packet) { // create packet if null if (packet == null) { packet = UdpPacket.make(null, null, new MemBuf(1024)); } // map buf bytes to packet MemBuf data = (MemBuf)packet.data(); byte[] buf = data.m_buf; int off = data.m_pos; int len = buf.Length - off; int recv = 0; EndPoint sender = new IPEndPoint(IPAddress.Any, 0); // receive if (isConnected(fan)) { try { recv = m_dotnet.Receive(buf, off, len, SocketFlags.None); sender = m_dotnet.RemoteEndPoint; } catch (SocketException e) { // .NET will truncate contents correctly, but still throws a // SocketException, so catch that specific case and allow it if (e.Message.StartsWith("A message sent on a datagram socket was larger")) { recv = len; sender = m_dotnet.RemoteEndPoint; } else { throw IOErr.make(e).val; } } } else { try { if (m_dotnet == null) { m_dotnet = createSocket(); } recv = m_dotnet.ReceiveFrom(buf, off, len, SocketFlags.None, ref sender); } catch (SocketException e) { // .NET will truncate contents correctly, but still throws a // SocketException, so catch that specific case and allow it if (e.Message.StartsWith("A message sent on a datagram socket was larger")) { recv = len; } else { throw IOErr.make(e).val; } } } // update packet with received message IPEndPoint endPoint = sender as IPEndPoint; packet.addr(IpAddrPeer.make(endPoint.Address)); packet.port(Long.valueOf(endPoint.Port)); data.m_pos += recv; data.m_size += recv; return(packet); }