using System; using System.Windows.Forms; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.Serialization.Formatters.Binary; using System.IO; namespace TCP_Handler { public class SerialMediator { /// <summary> /// Serializes a packet into a byte array /// </summary> /// <param name="pkt"></param> /// <returns></returns> public static Byte[] Serialize(Packet pkt) { BinaryFormatter bf = new BinaryFormatter(); MemoryStream ms = new MemoryStream(); bf.Serialize(ms, (Object)pkt); return ms.ToArray(); } /// <summary> /// Deserializes a packet from a byte array /// </summary> /// <param name="arr"></param> /// <returns></returns> public static Packet Deserialize(Byte[] arr) { BinaryFormatter bf = new BinaryFormatter(); MemoryStream ms = new MemoryStream(); ms.Write(arr, 0, arr.Length); ms.Seek(0, SeekOrigin.Begin); try { return (Packet)bf.Deserialize(ms); } catch (Exception ex) { MessageBox.Show(ex.Message); throw new Exception("Byte array did not represent a packet"); } } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)