private byte[] MainWrite(object obj) { MemoryStream ms = new MemoryStream(); PrimitiveValueWrapper mainStage = new PrimitiveValueWrapper(ms); Serializers[obj.GetType()].Serialize(mainStage, obj); mainStage.CompleteWrite(); mainStage.SetInvalid(); //Read Payload ms.Position = 0; byte[] buf = new byte[ms.Length]; ms.Read(buf, 0, buf.Length); ms.Close(); return(buf); }
private bool TryBaseRead(Stream stream, out BasePacket packet) { PrimitiveValueWrapper baseStage = new PrimitiveValueWrapper(stream); try { packet = BaseSerializer.DeserializePacket(baseStage); baseStage.SetInvalid(); return(true); } catch (Exception) { packet = null; return(false); } }
private object MainRead(BasePacket basePacket) { MemoryStream ms = new MemoryStream(basePacket.Payload) { Position = 0 }; Type packetType = GetTypeByKey(basePacket.PacketType); PrimitiveValueWrapper mainStage = new PrimitiveValueWrapper(ms); object ret = Serializers[packetType].Deserialize(mainStage); mainStage.SetInvalid(); ms.Close(); return(ret); }
public static byte[] WriteArray <T>(T[] input) { int len = input.Length; ASerializer s = SerializableTypes[typeof(T)]; MemoryStream ms = new MemoryStream(); PrimitiveValueWrapper pvw = new PrimitiveValueWrapper(ms); pvw.Write(len); for (int i = 0; i < input.Length; i++) { s.Serialize(pvw, input[i]); } pvw.CompleteWrite(); byte[] ret = ms.ToArray(); ms.Close(); return(ret); }
private static bool TryBaseWrite(Stream stream, BasePacket packet) { PrimitiveValueWrapper baseStage = new PrimitiveValueWrapper(stream); bool ret = true; try { BaseSerializer.Serialize(baseStage, packet); baseStage.CompleteWrite(); } catch (Exception) { ret = false; } baseStage.SetInvalid(); return(ret); }
public static T[] ReadArray <T>(byte[] input) { int len = ReadInt(input); ASerializer s = SerializableTypes[typeof(T)]; T[] ret = new T[len]; MemoryStream ms = new MemoryStream(input); ms.Position = sizeof(int); //Move the Position to the begin of the array PrimitiveValueWrapper pvw = new PrimitiveValueWrapper(ms); for (int i = 0; i < ret.Length; i++) { ret[i] = (T)s.Deserialize(pvw); } return(ret); }