Example #1
0
        protected virtual void FlushPacket(Packet packet)
        {
            if (ConnectionClient == null || !ConnectionClient.Connected)
            {
                return;
            }

            try
            {
                packet.Direction = Direction;

                EventHandler <PacketEventArgs> packetSending = PacketSending;
                if (packetSending != null)
                {
                    packetSending(this, new PacketEventArgs(Proxy, packet));
                }

                if (packet.Ignore)
                {
                    return;
                }

                byte[] buffer;

                GenericPacket gp = packet as GenericPacket;

                if (gp != null)
                {
                    buffer = gp.Data;
                }
                else
                {
                    buffer = PacketSerializer.Serialize(packet);
                }

                bool compressed = buffer.Length >= 4096 || packet.AlwaysCompress;

                if (compressed)
                {
                    buffer = ZlibStream.CompressBuffer(buffer);
                }

                int length = compressed ? -buffer.Length : buffer.Length;

                byte[] lenBuf = VLQ.CreateSigned(length);

                byte[] finalBuffer = new byte[1 + lenBuf.Length + buffer.Length];
                finalBuffer[0] = packet.PacketId;
                Buffer.BlockCopy(lenBuf, 0, finalBuffer, 1, lenBuf.Length);
                Buffer.BlockCopy(buffer, 0, finalBuffer, 1 + lenBuf.Length, buffer.Length);

                SocketAsyncEventArgs args = new SocketAsyncEventArgs();
                args.UserToken  = packet;
                args.Completed += Operation_Completed;
                args.SetBuffer(finalBuffer, 0, finalBuffer.Length);

                if (ConnectionClient != null && !ConnectionClient.SendAsync(args))
                {
                    Operation_Completed(this, args);
                }
            }
            catch
            {
                CloseAsync().Wait();
            }
        }
Example #2
0
        /// <summary>
        /// Write a Signed VLQ to the stream
        /// </summary>
        /// <param name="vlq">The VLQ to write</param>
        public void WriteSignedVLQ(long vlq)
        {
            byte[] buffer = VLQ.CreateSigned(vlq);

            Write(buffer);
        }