コード例 #1
0
        /// <summary>
        /// 序列化T类型消息
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="message"></param>
        /// <returns></returns>
        static public byte[] Serialize <T>(T message) where T : class
        {
            try
            {
                int    typeid   = typeof(T).AssemblyQualifiedName.GetHashCode();
                string TypeName = typeof(T).FullName + "," + "MessagesDefine";
                using (MemoryStream ms = new MemoryStream())
                {
                    //消息总长度
                    ms.WriteByte(0);
                    ms.WriteByte(0);
                    //消息类型id
                    ms.WriteByte((byte)(typeid & 0xff));
                    ms.WriteByte((byte)((typeid >> 8) & 0xff));
                    ms.WriteByte((byte)((typeid >> 16) & 0xff));
                    ms.WriteByte((byte)((typeid >> 24) & 0xff));
                    //序列化消息
                    MessageTypeModel.Serialize(ms, message);
                    byte[] msg         = ms.ToArray();
                    int    packet_size = msg.Length;

                    msg[0] = (byte)(packet_size & 0xff);
                    msg[1] = (byte)((packet_size >> 8) & 0xff);
                    return(msg);
                }
            }
            catch (Exception ex)
            {
                Log.Error($"ProtobufSerializer::Serialize() {ex.Message}");
            }
            return(null);
        }
コード例 #2
0
ファイル: Server.cs プロジェクト: Gtach/ruh
        public static void Send(IDomainMapper domainMapper, Socket publisher, RuntimeTypeModel typeModel, MemoryStream stream, DomainBase instance)
        {
            var status = publisher.SendMore(domainMapper.Type2Bytes(instance.GetType()));
            if (status != SendStatus.Sent) throw new InvalidOperationException("Key not sent!");

            stream.SetLength(0);
            typeModel.Serialize(stream, instance);
            status = publisher.Send(stream.ToArray());
            if (status != SendStatus.Sent) throw new InvalidOperationException("Instance not sent!");
        }
コード例 #3
0
 public static byte[] Serialize <TData>(TData data)
 {
     using (var stream = new MemoryStream())
     {
         model.Serialize(stream, data);
         stream.Flush();
         stream.Position = 0;
         return(stream.ToArray());
     }
 }
コード例 #4
0
        protected void WriteMessage(RuntimeTypeModel typeModel, object message)
        {
            Require.NotNull(typeModel, "typeModel");
            Require.NotNull(message, "message");

            typeModel.Serialize(_sendStream, message);
        }