示例#1
0
 public void UnregisterEndPoint(IPEndPoint ep)
 {
     if (_useNativeSockets && ep is NativeEndPoint nep)
     {
         var nativeAddr = new NativeAddr(nep.NativeAddress, nep.NativeAddress.Length);
         _nativeAddrMap.Remove(nativeAddr);
     }
 }
示例#2
0
        private void NativeReceiveLogic(object state)
        {
            Socket socket       = (Socket)state;
            IntPtr socketHandle = socket.Handle;

            byte[] addrBuffer = new byte[socket.AddressFamily == AddressFamily.InterNetwork
                ? NativeSocket.IPv4AddrSize
                : NativeSocket.IPv6AddrSize];

            int        addrSize = addrBuffer.Length;
            IPEndPoint endPoint = null;
            NetPacket  packet   = _listener.NetPacketPool.GetPacket(NetConstants.MaxPacketSize);

            while (IsActive())
            {
                //Reading data
                packet.Size = NativeSocket.RecvFrom(socketHandle, packet.RawData, NetConstants.MaxPacketSize, addrBuffer, ref addrSize);
                if (packet.Size == 0)
                {
                    return;
                }
                if (packet.Size == -1)
                {
                    SocketError errorCode = NativeSocket.GetSocketError();
                    if (errorCode == SocketError.WouldBlock || errorCode == SocketError.TimedOut) //Linux timeout EAGAIN
                    {
                        continue;
                    }
                    if (ProcessError(new SocketException((int)errorCode), endPoint))
                    {
                        return;
                    }
                    continue;
                }

                NativeAddr nativeAddr = new NativeAddr(addrBuffer, addrSize);
                if (!_nativeAddrMap.TryGetValue(nativeAddr, out endPoint))
                {
                    endPoint = new NativeEndPoint(addrBuffer);
                }

                //All ok!
                //NetDebug.WriteForce($"[R]Received data from {endPoint}, result: {packet.Size}");
                _listener.OnMessageReceived(packet, 0, endPoint);
                packet = _listener.NetPacketPool.GetPacket(NetConstants.MaxPacketSize);
            }
        }
示例#3
0
        private void NativeReceiveLogic(object state)
        {
            Socket socket       = (Socket)state;
            IntPtr socketHandle = socket.Handle;

            byte[] addrBuffer = new byte[socket.AddressFamily == AddressFamily.InterNetwork
                ? NativeSocket.IPv4AddrSize
                : NativeSocket.IPv6AddrSize];

            int             addrSize  = addrBuffer.Length;
            IPEndPoint      endPoint  = null;
            NativeTimeValue timeValue = new NativeTimeValue
            {
                Seconds      = (int)(ReceivePollingTime / 1000000L),
                Microseconds = (int)(ReceivePollingTime % 1000000L)
            };
            var pollHandle = new IntPtr[2];

            while (IsActive())
            {
                NetPacket packet;

                //Reading data
                try
                {
                    if (socket.Available == 0)
                    {
                        pollHandle[0] = (IntPtr)1;
                        pollHandle[1] = socketHandle;
                        if (NativeSocket.Poll(pollHandle, ref timeValue) == -1)
                        {
                            throw new SocketException((int)NativeSocket.GetSocketError());
                        }
                        if ((int)pollHandle[0] == 0 || pollHandle[1] != socketHandle)
                        {
                            continue;
                        }
                    }
                    packet      = _listener.NetPacketPool.GetPacket(NetConstants.MaxPacketSize);
                    packet.Size = NativeSocket.RecvFrom(socketHandle, packet.RawData, NetConstants.MaxPacketSize, addrBuffer, ref addrSize);
                    if (packet.Size == -1)
                    {
                        throw new SocketException((int)NativeSocket.GetSocketError());
                    }

                    NativeAddr nativeAddr = new NativeAddr(addrBuffer, addrSize);
                    if (!_nativeAddrMap.TryGetValue(nativeAddr, out endPoint))
                    {
                        endPoint = new NativeEndPoint(addrBuffer);
                    }
                }
                catch (SocketException ex)
                {
                    if (ProcessError(ex, endPoint))
                    {
                        return;
                    }
                    continue;
                }
                catch (ObjectDisposedException)
                {
                    return;
                }

                //All ok!
                //NetDebug.Write(NetLogLevel.Trace, "[R]Received data from {0}, result: {1}", endPoint.ToString(), packet.Size);
                _listener.OnMessageReceived(packet, 0, endPoint);
            }
        }