private void ProcessUdpBytes(byte[] bytes, IPEndPoint remoteEndPoint, UdpClient client)
        {
            ThreadPool.QueueUserWorkItem(_ =>
            {
                try
                {
                    if (!localIPs.Any(addr => addr.Equals(remoteEndPoint.Address)))
                    {
                        if (bytes != null && bytes.Length >= PacketHeader.DefaultLength)
                        {
                            var header = PacketHeader.FromBytes(bytes, 0, PacketHeader.DefaultLength);

                            if (header != null)
                            {
                                var body = new byte[header.BodyLength];
                                Buffer.BlockCopy(bytes, PacketHeader.DefaultLength, body, 0, body.Length);
                                var packet = new Packet(header, body);
                                OnPacketReceived(packet, remoteEndPoint, client);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Env.Instance.Logger.Error(ex, "Process udp bytes error.");
                }
            });
        }
Exemple #2
0
        Task ProcessUdpBytesAsync(byte[] bytes, IPEndPoint remoteEndPoint)
        {
            //为了避免处理时间过程,影响接收下一个udp消息,启动到一个新的线程中。这里如果再用await呢?
            return(Task.Run(() =>
            {
                try
                {
                    if (bytes != null && bytes.Length >= PacketHeader.DefaultLength)
                    {
                        var header = PacketHeader.FromBytes(bytes, 0, PacketHeader.DefaultLength);

                        if (header != null)
                        {
                            var body = new byte[header.BodyLength];
                            Buffer.BlockCopy(bytes, PacketHeader.DefaultLength, body, 0, body.Length);
                            var packet = new Packet(header, body);
                            OnPacketReceived(packet, remoteEndPoint);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Env.Logger.Log("Process udp bytes error.", stackTrace: ex.StackTrace);
                }
            }));
        }
Exemple #3
0
        PacketHeader ReadPacketHeader()
        {
            PacketHeader header      = null;
            int          readedCount = 0;
            var          bytes       = Receive(PacketHeader.DefaultLength, out readedCount);

            if (readedCount == PacketHeader.DefaultLength)
            {
                header = PacketHeader.FromBytes(bytes);
                if (!IsValidHeader(header))
                {
                    header = null;
                }
            }
            return(header);
        }