/// <summary> /// /// </summary> /// <param name="reader"></param> protected virtual void SerializePayload(NetMessageSerializer reader) { // Implement in derived class. }
/// <summary> /// /// </summary> /// <param name="Buffer"></param> /// <param name="WasMemoryAvailable"></param> /// <returns></returns> public static NetMessage FromByteArray(byte[] Buffer, out bool WasMemoryAvailable, int Version) { WasMemoryAvailable = true; if (MessageTypes.Count == 0) { LoadMessageTypes(); } NetMessage Msg = new NetMessage(); Msg.ReadHeader(Buffer); if (!MessageTypes.ContainsKey(Msg.Id)) { return(null); } //Stopwatch stop1 = new Stopwatch(); //stop1.Start(); Msg = Activator.CreateInstance(MessageTypes[Msg.Id]) as NetMessage; if (Msg == null) { return(null); } //stop1.Stop(); //Console.WriteLine(" - new instance {0}", ((float)stop1.ElapsedTicks / (Stopwatch.Frequency / 1000.0))); //stop1.Restart(); using (MemoryStream dataStream = new MemoryStream(Buffer, HeaderSize, Buffer.Length - HeaderSize, false)) { using (BinaryReader dataReader = new BinaryReader(dataStream)) { //stop1.Stop(); //Console.WriteLine(" - new readers {0}", ((float)stop1.ElapsedTicks / (Stopwatch.Frequency / 1000.0))); try { //Stopwatch stop = new Stopwatch(); //stop.Start(); NetMessageSerializer Serializer = new NetMessageSerializer(dataReader, Version); Msg.SerializePayload(Serializer); if (Serializer.FailedOutOfMemory) { WasMemoryAvailable = false; return(null); } //stop.Stop(); //Console.WriteLine(" - deserialize {0}", ((float)stop.ElapsedTicks / (Stopwatch.Frequency / 1000.0))); } catch (Exception ex) { Logger.Log(LogLevel.Error, LogCategory.Transport, "Failed to decode message, with error {0}", ex.Message); } return(Msg); } } }