private object SerializeDeserialize(object obj)
        {
            MemoryStream memoryStream = null;

            var serializerResolver = new SerializerResolverBuilder <MessageBody>().Build();
            var serializer         = serializerResolver.GetSerializer(obj.GetType());

            try
            {
                memoryStream = new MemoryStream();
                using (var streamWriter = new StreamWriter(memoryStream))
                    using (var streamReader = new StreamReader(memoryStream))
                    {
                        var serializationContext = new SerializationContext(serializerResolver);
                        serializer.Serialize(streamWriter, serializationContext, obj);
                        var arr = memoryStream.ToArray();
                        Console.WriteLine(BitConverter.ToString(arr));

                        memoryStream.Position = 0;
                        var deserializationContext = new SerializationContext(serializerResolver);
                        var result = serializer.Deserialize(streamReader, deserializationContext);
                        memoryStream = null;
                        return(result);
                    }
            }
            finally
            {
                if (memoryStream != null)
                {
                    memoryStream.Dispose();
                }
            }
        }
Exemplo n.º 2
0
        public void Broadcast(IPCMessage msg)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                ISerializer serializer = _serializerResolver.GetSerializer(msg.GetType());
                if (serializer == null)
                {
                    return;
                }

                SerializationContext serializationContext = new SerializationContext(_serializerResolver);
                StreamWriter         writer = new StreamWriter(stream)
                {
                    Position = 0
                };
                writer.WriteUInt16(PacketPrefix);
                writer.WriteInt16(0);
                writer.WriteByte(_channelId);
                writer.WriteInt32(DynelManager.LocalPlayer.Identity.Instance);
                writer.WriteInt16(msg.Opcode);
                serializer.Serialize(writer, serializationContext, msg);
                long length = writer.Position;
                writer.Position = 2;
                writer.WriteInt16((short)length);
                writer.Dispose();

                byte[] serialized = stream.ToArray();
                _udpClient.Send(serialized, serialized.Length, _remoteEndPoint);
            }
        }
        private object SerializeDeserialize(object obj)
        {
            MemoryStream memoryStream = null;

            var serializerResolver = new DebuggingSerializerResolverBuilder<MessageBody>().Build();
            var serializer = serializerResolver.GetSerializer(obj.GetType());

            try
            {
                memoryStream = new MemoryStream();
                using (var streamWriter = new StreamWriter(memoryStream))
                using (var streamReader = new StreamReader(memoryStream))
                {
                    var serializationContext = new SerializationContext(serializerResolver);
                    serializer.Serialize(streamWriter, serializationContext, obj);
                    var arr = memoryStream.ToArray();
                    Console.WriteLine(BitConverter.ToString(arr));

                    memoryStream.Position = 0;
                    var deserializationContext = new SerializationContext(serializerResolver);
                    var result = serializer.Deserialize(streamReader, deserializationContext);
                    memoryStream = null;
                    return result;
                }
            }
            finally
            {
                if (memoryStream != null)
                {
                    memoryStream.Dispose();
                }
            }
        }