Exemplo 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");
            }
        }
Exemplo n.º 2
0
        private static void NetTick()
        {
            var netbuffer = new byte[16936];
            var serverEp  = (EndPoint) new IPEndPoint(IPAddress.Any, 0);

            while (true)
            {
                try {
                    var recvLen = 0;
                    try {
                        recvLen = _socket.ReceiveFrom(netbuffer, netbuffer.Length, SocketFlags.None, ref serverEp);
                    } catch (SocketException e) {
                        Console.WriteLine("Receive error ({0}): {1}", e.GetType(), e.Message);
                        continue;
                    }

                    if (recvLen <= 0)
                    {
                        continue;
                    }

                    var packet = new NetPacket();
                    packet.Assign(serverEp, recvLen, netbuffer);

                    if (Networking.IsConnectionlessHeader(packet.Data))
                    {
                        packet.Message.ReadInt(); // read connectionless header (the -1)

                        _clientState.ProcessConnectionlessPacket(packet);
                        continue;
                    }

                    if (_clientState.NetChannel != null)
                    {
                        _clientState.NetChannel.ProcessPacket(packet, true);
                    }
                } catch (Exception e) {
                    Console.WriteLine("Unknown network think error ({0}): {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
                }
            }
        }