Beispiel #1
0
        public void AddOutgoingPacket(byte[] packet, PacketType packetType, PacketFlags addFlags = PacketFlags.None)
        {
            lock (sendLoopLock)
            {
                if (Closed)
                {
                    return;
                }

                if (NeedsSplitting(packet.Length) && packetType != PacketType.VoiceWhisper)
                {
                    // VoiceWhisper packets are for some reason excluded
                    if (packetType == PacketType.Voice)
                    {
                        return;                         // Exception maybe ??? This happens when a voice packet is bigger then the allowed size
                    }
                    var tmpCompress = QuickerLz.Compress(packet, 1);
                    if (tmpCompress.Length < packet.Length)
                    {
                        packet    = tmpCompress.ToArr();
                        addFlags |= PacketFlags.Compressed;
                    }

                    if (NeedsSplitting(packet.Length))
                    {
                        foreach (var splitPacket in BuildSplitList(packet, packetType))
                        {
                            AddOutgoingPacket(splitPacket, addFlags);
                        }
                        return;
                    }
                }
                AddOutgoingPacket(new OutgoingPacket(packet, packetType), addFlags);
            }
        }
Beispiel #2
0
        public void AddOutgoingPacket(ReadOnlySpan <byte> packet, PacketType packetType, PacketFlags addFlags = PacketFlags.None)
        {
            lock (sendLoopLock)
            {
                if (Closed)
                {
                    return;
                }

                if (NeedsSplitting(packet.Length) && packetType != PacketType.VoiceWhisper)
                {
                    // VoiceWhisper packets are for some reason excluded
                    if (packetType == PacketType.Voice)
                    {
                        return;                         // Exception maybe ??? This happens when a voice packet is bigger than the allowed size
                    }
                    var tmpCompress = QuickerLz.Compress(packet, 1);
                    if (tmpCompress.Length < packet.Length)
                    {
                        packet    = tmpCompress;
                        addFlags |= PacketFlags.Compressed;
                    }

                    if (NeedsSplitting(packet.Length))
                    {
                        AddOutgoingSplitData(packet, packetType, addFlags);
                        return;
                    }
                }
                SendOutgoingData(packet, packetType, addFlags);
            }
        }
Beispiel #3
0
        public E <string> AddOutgoingPacket(ReadOnlySpan <byte> packet, PacketType packetType, PacketFlags addFlags = PacketFlags.None)
        {
            lock (sendLoopLock)
            {
                if (closed != 0)
                {
                    return("Connection closed");
                }

                if (NeedsSplitting(packet.Length) && packetType != PacketType.VoiceWhisper)
                {
                    // VoiceWhisper packets are excluded for some reason
                    if (packetType == PacketType.Voice)
                    {
                        return("Voice packet too big");                        // This happens when a voice packet is bigger than the allowed size
                    }
                    var tmpCompress = QuickerLz.Compress(packet, 1);
                    if (tmpCompress.Length < packet.Length)
                    {
                        packet    = tmpCompress;
                        addFlags |= PacketFlags.Compressed;
                    }

                    if (NeedsSplitting(packet.Length))
                    {
                        return(AddOutgoingSplitData(packet, packetType, addFlags));
                    }
                }
                return(SendOutgoingData(packet, packetType, addFlags));
            }
        }
Beispiel #4
0
        private static bool TryFetchPacket(RingQueue <S2CPacket> packetQueue, out S2CPacket packet)
        {
            if (packetQueue.Count <= 0)
            {
                packet = null; return(false);
            }

            int  take     = 0;
            int  takeLen  = 0;
            bool hasStart = false;
            bool hasEnd   = false;

            for (int i = 0; i < packetQueue.Count; i++)
            {
                if (packetQueue.TryPeekStart(i, out var peekPacket))
                {
                    take++;
                    takeLen += peekPacket.Size;
                    if (peekPacket.FragmentedFlag)
                    {
                        if (!hasStart)
                        {
                            hasStart = true;
                        }
                        else
                        {
                            hasEnd = true; break;
                        }
                    }
                    else
                    {
                        if (!hasStart)
                        {
                            hasStart = true; hasEnd = true; break;
                        }
                    }
                }
                else
                {
                    break;
                }
            }

            if (!hasStart || !hasEnd)
            {
                packet = null; return(false);
            }

            // GET
            if (!packetQueue.TryDequeue(out packet))
            {
                throw new InvalidOperationException("Packet in queue got missing (?)");
            }

            // MERGE
            if (take > 1)
            {
                var preFinalArray = new byte[takeLen];

                // for loop at 0th element
                int curCopyPos = packet.Size;
                Array.Copy(packet.Data, 0, preFinalArray, 0, packet.Size);

                for (int i = 1; i < take; i++)
                {
                    if (!packetQueue.TryDequeue(out S2CPacket nextPacket))
                    {
                        throw new InvalidOperationException("Packet in queue got missing (?)");
                    }

                    Array.Copy(nextPacket.Data, 0, preFinalArray, curCopyPos, nextPacket.Size);
                    curCopyPos += nextPacket.Size;
                }
                packet.Data = preFinalArray;
            }

            // DECOMPRESS
            if (packet.CompressedFlag)
            {
                try
                {
                    packet.Data = QuickerLz.Decompress(packet.Data, MaxDecompressedSize);
                }
                catch (Exception)
                {
                    Debug.WriteLine("Got invalid compressed data.");
                    return(false);
                }
            }
            return(true);
        }