void HandleConnectionlessInboundPacket(NetConnectionlessInboundPacket packet)
 {
     if (packet.Type == NetPacketType.ConnectionRequest)
     {
         NetConnectionRequest request = new NetConnectionRequest(packet);
         HandleConnectionRequest(request);
     }
     else if (packet.Type == NetPacketType.ConnectionApproved)
     {
         HandleConnectionApproved(packet.SenderIP);
     }
     else if (packet.Type == NetPacketType.ConnectionDenied)
     {
         NetDenialReason reason = (NetDenialReason)packet.ReadByte();
         HandleConnectionDenied(packet.SenderIP, reason);
     }
     else if (packet.Type == NetPacketType.ConnectionReady)
     {
         HandleConnectionReady(packet.SenderIP);
     }
     else
     {
         AddWatchedConnection(packet.SenderIP, string.Format("Sent invalid connectionless packet ({0})", packet.Type));
     }
 }
Exemplo n.º 2
0
        internal NetConnectionRequest(NetConnectionlessInboundPacket packet)
        {
            EndPoint = packet.SenderIP;

            // If the packet contains a password
            if (packet.ReadBool())
            {
                Password = packet.ReadString();
            }
        }
        void PacketReceived(byte[] data, IPEndPoint sender, bool parentWasChunk = false)
        {
            NetInboundPacketBase packet;
            NetInboundPacket     connectionyPacket = null;
            bool connectionless;

            NetConnection senderConn;

            if (Connections.TryGetValue(sender, out senderConn))
            {
                connectionless = false;
                packet         = connectionyPacket = new NetInboundPacket(senderConn, data);

                if (!parentWasChunk)
                {
                    senderConn.PhysicalPacketReceived();
                }
                else
                {
                    senderConn.PacketReceived();
                }
            }
            else
            {
                connectionless = true;
                packet         = new NetConnectionlessInboundPacket(sender, data);
            }

            if (packet.ReadHeader())
            {
                if (packet.isChunked)
                {
                    byte[][] chunks = packet.GetChunked();
                    for (int i = 0; i < chunks.Length; i++)
                    {
                        PacketReceived(chunks[i], sender, true);
                    }
                }
                else
                {
                    if (packet.Type == NetPacketType.MTUTest)
                    {
                        return;
                    }

                    if (ignoredConnections.Count > 0 && ignoredConnections.ContainsKey(sender))
                    {
                        return;
                    }

                    bool packetAlreadyReceived = connectionless ? false : !connectionyPacket.Sender.TryHandlePacket(packet.Id);

                    if (NetLogger.LogPacketReceives && !IsPingPacket(packet.Type) && !packetAlreadyReceived)
                    {
                        NetLogger.LogVerbose("[Inbound:{0}] {1}", sender, packet.ToInternalString());
                    }

                    if (packet.isReliable && !connectionless)
                    {
                        ReplyAck(connectionyPacket, sender);
                    }
                    else if (packet.isReliable && connectionless)
                    {
                        AddWatchedConnection(sender, "Sent reliable connectionless packet");
                    }

                    if (packetAlreadyReceived)
                    {
                        if (NetLogger.LogAlreadyHandledPackets)
                        {
                            NetLogger.LogWarning("[DUPLICATE PACKET:{0}] Ignoring packet {1}", sender, packet.Id);
                        }

                        return;
                    }

                    if (packet.isEncrypted)
                    {
                        NetEncryption.DecryptPacket(packet);
                    }

                    if (packet.isCompressed)
                    {
                        NetCompressor.Decompress(packet);
                    }

                    if (packet.isPartial)
                    {
                        if (connectionyPacket == null)
                        {
                            AddWatchedConnection(sender, "Sent connectionless partial packet");
                            return;
                        }

                        if (!HandlePartial(ref connectionyPacket))
                        {
                            // Partial is not complete yet, so just return
                            return;
                        }
                        else
                        {
                            // Partial is complete, so overrite the current
                            // packet with the completed packet.
                            packet = connectionyPacket;
                        }
                    }

                    if (connectionless)
                    {
                        HandleConnectionlessInboundPacket((NetConnectionlessInboundPacket)packet);
                    }
                    else
                    {
                        HandleInboundPacket((NetInboundPacket)packet);
                    }
                }
            }
            else
            {
                AddWatchedConnection(sender, "Sent packet with invalid signature");
            }
        }