public static object DeserializeFrom(ushort opcode, Type type, MemoryStream memoryStream) { if (opcode < PbMaxOpcode) { return(ProtobufHelper.FromStream(type, memoryStream)); } if (opcode >= JsonMinOpcode) { return(JsonHelper.FromJson(type, memoryStream.GetBuffer().ToStr((int)memoryStream.Position, (int)(memoryStream.Length - memoryStream.Position)))); } #if !DEBUG_CLIENT return(MongoHelper.FromStream(type, memoryStream)); #else throw new Exception($"client no message: {opcode}"); #endif }
public static void SerializeTo(ushort opcode, object obj, MemoryStream memoryStream) { if (opcode < PbMaxOpcode) { ProtobufHelper.ToStream(obj, memoryStream); return; } if (opcode >= JsonMinOpcode) { string s = JsonHelper.ToJson(obj); byte[] bytes = s.ToUtf8(); memoryStream.Write(bytes, 0, bytes.Length); return; } #if !DEBUG_CLIENT MongoHelper.ToStream(obj, memoryStream); #else throw new Exception($"client no message: {opcode}"); #endif }