void HandleInboundPacket(NetInboundPacket packet)
 {
     if (IsPingPacket(packet.Type))
     {
         HandlePingPacket(packet);
     }
     else if (packet.Type == NetPacketType.AckResponse)
     {
         // Handle ack
         ushort ackid = packet.ReadUInt16();
         if (NetLogger.LogAcks)
         {
             NetLogger.LogVerbose("[ACK] Received {0} from {1}", ackid, packet.Sender);
         }
         packet.Sender.HandleAck(ackid);
     }
     else if (packet.Type == NetPacketType.Custom)
     {
         receivedPackets.Enqueue(packet);
     }
     else if (packet.Type == NetPacketType.Disconnected)
     {
         string reason         = packet.ReadString();
         bool   lostConnection = packet.ReadBool();
         packet.Sender.Disconnect(reason, lostConnection);
     }
     else if (IsRemotePacket(packet.Type))
     {
         HandleRemotePacket(packet);
     }
     else
     {
         NetLogger.LogWarning("Invalid packet sent from {0}. Type: {1}", packet.Sender, packet.Type);
     }
 }
        void ReplyAck(NetInboundPacket packet, IPEndPoint sender)
        {
            // Create the ack packet
            NetOutboundPacket ack = new NetOutboundPacket(NetDeliveryMethod.Unreliable, 3);

            ack.Type = NetPacketType.AckResponse;
            ack.Write(packet.Id); // Write the id

            if (NetLogger.LogAcks)
            {
                NetLogger.LogVerbose("[ACK] Replying {0} to {1}", packet.Id, packet.Sender);
            }

            // And send
            packet.Sender.SendPacket(ack);
        }
        NetOutboundPacket[] SplitPacketIntoPartials(NetOutboundPacket packet)
        {
            packet.position = 0;

            // Calculate the number of packets to split it into,
            // and each of their sizes
            int    numPackets       = (int)Math.Ceiling((double)(packet.Length) / (MTU - 12));
            int    newPacketSize    = MTU - 12;
            int    packetChunkIndex = 0;
            ushort id = nextPartialId++;

            NetOutboundPacket[] partialPackets = new NetOutboundPacket[numPackets];

            for (byte i = 0; i < numPackets; i++)
            {
                int packetSize = Math.Min(newPacketSize, packet.Length - packetChunkIndex);

                // Create the new partial
                NetOutboundPacket partial = packet.Clone(true);
                partial.isPartial   = true;
                partial.Encrypt     = false;
                partial.Compression = NetPacketCompression.None;
                partial.Clear(6 + packetSize);

                // Write the partial header
                partial.Write(id);
                partial.Write(i);
                partial.Write((byte)numPackets);
                partial.Write((ushort)packetSize);

                // Write the data allocated to this partial
                partial.WriteBytes(packet.ReadBytes(packetChunkIndex, packetSize));

                // Add it to the list of partials
                partialPackets[i] = partial;
                packetChunkIndex += packetSize;
            }

            if (NetLogger.LogPartials)
            {
                NetLogger.LogVerbose("[Partial] Split Packet into {0} parts; Size: {1}b, Original Size: {2}b",
                                     numPackets, newPacketSize, packet.Length);
            }
            return(partialPackets);
        }
        internal void SendInternalPacket(NetOutboundPacket packet, IPEndPoint to)
        {
            packet.RemovePadding();
            packet.SetId(AllocatePacketId());

            if (packet.Type == NetPacketType.ConnectionRequest)
            {
                NetEncryption.EncryptPacket(packet);
            }

            packet.PrependHeader();

            if (NetLogger.LogPacketSends && !IsPingPacket(packet.Type))
            {
                NetLogger.LogVerbose("[Outbound:{0}] {1}", to, packet.ToInternalString());
            }

            SendDataToSocket(packet.data, to, false);
        }
        void AddPacketToChunkQueue(NetOutboundPacket packet)
        {
            // If it is reliable we need to mark it as a new send
            TryMarkReliablePacket(packet);

            // Log send
            if (NetLogger.LogPacketSends && CanLogSend(packet.Type) && !IsPingPacket(packet.Type))
            {
                NetLogger.LogVerbose("[Outbound:{0}] {1}", EndPoint, packet.ToInternalString());
            }

            // Send packet or add it to queue
            if (packet.SendImmediately)
            {
                // Just send it if needed
                SendPacketToSocket(packet); // Send
            }
            else
            {
                // Try to add the packet to the chunked queue
                if (!packetChunkQueue.Add(packet))
                {
                    NetLogger.LogError(
                        "Failed to add packet to the chunk queue. Id: {0}, Type: {1}, DeliveryMethod: {2}, Duplicate Id Added: {3}",
                        packet.Id, packet.Type, packet.DeliveryMethod, packetChunkQueue.Contains(packet));
                }
                else
                {
                    // Increment the chunks current size
                    currentChunkSize += packet.Length;

                    // If the chunk is too big, send it before any other chunks are added
                    if (currentChunkSize >= maxChunkSize)
                    {
                        SendChunk();
                    }
                }
            }
        }
        void AddRecentPing(int ping)
        {
            // Add the new ping
            recentPings[avgPingI++] = ping;

            // If we've reached the end of one ping collection
            if (avgPingI == recentPings.Length)
            {
                // Average the pings we collected
                AverageRecentPings();
                avgPingI = 0;

                // If we are calculating the base ping as well, set it
                if (baseAvgPing == -1)
                {
                    if (NetLogger.LogFlowControl)
                    {
                        NetLogger.LogVerbose("[FlowControl] Base Ping for {0} set to {1}", EndPoint, lastAvgPing);
                    }
                    baseAvgPing = lastAvgPing;
                }
                else if (NetTime.Now - sendRateSwitchCooldown >= 0)
                {
                    // If the ping increased too much, drop the send rate
                    if (lastAvgPing - baseAvgPing > 80)
                    {
                        sendRateDropped = true;
                        if (!config.DontApplyPingControl)
                        {
                            if (!NetLogger.IgnoreSendRateChanges)
                            {
                                NetLogger.LogWarning("[FlowControl] Dropped send rate for {0}", EndPoint);
                            }
                            chunkSendDelay = droppedSendRate[0];
                            PacketSendRate = droppedSendRate[1];
                        }
                        sendRateSwitchCooldown = NetTime.Now + sendRateSwitchCooldownDelay;
                    }
                    // If the ping returned to normal, try increasing the send rate
                    else if (sendRateDropped && lastAvgPing - baseAvgPing < 80)
                    {
                        sendRateDropped = false;
                        if (!config.DontApplyPingControl)
                        {
                            if (!NetLogger.IgnoreSendRateChanges)
                            {
                                NetLogger.Log("[FlowControl] Send rate set to normal for {0}", EndPoint);
                            }
                            chunkSendDelay = normalSendRate[0];
                            PacketSendRate = normalSendRate[1];
                        }
                        sendRateSwitchCooldown = NetTime.Now + sendRateSwitchCooldownDelay;
                    }
                    // If the average ping dropped, see if we can lower the base ping
                    else if (!sendRateDropped && lastAvgPing - baseAvgPing < -20)
                    {
                        if (NetLogger.LogFlowControl)
                        {
                            NetLogger.LogVerbose("[FlowControl] Attempting to increase base ping for {0}...", EndPoint);
                        }
                        RecalculateBasePing();
                    }
                }
            }
        }
        void MTUHeartbeat(int now)
        {
            if (mtu_status == MTUStatus.Unset)
            {
                // Prep next MTU test
                mtu_status  = MTUStatus.Setting;
                nextMTUTest = now + MTU_UnsetTry_Delay + 1.5f + Stats.Ping;
                if (NetLogger.LogFlowControl)
                {
                    NetLogger.LogVerbose("[FlowControl] Expanding MTU for {0}...", EndPoint);
                }
            }
            else if (now >= nextMTUTest)
            {
                if (mtu_status == MTUStatus.Setting)
                {
                    // Try and send an MTU packet
                    if (lastMTUTest < NetMessenger.MAX_UDP_PACKET_SIZE && TrySendMTU(lastMTUTest))
                    {
                        // If it succeeds then continue
                        lastMTUTest = (int)(lastMTUTest * MTU_TryInc);
                        now        += MTU_UnsetTry_Delay;
                    }
                    else
                    {
                        // If it fails, set the MTU and stop trying to expand it
                        if (lastMTUTest > NetMessenger.MAX_UDP_PACKET_SIZE)
                        {
                            lastMTUTest = NetMessenger.MAX_UDP_PACKET_SIZE;
                        }

                        // Use 75% of the MTU as the max chunk size, and cap it at 5000 bytes.
                        maxChunkSize = (int)Math.Min(MaxUDP_MTU, lastMTUTest * 0.75f);
                        MTU          = maxChunkSize;
                        Stats.MTU    = MTU;

                        MTUEventNeedsCall = true;

                        if (NetLogger.LogFlowControl)
                        {
                            NetLogger.LogVerbose("[FlowControl] MTU for {0} set to {1}", EndPoint, MTU);
                        }
                        mtu_status = MTUStatus.Set;
                        now       += MTU_SetTry_Delay;
                    }
                }
                // No need.

                /*else if (status == MTUStatus.Set)
                 * {
                 *  int tryExpandAmount = Math.Min((int)(lastMTUTest * MTU_TryInc), MaxUDP_MTU);
                 *
                 *  if (TrySendMTU(tryExpandAmount))
                 *  {
                 *      lastMTUTest = (int)(lastMTUTest * MTU_TryInc);
                 *      now += MTU_UnsetTry_Delay;
                 *      MTU_Status = MTUStatus.Setting;
                 *      NetLogger.LogVerbose("Expanding MTU further for {0}...", EndPoint);
                 *  }
                 *  else
                 *      now += MTU_SetTry_Delay;
                 * }*/
            }
        }
        bool HandlePartial(ref NetInboundPacket packet)
        {
            // Read the partials header (6 bytes)
            ushort partialId   = packet.ReadUInt16();
            byte   index       = packet.ReadByte();
            byte   numPartials = packet.ReadByte();
            ushort partialSize = packet.ReadUInt16();

            PartialPacket partial;

            // See if the partial already exists
            if (!packet.Sender.Partials.TryGetValue(partialId, out partial))
            {
                // Since the partial doesn't exist, create one.
                if (!packet.Sender.Partials.TryAdd(partialId,
                                                   partial = new PartialPacket(packet.Sender, partialId, numPartials, partialSize)))
                {
                    //NetLogger.LogError("[Partial:{0}:{1}] Failed to add new partial!", packet.Sender.EndPoint, partialId);
                    //return false;

                    // TODO: See if theres a better way to handle this partial packet concurrency issue

                    // If for some reason, two partials are processed simultaneously,
                    // and it tried adding two packets at once,
                    // we'll just grab the packet created by the other and move on.
                    partial = packet.Sender.Partials[partialId];
                }
                else
                {
                    if (NetLogger.LogPartials)
                    {
                        NetLogger.LogVerbose("[Partial:{0}:{1}] Starting new; NumPackets: {2}; PacketSize: {3}b",
                                             packet.Sender, partialId, numPartials, partialSize);
                    }
                }
            }

            // Add the current partial
            if (partial.AddPacket(index, packet.ReadBytes(partialSize)))
            {
                if (NetLogger.LogPartials)
                {
                    NetLogger.LogVerbose("[Partial:{0}:{1}] Adding index: {2}; PacketsLeft: {3}",
                                         packet.Sender, partialId, index, partial.numPartials - partial.numPartialsReceived);
                }

                // Check if the partial is complete
                if (partial.numPartialsReceived >= partial.numPartials)
                {
                    // Remove the partial from the connections queue
                    if (packet.Sender.Partials.TryRemove(partial.id, out partial))
                    {
                        // Save the old sender
                        NetConnection sender = packet.Sender;
                        // Construct the final packet
                        packet = partial.Construct(packet);
                        // Reset the packets parameters
                        packet.ReadOnly = true;

                        if (NetLogger.LogPartials)
                        {
                            NetLogger.LogVerbose("[Partial:{0}:{1}] Constructed final partial; FullSize: {2}b",
                                                 sender, partialId, packet.Length);
                        }

                        // Process the partial like a physical packet
                        packet.position = 0;
                        if (packet.ReadHeader())
                        {
                            if (packet.isEncrypted)
                            {
                                NetEncryption.DecryptPacket(packet);
                            }
                            if (packet.isCompressed)
                            {
                                NetCompressor.Decompress(packet);
                            }

                            // Update the stats
                            packet.Sender.PartialPacketReceived(numPartials);

                            // Tell the caller this partial is ready!
                            return(true);
                        }
                        else
                        {
                            // Bad stuff happened
                            NetLogger.LogWarning("[Partial:{0}:{1}] Constructed partial packet had invalid header!",
                                                 sender, partialId);
                            return(false);
                        }
                    }
                    else
                    {
                        NetLogger.LogError("[Partial:{0}:{1}] Failed to remove completed partial!",
                                           packet.Sender, partialId);
                    }
                }
            }

            // Tell the caller this partial is not complete
            return(false);
        }
        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");
            }
        }