public void Write <T>(SerializationCtx ctx, UnsafeWriter writer, T value) { #if !NET35 myBackgroundRegistrar.WaitForEmpty(); #endif if (value == null) { // ReSharper disable once ImpureMethodCallOnReadonlyValueField RdId.Nil.Write(writer); return; } RdId typeId; var type = value.GetType(); if (!myTypeMapping.TryGetValue(type, out typeId)) { myPolymorphicCatalog?.TryDiscoverRegister(type, this); if (!myTypeMapping.TryGetValue(type, out typeId)) { throw new KeyNotFoundException($"Type {type.FullName} have not registered"); } } typeId.Write(writer); // Don't dispose this cookie, otherwise it will delete all written data var cookie = new UnsafeWriter.Cookie(writer); writer.Write(0); var writerDelegate = myWriters[typeId]; writerDelegate(ctx, writer, value); cookie.WriteIntLengthToCookieStart(); }
public static RdList <V> Read(SerializationCtx ctx, UnsafeReader reader, CtxReadDelegate <V> readValue, CtxWriteDelegate <V> writeValue) { var nextVersion = reader.ReadLong(); var id = reader.ReadRdId(); return(new RdList <V>(readValue, writeValue, nextVersion).WithId(id)); }
public static RdEndpoint <TReq, TRes> Read(SerializationCtx ctx, UnsafeReader reader, CtxReadDelegate <TReq> readRequest, CtxWriteDelegate <TReq> writeRequest, CtxReadDelegate <TRes> readResponse, CtxWriteDelegate <TRes> writeResponse) { var id = reader.ReadRdId(); return(new RdEndpoint <TReq, TRes>(readRequest, writeRequest, readResponse, writeResponse).WithId(id)); // throw new InvalidOperationException("Deserialization of RdEndpoint is not allowed, the only valid option is to create RdEndpoint with constructor."); }
public void WriteValue(SerializationCtx context, UnsafeWriter writer) { Assertion.Assert(!myHandler.IsSendWithoutContexts, "!myHandler.IsWritingOwnMessages"); var value = Context.Value; if (value == null) { InternId.Write(writer, InternId.Invalid); writer.Write(false); } else { using (myHandler.CreateSendWithoutContextsCookie()) { if (!myProtocolValueSet.Contains(value)) { Assertion.Require(Proto.Scheduler.IsActive, "Attempting to use previously unused context value {0} on a background thread for key {1}", value, Context.Key); myProtocolValueSet.Add(Context.Value); } var internedId = myInternRoot.Intern(value); InternId.Write(writer, internedId); if (!internedId.IsValid) { writer.Write(true); Context.WriteDelegate(context, writer, value); } } } }
public T ReadValue(SerializationCtx context, UnsafeReader reader) { var hasValue = reader.ReadBool(); if (!hasValue) { return(default);
public void WriteValue(SerializationCtx context, UnsafeWriter writer) { Assertion.Assert(!myHandler.IsSendWithoutContexts, "!myHandler.IsWritingOwnMessages"); var value = Context.Value; if (value == null) { InternId.Write(writer, InternId.Invalid); writer.Write(false); } else { using (myHandler.CreateSendWithoutContextsCookie()) { AddValueToProtocolValueSetImpl(value); var internedId = myInternRoot.Intern(value); InternId.Write(writer, internedId); if (!internedId.IsValid) { writer.Write(true); Context.WriteDelegate(context, writer, value); } } } }
public static void WriteEnum <T>(SerializationCtx ctx, UnsafeWriter writer, T value) where T : #if !NET35 unmanaged, #endif Enum { writer.Write(Cast32BitEnum <T> .ToInt(value)); }
public static RdFault Read(SerializationCtx ctx, UnsafeReader reader) { var typeFqn = reader.ReadString(); var message = reader.ReadString(); var body = reader.ReadString(); return(new RdFault(typeFqn, message, body)); }
public static T ReadEnum <T>(SerializationCtx ctx, UnsafeReader reader) where T : #if !NET35 unmanaged, #endif Enum { Assertion.Assert(typeof(T).IsSubclassOf(typeof(Enum)), "{0}", typeof(T)); return(Cast32BitEnum <T> .FromInt(reader.ReadInt())); }
public void WriteValue(SerializationCtx context, UnsafeWriter writer) { var value = Context.Value; writer.Write(value != null); if (value != null) { Context.WriteDelegate(context, writer, value); } }
public static RdProperty <T> Read(SerializationCtx ctx, UnsafeReader reader, CtxReadDelegate <T> readValue, CtxWriteDelegate <T> writeValue) { var id = reader.ReadRdId(); var res = new RdProperty <T>(readValue, writeValue).WithId(id); if (reader.ReadBool()) { res.myProperty.Value = readValue(ctx, reader); } return(res); }
protected override void Init(Lifetime lifetime) { base.Init(lifetime); myBindLifetime = lifetime; //because we advise synchronous scheduler SerializationContext = base.SerializationContext; Wire.Advise(lifetime, this); }
public T ReadValue(SerializationCtx context, UnsafeReader reader) { var id = InternId.Read(reader); if (!id.IsValid) { var hasValue = reader.ReadBool(); if (hasValue) { return(Context.ReadDelegate(context, reader)); } return(default);
public static void Write(SerializationCtx ctx, UnsafeWriter writer, RdProperty <T> value) { Assertion.Assert(!value.RdId.IsNil, "!value.RdId.IsNil"); writer.Write(value.RdId); if (value.HasValue()) { writer.Write(true); value.WriteValueDelegate(ctx, writer, value.Value); } else { writer.Write(false); } }
public static RdTaskResult <T> Read(CtxReadDelegate <T> readDelegate, SerializationCtx ctx, UnsafeReader reader) { var status = (RdTaskStatus)reader.ReadInt(); switch (status) { case RdTaskStatus.Success: return(Success(readDelegate(ctx, reader))); case RdTaskStatus.Canceled: return(Cancelled()); case RdTaskStatus.Faulted: return(Faulted(RdFault.Read(ctx, reader))); default: throw new ArgumentOutOfRangeException($"Unknown status of {nameof(RdTaskStatus)}: {status}"); } }
public T Read <T>(SerializationCtx ctx, UnsafeReader reader, [CanBeNull] CtxReadDelegate <T> unknownInstanceReader = null) { bool TryGetReader(RdId rdId, out CtxReadDelegate <object> readDelegate) { lock (myLock) return(myReaders.TryGetValue(rdId, out readDelegate)); } #if !NET35 myBackgroundRegistrar.WaitForEmpty(); #endif var typeId = RdId.Read(reader); if (typeId.IsNil) { return(default(T)); } var size = reader.ReadInt(); if (!TryGetReader(typeId, out var ctxReadDelegate)) { if (unknownInstanceReader == null) { myRegistrar?.TryRegister(typeId, this); myRegistrar?.TryRegister(typeof(T), this); if (!TryGetReader(typeId, out ctxReadDelegate)) { var realType = myTypeMapping.SingleOrDefault(c => Equals(c.Value, typeId)); //ok because it's rarely needed throw new KeyNotFoundException(string.Format("There is no readers found. Requested type '{0}'. Real type {1}", typeof(T).FullName, realType)); } } else { var objectStart = reader.Position; var result = unknownInstanceReader(ctx, reader); var bytesRead = reader.Position - objectStart; reader.Skip(size - bytesRead); return(result); } } var uncasted = ctxReadDelegate(ctx, reader); Assertion.Assert(uncasted is T, "Bad cast for id {0}. Expected: {1}, actual: {2}", typeId, typeof(T).Name, uncasted.GetType().Name); return((T)uncasted); }
public void TestPolymorphicSimple() { var serializers = new Serializers(); serializers.Register(MyTestObject.Read, MyTestObject.Write); var serializationCtx = new SerializationCtx(serializers); var testObject = new MyTestObject("Monomorphic"); byte[] data; using (var cookie = UnsafeWriter.NewThreadLocalWriter()) { serializers.Write(serializationCtx, cookie.Writer, testObject); data = cookie.CloneData(); } MyTestObject newTestObject = null; UnsafeReader.With(data, reader => newTestObject = serializers.Read <MyTestObject>(serializationCtx, reader, null)); Assert.AreEqual(testObject.Data, newTestObject.Data); }
public static void Write(CtxWriteDelegate <T> writeDelegate, SerializationCtx ctx, UnsafeWriter writer, RdTaskResult <T> value) { writer.Write((int)value.Status); switch (value.Status) { case RdTaskStatus.Success: writeDelegate(ctx, writer, value.Result); break; case RdTaskStatus.Canceled: break; case RdTaskStatus.Faulted: RdFault.Write(ctx, writer, value.Error); break; default: throw new ArgumentOutOfRangeException(); } }
public void Write <T>(SerializationCtx ctx, UnsafeWriter writer, T value) { bool TryGetTypeMapping(Type type1, out RdId rdId) { lock (myLock) return(myTypeMapping.TryGetValue(type1, out rdId)); } #if !NET35 myBackgroundRegistrar.WaitForEmpty(); #endif if (value == null) { // ReSharper disable once ImpureMethodCallOnReadonlyValueField RdId.Nil.Write(writer); return; } var type = value.GetType(); if (!TryGetTypeMapping(type, out var typeId)) { myRegistrar?.TryRegister(type, this); if (!TryGetTypeMapping(type, out typeId)) { throw new KeyNotFoundException($"Type {type.FullName} have not registered"); } } typeId.Write(writer); // Don't dispose this cookie, otherwise it will delete all written data var bookmark = new UnsafeWriter.Bookmark(writer); writer.Write(0); CtxWriteDelegate <object> writerDelegate; lock (myLock) writerDelegate = myWriters[typeId]; writerDelegate(ctx, writer, value); bookmark.WriteIntLength(); }
public IMyInterface Read(SerializationCtx ctx, UnsafeReader reader) { return(new MyImpl2()); }
public static T ReadNullableClass <T>(this UnsafeReader reader, CtxReadDelegate <T> itemReader, SerializationCtx ctx) where T : class { return(reader.ReadNullness() ? itemReader(ctx, reader) : null); }
public static void WriteArray <T>(this UnsafeWriter writer, CtxWriteDelegate <T> itemWriter, SerializationCtx ctx, T[] value) { if (value == null) { writer.Write(-1); return; } writer.Write(value.Length); // ReSharper disable once ForCanBeConvertedToForeach for (var i = 0; i < value.Length; i++) { itemWriter(ctx, writer, value[i]); } }
public static RdProperty <T> Read(SerializationCtx ctx, UnsafeReader reader) { return(Read(ctx, reader, Polymorphic <T> .Read, Polymorphic <T> .Write)); }
public static void WriteEnumerable <T>(this UnsafeWriter writer, CtxWriteDelegate <T> itemWriter, SerializationCtx ctx, IEnumerable <T> value) { if (value == null) { writer.Write(-1); return; } var cookie = new UnsafeWriter.Bookmark(writer); writer.Write(-1); // length int i = 0; foreach (var item in value) { ++i; itemWriter(ctx, writer, item); } cookie.WriteIntLength(i); }
public static T[] ReadArray <T>(this UnsafeReader reader, CtxReadDelegate <T> itemReader, SerializationCtx ctx) { int count = reader.ReadInt32(); if (count < 0) { return(null); } var res = new T[count]; for (var i = 0; i < count; i++) { res[i] = itemReader(ctx, reader); } return(res); }
public static void WriteNullableStruct <T>(this UnsafeWriter writer, CtxWriteDelegate <T> itemWriter, SerializationCtx ctx, T?value) where T : struct { if (writer.WriteNullness(value)) { itemWriter(ctx, writer, value.Value); } }
public static void WriteNullableClass <T>(this UnsafeWriter writer, CtxWriteDelegate <T> itemWriter, SerializationCtx ctx, T value) where T : class { if (writer.WriteNullness(value)) { itemWriter(ctx, writer, value); } }
public static T?ReadNullableStruct <T>(this UnsafeReader reader, CtxReadDelegate <T> itemReader, SerializationCtx ctx) where T : struct { return(reader.ReadNullness() ? itemReader(ctx, reader).ToNullable() : null); }
public static List <T> ReadList <T>(this UnsafeReader reader, CtxReadDelegate <T> itemReader, SerializationCtx ctx) { int count = reader.ReadInt32(); if (count < 0) { return(null); } var res = new List <T>(count); for (var i = 0; i < count; i++) { res.Add(itemReader(ctx, reader)); } return(res); }
public void Write(SerializationCtx ctx, UnsafeWriter writer, IMyInterface value) { }