Ejemplo n.º 1
0
        private static void DoReceiveFrom(IAsyncResult iar)
        {
            try {
                var packet   = new NetPacket();
                var clientEp = (EndPoint) new IPEndPoint(IPAddress.Any, 0);
                var dataLen  = 0;
                try {
                    dataLen = _socket.EndReceiveFrom(iar, ref clientEp);
                    packet.Assign(clientEp, dataLen, _buffer);
                } catch (SocketException se) {
                    ProcessAsyncReadError(se, clientEp);
                } finally {
                    var newEp = (EndPoint) new IPEndPoint(IPAddress.Any, 0);
                    while (true)
                    {
                        try {
                            _socket.BeginReceiveFrom(_buffer, 0, _buffer.Length, SocketFlags.None, ref newEp, DoReceiveFrom, newEp);
                            break;
                        } catch (SocketException se) {
                            ProcessAsyncReadError(se, newEp);
                        }
                    }
                }

                // do not need process empty packets
                if (dataLen == 0 || !packet.HasData)
                {
                    return;
                }

                // check for connectionless header (0xffffffff) first
                if (Networking.IsConnectionlessHeader(packet.Data))
                {
                    packet.Message.ReadInt(); // read connectionless header (the -1)

                    Server.ProcessConnectionlessPacket(packet);
                    return;
                }

                var channel = Networking.FindNetChannelFor(_socket, packet.From);
                if (channel != null)
                {
                    channel.ProcessPacket(packet, true);
                }
            } catch (ObjectDisposedException e) {
                Out.Exception(e, "DoReceiveFrom cause error");
            }
        }