Exemple #1
0
        public void TestBasicBuffering()
        {
            DummySession sess = new DummySession();

            sess.FilterChain.AddFirst("peer", new DummyFilter());
            sess.FilterChain.AddFirst("logger", new LoggingFilter());
            BufferedWriteFilter bFilter = new BufferedWriteFilter(10);

            sess.FilterChain.AddLast("buffer", bFilter);

            IoBuffer data = IoBuffer.Allocate(1);

            for (byte i = 0; i < 20; i++)
            {
                data.Put((byte)(0x30 + i));
                data.Flip();
                sess.Write(data);
                data.Clear();
            }

            // Add one more byte to overflow the final buffer
            data.Put((byte)0);
            data.Flip();
            sess.Write(data);

            // Flush the final byte
            bFilter.Flush(sess);

            sess.Close(true);
        }
        public static void PutPascalVLCString(this IoBuffer buffer, String value)
        {
            byte[] encode = null;
            long   strlen = 0;

            if (value != null)
            {
                encode = Encoding.UTF8.GetBytes(value);
                strlen = encode.Length;
            }

            bool write = strlen > 0;
            bool first = true;

            while (strlen > 0 || first)
            {
                buffer.Put((byte)(((strlen > 127) ? (uint)128 : 0) | (strlen & 127)));
                strlen >>= 7;
                first    = false;
            }

            if (write)
            {
                buffer.Put(encode);
            }
        }
Exemple #3
0
        public override void Serialize(IoBuffer output, ISerializationContext context)
        {
            output.Skip(37);

            output.PutPascalVLCString(Name);
            output.PutPascalVLCString(Description);
            output.Put(Gender == Gender.FEMALE ? (byte)0x01 : (byte)0x00);

            switch (SkinTone)
            {
            case SkinTone.LIGHT:
                output.Put(0x00);
                break;

            case SkinTone.MEDIUM:
                output.Put(0x01);
                break;

            case SkinTone.DARK:
                output.Put(0x02);
                break;
            }

            output.PutUInt32(HeadOutfitId);
            output.Skip(4);//Unknown
            output.PutUInt32(BodyOutfitId);
        }
        public static void PutPascalVLCString(this IoBuffer buffer, string value)
        {
            long strlen = 0;

            if (value != null)
            {
                strlen = value.Length;
            }

            bool write = strlen > 0;
            bool first = true;

            while (strlen > 0 || first)
            {
                buffer.Put((byte)(((strlen > 127) ? 128 : 0) | (strlen & 127)));
                strlen >>= 7;
                first    = false;
            }

            if (write)
            {
                foreach (char ch in value.ToCharArray())
                {
                    buffer.Put((byte)ch);
                }
            }
        }
Exemple #5
0
        public IDecodingState Decode(IoBuffer input, IProtocolDecoderOutput output)
        {
            if (_buffer == null)
            {
                if (input.Remaining >= _length)
                {
                    Int32 limit = input.Limit;
                    input.Limit = input.Position + _length;
                    IoBuffer product = input.Slice();
                    input.Position = input.Position + _length;
                    input.Limit    = limit;
                    return(FinishDecode(product, output));
                }

                _buffer = IoBuffer.Allocate(_length);
                _buffer.Put(input);
                return(this);
            }

            if (input.Remaining >= _length - _buffer.Position)
            {
                Int32 limit = input.Limit;
                input.Limit = input.Position + _length - _buffer.Position;
                _buffer.Put(input);
                input.Limit = limit;
                IoBuffer product = _buffer;
                _buffer = null;
                return(FinishDecode(product.Flip(), output));
            }

            _buffer.Put(input);
            return(this);
        }
        public static void PutPascalString(this IoBuffer buffer, string value)
        {
            long strlen = 0;

            if (value != null)
            {
                strlen = value.Length;
            }

            byte len1 = (byte)((strlen >> 24) | 0x80);
            byte len2 = (byte)((strlen >> 16) & 0xFF);
            byte len3 = (byte)((strlen >> 8) & 0xFF);
            byte len4 = (byte)(strlen & 0xFF);

            buffer.Put(len1);
            buffer.Put(len2);
            buffer.Put(len3);
            buffer.Put(len4);

            if (strlen > 0)
            {
                foreach (char ch in value.ToCharArray())
                {
                    buffer.Put((byte)ch);
                }
            }
        }
Exemple #7
0
 protected override void Fill(IoBuffer buffer)
 {
     buffer.Put(Length);
     buffer.Put(FRAME_SOURCE_ADDR);
     buffer.Put(Protocol);
     buffer.Put(FRAME_SEQUENCE_VALUE);
     this.FillData(buffer);
 }
