Example #1
0
    internal void SendMessage(int id, Google.Protobuf.IMessage message)
    {
        //将发送过来的消息封装为buffer
        MemoryStream ms = new MemoryStream();

        message.WriteTo(ms);
        byte[] messageBytes = ms.ToArray();

        ByteBuffer bb = new ByteBuffer();

        //主体消息的长度
        bb.WriteInt32(messageBytes.Length);
        bb.WriteInt32(id);
        bb.WriteBytes(messageBytes);
        Byte[] sendData = bb.ToArray();

        //数据的发送
        if (client.Connected)
        {
            if (LogIO)
            {
                string strBuff = "";
                foreach (var c in sendData)
                {
                    strBuff += c.ToString("X2") + " ";
                }
                Debug.Log("Send data:" + strBuff);
            }
            this.client.GetStream().BeginWrite(sendData, 0, sendData.Length, new AsyncCallback(OnWrite), sendData);
        }
        else
        {
            Debug.Log("socket client not been connected");
        }
    }
Example #2
0
 private byte[] Serialize(Google.Protobuf.IMessage msg)
 {
     using (MemoryStream sndms = new MemoryStream())
     {
         msg.WriteTo(sndms);
         return(sndms.ToArray());
     }
 }
Example #3
0
 /// <summary>
 /// Convert class ByteString
 /// </summary>
 /// <param name="msg"></param>
 /// <returns></returns>
 public static Google.Protobuf.ByteString Marshal(Google.Protobuf.IMessage msg)
 {
     using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
     {
         var s = new Google.Protobuf.CodedOutputStream(ms);
         msg.WriteTo(s);
         s.Flush();
         var str = Google.Protobuf.ByteString.CopyFrom(ms.ToArray());
         return(str);
     }
 }
Example #4
0
        /// <summary>
        /// Convert class ByteString
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static byte[] MarshalByte(Google.Protobuf.IMessage msg)
        {
            using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
            {
                var s = new Google.Protobuf.CodedOutputStream(ms);
                msg.WriteTo(s);
                s.Flush();

                return(ms.ToArray());
            }
        }
Example #5
0
        static void WriteMessage(object value, CodedOutputStream stream)
        {
            var savedCachedBuffer = cachedBuffer;
            var savedCachedStream = cachedStream;

            cachedBuffer = new MemoryStream();
            cachedStream = new CodedOutputStream(cachedBuffer);
            Google.Protobuf.IMessage message = ((Service.Messages.IMessage)value).ToProtobufMessage();
            cachedBuffer = savedCachedBuffer;
            cachedStream = savedCachedStream;
            message.WriteTo(stream);
        }
Example #6
0
    static public byte[] Serialize(Google.Protobuf.IMessage msg)
    {
        byte[] result = null;

        if (msg != null)
        {
            using (var stream = new MemoryStream())
            {
                msg.WriteTo(stream);
                //Serializer.Serialize<T>(stream, msg);
                result = stream.ToArray();
            }
        }

        return(result);
    }