コード例 #1
0
 /// <summary>
 /// 往消息队列中发数据
 /// </summary>
 /// <param name="message"></param>
 public void SendMsg(string message)
 {
     if (IsConnected())
     {
         byte[] msg = ifCompress ? CHMatchReceiveMsg.EncodeMessage(message) : Encoding.UTF8.GetBytes(message);
         _socket.BeginSend(msg, 0, msg.Length, SocketFlags.None, SendSuccess, null);
         //int msgLent = msg.Length;
         //_sendBuffer.Write(msgLent);
         //_sendBuffer.Write(msg);
     }
 }
コード例 #2
0
    public static CHMatchReceiveMsg Decode(byte[] buffer, int offset, int count, bool ifcompress)
    {
        if (count < 8) // header + tail
        {
            return(null);
        }

        CHMatchReceiveMsg _msg = new CHMatchReceiveMsg();
        // 开始标签
        int    size      = 2;
        ushort signature = LittleEndianBytesToUshort(buffer, offset);

        //Debug.Log("signature: " + signature);
        if (signature != CHMatchReceiveMsg.SIGNATURE_START)
        {
            return(null);
        }
        offset += size;

        //消息长度
        size        = 2;
        _msg.length = LittleEndianBytesToUshort(buffer, offset);
        //Debug.Log("_msg.length: " + _msg.length);
        if (count < _msg.length)
        {
            return(null);
        }
        offset += size;

        //消息键值
        size     = 2;
        _msg.key = LittleEndianBytesToUshort(buffer, offset);
        //Debug.Log("_msg.key: " + _msg.key);
        offset += size;

        //消息内容
        size     = _msg.length - 8;
        _msg.msg = LittleEndianBytesToString(buffer, offset, size, ifcompress);
        //Debug.Log("_msg.msg: " + _msg.msg);
        offset += size;

        //结束标签
        size      = 2;
        signature = LittleEndianBytesToUshort(buffer, offset);
        //Debug.Log("signature end: " + signature);
        if (signature != CHMatchReceiveMsg.SIGNATURE_END)
        {
            return(null);
        }

        return(_msg);
    }