Advertisement
Guest User

Untitled

a guest
Jun 24th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.82 KB | None | 0 0
  1. namespace GameObjectsLib.NetworkCommObjects
  2. {
  3.     using System;
  4.     using System.IO;
  5.     using System.Threading.Tasks;
  6.     using Game;
  7.     using Message;
  8.     using ProtoBuf;
  9.  
  10.     /// <summary>
  11.     ///     Network object wrapper whose purpose is to enable network communication via objects.
  12.     /// </summary>
  13.     [ProtoContract]
  14.     [ProtoInclude(100, typeof(SerializationObjectWrapper<CreateGameRequestMessage>))]
  15.     [ProtoInclude(101, typeof(SerializationObjectWrapper<CreateGameResponseMessage>))]
  16.     [ProtoInclude(102, typeof(SerializationObjectWrapper<JoinGameRequestMessage>))]
  17.     [ProtoInclude(103, typeof(SerializationObjectWrapper<JoinGameResponseMessage>))]
  18.     [ProtoInclude(104, typeof(SerializationObjectWrapper<LoadGameRequestMessage>))]
  19.     [ProtoInclude(105, typeof(SerializationObjectWrapper<LoadGameResponseMessage<bool>>))]
  20.     [ProtoInclude(106, typeof(SerializationObjectWrapper<LoadMyGamesListRequestMessage>))]
  21.     [ProtoInclude(107, typeof(SerializationObjectWrapper<LoadMyGamesListResponseMessage>))]
  22.     [ProtoInclude(108, typeof(SerializationObjectWrapper<UserLogInRequestMessage>))]
  23.     [ProtoInclude(109, typeof(SerializationObjectWrapper<UserLogInResponseMessage>))]
  24.     [ProtoInclude(110, typeof(SerializationObjectWrapper<LoadOpenedGamesListRequestMessage>))]
  25.     [ProtoInclude(111, typeof(SerializationObjectWrapper<LoadOpenedGamesListResponseMessage>))]
  26.     [ProtoInclude(112, typeof(SerializationObjectWrapper<LoadOpenedGameRequestMessage>))]
  27.     [ProtoInclude(113, typeof(SerializationObjectWrapper<LoadOpenedGameResponseMessage>))]
  28.     [ProtoInclude(120, typeof(SerializationObjectWrapper<Game>))]
  29.     public abstract class SerializationObjectWrapper
  30.     {
  31.         public abstract object Value { get; }
  32.  
  33.         /// <summary>
  34.         ///     Asynchronously serializes this instance of the object into <see cref="stream" />.
  35.         /// </summary>
  36.         /// <param name="stream"></param>
  37.         /// <returns></returns>
  38.         public async Task SerializeAsync(Stream stream)
  39.         {
  40.             await Task.Factory.StartNew(() => Serializer.SerializeWithLengthPrefix(stream, this, PrefixStyle.Base128),
  41.                 TaskCreationOptions.DenyChildAttach);
  42.         }
  43.  
  44.         public void Serialize(Stream stream)
  45.         {
  46.             Serializer.SerializeWithLengthPrefix(stream, this, PrefixStyle.Base128);
  47.         }
  48.  
  49.         /// <summary>
  50.         ///     Asynchronously deserializes object from the stream and returns it.
  51.         /// </summary>
  52.         /// <param name="stream"></param>
  53.         /// <returns></returns>
  54.         public static async Task<SerializationObjectWrapper> DeserializeAsync(Stream stream)
  55.         {
  56.             int length = 0;
  57.             if (!await Task.Factory.StartNew(
  58.                 () => Serializer.TryReadLengthPrefix(stream, PrefixStyle.Base128, out length),
  59.                 TaskCreationOptions.DenyChildAttach))
  60.             {
  61.                 throw new ArgumentException();
  62.             }
  63.  
  64.             var buffer = new byte[length];
  65.             await stream.ReadAsync(buffer, 0, buffer.Length);
  66.  
  67.             using (MemoryStream ms = new MemoryStream())
  68.             {
  69.                 await ms.WriteAsync(buffer, 0, buffer.Length);
  70.  
  71.                 ms.Position = 0;
  72.  
  73.                 SerializationObjectWrapper wrapper = Serializer.Deserialize<SerializationObjectWrapper>(ms);
  74.  
  75.                 return wrapper;
  76.             }
  77.         }
  78.  
  79.         public static SerializationObjectWrapper Deserialize(Stream stream)
  80.         {
  81.             int length;
  82.             if (Serializer.TryReadLengthPrefix(stream, PrefixStyle.Base128, out length))
  83.             {
  84.                 var buffer = new byte[length];
  85.                 stream.Read(buffer, 0, buffer.Length);
  86.  
  87.                 using (MemoryStream ms = new MemoryStream())
  88.                 {
  89.                     ms.Write(buffer, 0, buffer.Length);
  90.  
  91.                     ms.Position = 0;
  92.  
  93.                     SerializationObjectWrapper wrapper = Serializer.Deserialize<SerializationObjectWrapper>(ms);
  94.  
  95.                     return wrapper;
  96.                 }
  97.             }
  98.  
  99.             throw new ArgumentException();
  100.         }
  101.  
  102.         public static SerializationObjectWrapper Deserialize(byte[] serializedObject)
  103.         {
  104.             using (var ms = new MemoryStream(serializedObject))
  105.             {
  106.                 ms.Position = 0;
  107.  
  108.                 return Deserialize(ms);
  109.             }
  110.         }
  111.     }
  112.  
  113.     /// <summary>
  114.     ///     Typed NetworkObjectWrapper.
  115.     /// </summary>
  116.     /// <typeparam name="T">Type.</typeparam>
  117.     [ProtoContract]
  118.     public class SerializationObjectWrapper<T> : SerializationObjectWrapper
  119.     {
  120.         public override object Value
  121.         {
  122.             get { return TypedValue; }
  123.         }
  124.  
  125.         [ProtoMember(1)]
  126.         public T TypedValue { get; set; }
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement