コード例 #1
0
        public void DealMsg(ReceiveMsgData _data)
        {
            if (_data == null)
            {
                return;
            }

            byte[]     _dataBytes   = _data.receiveBytes;
            IPEndPoint receivePoint = _data.receivePoint;
            ByteBuffer _buff        = new ByteBuffer(_dataBytes);
            int        length       = _buff.ReadInt32(); //收到的消息结构实际长度

            if (length > 0)
            {
                if (length <= _data.receiveLength)
                {
                    byte[]      _bytes = _buff.ReadBytes(length);
                    MessageData data   = ProtoBufTools.DeSerialize <MessageData>(_bytes);
                    data.receivePoint = receivePoint;
                    DealMsgSwitch(data);
                }

                //除本次包内容外还有其它数据,有多余的字节,黏包,再处理一次
                int dataLength = _data.receiveLength - 4;
                if (dataLength > length)
                {
                    int leftLength = dataLength - length;
                    _data.receiveBytes  = _buff.ReadBytes(leftLength);
                    _data.receiveLength = leftLength;
                    DealMsg(_data);
                }
            }
        }
コード例 #2
0
        public void SendMsg(int cmd, int scmd, ByteBuffer buffer, List <Client> _users = null)
        {
            MessageData sendMsg = new MessageData();
            HeadMsg     head    = new HeadMsg();

            head.cmd     = cmd;
            head.scmd    = scmd;
            sendMsg.head = head;
            sendMsg.data = buffer.ToBytes();
            byte[]     bytes      = ProtoBufTools.Serialize(sendMsg);
            ByteBuffer sendBuffer = new ByteBuffer();

            sendBuffer.WriteInt32(bytes.Length);
            sendBuffer.WriteBytes(bytes);
            bytes = sendBuffer.ToBytes();
            if (_users == null)
            {
                _users = GlobalData.GetUsersAllClient();
            }
            foreach (var socket in _users)
            {
                if (socket.Connected)
                {
                    socket.socket.Send(bytes);
                }
            }
        }
コード例 #3
0
        public void DealMsg(ReceiveMessageData _data)
        {
            if (_data == null)
            {
                return;
            }
            byte[]     _dataBytes = _data.ReceiveBytes;
            ByteBuffer _buff      = new ByteBuffer(_dataBytes);
            int        length     = _buff.ReadInt32(); //收到的消息结构长度

            byte[] _bytes = _buff.ReadBytes(length);

            int dataLength = _data.ReceiveLength - 4; //去掉包长度的字节,剩下的为内容长度

            if (dataLength > length)                  //有多余的字节,黏包
            {
                int leftLength = dataLength - length;
                _data.ReceiveBytes  = _buff.ReadBytes(leftLength);
                _data.ReceiveLength = leftLength;
                DealMsg(_data);
            }
            else if (dataLength == length)
            {
                MessageData data = ProtoBufTools.DeSerialize <MessageData>(_bytes);
                data.Head.ReceivePoint = _data.ReceivePoint;
                if (DealMsgEvent != null)
                {
                    DealMsgEvent(data);
                }
            }
        }