Exemplo n.º 1
0
 public void SendHeader(Header header, int requestId)
 {
     lock (_sendBufferLock)
     {
         _sendBuffer.Offset = _sendBuffer.Length;
         _sendBuffer.Write((int)header);
         _sendBuffer.Write(requestId);
         _sendBuffer.Write(0); // packet size
         _sendBufferEvent.Set();
     }
 }
Exemplo n.º 2
0
        protected virtual void DecodeProtocolData(byte[] buffer, int offset, int length)
        {
            _buffer.Offset = _buffer.Length;
            _buffer.Write(buffer, offset, length);

            while (_buffer.Length >= Packet.HeaderSize)
            {
                _buffer.Offset = 0;
                var header     = _buffer.ReadInt32();
                var requestId  = _buffer.ReadInt32();
                var packetSize = _buffer.ReadInt32();

                if (_buffer.Length - _buffer.Offset < packetSize)
                {
                    return; // need more data
                }
                if (header <= 0 ||
                    header >= (int)Header.MaxHeader)
                {
                    throw new ProtocolException($"The header is invalid: 0x{header.ToString("x8")}");
                }

                if (PreProcessPacket((Header)header, requestId, packetSize, _buffer))
                {
                    ProcessPacket((Header)header, requestId);
                }

                _buffer.Remove(Packet.HeaderSize + packetSize);
            }
        }
Exemplo n.º 3
0
        public static DataBuffer FromString(string content, Encoding encoding)
        {
            var bytes  = encoding.GetBytes(content);
            var buffer = new DataBuffer();

            buffer.Write(bytes, 0, bytes.Length);
            return(buffer);
        }
Exemplo n.º 4
0
 public void WriteTo(DataBuffer buffer)
 {
     buffer.Write(_buffer.InternalBuffer, 0, _buffer.Length);
 }
Exemplo n.º 5
0
        public Packet(Header header, int requestId)
        {
            _header = header;

            _buffer.Write((int)header);
            _buffer.Write(requestId);
            _buffer.Write(0); // packet size
        }