public static T BytesToObject <T>(byte[] bytesData, int offset, int length) { T result; if (bytesData.Length == 0) { result = default(T); } else { try { byte[] copyData = new byte[length]; DataHelper.CopyBytes(copyData, 0, bytesData, offset, length); copyData = DataHelper.Uncompress(copyData); TMSKThreadStaticClass tsc = TMSKThreadStaticClass.GetInstance(); MemoryStream ms = tsc.PopMemoryStream(); ms.Write(copyData, 0, copyData.Length); ms.Position = 0L; T t = Serializer.Deserialize <T>(ms); tsc.PushMemoryStream(ms); return(t); } catch (Exception ex) { DataHelper.WriteExceptionLogEx(ex, "将字节数据转为对象发生异常:"); } result = default(T); } return(result); }
/// <summary> /// 将对象转为字节流 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="instance"></param> /// <param name="pool"></param> /// <param name="cmdID"></param> /// <returns></returns> public static byte[] ObjectToBytes <T>(T instance) { try { byte[] bytesCmd = null; if (null == instance) { bytesCmd = new byte[0]; } else if (instance is IProtoBuffData) { return((instance as IProtoBuffData).toBytes()); } else { if (GameManager.FlagOptimizeThreadPool) { TMSKThreadStaticClass tsc = TMSKThreadStaticClass.GetInstance(); MemoryStream ms = tsc.PopMemoryStream(); Serializer.Serialize <T>(ms, instance); bytesCmd = new byte[ms.Length]; ms.Position = 0; ms.Read(bytesCmd, 0, bytesCmd.Length); tsc.PushMemoryStream(ms); } else { MemoryStream ms = new MemoryStream(); Serializer.Serialize <T>(ms, instance); bytesCmd = new byte[ms.Length]; ms.Position = 0; ms.Read(bytesCmd, 0, bytesCmd.Length); ms.Dispose(); ms = null; } } if (bytesCmd.Length > DataHelper.MinZipBytesSize) //大于256字节的才压缩, 节省cpu占用,想一想,每秒10兆小流量的吐出,都在压缩,cpu占用当然会高, 带宽其实不是问题, 不会达到上限(100兆共享) { //zlib压缩算法 byte[] newBytes = DataHelper.Compress(bytesCmd); if (null != newBytes) { if (newBytes.Length < bytesCmd.Length) { //System.Diagnostics.Debug.WriteLine(string.Format("{0}压缩率: {1}", instance.GetType(), ((double)newBytes.Length / bytesCmd.Length) * 100.0)); bytesCmd = newBytes; } } } return(bytesCmd); } catch (Exception ex) { WriteExceptionLogEx(ex, "将对象转为字节流发生异常:"); } return(new byte[0]); }
//public static T BytesToProtocol<T>(byte[] bytesData, int offset, int length) where T : class, IProtoBuffData, new() //{ // T szProtocol = new T(); // try // { // MemoryStream msRecvDatas; // msRecvDatas = new MemoryStream(bytesData, offset, length); // szProtocol = Serializer.Deserialize<T>(msRecvDatas); // } // catch (Exception ex) // { // SysConOut.WriteLine("解析切换地图消息错误"); // LogManager.WriteLog(LogTypes.Data, string.Format("解析客户端发上来的数据{0}异常,IP:{1},数据内容:{2}", t.ToString(), socket.RemoteEndPoint.ToString(), Convert.ToBase64String(bytesData, offset, length))); // } // return default(T); //} /// <summary> /// 将字节数据转为对象 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="bytesData"></param> /// <returns></returns> public static T BytesToObject <T>(byte[] bytesData, int offset, int length) { if (bytesData.Length == 0) { return(default(T)); } try { //zlib解压缩算法 byte[] copyData = new byte[length]; DataHelper.CopyBytes(copyData, 0, bytesData, offset, length); copyData = DataHelper.Uncompress(copyData); if (GameManager.FlagOptimizeThreadPool) { TMSKThreadStaticClass tsc = TMSKThreadStaticClass.GetInstance(); MemoryStream ms = tsc.PopMemoryStream(); ms.Write(copyData, 0, copyData.Length); ms.Position = 0; T t = Serializer.Deserialize <T>(ms); tsc.PushMemoryStream(ms); return(t); } else { MemoryStream ms = new MemoryStream(); ms.Write(copyData, 0, copyData.Length); ms.Position = 0; T t = Serializer.Deserialize <T>(ms); ms.Dispose(); ms = null; return(t); } } catch (Exception ex) { WriteExceptionLogEx(ex, "将字节数据转为对象发生异常:"); } return(default(T)); }
public static byte[] ObjectToBytes <T>(T instance) { try { byte[] bytesCmd; if (null == instance) { bytesCmd = new byte[0]; } else { if (instance is IProtoBuffData) { return((instance as IProtoBuffData).toBytes()); } TMSKThreadStaticClass tsc = TMSKThreadStaticClass.GetInstance(); MemoryStream ms = tsc.PopMemoryStream(); Serializer.Serialize <T>(ms, instance); bytesCmd = new byte[ms.Length]; ms.Position = 0L; ms.Read(bytesCmd, 0, bytesCmd.Length); tsc.PushMemoryStream(ms); } if (bytesCmd.Length > DataHelper.MinZipBytesSize && instance is ICompressed) { byte[] newBytes = DataHelper.Compress(bytesCmd); if (null != newBytes) { if (newBytes.Length < bytesCmd.Length) { bytesCmd = newBytes; } } } return(bytesCmd); } catch (Exception ex) { DataHelper.WriteExceptionLogEx(ex, "将对象转为字节流发生异常:"); } return(new byte[0]); }