Exemple #8
0
 public override void Serialize(IoBuffer output, ISerializationContext context)
 {
     output.Put(EchoRequested ? (byte)1 : (byte)0);
     PutSender(output, Sender);
     output.Put(Badge);
     output.Put(Alertable);
     output.PutPascalString(Message);
 }
Exemple #9
0
 public override void Serialize(IoBuffer output, ISerializationContext context)
 {
     output.PutPascalString(SenderID);
     output.PutPascalString(SenderAccount);
     output.Put(Badge);
     output.Put(IsAlertable);
     output.PutPascalString(Subject);
     output.PutPascalString(Message);
 }
Exemple #10
0
        public IDecodingState Decode(IoBuffer input, IProtocolDecoderOutput output)
        {
            Int32 terminatorPos = input.IndexOf(_terminator);

            if (terminatorPos >= 0)
            {
                Int32    limit = input.Limit;
                IoBuffer product;

                if (input.Position < terminatorPos)
                {
                    input.Limit = terminatorPos;

                    if (_buffer == null)
                    {
                        product = input.Slice();
                    }
                    else
                    {
                        _buffer.Put(input);
                        product = _buffer.Flip();
                        _buffer = null;
                    }

                    input.Limit = limit;
                }
                else
                {
                    // When input contained only terminator rather than actual data...
                    if (_buffer == null)
                    {
                        product = IoBuffer.Allocate(0);
                    }
                    else
                    {
                        product = _buffer.Flip();
                        _buffer = null;
                    }
                }
                input.Position = terminatorPos + 1;
                return(FinishDecode(product, output));
            }

            if (_buffer == null)
            {
                _buffer            = IoBuffer.Allocate(input.Remaining);
                _buffer.AutoExpand = true;
            }

            _buffer.Put(input);
            return(this);
        }
Exemple #11
0
        public override void Serialize(IoBuffer output, ISerializationContext context)
        {
            output.PutUInt32(SendingAvatarID);
            PutSender(output, Sender);
            output.Put(Badge);
            output.Put(IsAlertable);

            if (Body != null)
            {
                var bodyBytes = context.ModelSerializer.SerializeBuffer(Body, context, true);
                output.PutUInt32((uint)bodyBytes.Remaining);
                output.Put(bodyBytes);
            }
        }
Exemple #12
0
 public override void Serialize(IoBuffer output, ISerializationContext context)
 {
     using (var mem = new MemoryStream())
     {
         using (var writer = new BinaryWriter(mem))
         {
             Tuning.SerializeInto(writer);
             var result = mem.ToArray();
             output.PutInt32(result.Length);
             output.Put(result, 0, result.Length);
         }
     }
     output.PutInt32(ObjectUpgrades.Length);
     output.Put(ObjectUpgrades, 0, ObjectUpgrades.Length);
 }
        private void Write(IoSession session, IoBuffer data, IoBuffer buf)
        {
            try
            {
                Int32 len = data.Remaining;
                if (len >= buf.Capacity)
                {
                    /*
                     * If the request length exceeds the size of the output buffer,
                     * flush the output buffer and then write the data directly.
                     */
                    INextFilter nextFilter = session.FilterChain.GetNextFilter(this);
                    InternalFlush(nextFilter, session, buf);
                    nextFilter.FilterWrite(session, new DefaultWriteRequest(data));
                    return;
                }
                if (len > (buf.Limit - buf.Position))
                {
                    InternalFlush(session.FilterChain.GetNextFilter(this), session, buf);
                }

                lock (buf)
                {
                    buf.Put(data);
                }
            }
            catch (Exception e)
            {
                session.FilterChain.FireExceptionCaught(e);
            }
        }
Exemple #14
0
        public void SendMsg()
        {
            //lock (this)
            {
                while (m_sendMsgs.Count > 0)
                {
                    MessageBody msg = m_sendMsgs.Dequeue();
                    if (msg != null)
                    {
                        m_sendBuffer.Clear();
                        Array.Clear(m_sendData, 0, m_sendData.Length);

                        int len = CPacket.HEADER_TYPE_BYTES + msg.data.Length;
                        m_sendBuffer.PutInt32(len);
                        m_sendBuffer.PutInt16((short)msg.type);
                        m_sendBuffer.Put(msg.data);
                        m_sendBuffer.Flip();

                        m_sendBuffer.Get(m_sendData, 0, len + CPacket.HEADER_LENGTH_BYTES);
                        m_currentSendBufferWritePosition = len + CPacket.HEADER_LENGTH_BYTES;
                        SendMsg(m_sendData);
                    }
                }
            }
        }
