示例#1
0
        //粘包分包处理
        private void ProcessData(Conn conn)
        {
            if (conn.buffCount < sizeof(Int32))
            {
                return;
            }
            //将前四个字节复制到
            Array.Copy(conn.readBuff, conn.lenBytes, sizeof(Int32));
            conn.msglength = BitConverter.ToInt32(conn.lenBytes, 0);
            if (conn.buffCount < conn.msglength + sizeof(Int32))
            {
                return;
            }
            //处理消息
            //string str = System.Text.Encoding.UTF8.GetString(conn.readBuff, sizeof(Int32), conn.msglength);
            //Send(conn,str);
            ProtocolBase protocol = proto.Decode(conn.readBuff, sizeof(Int32), conn.msglength);

            HandleMsg(conn, protocol);
            //清除已处理的消息
            int count = conn.buffCount - sizeof(Int32) - conn.msglength;

            Array.Copy(conn.readBuff, sizeof(Int32) + conn.msglength, conn.readBuff, 0, count);
            conn.buffCount = count;
            if (conn.buffCount > 0)
            {
                ProcessData(conn);
            }
        }
示例#2
0
        private void ProcessData(Conn conn)
        {
            if (conn.buffCount < sizeof(Int32))
            {
                return;
            }
            //获取消息长度
            Array.Copy(conn.readBuff, conn.lenBytes, sizeof(Int32));
            conn.msgLength = BitConverter.ToInt32(conn.lenBytes, 0);
            //Console.WriteLine("收到长度[" + conn.msgLength + "]");
            if (conn.buffCount < conn.msgLength + sizeof(Int32))
            {
                return;
            }
            ProtocolBase protocol = proto.Decode(conn.readBuff, sizeof(Int32), conn.msgLength);

            HandleMsg(conn, protocol);
            //清除掉已经处理的消息
            int count = conn.buffCount - conn.msgLength - sizeof(Int32);

            Array.Copy(conn.readBuff, sizeof(Int32) + conn.msgLength, conn.readBuff, 0, count);
            conn.buffCount = count;
            //清空缓存区
            if (conn.buffCount > 0)
            {
                ProcessData(conn);
            }
        }
示例#3
0
        void ProcessData(Conn conn)
        {
            if (conn.buffCount < sizeof(Int32))
            {
                return;
            }

            Array.Copy(conn.readBuff, conn.lenBuff, sizeof(Int32));
            conn.msgLength = BitConverter.ToInt16(conn.lenBuff, 0);
            if (conn.buffCount < sizeof(Int32) + conn.msgLength)
            {
                return;
            }

            ProtocolBase protocol = proto.Decode(conn.readBuff, sizeof(Int32), conn.msgLength);

            HandleMsg(conn, protocol);

            int count = conn.buffCount - sizeof(Int32) - conn.msgLength;

            Array.Copy(conn.readBuff, sizeof(Int32) + conn.msgLength, conn.readBuff, 0, count);
            conn.buffCount = count;
            if (conn.buffCount > 0)
            {
                ProcessData(conn);
            }
        }
        public void ProcessData(Conn conn)
        {
            //小于长度字节
            if (conn.buffCount < sizeof(Int32))
            {
                return;
            }
            //消息长度

            Array.Copy(conn.readBuff, conn.lenBytes, sizeof(Int32));
            conn.msgLength = BitConverter.ToInt32(conn.lenBytes, 0);
            if (conn.buffCount < conn.msgLength + sizeof(Int32))
            {
                return;
            }
            //处理消息
            ProtocolBase protocol = proto.Decode(conn.readBuff, sizeof(Int32), conn.msgLength);

            HandleMsg(conn, protocol);

            //清除处理过的消息
            int count = conn.buffCount - conn.msgLength - sizeof(Int32);

            Array.Copy(conn.readBuff, sizeof(Int32) + conn.msgLength, conn.readBuff, 0, count);
            conn.buffCount = count;
            if (conn.buffCount > 0)
            {
                ProcessData(conn);
            }
        }