using StuMgmLib.MyNameSpace;
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace StuMgmClient
{
    class Utility
    {
        internal static bool BinSerialize<T>(T data, out byte[] buff)
        {
            try
            {
                MemoryStream ms = new MemoryStream();
                BinaryFormatter iFormatter = new BinaryFormatter();
                iFormatter.Serialize(ms, data);
                buff = ms.GetBuffer();
                return true;
            }
            catch (Exception e)
            {
                Debug.Print(e.Message);
                buff = null;
                return false;
            }
        }

        internal static bool BinDeserialize(byte[] data, out ServerResponse o)
        {
            try
            {
                MemoryStream ms = new MemoryStream(data);
                BinaryFormatter iFormatter = new BinaryFormatter();
                o = (ServerResponse)iFormatter.Deserialize(ms);
                return true;
            }
            catch (Exception e)
            {
                Debug.Print(e.Message);
                o = null;
                return false;
            }
        }
    }
}