Exemple #15
0
        /// <summary>
        /// Processes receive events.
        /// </summary>
        /// <param name="e"></param>
        public void ProcessReceive(SocketAsyncEventArgs e)
        {
            if (e.SocketError == SocketError.Success)
            {
                if (e.BytesTransferred > 0)
                {
                    _readBuffer.Position = e.BytesTransferred;
                    _readBuffer.Flip();

                    if (ReuseBuffer)
                    {
                        EndReceive(_readBuffer);
                    }
                    else
                    {
                        IoBuffer buf = IoBuffer.Allocate(_readBuffer.Remaining);
                        buf.Put(_readBuffer);
                        buf.Flip();
                        EndReceive(buf);
                    }

                    return;
                }
                else
                {
                    // closed
                    //Processor.Remove(this);
                    this.FilterChain.FireInputClosed();
                }
            }
            else
            {
                EndReceive(new SocketException((Int32)e.SocketError));
            }
        }
Exemple #16
0
        /// <summary>
        /// Processes receive events.
        /// </summary>
        /// <param name="e"></param>
        public void ProcessReceive(SocketAsyncEventArgs e)
        {
            if (e.SocketError == SocketError.Success)
            {
                if (e.BytesTransferred > 0)
                {
                    _readBuffer.Position = e.BytesTransferred;
                    _readBuffer.Flip();

                    if (ReuseBuffer)
                    {
                        EndReceive(_readBuffer);
                    }
                    else
                    {
                        IoBuffer buf = IoBuffer.Allocate(_readBuffer.Remaining);
                        buf.Put(_readBuffer);
                        buf.Flip();
                        EndReceive(buf);
                    }

                    return;
                }
            }
            else if (e.SocketError != SocketError.OperationAborted &&
                     e.SocketError != SocketError.Interrupted)
            {
                EndReceive(new SocketException((Int32)e.SocketError));
            }
        }
        /// <inheritdoc/>
        public void MergeAll()
        {
            if (!_buffersOnly)
            {
                throw new InvalidOperationException("The encoded messages contains a non-buffer.");
            }

            if (_queue.Count < 2)
            {
                // no need to merge!
                return;
            }

            Int32 sum = 0;

            foreach (var item in _queue)
            {
                sum += ((IoBuffer)item).Remaining;
            }

            IoBuffer newBuf = IoBuffer.Allocate(sum);

            for (; ;)
            {
                Object obj = _queue.Dequeue();
                if (obj == null)
                {
                    break;
                }
                newBuf.Put((IoBuffer)obj);
            }

            newBuf.Flip();
            _queue.Enqueue(newBuf);
        }
Exemple #18
0
 public override void Serialize(IoBuffer output, ISerializationContext context)
 {
     output.PutEnum(Type);
     if (Type == MailRequestType.SEND)
     {
         byte[] dat;
         if (Item == null)
         {
             dat = new byte[0];
         }
         else
         {
             using (var str = new MemoryStream())
             {
                 Item.Save(str);
                 dat = str.ToArray();
             }
         }
         output.PutInt32(dat.Length);
         foreach (var b in dat)
         {
             output.Put(b);
         }
     }
     else
     {
         output.PutInt64(TimestampID);
     }
 }
Exemple #19
0
        public void GetMsg(object o)
        {
            while (m_isSocketReady)
            {
                int recieveLen = 0;
                try
                {
                    if (m_mySocket.Available > 0)
                    {
                        recieveLen = m_mySocket.Client.Receive(m_getData, 0, (int)m_mySocket.ReceiveBufferSize, SocketFlags.None);
                    }
                    else
                    {
                        System.Threading.Thread.Sleep(1);
                        break;
                    }
                }
                catch (Exception e)
                {
                    DEBUG.Error(e.Message);
                    break;
                }

                m_readBuffer.Put(m_getData, 0, recieveLen);
                Array.Clear(m_getData, 0, m_getData.Length);
                m_readBuffer.Flip();
                int len = GetPacketLength(m_readBuffer);

                if (len < 0)
                {
                    break;
                }

                MessageBody msg = new MessageBody();
                msg.size = (uint)CPacket.PopPacketLength(m_readBuffer);
                msg.type = (ushort)CPacket.PopPacketType(m_readBuffer);
                byte[] data = new byte[len - CPacket.HEADER_TYPE_BYTES];
                m_readBuffer.Get(data, 0, len - CPacket.HEADER_TYPE_BYTES);
                msg.data = data;

                m_recievedMsgs.Add(msg);

                bool isFinish = false;
                if (m_readBuffer.HasRemaining)
                {
                    m_readBuffer.Compact();
                }
                else
                {
                    m_readBuffer.Clear();
                    isFinish = true;
                }

                if (isFinish)
                {
                    break;
                }
            }
        }
