Пример #1
0
        /// <summary>
        /// 接收到消息
        /// </summary>
        private void OnReceive(byte[] _bytes, int _length)
        {
            memStream.Seek(0, SeekOrigin.End);
            memStream.Write(_bytes, 0, _length);
            //Reset to beginning
            memStream.Seek(0, SeekOrigin.Begin);
            //消息体:先判断是否大于头部
            while (RemainingBytes() > protocol.totalHead)
            {
                //直接抛给业务层处理
                ByteBuffer _buffer = protocol.Get(memStream, reader);
                if (_buffer != null)
                {
                    if (onGetted != null)
                    {
                        onGetted(_buffer);
                    }
                }
                else
                {
                    return;
                }

                ////这里读取了4字节的消息头部
                //int _messageLen = reader.ReadInt32();
                ////回滚4字节消息头,计算总体长度
                //memStream.Position = memStream.Position - 4;
                //// 总长 = 消息偏移量 + 消息体长
                //int _totalLength = protocol.totalHead + _messageLen;
                //if (HasEnough(_totalLength))
                //{
                //BinaryWriter _writer = new BinaryWriter(_ms);
                //_writer.Write(reader.ReadBytes(_totalLength));
                //_ms.Seek(0, SeekOrigin.Begin);
                //OnReceivedMessage(_ms);
                //}
                //else
                //{
                //    return;
                //}
            }
            //Create a new stream with any leftover bytes
            byte[] _leftover = reader.ReadBytes((int)RemainingBytes());
            memStream.SetLength(0);
            //Clear
            memStream.Write(_leftover, 0, _leftover.Length);
        }
Пример #2
0
 public override bool Get <T>(DataKey <T> datakey)
 {
     return(m_protocol.Get(GetOperation.Get, datakey));
 }