Exemple #20
0
 public override void Serialize(IoBuffer output, ISerializationContext context)
 {
     output.PutInt16(MaxLots);
     output.PutInt16(CurrentLots);
     output.Put(CpuPercentAvg);
     output.PutInt64(RamUsed);
     output.PutInt64(RamAvaliable);
 }
            public override void WriteByte(Byte value)
            {
                IoBuffer buf = IoBuffer.Allocate(1);

                buf.Put(value);
                buf.Flip();
                _sslHandler.WriteBuffer(buf);
            }
        private void ReceiveCallback(IAsyncResult ar)
        {
            System.Net.Sockets.Socket socket = (System.Net.Sockets.Socket)ar.AsyncState;
            Int32 read = 0;

            try
            {
                read = socket.EndReceive(ar);
            }
            catch (ObjectDisposedException)
            {
                // do nothing
                return;
            }
            catch (SocketException ex)
            {
                if (ex.SocketErrorCode != SocketError.OperationAborted &&
                    ex.SocketErrorCode != SocketError.Interrupted &&
                    ex.SocketErrorCode != SocketError.ConnectionReset)
                {
                    EndReceive(ex);
                }
                else
                {
                    // closed
                    Processor.Remove(this);
                }
                return;
            }
            catch (Exception ex)
            {
                EndReceive(ex);
                return;
            }

            if (read > 0)
            {
                if (ReuseBuffer)
                {
                    EndReceive(IoBuffer.Wrap(_readBuffer, 0, read));
                }
                else
                {
                    IoBuffer buf = IoBuffer.Allocate(read);
                    buf.Put(_readBuffer, 0, read);
                    buf.Flip();
                    EndReceive(buf);
                }
            }
            else
            {
                // closed
                //Processor.Remove(this);
                this.FilterChain.FireInputClosed();
            }
        }
Exemple #23
0
    public void Encode(IoSession session, PacketOutStream message, IProtocolEncoderOutput output)
    {
        int      size = message.GetSize();
        IoBuffer buf  = IoBuffer.Allocate(size + 4);

        buf.PutInt32(size);
        buf.Put(message.getPackets2());
        buf.Flip();
        output.Write(buf);
    }
Exemple #24
0
        public void Serialize(IoBuffer output, object value, ISerializationContext serializer)
        {
            var list = (IList <sbyte>)value;

            output.PutUInt32((uint)list.Count);
            for (int i = 0; i < list.Count; i++)
            {
                output.Put((byte)list[i]);
            }
        }
Exemple #25
0
        private void StoreRemainingInSession(IoBuffer buf, IoSession session)
        {
            IoBuffer remainingBuf = IoBuffer.Allocate(buf.Capacity);

            remainingBuf.AutoExpand = true;

            remainingBuf.Order = buf.Order;
            remainingBuf.Put(buf);

            session.SetAttribute(BUFFER, remainingBuf);
        }
Exemple #26
0
        public void Serialize(IoBuffer output, object value, ISerializationContext serializer)
        {
            var dict = (ImmutableDictionary <uint, bool>)value;

            output.PutUInt32((uint)dict.Count);
            foreach (var elem in dict)
            {
                output.PutUInt32(elem.Key);
                output.Put((byte)(elem.Value ? 1 : 0));
            }
        }
Exemple #27
0
 public void Serialize(IoBuffer output, ISerializationContext context)
 {
     output.PutUInt32(outfit_id);
     output.PutUInt64(asset_id);
     output.PutInt32(sale_price);
     output.PutInt32(purchase_price);
     output.PutEnum(owner_type);
     output.PutUInt32(owner_id);
     output.Put(outfit_type);
     output.PutEnum(outfit_source);
 }
Exemple #28
0
        public void Serialize(IoBuffer output, object value, ISerializationContext serializer)
        {
            var dat = (cTSOGenericData)value;

            output.PutUInt32(0x0A2C6585);
            output.PutUInt32((uint)dat.Data.Length);
            for (int i = 0; i < dat.Data.Length; i++)
            {
                output.Put(dat.Data[i]);
            }
        }
Exemple #29
0
 public void Serialize(IoBuffer output, ISerializationContext context)
 {
     output.PutString(this.User, 112, Encoding.ASCII);
     output.PutString(this.AriesVersion, 80, Encoding.ASCII);
     output.PutString(this.Email, 40, Encoding.ASCII);
     output.PutString(this.Authserv, 84, Encoding.ASCII);
     output.PutUInt16(this.Product);
     output.Put(this.Unknown);
     output.PutString(this.ServiceIdent, 3, Encoding.ASCII);
     output.PutUInt16(this.Unknown2);
     output.PutString(this.Password, 32, Encoding.ASCII);
 }
Exemple #30
0
 /// <inheritdoc/>
 public Int32 Read(IoBuffer buffer)
 {
     using (FileStream fs = _file.OpenRead())
     {
         fs.Position = _position;
         Byte[] bytes = new Byte[buffer.Remaining];
         Int32  read  = fs.Read(bytes, 0, bytes.Length);
         buffer.Put(bytes, 0, read);
         Update(read);
         return(read);
     }
 }