From 3c9c98bc2b845433d3a5edf95450c8789dff637e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20K=C3=B6llner?= Date: Sat, 11 Jan 2020 17:21:31 +0100 Subject: [PATCH] nullable first step --- Capnp.Net.Runtime/AnyPointer.cs | 4 +- Capnp.Net.Runtime/Capnp.Net.Runtime.csproj | 4 +- Capnp.Net.Runtime/CapnpSerializable.cs | 44 +++++----- Capnp.Net.Runtime/DeserializationException.cs | 2 + Capnp.Net.Runtime/DeserializerState.cs | 29 ++++-- Capnp.Net.Runtime/DynamicSerializerState.cs | 2 + Capnp.Net.Runtime/EmptyList.cs | 2 + Capnp.Net.Runtime/EmptyListDeserializer.cs | 2 + Capnp.Net.Runtime/FramePump.cs | 6 +- .../FrameTracing/IFrameTracer.cs | 2 + .../FrameTracing/RpcFrameTracer.cs | 2 + Capnp.Net.Runtime/Framing.cs | 2 + Capnp.Net.Runtime/ICapnpSerializable.cs | 4 +- Capnp.Net.Runtime/ISegmentAllocator.cs | 2 + Capnp.Net.Runtime/IStructDeserializer.cs | 2 + Capnp.Net.Runtime/IStructSerializer.cs | 2 + Capnp.Net.Runtime/ListDeserializer.cs | 4 +- Capnp.Net.Runtime/ListKind.cs | 4 +- Capnp.Net.Runtime/ListOfBitsDeserializer.cs | 2 + Capnp.Net.Runtime/ListOfBitsSerializer.cs | 2 + Capnp.Net.Runtime/ListOfCapsDeserializer.cs | 4 +- Capnp.Net.Runtime/ListOfCapsSerializer.cs | 5 +- Capnp.Net.Runtime/ListOfEmptyDeserializer.cs | 2 + Capnp.Net.Runtime/ListOfEmptySerializer.cs | 2 + .../ListOfPointersDeserializer.cs | 2 + Capnp.Net.Runtime/ListOfPointersSerializer.cs | 9 +- .../ListOfPrimitivesDeserializer.cs | 2 + .../ListOfPrimitivesSerializer.cs | 2 + .../ListOfStructsDeserializer.cs | 2 + Capnp.Net.Runtime/ListOfStructsSerializer.cs | 2 + Capnp.Net.Runtime/ListOfTextSerializer.cs | 10 ++- Capnp.Net.Runtime/Logging.cs | 2 + Capnp.Net.Runtime/MessageBuilder.cs | 13 +-- Capnp.Net.Runtime/ObjectKind.cs | 2 + Capnp.Net.Runtime/PrimitiveCoder.cs | 4 +- Capnp.Net.Runtime/ReadOnlyListExtensions.cs | 2 + Capnp.Net.Runtime/Reserializing.cs | 2 + .../Rpc/AnswerOrCounterquestion.cs | 8 +- Capnp.Net.Runtime/Rpc/BareProxy.cs | 6 +- Capnp.Net.Runtime/Rpc/CapabilityReflection.cs | 8 +- Capnp.Net.Runtime/Rpc/ConnectionState.cs | 4 +- Capnp.Net.Runtime/Rpc/ConsumedCapability.cs | 4 +- Capnp.Net.Runtime/Rpc/IConnection.cs | 2 + Capnp.Net.Runtime/Rpc/IEndpoint.cs | 4 +- Capnp.Net.Runtime/Rpc/IMonoSkeleton.cs | 4 +- Capnp.Net.Runtime/Rpc/IPromisedAnswer.cs | 4 +- Capnp.Net.Runtime/Rpc/IProvidedCapability.cs | 2 + Capnp.Net.Runtime/Rpc/IResolvingCapability.cs | 2 + Capnp.Net.Runtime/Rpc/IRpcEndpoint.cs | 2 + Capnp.Net.Runtime/Rpc/Impatient.cs | 20 +++-- Capnp.Net.Runtime/Rpc/ImportedCapability.cs | 2 + .../Rpc/Interception/CallContext.cs | 60 +++++++++---- .../Rpc/Interception/CensorCapability.cs | 6 +- .../Rpc/Interception/IInterceptionPolicy.cs | 2 + .../Rpc/Interception/InterceptionState.cs | 4 +- .../Rpc/Interception/Interceptor.cs | 20 +++-- .../InvalidCapabilityInterfaceException.cs | 4 +- Capnp.Net.Runtime/Rpc/LazyCapability.cs | 6 +- Capnp.Net.Runtime/Rpc/LocalAnswer.cs | 2 + .../Rpc/LocalAnswerCapability.cs | 6 +- Capnp.Net.Runtime/Rpc/LocalCapability.cs | 8 +- Capnp.Net.Runtime/Rpc/MemberAccessPath.cs | 6 +- Capnp.Net.Runtime/Rpc/PendingAnswer.cs | 16 ++-- Capnp.Net.Runtime/Rpc/PendingQuestion.cs | 35 ++++---- Capnp.Net.Runtime/Rpc/PolySkeleton.cs | 2 + Capnp.Net.Runtime/Rpc/PromisedCapability.cs | 10 ++- Capnp.Net.Runtime/Rpc/Proxy.cs | 14 +-- Capnp.Net.Runtime/Rpc/ProxyAttribute.cs | 2 + .../Rpc/RefCountingCapability.cs | 2 + .../Rpc/RemoteAnswerCapability.cs | 18 ++-- Capnp.Net.Runtime/Rpc/RemoteCapability.cs | 5 ++ .../Rpc/RemoteResolvingCapability.cs | 9 +- .../Rpc/ResolvingCapabilityExtensions.cs | 6 +- Capnp.Net.Runtime/Rpc/RpcEngine.cs | 61 ++++++------- Capnp.Net.Runtime/Rpc/RpcException.cs | 4 +- .../Rpc/RpcUnimplementedException.cs | 4 +- Capnp.Net.Runtime/Rpc/Skeleton.cs | 8 +- Capnp.Net.Runtime/Rpc/SkeletonAttribute.cs | 2 + Capnp.Net.Runtime/Rpc/TcpRpcClient.cs | 40 +++++---- Capnp.Net.Runtime/Rpc/TcpRpcServer.cs | 17 ++-- Capnp.Net.Runtime/Rpc/Vine.cs | 2 + Capnp.Net.Runtime/SecurityOptions.cs | 4 +- Capnp.Net.Runtime/SegmentAllocator.cs | 2 + Capnp.Net.Runtime/SegmentSlice.cs | 2 + Capnp.Net.Runtime/SerializerExtensions.cs | 2 + Capnp.Net.Runtime/SerializerState.cs | 88 +++++++++++-------- Capnp.Net.Runtime/TypeIdAttribute.cs | 2 + Capnp.Net.Runtime/UtilityExtensions.cs | 14 +-- Capnp.Net.Runtime/WireFrame.cs | 2 + Capnp.Net.Runtime/WirePointer.cs | 2 + 90 files changed, 501 insertions(+), 260 deletions(-) diff --git a/Capnp.Net.Runtime/AnyPointer.cs b/Capnp.Net.Runtime/AnyPointer.cs index 02a5efc..80138b4 100644 --- a/Capnp.Net.Runtime/AnyPointer.cs +++ b/Capnp.Net.Runtime/AnyPointer.cs @@ -1,4 +1,5 @@ -namespace Capnp +#nullable enable +namespace Capnp { /// /// Generic implementation, based on a wrapper around . @@ -29,3 +30,4 @@ } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/Capnp.Net.Runtime.csproj b/Capnp.Net.Runtime/Capnp.Net.Runtime.csproj index 2d6ac0e..82ca384 100644 --- a/Capnp.Net.Runtime/Capnp.Net.Runtime.csproj +++ b/Capnp.Net.Runtime/Capnp.Net.Runtime.csproj @@ -1,9 +1,9 @@  - netstandard2.0;netcoreapp2.1 + netstandard2.0;netcoreapp2.1;netcoreapp3.0 Capnp - 7.2 + 8.0 Capnp.Net.Runtime Capnp.Net.Runtime true diff --git a/Capnp.Net.Runtime/CapnpSerializable.cs b/Capnp.Net.Runtime/CapnpSerializable.cs index 054d5b5..1afa8d6 100644 --- a/Capnp.Net.Runtime/CapnpSerializable.cs +++ b/Capnp.Net.Runtime/CapnpSerializable.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Reflection; using System.Runtime.CompilerServices; +#nullable enable namespace Capnp { /// @@ -10,15 +11,15 @@ namespace Capnp /// public static class CapnpSerializable { - interface IConstructibleFromDeserializerState + interface IConstructibleFromDeserializerState { - T Create(DeserializerState state); + object? Create(DeserializerState state); } - class FromStruct: IConstructibleFromDeserializerState + class FromStruct: IConstructibleFromDeserializerState where T : ICapnpSerializable, new() { - public T Create(DeserializerState state) + public object Create(DeserializerState state) { var result = new T(); if (state.Kind != ObjectKind.Nil) @@ -29,7 +30,7 @@ namespace Capnp } } - class FromList: IConstructibleFromDeserializerState> + class FromList: IConstructibleFromDeserializerState where T: class { readonly Func _elementSerializer; @@ -39,23 +40,23 @@ namespace Capnp _elementSerializer = (Func)GetSerializer(typeof(T)); } - public IReadOnlyList Create(DeserializerState state) + public object Create(DeserializerState state) { return state.RequireList().Cast(_elementSerializer); } } - class FromCapability: IConstructibleFromDeserializerState + class FromCapability: IConstructibleFromDeserializerState where T: class { - public T Create(DeserializerState state) + public object? Create(DeserializerState state) { return state.RequireCap(); } } - static readonly ConditionalWeakTable> _typeMap = - new ConditionalWeakTable>(); + static readonly ConditionalWeakTable> _typeMap = + new ConditionalWeakTable>(); static CapnpSerializable() { @@ -73,14 +74,14 @@ namespace Capnp _typeMap.Add(typeof(IReadOnlyList), d => d.RequireList().CastDouble()); } - static Func CreateSerializer(Type type) + static Func CreateSerializer(Type type) { if (typeof(ICapnpSerializable).IsAssignableFrom(type)) { try { - return ((IConstructibleFromDeserializerState) - Activator.CreateInstance(typeof(FromStruct<>).MakeGenericType(type))).Create; + return ((IConstructibleFromDeserializerState) + Activator.CreateInstance(typeof(FromStruct<>).MakeGenericType(type))!).Create; } catch (Exception ex) { @@ -94,12 +95,12 @@ namespace Capnp try { var elementType = type.GetGenericArguments()[0]; - return ((IConstructibleFromDeserializerState) - Activator.CreateInstance(typeof(FromList<>).MakeGenericType(elementType))).Create; + return ((IConstructibleFromDeserializerState) + Activator.CreateInstance(typeof(FromList<>).MakeGenericType(elementType))!).Create; } catch (TargetInvocationException ex) { - throw ex.InnerException; + throw ex.InnerException!; } catch (Exception ex) { @@ -123,8 +124,8 @@ namespace Capnp try { - return ((IConstructibleFromDeserializerState) - Activator.CreateInstance(typeof(FromCapability<>).MakeGenericType(type))).Create; + return ((IConstructibleFromDeserializerState) + Activator.CreateInstance(typeof(FromCapability<>).MakeGenericType(type))!).Create; } catch (Exception ex) { @@ -135,7 +136,7 @@ namespace Capnp } } - static Func GetSerializer(Type type) + static Func GetSerializer(Type type) { return _typeMap.GetValue(type, CreateSerializer); } @@ -164,10 +165,11 @@ namespace Capnp /// /// /// - public static T Create(DeserializerState state) + public static T? Create(DeserializerState state) where T: class { - return (T)GetSerializer(typeof(T))(state); + return (T?)GetSerializer(typeof(T))(state); } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/DeserializationException.cs b/Capnp.Net.Runtime/DeserializationException.cs index a9dfcaa..e2d8c31 100644 --- a/Capnp.Net.Runtime/DeserializationException.cs +++ b/Capnp.Net.Runtime/DeserializationException.cs @@ -1,5 +1,6 @@ using System; +#nullable enable namespace Capnp { /// @@ -23,3 +24,4 @@ namespace Capnp } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/DeserializerState.cs b/Capnp.Net.Runtime/DeserializerState.cs index 830ff83..f879870 100644 --- a/Capnp.Net.Runtime/DeserializerState.cs +++ b/Capnp.Net.Runtime/DeserializerState.cs @@ -1,6 +1,8 @@ using System; using System.Collections.Generic; +using System.Diagnostics; +#nullable enable namespace Capnp { /// @@ -47,7 +49,7 @@ namespace Capnp /// /// The capabilities imported from the capability table. Only valid in RPC context. /// - public IList Caps { get; set; } + public IList? Caps { get; set; } /// /// Current segment (essentially Segments[CurrentSegmentIndex] /// @@ -83,10 +85,15 @@ namespace Capnp /// The conversion is cheap, since it does not involve copying any payload. /// /// The serializer state to be converted + /// is null + /// is not bound to a MessageBuilder public static implicit operator DeserializerState(SerializerState state) { if (state == null) throw new ArgumentNullException(nameof(state)); + + if (state.MsgBuilder == null) + throw new InvalidOperationException("state is not bound to a MessageBuilder"); switch (state.Kind) { @@ -100,7 +107,7 @@ namespace Capnp case ObjectKind.ListOfStructs: case ObjectKind.Nil: case ObjectKind.Struct: - return new DeserializerState(state.Allocator.Segments) + return new DeserializerState(state.Allocator!.Segments) { CurrentSegmentIndex = state.SegmentIndex, Offset = state.Offset, @@ -112,7 +119,7 @@ namespace Capnp }; case ObjectKind.Capability: - return new DeserializerState(state.Allocator.Segments) + return new DeserializerState(state.Allocator!.Segments) { Kind = ObjectKind.Capability, Caps = state.Caps, @@ -376,11 +383,11 @@ namespace Capnp /// the capability table. Does not mutate this state. /// /// Offset relative to this.Offset within current segment - /// the low-level capability object + /// the low-level capability object, or null if it is a null pointer /// offset negative or out of range /// capability table not set /// not a capability pointer or invalid capability index - internal Rpc.ConsumedCapability DecodeCapPointer(int offset) + internal Rpc.ConsumedCapability? DecodeCapPointer(int offset) { if (offset < 0) { @@ -490,7 +497,7 @@ namespace Capnp return state; } - internal Rpc.ConsumedCapability StructReadRawCap(int index) + internal Rpc.ConsumedCapability? StructReadRawCap(int index) { if (Kind != ObjectKind.Struct && Kind != ObjectKind.Nil) throw new InvalidOperationException("Allowed on structs only"); @@ -567,7 +574,7 @@ namespace Capnp /// negative index /// state does not represent a struct, invalid pointer, /// non-list-of-bytes pointer, traversal limit exceeded - public string ReadText(int index, string defaultText = null) + public string? ReadText(int index, string? defaultText = null) { return StructReadPointer(index).RequireList().CastText() ?? defaultText; } @@ -646,7 +653,7 @@ namespace Capnp /// negative index /// state does not represent a struct, invalid pointer, /// non-capability pointer, traversal limit exceeded - public T ReadCap(int index, + public T? ReadCap(int index, [System.Runtime.CompilerServices.CallerMemberName] string memberName = "", [System.Runtime.CompilerServices.CallerFilePath] string sourceFilePath = "", [System.Runtime.CompilerServices.CallerLineNumber] int sourceLineNumber = 0) where T: class @@ -677,7 +684,7 @@ namespace Capnp /// capability instance or null if pointer was null /// negative index /// state does not represent a capability - public T RequireCap() where T: class + public T? RequireCap() where T: class { if (Kind == ObjectKind.Nil) return null; @@ -685,7 +692,11 @@ namespace Capnp if (Kind != ObjectKind.Capability) throw new DeserializationException("Expected a capability"); + if (Caps == null) + throw new InvalidOperationException("Capability table not set. This is a bug."); + return Rpc.CapabilityReflection.CreateProxy(Caps[(int)CapabilityIndex]) as T; } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/DynamicSerializerState.cs b/Capnp.Net.Runtime/DynamicSerializerState.cs index 793c8f9..5faa19d 100644 --- a/Capnp.Net.Runtime/DynamicSerializerState.cs +++ b/Capnp.Net.Runtime/DynamicSerializerState.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; +#nullable enable namespace Capnp { /// @@ -226,3 +227,4 @@ namespace Capnp } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/EmptyList.cs b/Capnp.Net.Runtime/EmptyList.cs index 8db5d94..dbdba4e 100644 --- a/Capnp.Net.Runtime/EmptyList.cs +++ b/Capnp.Net.Runtime/EmptyList.cs @@ -3,6 +3,7 @@ using System.Collections; using System.Collections.Generic; using System.Linq; +#nullable enable namespace Capnp { /// @@ -36,3 +37,4 @@ namespace Capnp } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/EmptyListDeserializer.cs b/Capnp.Net.Runtime/EmptyListDeserializer.cs index ac7be4f..d731892 100644 --- a/Capnp.Net.Runtime/EmptyListDeserializer.cs +++ b/Capnp.Net.Runtime/EmptyListDeserializer.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; +#nullable enable namespace Capnp { /// @@ -86,3 +87,4 @@ namespace Capnp public override IReadOnlyList CastUShort() => new EmptyList(); } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/FramePump.cs b/Capnp.Net.Runtime/FramePump.cs index fa841cc..d92317f 100644 --- a/Capnp.Net.Runtime/FramePump.cs +++ b/Capnp.Net.Runtime/FramePump.cs @@ -9,6 +9,7 @@ using System.Runtime.InteropServices; using System.Text; using System.Threading; +#nullable enable namespace Capnp { /// @@ -22,7 +23,7 @@ namespace Capnp int _disposing; readonly Stream _stream; - readonly BinaryWriter _writer; + readonly BinaryWriter? _writer; readonly object _writeLock = new object(); readonly List _tracers = new List(); @@ -62,7 +63,7 @@ namespace Capnp /// /// Event handler for frame reception. /// - public event Action FrameReceived; + public event Action? FrameReceived; /// /// Sends a message over the stream. @@ -196,3 +197,4 @@ namespace Capnp } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/FrameTracing/IFrameTracer.cs b/Capnp.Net.Runtime/FrameTracing/IFrameTracer.cs index 3255b29..5172dfb 100644 --- a/Capnp.Net.Runtime/FrameTracing/IFrameTracer.cs +++ b/Capnp.Net.Runtime/FrameTracing/IFrameTracer.cs @@ -1,5 +1,6 @@ using System; +#nullable enable namespace Capnp.FrameTracing { /// @@ -31,3 +32,4 @@ namespace Capnp.FrameTracing void TraceFrame(FrameDirection direction, WireFrame frame); } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/FrameTracing/RpcFrameTracer.cs b/Capnp.Net.Runtime/FrameTracing/RpcFrameTracer.cs index f2c3ca1..2c85285 100644 --- a/Capnp.Net.Runtime/FrameTracing/RpcFrameTracer.cs +++ b/Capnp.Net.Runtime/FrameTracing/RpcFrameTracer.cs @@ -6,6 +6,7 @@ using System.IO; using System.Linq; using System.Threading; +#nullable enable namespace Capnp.FrameTracing { /// @@ -245,3 +246,4 @@ namespace Capnp.FrameTracing } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/Framing.cs b/Capnp.Net.Runtime/Framing.cs index e9707ce..fd6fb04 100644 --- a/Capnp.Net.Runtime/Framing.cs +++ b/Capnp.Net.Runtime/Framing.cs @@ -3,6 +3,7 @@ using System.IO; using System.Runtime.InteropServices; using System.Text; +#nullable enable namespace Capnp { /// @@ -107,3 +108,4 @@ namespace Capnp } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/ICapnpSerializable.cs b/Capnp.Net.Runtime/ICapnpSerializable.cs index 36eaeea..029c393 100644 --- a/Capnp.Net.Runtime/ICapnpSerializable.cs +++ b/Capnp.Net.Runtime/ICapnpSerializable.cs @@ -1,4 +1,5 @@ -namespace Capnp +#nullable enable +namespace Capnp { /// /// This interface is intended to be implemented by schema-generated domain classes which support deserialization from @@ -19,3 +20,4 @@ void Deserialize(DeserializerState state); } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/ISegmentAllocator.cs b/Capnp.Net.Runtime/ISegmentAllocator.cs index abcfb8d..96f615a 100644 --- a/Capnp.Net.Runtime/ISegmentAllocator.cs +++ b/Capnp.Net.Runtime/ISegmentAllocator.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; +#nullable enable namespace Capnp { /// @@ -27,3 +28,4 @@ namespace Capnp bool Allocate(uint nwords, uint preferredSegment, out SegmentSlice slice, bool forcePreferredSegment); } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/IStructDeserializer.cs b/Capnp.Net.Runtime/IStructDeserializer.cs index 24cd8d3..cec7283 100644 --- a/Capnp.Net.Runtime/IStructDeserializer.cs +++ b/Capnp.Net.Runtime/IStructDeserializer.cs @@ -1,5 +1,6 @@ using System; +#nullable enable namespace Capnp { /// @@ -20,3 +21,4 @@ namespace Capnp ulong StructReadData(ulong bitOffset, int bitCount); } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/IStructSerializer.cs b/Capnp.Net.Runtime/IStructSerializer.cs index 8678c33..14a321b 100644 --- a/Capnp.Net.Runtime/IStructSerializer.cs +++ b/Capnp.Net.Runtime/IStructSerializer.cs @@ -1,5 +1,6 @@ using System; +#nullable enable namespace Capnp { /// @@ -25,3 +26,4 @@ namespace Capnp Span StructDataSection { get; } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/ListDeserializer.cs b/Capnp.Net.Runtime/ListDeserializer.cs index cfe2acd..d9a8d1a 100644 --- a/Capnp.Net.Runtime/ListDeserializer.cs +++ b/Capnp.Net.Runtime/ListDeserializer.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; +#nullable enable namespace Capnp { /// @@ -10,7 +11,7 @@ namespace Capnp { static class GenericCasts { - public static Func CastFunc; + public static Func? CastFunc; } static ListDeserializer() @@ -401,3 +402,4 @@ namespace Capnp } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/ListKind.cs b/Capnp.Net.Runtime/ListKind.cs index 66d5492..91e856e 100644 --- a/Capnp.Net.Runtime/ListKind.cs +++ b/Capnp.Net.Runtime/ListKind.cs @@ -1,4 +1,5 @@ -namespace Capnp +#nullable enable +namespace Capnp { /// /// Enumerates the list element categories which are defined by Cap'n Proto. @@ -46,3 +47,4 @@ ListOfStructs = 7 } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/ListOfBitsDeserializer.cs b/Capnp.Net.Runtime/ListOfBitsDeserializer.cs index 7415ffb..ff5bf5f 100644 --- a/Capnp.Net.Runtime/ListOfBitsDeserializer.cs +++ b/Capnp.Net.Runtime/ListOfBitsDeserializer.cs @@ -2,6 +2,7 @@ using System.Collections; using System.Collections.Generic; +#nullable enable namespace Capnp { /// @@ -77,3 +78,4 @@ namespace Capnp } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/ListOfBitsSerializer.cs b/Capnp.Net.Runtime/ListOfBitsSerializer.cs index cbbfe3d..b22b1cc 100644 --- a/Capnp.Net.Runtime/ListOfBitsSerializer.cs +++ b/Capnp.Net.Runtime/ListOfBitsSerializer.cs @@ -3,6 +3,7 @@ using System.Collections; using System.Collections.Generic; using System.Linq; +#nullable enable namespace Capnp { /// @@ -96,3 +97,4 @@ namespace Capnp IEnumerator IEnumerable.GetEnumerator() => this.ToArray().GetEnumerator(); } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/ListOfCapsDeserializer.cs b/Capnp.Net.Runtime/ListOfCapsDeserializer.cs index c80eff3..c546e13 100644 --- a/Capnp.Net.Runtime/ListOfCapsDeserializer.cs +++ b/Capnp.Net.Runtime/ListOfCapsDeserializer.cs @@ -3,6 +3,7 @@ using System.Collections; using System.Collections.Generic; using System.Text; +#nullable enable namespace Capnp { /// @@ -29,7 +30,7 @@ namespace Capnp if (index < 0 || index >= Count) throw new IndexOutOfRangeException(); - return Rpc.CapabilityReflection.CreateProxy(State.DecodeCapPointer(index)) as T; + return (Rpc.CapabilityReflection.CreateProxy(State.DecodeCapPointer(index)) as T)!; } } @@ -70,3 +71,4 @@ namespace Capnp } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/ListOfCapsSerializer.cs b/Capnp.Net.Runtime/ListOfCapsSerializer.cs index 960a8e3..fcaeb57 100644 --- a/Capnp.Net.Runtime/ListOfCapsSerializer.cs +++ b/Capnp.Net.Runtime/ListOfCapsSerializer.cs @@ -1,8 +1,8 @@ using System; using System.Collections; using System.Collections.Generic; -using System.Text; +#nullable enable namespace Capnp { /// @@ -34,7 +34,7 @@ namespace Capnp /// is out of range. public T this[int index] { - get => Rpc.CapabilityReflection.CreateProxy(DecodeCapPointer(index)) as T; + get => (Rpc.CapabilityReflection.CreateProxy(DecodeCapPointer(index)) as T)!; set { if (!IsAllocated) @@ -109,3 +109,4 @@ namespace Capnp } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/ListOfEmptyDeserializer.cs b/Capnp.Net.Runtime/ListOfEmptyDeserializer.cs index e8d1d2f..9ca54af 100644 --- a/Capnp.Net.Runtime/ListOfEmptyDeserializer.cs +++ b/Capnp.Net.Runtime/ListOfEmptyDeserializer.cs @@ -3,6 +3,7 @@ using System.Collections; using System.Collections.Generic; using System.Linq; +#nullable enable namespace Capnp { /// @@ -64,3 +65,4 @@ namespace Capnp } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/ListOfEmptySerializer.cs b/Capnp.Net.Runtime/ListOfEmptySerializer.cs index 7638b61..8ebb0cf 100644 --- a/Capnp.Net.Runtime/ListOfEmptySerializer.cs +++ b/Capnp.Net.Runtime/ListOfEmptySerializer.cs @@ -1,5 +1,6 @@ using System; +#nullable enable namespace Capnp { /// @@ -31,3 +32,4 @@ namespace Capnp } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/ListOfPointersDeserializer.cs b/Capnp.Net.Runtime/ListOfPointersDeserializer.cs index a3e679e..e20e737 100644 --- a/Capnp.Net.Runtime/ListOfPointersDeserializer.cs +++ b/Capnp.Net.Runtime/ListOfPointersDeserializer.cs @@ -2,6 +2,7 @@ using System.Collections; using System.Collections.Generic; +#nullable enable namespace Capnp { /// @@ -89,3 +90,4 @@ namespace Capnp } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/ListOfPointersSerializer.cs b/Capnp.Net.Runtime/ListOfPointersSerializer.cs index 15d9cdd..5bf014f 100644 --- a/Capnp.Net.Runtime/ListOfPointersSerializer.cs +++ b/Capnp.Net.Runtime/ListOfPointersSerializer.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; +#nullable enable namespace Capnp { /// @@ -12,7 +13,7 @@ namespace Capnp /// SerializerState which represents the element type public class ListOfPointersSerializer: SerializerState, - IReadOnlyList + IReadOnlyList where TS: SerializerState, new() { /// @@ -51,7 +52,7 @@ namespace Capnp /// public int Count => ListElementCount; - IEnumerable Enumerate() + IEnumerable Enumerate() { int count = Count; @@ -64,7 +65,7 @@ namespace Capnp /// /// Implements . /// - public IEnumerator GetEnumerator() + public IEnumerator GetEnumerator() { return Enumerate().GetEnumerator(); } @@ -115,4 +116,4 @@ namespace Capnp } } } - +#nullable enable \ No newline at end of file diff --git a/Capnp.Net.Runtime/ListOfPrimitivesDeserializer.cs b/Capnp.Net.Runtime/ListOfPrimitivesDeserializer.cs index 2c9d887..ce8dbad 100644 --- a/Capnp.Net.Runtime/ListOfPrimitivesDeserializer.cs +++ b/Capnp.Net.Runtime/ListOfPrimitivesDeserializer.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; +#nullable enable namespace Capnp { /// @@ -232,3 +233,4 @@ namespace Capnp } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/ListOfPrimitivesSerializer.cs b/Capnp.Net.Runtime/ListOfPrimitivesSerializer.cs index a558475..2a4a52e 100644 --- a/Capnp.Net.Runtime/ListOfPrimitivesSerializer.cs +++ b/Capnp.Net.Runtime/ListOfPrimitivesSerializer.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; +#nullable enable namespace Capnp { /// @@ -94,3 +95,4 @@ namespace Capnp IEnumerator IEnumerable.GetEnumerator() => Data.ToArray().GetEnumerator(); } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/ListOfStructsDeserializer.cs b/Capnp.Net.Runtime/ListOfStructsDeserializer.cs index 0b42b07..dfdc97e 100644 --- a/Capnp.Net.Runtime/ListOfStructsDeserializer.cs +++ b/Capnp.Net.Runtime/ListOfStructsDeserializer.cs @@ -3,6 +3,7 @@ using System.Collections; using System.Collections.Generic; using System.Linq; +#nullable enable namespace Capnp { /// @@ -79,3 +80,4 @@ namespace Capnp } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/ListOfStructsSerializer.cs b/Capnp.Net.Runtime/ListOfStructsSerializer.cs index 6f25afb..04cbc01 100644 --- a/Capnp.Net.Runtime/ListOfStructsSerializer.cs +++ b/Capnp.Net.Runtime/ListOfStructsSerializer.cs @@ -3,6 +3,7 @@ using System.Collections; using System.Collections.Generic; using System.Linq; +#nullable enable namespace Capnp { /// @@ -93,3 +94,4 @@ namespace Capnp } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/ListOfTextSerializer.cs b/Capnp.Net.Runtime/ListOfTextSerializer.cs index b8e4da1..2ebe4fa 100644 --- a/Capnp.Net.Runtime/ListOfTextSerializer.cs +++ b/Capnp.Net.Runtime/ListOfTextSerializer.cs @@ -2,6 +2,7 @@ using System.Collections; using System.Collections.Generic; +#nullable enable namespace Capnp { /// @@ -9,7 +10,7 @@ namespace Capnp /// public class ListOfTextSerializer : SerializerState, - IReadOnlyList + IReadOnlyList { /// /// Gets or sets the text at given index. Once an element is set, it cannot be overwritten. @@ -18,7 +19,7 @@ namespace Capnp /// List is not initialized /// is out of range. /// UTF-8 encoding exceeds 2^29-2 bytes - public string this[int index] + public string? this[int index] { get { @@ -47,7 +48,7 @@ namespace Capnp /// public int Count => ListElementCount; - IEnumerable Enumerate() + IEnumerable Enumerate() { int count = Count; @@ -60,7 +61,7 @@ namespace Capnp /// /// Implementation of /> /// - public IEnumerator GetEnumerator() + public IEnumerator GetEnumerator() { return Enumerate().GetEnumerator(); } @@ -110,3 +111,4 @@ namespace Capnp } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/Logging.cs b/Capnp.Net.Runtime/Logging.cs index 53e2e42..4842d32 100644 --- a/Capnp.Net.Runtime/Logging.cs +++ b/Capnp.Net.Runtime/Logging.cs @@ -1,6 +1,7 @@ using System; using Microsoft.Extensions.Logging; +#nullable enable namespace Capnp { /// @@ -21,3 +22,4 @@ namespace Capnp public static ILogger CreateLogger() => LoggerFactory.CreateLogger(); } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/MessageBuilder.cs b/Capnp.Net.Runtime/MessageBuilder.cs index d0fe6a0..b1719da 100644 --- a/Capnp.Net.Runtime/MessageBuilder.cs +++ b/Capnp.Net.Runtime/MessageBuilder.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Runtime.CompilerServices; using System.Text; +#nullable enable namespace Capnp { /// @@ -12,7 +13,7 @@ namespace Capnp { readonly ISegmentAllocator _allocator; readonly DynamicSerializerState _rootPtrBuilder; - List _capTable; + List? _capTable; MessageBuilder(ISegmentAllocator allocator) { @@ -56,10 +57,11 @@ namespace Capnp /// Gets or sets the root object. The root object must be set exactly once per message. /// Setting it manually is only required (and allowed) when it was created with . /// - public SerializerState Root + /// Attempt to set null reference + public SerializerState? Root { get => _rootPtrBuilder.TryGetPointer(0); - set => _rootPtrBuilder.Link(0, value); + set => _rootPtrBuilder.Link(0, value ?? throw new ArgumentNullException(nameof(value))); } /// @@ -90,13 +92,14 @@ namespace Capnp if (_capTable != null) throw new InvalidOperationException("Capability table was already initialized"); - _capTable = new List(); + _capTable = new List(); } /// /// Returns this message builder's segment allocator. /// public ISegmentAllocator Allocator => _allocator; - internal List Caps => _capTable; + internal List? Caps => _capTable; } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/ObjectKind.cs b/Capnp.Net.Runtime/ObjectKind.cs index c3b83f8..3456130 100644 --- a/Capnp.Net.Runtime/ObjectKind.cs +++ b/Capnp.Net.Runtime/ObjectKind.cs @@ -1,5 +1,6 @@ using System; +#nullable enable namespace Capnp { /// @@ -71,3 +72,4 @@ namespace Capnp Value = 16 } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/PrimitiveCoder.cs b/Capnp.Net.Runtime/PrimitiveCoder.cs index 3614f67..dcb7516 100644 --- a/Capnp.Net.Runtime/PrimitiveCoder.cs +++ b/Capnp.Net.Runtime/PrimitiveCoder.cs @@ -2,13 +2,14 @@ using System.Collections.Generic; using System.Text; +#nullable enable namespace Capnp { class PrimitiveCoder { class Coder { - public static Func Fn { get; set; } + public static Func? Fn { get; set; } } static PrimitiveCoder() @@ -45,3 +46,4 @@ namespace Capnp } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/ReadOnlyListExtensions.cs b/Capnp.Net.Runtime/ReadOnlyListExtensions.cs index 656ec19..f259e95 100644 --- a/Capnp.Net.Runtime/ReadOnlyListExtensions.cs +++ b/Capnp.Net.Runtime/ReadOnlyListExtensions.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; +#nullable enable namespace Capnp { /// @@ -71,3 +72,4 @@ namespace Capnp } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/Reserializing.cs b/Capnp.Net.Runtime/Reserializing.cs index a2bbf6c..3beacd4 100644 --- a/Capnp.Net.Runtime/Reserializing.cs +++ b/Capnp.Net.Runtime/Reserializing.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Text; +#nullable enable namespace Capnp { /// @@ -103,3 +104,4 @@ namespace Capnp } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/Rpc/AnswerOrCounterquestion.cs b/Capnp.Net.Runtime/Rpc/AnswerOrCounterquestion.cs index 1cdd5d6..893f1d7 100644 --- a/Capnp.Net.Runtime/Rpc/AnswerOrCounterquestion.cs +++ b/Capnp.Net.Runtime/Rpc/AnswerOrCounterquestion.cs @@ -1,4 +1,5 @@ -namespace Capnp.Rpc +#nullable enable +namespace Capnp.Rpc { /// /// Helper struct to support tail calls @@ -33,11 +34,12 @@ /// /// SerializerState, if applicable /// - public SerializerState Answer => _obj as SerializerState; + public SerializerState? Answer => _obj as SerializerState; /// /// PendingQuestion, if applicable /// - public PendingQuestion Counterquestion => _obj as PendingQuestion; + public PendingQuestion? Counterquestion => _obj as PendingQuestion; } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/Rpc/BareProxy.cs b/Capnp.Net.Runtime/Rpc/BareProxy.cs index 50d993d..d690411 100644 --- a/Capnp.Net.Runtime/Rpc/BareProxy.cs +++ b/Capnp.Net.Runtime/Rpc/BareProxy.cs @@ -1,4 +1,5 @@ -namespace Capnp.Rpc +#nullable enable +namespace Capnp.Rpc { /// /// Generic Proxy implementation which exposes the (usually protected) Call method. @@ -33,7 +34,7 @@ /// Constructs an instance and binds it to the given low-level capability. /// /// low-level capability - public BareProxy(ConsumedCapability cap): base(cap) + public BareProxy(ConsumedCapability? cap): base(cap) { } @@ -50,3 +51,4 @@ } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/Rpc/CapabilityReflection.cs b/Capnp.Net.Runtime/Rpc/CapabilityReflection.cs index 2cde6e6..1eb323c 100644 --- a/Capnp.Net.Runtime/Rpc/CapabilityReflection.cs +++ b/Capnp.Net.Runtime/Rpc/CapabilityReflection.cs @@ -3,6 +3,7 @@ using System.Diagnostics; using System.Linq; using System.Runtime.CompilerServices; +#nullable enable namespace Capnp.Rpc { /// @@ -110,7 +111,7 @@ namespace Capnp.Rpc return (SkeletonFactory)Activator.CreateInstance( typeof(SkeletonFactory<>) - .MakeGenericType(skeletonClass)); + .MakeGenericType(skeletonClass))!; } static SkeletonFactory GetSkeletonFactory(Type type) @@ -190,7 +191,7 @@ namespace Capnp.Rpc return (ProxyFactory)Activator.CreateInstance( typeof(ProxyFactory<>) - .MakeGenericType(proxyClass)); + .MakeGenericType(proxyClass))!; } else { @@ -263,7 +264,7 @@ namespace Capnp.Rpc /// Problem with instatiating the Proxy (constructor threw exception). /// Caller does not have permission to invoke the Proxy constructor. /// Problem with building the Proxy type, or problem with loading some dependent class. - public static Proxy CreateProxy(ConsumedCapability cap, + public static Proxy CreateProxy(ConsumedCapability? cap, [System.Runtime.CompilerServices.CallerMemberName] string memberName = "", [System.Runtime.CompilerServices.CallerFilePath] string sourceFilePath = "", [System.Runtime.CompilerServices.CallerLineNumber] int sourceLineNumber = 0) @@ -286,3 +287,4 @@ namespace Capnp.Rpc } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/Rpc/ConnectionState.cs b/Capnp.Net.Runtime/Rpc/ConnectionState.cs index 5084a54..b7ff923 100644 --- a/Capnp.Net.Runtime/Rpc/ConnectionState.cs +++ b/Capnp.Net.Runtime/Rpc/ConnectionState.cs @@ -1,4 +1,5 @@ -namespace Capnp.Rpc +#nullable enable +namespace Capnp.Rpc { /// /// State of an RPC connection @@ -22,3 +23,4 @@ Down } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/Rpc/ConsumedCapability.cs b/Capnp.Net.Runtime/Rpc/ConsumedCapability.cs index 8152d44..0f048de 100644 --- a/Capnp.Net.Runtime/Rpc/ConsumedCapability.cs +++ b/Capnp.Net.Runtime/Rpc/ConsumedCapability.cs @@ -2,6 +2,7 @@ using System.Threading; using System.Threading.Tasks; +#nullable enable namespace Capnp.Rpc { /// @@ -18,7 +19,7 @@ namespace Capnp.Rpc /// protected abstract void ReleaseRemotely(); internal abstract void Export(IRpcEndpoint endpoint, CapDescriptor.WRITER writer); - internal abstract void Freeze(out IRpcEndpoint boundEndpoint); + internal abstract void Freeze(out IRpcEndpoint? boundEndpoint); internal abstract void Unfreeze(); internal abstract void AddRef(); @@ -31,3 +32,4 @@ namespace Capnp.Rpc #endif } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/Rpc/IConnection.cs b/Capnp.Net.Runtime/Rpc/IConnection.cs index 582b17e..d7b8381 100644 --- a/Capnp.Net.Runtime/Rpc/IConnection.cs +++ b/Capnp.Net.Runtime/Rpc/IConnection.cs @@ -1,6 +1,7 @@ using Capnp.FrameTracing; using System; +#nullable enable namespace Capnp.Rpc { /// @@ -58,3 +59,4 @@ namespace Capnp.Rpc void Close(); } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/Rpc/IEndpoint.cs b/Capnp.Net.Runtime/Rpc/IEndpoint.cs index 5c695f2..6112dbb 100644 --- a/Capnp.Net.Runtime/Rpc/IEndpoint.cs +++ b/Capnp.Net.Runtime/Rpc/IEndpoint.cs @@ -1,4 +1,5 @@ -namespace Capnp.Rpc +#nullable enable +namespace Capnp.Rpc { /// /// A uni-directional endpoint, used in conjunction with the . @@ -16,3 +17,4 @@ void Dismiss(); } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/Rpc/IMonoSkeleton.cs b/Capnp.Net.Runtime/Rpc/IMonoSkeleton.cs index 4c41061..14972de 100644 --- a/Capnp.Net.Runtime/Rpc/IMonoSkeleton.cs +++ b/Capnp.Net.Runtime/Rpc/IMonoSkeleton.cs @@ -1,4 +1,5 @@ -namespace Capnp.Rpc +#nullable enable +namespace Capnp.Rpc { /// /// A mono skeleton (as opposed to ) is a skeleton which implements one particular RPC interface. @@ -11,3 +12,4 @@ ulong InterfaceId { get; } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/Rpc/IPromisedAnswer.cs b/Capnp.Net.Runtime/Rpc/IPromisedAnswer.cs index aedcbe4..df1e3ad 100644 --- a/Capnp.Net.Runtime/Rpc/IPromisedAnswer.cs +++ b/Capnp.Net.Runtime/Rpc/IPromisedAnswer.cs @@ -1,6 +1,7 @@ using System; using System.Threading.Tasks; +#nullable enable namespace Capnp.Rpc { /// @@ -22,6 +23,7 @@ namespace Capnp.Rpc /// /// Path to the desired capability inside the result struct. /// Pipelined low-level capability - ConsumedCapability Access(MemberAccessPath access); + ConsumedCapability? Access(MemberAccessPath access); } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/Rpc/IProvidedCapability.cs b/Capnp.Net.Runtime/Rpc/IProvidedCapability.cs index 3cbc384..aa4c6b2 100644 --- a/Capnp.Net.Runtime/Rpc/IProvidedCapability.cs +++ b/Capnp.Net.Runtime/Rpc/IProvidedCapability.cs @@ -2,6 +2,7 @@ using System.Threading; using System.Threading.Tasks; +#nullable enable namespace Capnp.Rpc { @@ -22,3 +23,4 @@ namespace Capnp.Rpc DeserializerState args, CancellationToken cancellationToken = default); } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/Rpc/IResolvingCapability.cs b/Capnp.Net.Runtime/Rpc/IResolvingCapability.cs index e8b29da..877298a 100644 --- a/Capnp.Net.Runtime/Rpc/IResolvingCapability.cs +++ b/Capnp.Net.Runtime/Rpc/IResolvingCapability.cs @@ -1,5 +1,6 @@ using System.Threading.Tasks; +#nullable enable namespace Capnp.Rpc { /// @@ -13,3 +14,4 @@ namespace Capnp.Rpc Task WhenResolved { get; } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/Rpc/IRpcEndpoint.cs b/Capnp.Net.Runtime/Rpc/IRpcEndpoint.cs index e72e140..192daa1 100644 --- a/Capnp.Net.Runtime/Rpc/IRpcEndpoint.cs +++ b/Capnp.Net.Runtime/Rpc/IRpcEndpoint.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; +#nullable enable namespace Capnp.Rpc { internal interface IRpcEndpoint @@ -19,3 +20,4 @@ namespace Capnp.Rpc void DeleteQuestion(PendingQuestion question); } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/Rpc/Impatient.cs b/Capnp.Net.Runtime/Rpc/Impatient.cs index 9dd1e08..84a76fe 100644 --- a/Capnp.Net.Runtime/Rpc/Impatient.cs +++ b/Capnp.Net.Runtime/Rpc/Impatient.cs @@ -3,6 +3,7 @@ using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; +#nullable enable namespace Capnp.Rpc { /// @@ -11,7 +12,7 @@ namespace Capnp.Rpc public static class Impatient { static readonly ConditionalWeakTable _taskTable = new ConditionalWeakTable(); - static readonly ThreadLocal _askingEndpoint = new ThreadLocal(); + static readonly ThreadLocal _askingEndpoint = new ThreadLocal(); /// /// Attaches a continuation to the given promise and registers the resulting task for pipelining. @@ -51,7 +52,7 @@ namespace Capnp.Rpc } else if (rtask.IsFaulted) { - rtask = Task.FromException(rtask.Exception.InnerException); + rtask = Task.FromException(rtask.Exception!.InnerException!); } else { @@ -86,7 +87,7 @@ namespace Capnp.Rpc return answer; } - internal static IPromisedAnswer TryGetAnswer(Task task) + internal static IPromisedAnswer? TryGetAnswer(Task task) { _taskTable.TryGetValue(task, out var answer); return answer; @@ -102,10 +103,10 @@ namespace Capnp.Rpc return proxy; case null: - return null; + return CapabilityReflection.CreateProxy(null); } - var skel = Skeleton.GetOrCreateSkeleton(item, false); + var skel = Skeleton.GetOrCreateSkeleton(item!, false); var localCap = LocalCapability.Create(skel); return CapabilityReflection.CreateProxy(localCap); } @@ -131,7 +132,7 @@ namespace Capnp.Rpc where TInterface : class { var lazyCap = new LazyCapability(AwaitProxy(task)); - return CapabilityReflection.CreateProxy(lazyCap, memberName, sourceFilePath, sourceLineNumber) as TInterface; + return (CapabilityReflection.CreateProxy(lazyCap, memberName, sourceFilePath, sourceLineNumber) as TInterface)!; } static readonly MemberAccessPath Path_OneAndOnly = new MemberAccessPath(0U); @@ -168,15 +169,15 @@ namespace Capnp.Rpc } var lazyCap = new LazyCapability(AwaitProxy(task)); - return CapabilityReflection.CreateProxy(lazyCap) as TInterface; + return (CapabilityReflection.CreateProxy(lazyCap) as TInterface)!; } else { - return CapabilityReflection.CreateProxy(answer.Access(Path_OneAndOnly)) as TInterface; + return (CapabilityReflection.CreateProxy(answer.Access(Path_OneAndOnly)) as TInterface)!; } } - internal static IRpcEndpoint AskingEndpoint + internal static IRpcEndpoint? AskingEndpoint { get => _askingEndpoint.Value; set { _askingEndpoint.Value = value; } @@ -252,3 +253,4 @@ namespace Capnp.Rpc } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/Rpc/ImportedCapability.cs b/Capnp.Net.Runtime/Rpc/ImportedCapability.cs index c821634..2227264 100644 --- a/Capnp.Net.Runtime/Rpc/ImportedCapability.cs +++ b/Capnp.Net.Runtime/Rpc/ImportedCapability.cs @@ -1,5 +1,6 @@ using System.Threading.Tasks; +#nullable enable namespace Capnp.Rpc { /// @@ -52,3 +53,4 @@ namespace Capnp.Rpc } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/Rpc/Interception/CallContext.cs b/Capnp.Net.Runtime/Rpc/Interception/CallContext.cs index 8daaaba..914bf87 100644 --- a/Capnp.Net.Runtime/Rpc/Interception/CallContext.cs +++ b/Capnp.Net.Runtime/Rpc/Interception/CallContext.cs @@ -5,6 +5,7 @@ using System.Text; using System.Threading; using System.Threading.Tasks; +#nullable enable namespace Capnp.Rpc.Interception { @@ -34,7 +35,7 @@ namespace Capnp.Rpc.Interception return new Proxy(Access(access)); } - public ConsumedCapability Access(MemberAccessPath access) + public ConsumedCapability? Access(MemberAccessPath access) { if (_futureResult.Task.IsCompleted) { @@ -44,7 +45,7 @@ namespace Capnp.Rpc.Interception } catch (AggregateException exception) { - throw exception.InnerException; + throw exception.InnerException!; } } else @@ -107,7 +108,7 @@ namespace Capnp.Rpc.Interception /// /// Input arguments /// - public SerializerState InArgs { get; set; } + public SerializerState? InArgs { get; set; } /// /// Output arguments ("return value") @@ -117,7 +118,7 @@ namespace Capnp.Rpc.Interception /// /// Exception text, or null if there is no exception /// - public string Exception { get; set; } + public string? Exception { get; set; } /// /// Whether the call should return in canceled state to Alice (the original caller). @@ -156,7 +157,7 @@ namespace Capnp.Rpc.Interception /// null /// /// - public object Bob + public object? Bob { get => _bob; set @@ -196,11 +197,11 @@ namespace Capnp.Rpc.Interception } } - internal Proxy BobProxy { get; private set; } + internal Proxy? BobProxy { get; private set; } readonly CensorCapability _censorCapability; PromisedAnswer _promisedAnswer; - object _bob; + object? _bob; internal IPromisedAnswer Answer => _promisedAnswer; @@ -224,8 +225,13 @@ namespace Capnp.Rpc.Interception { for (int i = 0; i < state.Caps.Count; i++) { - state.Caps[i] = policy.Attach(state.Caps[i]); - state.Caps[i].AddRef(); + var cap = state.Caps[i]; + if (cap != null) + { + cap = policy.Attach(cap); + state.Caps[i] = cap; + cap.AddRef(); + } } } } @@ -236,8 +242,13 @@ namespace Capnp.Rpc.Interception { for (int i = 0; i < state.Caps.Count; i++) { - state.Caps[i] = policy.Detach(state.Caps[i]); - state.Caps[i].AddRef(); + var cap = state.Caps[i]; + if (cap != null) + { + cap = policy.Detach(cap); + state.Caps[i] = cap; + cap.AddRef(); + } } } } @@ -246,8 +257,12 @@ namespace Capnp.Rpc.Interception /// Intercepts all capabilies inside the input arguments /// /// Policy to use, or null to further use present policy - public void InterceptInCaps(IInterceptionPolicy policyOverride = null) + /// InArgs not set + public void InterceptInCaps(IInterceptionPolicy? policyOverride = null) { + if (InArgs == null) + throw new InvalidOperationException("InArgs not set"); + InterceptCaps(InArgs, policyOverride ?? _censorCapability.Policy); } @@ -255,7 +270,7 @@ namespace Capnp.Rpc.Interception /// Intercepts all capabilies inside the output arguments /// /// Policy to use, or null to further use present policy - public void InterceptOutCaps(IInterceptionPolicy policyOverride = null) + public void InterceptOutCaps(IInterceptionPolicy? policyOverride = null) { InterceptCaps(OutArgs, policyOverride ?? _censorCapability.Policy); } @@ -264,8 +279,12 @@ namespace Capnp.Rpc.Interception /// Unintercepts all capabilies inside the input arguments /// /// Policy to remove, or null to remove present policy - public void UninterceptInCaps(IInterceptionPolicy policyOverride = null) + /// InArgs not set + public void UninterceptInCaps(IInterceptionPolicy? policyOverride = null) { + if (InArgs == null) + throw new InvalidOperationException("InArgs not set"); + UninterceptCaps(InArgs, policyOverride ?? _censorCapability.Policy); } @@ -273,7 +292,7 @@ namespace Capnp.Rpc.Interception /// Unintercepts all capabilies inside the output arguments /// /// Policy to remove, or null to remove present policy - public void UninterceptOutCaps(IInterceptionPolicy policyOverride = null) + public void UninterceptOutCaps(IInterceptionPolicy? policyOverride = null) { UninterceptCaps(OutArgs, policyOverride ?? _censorCapability.Policy); } @@ -281,14 +300,16 @@ namespace Capnp.Rpc.Interception /// /// Forwards this intercepted call to the target capability ("Bob"). /// + /// Bob/InArgs not set public void ForwardToBob() { if (Bob == null) - { throw new InvalidOperationException("Bob is null"); - } - - var answer = BobProxy.Call(InterfaceId, MethodId, InArgs.Rewrap(), default, CancelToBob); + + if (InArgs == null) + throw new InvalidOperationException("InArgs not set"); + + var answer = BobProxy!.Call(InterfaceId, MethodId, InArgs.Rewrap(), default, CancelToBob); State = InterceptionState.ForwardedToBob; @@ -336,3 +357,4 @@ namespace Capnp.Rpc.Interception } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/Rpc/Interception/CensorCapability.cs b/Capnp.Net.Runtime/Rpc/Interception/CensorCapability.cs index abaa526..be3950c 100644 --- a/Capnp.Net.Runtime/Rpc/Interception/CensorCapability.cs +++ b/Capnp.Net.Runtime/Rpc/Interception/CensorCapability.cs @@ -1,4 +1,5 @@ -namespace Capnp.Rpc.Interception +#nullable enable +namespace Capnp.Rpc.Interception { class CensorCapability : RefCountingCapability { @@ -32,7 +33,7 @@ writer.SenderHosted = endpoint.AllocateExport(MyVine, out bool _); } - internal override void Freeze(out IRpcEndpoint boundEndpoint) + internal override void Freeze(out IRpcEndpoint? boundEndpoint) { boundEndpoint = null; } @@ -42,3 +43,4 @@ } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/Rpc/Interception/IInterceptionPolicy.cs b/Capnp.Net.Runtime/Rpc/Interception/IInterceptionPolicy.cs index a4d5706..79ae1bb 100644 --- a/Capnp.Net.Runtime/Rpc/Interception/IInterceptionPolicy.cs +++ b/Capnp.Net.Runtime/Rpc/Interception/IInterceptionPolicy.cs @@ -1,5 +1,6 @@ using System; +#nullable enable namespace Capnp.Rpc.Interception { /// @@ -21,3 +22,4 @@ namespace Capnp.Rpc.Interception void OnReturnFromBob(CallContext callContext); } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/Rpc/Interception/InterceptionState.cs b/Capnp.Net.Runtime/Rpc/Interception/InterceptionState.cs index 67eea0e..26d2fd8 100644 --- a/Capnp.Net.Runtime/Rpc/Interception/InterceptionState.cs +++ b/Capnp.Net.Runtime/Rpc/Interception/InterceptionState.cs @@ -1,4 +1,5 @@ -namespace Capnp.Rpc.Interception +#nullable enable +namespace Capnp.Rpc.Interception { /// /// The state of an intercepted call from Alice to Bob. @@ -26,3 +27,4 @@ ReturnedToAlice } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/Rpc/Interception/Interceptor.cs b/Capnp.Net.Runtime/Rpc/Interception/Interceptor.cs index 91c3ee3..47d826b 100644 --- a/Capnp.Net.Runtime/Rpc/Interception/Interceptor.cs +++ b/Capnp.Net.Runtime/Rpc/Interception/Interceptor.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Runtime.CompilerServices; +#nullable enable namespace Capnp.Rpc.Interception { /// @@ -45,16 +46,16 @@ namespace Capnp.Rpc.Interception switch (cap) { case Proxy proxy: - return CapabilityReflection.CreateProxy(Attach(policy, proxy.ConsumedCap)) as TCap; + return (CapabilityReflection.CreateProxy(Attach(policy, proxy.ConsumedCap!)) as TCap)!; case ConsumedCapability ccap: - return new CensorCapability(ccap, policy) as TCap; + return (new CensorCapability(ccap, policy) as TCap)!; default: - return Attach(policy, - CapabilityReflection.CreateProxy( + return (Attach(policy, + (CapabilityReflection.CreateProxy( LocalCapability.Create( - Skeleton.GetOrCreateSkeleton(cap, false))) as TCap); + Skeleton.GetOrCreateSkeleton(cap, false))) as TCap)!)); } } @@ -79,11 +80,11 @@ namespace Capnp.Rpc.Interception switch (cap) { case Proxy proxy: - return CapabilityReflection.CreateProxy(Detach(policy, proxy.ConsumedCap)) as TCap; + return (CapabilityReflection.CreateProxy(Detach(policy, proxy.ConsumedCap!)) as TCap)!; case CensorCapability ccap: { - var cur = ccap; + CensorCapability? cur = ccap; var stk = new Stack(); do @@ -96,7 +97,7 @@ namespace Capnp.Rpc.Interception { cur2 = p.Attach(cur2); } - return cur2 as TCap; + return (cur2 as TCap)!; } stk.Push(cur.Policy); @@ -104,7 +105,7 @@ namespace Capnp.Rpc.Interception } while (cur != null); - return ccap as TCap; + return (ccap as TCap)!; } default: @@ -113,3 +114,4 @@ namespace Capnp.Rpc.Interception } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/Rpc/InvalidCapabilityInterfaceException.cs b/Capnp.Net.Runtime/Rpc/InvalidCapabilityInterfaceException.cs index 2ce9466..b3ac8a5 100644 --- a/Capnp.Net.Runtime/Rpc/InvalidCapabilityInterfaceException.cs +++ b/Capnp.Net.Runtime/Rpc/InvalidCapabilityInterfaceException.cs @@ -1,4 +1,5 @@ -namespace Capnp.Rpc +#nullable enable +namespace Capnp.Rpc { /// /// Will be thrown if a type did not qualify as capability interface. @@ -22,3 +23,4 @@ } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/Rpc/LazyCapability.cs b/Capnp.Net.Runtime/Rpc/LazyCapability.cs index ed2ac40..c5df3f5 100644 --- a/Capnp.Net.Runtime/Rpc/LazyCapability.cs +++ b/Capnp.Net.Runtime/Rpc/LazyCapability.cs @@ -2,6 +2,7 @@ using System.Threading; using System.Threading.Tasks; +#nullable enable namespace Capnp.Rpc { class LazyCapability : RefCountingCapability, IResolvingCapability @@ -27,7 +28,7 @@ namespace Capnp.Rpc WhenResolved = capabilityTask; } - internal override void Freeze(out IRpcEndpoint boundEndpoint) + internal override void Freeze(out IRpcEndpoint? boundEndpoint) { if (WhenResolved.IsCompleted) { @@ -37,7 +38,7 @@ namespace Capnp.Rpc } catch (AggregateException exception) { - throw exception.InnerException; + throw exception.InnerException!; } } else @@ -106,3 +107,4 @@ namespace Capnp.Rpc } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/Rpc/LocalAnswer.cs b/Capnp.Net.Runtime/Rpc/LocalAnswer.cs index 80b5ac0..85c6865 100644 --- a/Capnp.Net.Runtime/Rpc/LocalAnswer.cs +++ b/Capnp.Net.Runtime/Rpc/LocalAnswer.cs @@ -2,6 +2,7 @@ using System.Threading; using System.Threading.Tasks; +#nullable enable namespace Capnp.Rpc { class LocalAnswer : IPromisedAnswer @@ -50,3 +51,4 @@ namespace Capnp.Rpc } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/Rpc/LocalAnswerCapability.cs b/Capnp.Net.Runtime/Rpc/LocalAnswerCapability.cs index 3dbf83b..a5c57fe 100644 --- a/Capnp.Net.Runtime/Rpc/LocalAnswerCapability.cs +++ b/Capnp.Net.Runtime/Rpc/LocalAnswerCapability.cs @@ -2,6 +2,7 @@ using System.Threading; using System.Threading.Tasks; +#nullable enable namespace Capnp.Rpc { class LocalAnswerCapability : RefCountingCapability, IResolvingCapability @@ -15,7 +16,7 @@ namespace Capnp.Rpc _access = access; } - internal override void Freeze(out IRpcEndpoint boundEndpoint) + internal override void Freeze(out IRpcEndpoint? boundEndpoint) { boundEndpoint = null; } @@ -43,7 +44,7 @@ namespace Capnp.Rpc } catch (AggregateException exception) { - throw exception.InnerException; + throw exception.InnerException!; } using (var proxy = new Proxy(_access.Eval(result))) @@ -87,3 +88,4 @@ namespace Capnp.Rpc } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/Rpc/LocalCapability.cs b/Capnp.Net.Runtime/Rpc/LocalCapability.cs index 3328a5f..2dfbe0b 100644 --- a/Capnp.Net.Runtime/Rpc/LocalCapability.cs +++ b/Capnp.Net.Runtime/Rpc/LocalCapability.cs @@ -4,6 +4,7 @@ using System.Text; using System.Threading; using System.Threading.Tasks; +#nullable enable namespace Capnp.Rpc { class LocalCapability : ConsumedCapability @@ -14,7 +15,7 @@ namespace Capnp.Rpc public static ConsumedCapability Create(Skeleton skeleton) { if (skeleton is Vine vine) - return vine.Proxy.ConsumedCap; + return vine.Proxy.ConsumedCap!; else return _localCaps.GetValue(skeleton, _ => new LocalCapability(_)); } @@ -22,7 +23,7 @@ namespace Capnp.Rpc static async Task AwaitAnswer(Task call) { var aorcq = await call; - return aorcq.Answer ?? await aorcq.Counterquestion.WhenReturned; + return aorcq.Answer ?? await aorcq.Counterquestion!.WhenReturned; } public Skeleton ProvidedCap { get; } @@ -55,7 +56,7 @@ namespace Capnp.Rpc capDesc.SenderHosted = endpoint.AllocateExport(ProvidedCap, out bool _); } - internal override void Freeze(out IRpcEndpoint boundEndpoint) + internal override void Freeze(out IRpcEndpoint? boundEndpoint) { boundEndpoint = null; } @@ -69,3 +70,4 @@ namespace Capnp.Rpc } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/Rpc/MemberAccessPath.cs b/Capnp.Net.Runtime/Rpc/MemberAccessPath.cs index 7b887c8..cb30670 100644 --- a/Capnp.Net.Runtime/Rpc/MemberAccessPath.cs +++ b/Capnp.Net.Runtime/Rpc/MemberAccessPath.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; +#nullable enable namespace Capnp.Rpc { /// @@ -169,7 +170,7 @@ namespace Capnp.Rpc /// The object (usually "params struct") on which to evaluate this path. /// Resulting low-level capability /// Evaluation of this path did not give a capability - public ConsumedCapability Eval(DeserializerState rpcState) + public ConsumedCapability? Eval(DeserializerState rpcState) { var cur = rpcState; @@ -184,7 +185,7 @@ namespace Capnp.Rpc return null; case ObjectKind.Capability: - return rpcState.Caps[(int)cur.CapabilityIndex]; + return rpcState.Caps![(int)cur.CapabilityIndex]; default: throw new DeserializationException("Access path did not result in a capability"); @@ -192,3 +193,4 @@ namespace Capnp.Rpc } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/Rpc/PendingAnswer.cs b/Capnp.Net.Runtime/Rpc/PendingAnswer.cs index 4031501..43803cd 100644 --- a/Capnp.Net.Runtime/Rpc/PendingAnswer.cs +++ b/Capnp.Net.Runtime/Rpc/PendingAnswer.cs @@ -3,19 +3,20 @@ using System.Diagnostics; using System.Threading; using System.Threading.Tasks; +#nullable enable namespace Capnp.Rpc { class PendingAnswer: IDisposable { readonly object _reentrancyBlocker = new object(); - readonly CancellationTokenSource _cts; + readonly CancellationTokenSource? _cts; readonly TaskCompletionSource _whenCanceled; Task _callTask; - Task _initialTask; - Task _chainedTask; + Task? _initialTask; + Task? _chainedTask; bool _disposed; - public PendingAnswer(Task callTask, CancellationTokenSource cts) + public PendingAnswer(Task callTask, CancellationTokenSource? cts) { _cts = cts; _callTask = callTask ?? throw new ArgumentNullException(nameof(callTask)); @@ -138,7 +139,7 @@ namespace Capnp.Rpc case ObjectKind.Capability: try { - var cap = aorcq.Answer.Caps[(int)cur.CapabilityIndex]; + var cap = aorcq.Answer.Caps![(int)cur.CapabilityIndex]; proxy = new Proxy(cap ?? LazyCapability.Null); } catch (ArgumentOutOfRangeException) @@ -154,7 +155,7 @@ namespace Capnp.Rpc else { var path = MemberAccessPath.Deserialize(rd); - var cap = new RemoteAnswerCapability(aorcq.Counterquestion, path); + var cap = new RemoteAnswerCapability(aorcq.Counterquestion!, path); return new Proxy(cap); } } @@ -169,7 +170,7 @@ namespace Capnp.Rpc { if (_cts != null) { - Task chainedTask; + Task? chainedTask; lock (_reentrancyBlocker) { @@ -199,3 +200,4 @@ namespace Capnp.Rpc } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/Rpc/PendingQuestion.cs b/Capnp.Net.Runtime/Rpc/PendingQuestion.cs index 2d7addc..047dd60 100644 --- a/Capnp.Net.Runtime/Rpc/PendingQuestion.cs +++ b/Capnp.Net.Runtime/Rpc/PendingQuestion.cs @@ -4,6 +4,7 @@ using System.Diagnostics; using System.Threading; using System.Threading.Tasks; +#nullable enable namespace Capnp.Rpc { /// @@ -61,11 +62,11 @@ namespace Capnp.Rpc readonly TaskCompletionSource _tcs = new TaskCompletionSource(); readonly uint _questionId; - ConsumedCapability _target; - SerializerState _inParams; + ConsumedCapability? _target; + SerializerState? _inParams; int _inhibitFinishCounter; - internal PendingQuestion(IRpcEndpoint ep, uint id, ConsumedCapability target, SerializerState inParams) + internal PendingQuestion(IRpcEndpoint ep, uint id, ConsumedCapability? target, SerializerState? inParams) { RpcEndpoint = ep ?? throw new ArgumentNullException(nameof(ep)); _questionId = id; @@ -75,7 +76,7 @@ namespace Capnp.Rpc if (inParams != null) { - foreach (var cap in inParams.Caps) + foreach (var cap in inParams.Caps!) { cap?.AddRef(); } @@ -236,7 +237,7 @@ namespace Capnp.Rpc /// Access path /// Low-level capability /// The referenced member does not exist or does not resolve to a capability pointer. - public ConsumedCapability Access(MemberAccessPath access) + public ConsumedCapability? Access(MemberAccessPath access) { lock (ReentrancyBlocker) { @@ -249,7 +250,7 @@ namespace Capnp.Rpc } catch (AggregateException exception) { - throw exception.InnerException; + throw exception.InnerException!; } } else @@ -259,11 +260,11 @@ namespace Capnp.Rpc } } - static void ReleaseCaps(ConsumedCapability target, SerializerState inParams) + static void ReleaseCaps(ConsumedCapability? target, SerializerState? inParams) { if (inParams != null) { - foreach (var cap in inParams.Caps) + foreach (var cap in inParams.Caps!) { cap?.Release(); } @@ -277,7 +278,7 @@ namespace Capnp.Rpc static void ReleaseOutCaps(DeserializerState outParams) { - foreach (var cap in outParams.Caps) + foreach (var cap in outParams.Caps!) { cap?.Release(); } @@ -285,12 +286,13 @@ namespace Capnp.Rpc internal void Send() { - SerializerState inParams; - ConsumedCapability target; + SerializerState? inParams; + ConsumedCapability? target; lock (ReentrancyBlocker) { - Debug.Assert(!StateFlags.HasFlag(State.Sent)); + if (StateFlags.HasFlag(State.Sent)) + throw new InvalidOperationException("Already sent"); inParams = _inParams; _inParams = null; @@ -299,7 +301,7 @@ namespace Capnp.Rpc StateFlags |= State.Sent; } - var msg = (Message.WRITER)inParams.MsgBuilder.Root; + var msg = (Message.WRITER)inParams!.MsgBuilder!.Root!; Debug.Assert(msg.Call.Target.which != MessageTarget.WHICH.undefined); var call = msg.Call; call.QuestionId = QuestionId; @@ -316,15 +318,15 @@ namespace Capnp.Rpc OnException(exception); } - ReleaseCaps(target, inParams); + ReleaseCaps(target!, inParams); } #region IDisposable Support void Dispose(bool disposing) { - SerializerState inParams; - ConsumedCapability target; + SerializerState? inParams; + ConsumedCapability? target; bool justDisposed = false; lock (ReentrancyBlocker) @@ -377,3 +379,4 @@ namespace Capnp.Rpc #endregion } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/Rpc/PolySkeleton.cs b/Capnp.Net.Runtime/Rpc/PolySkeleton.cs index a159f8e..1d0a579 100644 --- a/Capnp.Net.Runtime/Rpc/PolySkeleton.cs +++ b/Capnp.Net.Runtime/Rpc/PolySkeleton.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; +#nullable enable namespace Capnp.Rpc { /// @@ -71,3 +72,4 @@ namespace Capnp.Rpc } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/Rpc/PromisedCapability.cs b/Capnp.Net.Runtime/Rpc/PromisedCapability.cs index b704c7e..87be8ea 100644 --- a/Capnp.Net.Runtime/Rpc/PromisedCapability.cs +++ b/Capnp.Net.Runtime/Rpc/PromisedCapability.cs @@ -3,6 +3,7 @@ using System.Diagnostics; using System.Threading; using System.Threading.Tasks; +#nullable enable namespace Capnp.Rpc { class PromisedCapability : RemoteResolvingCapability @@ -19,7 +20,7 @@ namespace Capnp.Rpc public override Task WhenResolved => _resolvedCap.Task; - internal override void Freeze(out IRpcEndpoint boundEndpoint) + internal override void Freeze(out IRpcEndpoint? boundEndpoint) { lock (_reentrancyBlocker) { @@ -31,7 +32,7 @@ namespace Capnp.Rpc } catch (AggregateException exception) { - throw exception.InnerException; + throw exception.InnerException!; } } else @@ -148,7 +149,7 @@ namespace Capnp.Rpc } } - protected override Proxy ResolvedCap + protected override Proxy? ResolvedCap { get { @@ -158,7 +159,7 @@ namespace Capnp.Rpc } catch (AggregateException exception) { - throw exception.InnerException; + throw exception.InnerException!; } } } @@ -258,3 +259,4 @@ namespace Capnp.Rpc } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/Rpc/Proxy.cs b/Capnp.Net.Runtime/Rpc/Proxy.cs index 1574c5d..9529522 100644 --- a/Capnp.Net.Runtime/Rpc/Proxy.cs +++ b/Capnp.Net.Runtime/Rpc/Proxy.cs @@ -3,6 +3,7 @@ using System; using System.Threading; using System.Threading.Tasks; +#nullable enable namespace Capnp.Rpc { /// @@ -38,7 +39,7 @@ namespace Capnp.Rpc /// /// Underlying low-level capability /// - protected internal ConsumedCapability ConsumedCap { get; private set; } + protected internal ConsumedCapability? ConsumedCap { get; private set; } /// /// Whether is this a broken capability. @@ -99,12 +100,12 @@ namespace Capnp.Rpc { } - internal Proxy(ConsumedCapability cap) + internal Proxy(ConsumedCapability? cap) { Bind(cap); } - internal void Bind(ConsumedCapability cap) + internal void Bind(ConsumedCapability? cap) { if (ConsumedCap != null) throw new InvalidOperationException("Proxy was already bound"); @@ -116,7 +117,7 @@ namespace Capnp.Rpc cap.AddRef(); } - internal IProvidedCapability GetProvider() + internal IProvidedCapability? GetProvider() { switch (ConsumedCap) { @@ -200,7 +201,7 @@ namespace Capnp.Rpc using (disposeThis ? this : null) { - return CapabilityReflection.CreateProxy(ConsumedCap) as T; + return (CapabilityReflection.CreateProxy(ConsumedCap) as T)!; } } @@ -215,7 +216,7 @@ namespace Capnp.Rpc ConsumedCap.Export(endpoint, writer); } - internal void Freeze(out IRpcEndpoint boundEndpoint) + internal void Freeze(out IRpcEndpoint? boundEndpoint) { if (_disposedValue) throw new ObjectDisposedException(nameof(Proxy)); @@ -239,3 +240,4 @@ namespace Capnp.Rpc #endif } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/Rpc/ProxyAttribute.cs b/Capnp.Net.Runtime/Rpc/ProxyAttribute.cs index ccf38c5..54e65d5 100644 --- a/Capnp.Net.Runtime/Rpc/ProxyAttribute.cs +++ b/Capnp.Net.Runtime/Rpc/ProxyAttribute.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Text; +#nullable enable namespace Capnp.Rpc { /// @@ -28,3 +29,4 @@ namespace Capnp.Rpc public Type ProxyClass { get; } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/Rpc/RefCountingCapability.cs b/Capnp.Net.Runtime/Rpc/RefCountingCapability.cs index e60e3dc..f578159 100644 --- a/Capnp.Net.Runtime/Rpc/RefCountingCapability.cs +++ b/Capnp.Net.Runtime/Rpc/RefCountingCapability.cs @@ -2,6 +2,7 @@ using System.Threading; using System.Threading.Tasks; +#nullable enable namespace Capnp.Rpc { abstract class RefCountingCapability: ConsumedCapability @@ -112,3 +113,4 @@ namespace Capnp.Rpc } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/Rpc/RemoteAnswerCapability.cs b/Capnp.Net.Runtime/Rpc/RemoteAnswerCapability.cs index 27f8961..f9aec6c 100644 --- a/Capnp.Net.Runtime/Rpc/RemoteAnswerCapability.cs +++ b/Capnp.Net.Runtime/Rpc/RemoteAnswerCapability.cs @@ -3,6 +3,7 @@ using System; using System.Diagnostics; using System.Threading.Tasks; +#nullable enable namespace Capnp.Rpc { class RemoteAnswerCapability : RemoteResolvingCapability @@ -18,7 +19,7 @@ namespace Capnp.Rpc readonly PendingQuestion _question; readonly MemberAccessPath _access; - Proxy _resolvedCap; + Proxy? _resolvedCap; public RemoteAnswerCapability(PendingQuestion question, MemberAccessPath access): base(question.RpcEndpoint) { @@ -59,7 +60,7 @@ namespace Capnp.Rpc } } - protected override Proxy ResolvedCap + protected override Proxy? ResolvedCap { get { @@ -74,7 +75,7 @@ namespace Capnp.Rpc } catch (AggregateException exception) { - throw exception.InnerException; + throw exception.InnerException!; } _resolvedCap = new Proxy(_access.Eval(result)); @@ -87,7 +88,11 @@ namespace Capnp.Rpc async Task AwaitWhenResolved() { await _question.WhenReturned; - return ResolvedCap; + + if (_question.IsTailCall) + throw new InvalidOperationException("Question is a tail call, so won't resolve back."); + + return ResolvedCap!; } public override Task WhenResolved => AwaitWhenResolved(); @@ -168,7 +173,7 @@ namespace Capnp.Rpc return call; } - internal override void Freeze(out IRpcEndpoint boundEndpoint) + internal override void Freeze(out IRpcEndpoint? boundEndpoint) { lock (_question.ReentrancyBlocker) { @@ -216,7 +221,7 @@ namespace Capnp.Rpc if (_question.StateFlags.HasFlag(PendingQuestion.State.Returned)) { - ResolvedCap.Export(endpoint, writer); + ResolvedCap?.Export(endpoint, writer); } else { @@ -255,3 +260,4 @@ namespace Capnp.Rpc } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/Rpc/RemoteCapability.cs b/Capnp.Net.Runtime/Rpc/RemoteCapability.cs index 2dc470e..6e4c4f6 100644 --- a/Capnp.Net.Runtime/Rpc/RemoteCapability.cs +++ b/Capnp.Net.Runtime/Rpc/RemoteCapability.cs @@ -3,6 +3,7 @@ using System.Diagnostics; using System.Threading; using System.Threading.Tasks; +#nullable enable namespace Capnp.Rpc { @@ -24,6 +25,9 @@ namespace Capnp.Rpc protected virtual Call.WRITER SetupMessage(DynamicSerializerState args, ulong interfaceId, ushort methodId) { + if (args.MsgBuilder == null) + throw new ArgumentException("Unbound serializer state", nameof(args)); + var callMsg = args.MsgBuilder.BuildRoot(); callMsg.which = Message.WHICH.Call; @@ -38,3 +42,4 @@ namespace Capnp.Rpc } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/Rpc/RemoteResolvingCapability.cs b/Capnp.Net.Runtime/Rpc/RemoteResolvingCapability.cs index 4c8c9fb..c939c80 100644 --- a/Capnp.Net.Runtime/Rpc/RemoteResolvingCapability.cs +++ b/Capnp.Net.Runtime/Rpc/RemoteResolvingCapability.cs @@ -3,6 +3,7 @@ using System; using System.Threading; using System.Threading.Tasks; +#nullable enable namespace Capnp.Rpc { abstract class RemoteResolvingCapability : RemoteCapability, IResolvingCapability @@ -23,14 +24,17 @@ namespace Capnp.Rpc } protected int _pendingCallsOnPromise; - Task _disembargo; + Task? _disembargo; - protected abstract Proxy ResolvedCap { get; } + protected abstract Proxy? ResolvedCap { get; } protected abstract void GetMessageTarget(MessageTarget.WRITER wr); protected IPromisedAnswer CallOnResolution(ulong interfaceId, ushort methodId, DynamicSerializerState args) { + if (ResolvedCap == null) + throw new InvalidOperationException("Capability not yet resolved, calling on resolution not possible"); + try { ResolvedCap.Freeze(out var resolvedCapEndpoint); @@ -124,3 +128,4 @@ namespace Capnp.Rpc } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/Rpc/ResolvingCapabilityExtensions.cs b/Capnp.Net.Runtime/Rpc/ResolvingCapabilityExtensions.cs index d59173b..d865447 100644 --- a/Capnp.Net.Runtime/Rpc/ResolvingCapabilityExtensions.cs +++ b/Capnp.Net.Runtime/Rpc/ResolvingCapabilityExtensions.cs @@ -1,4 +1,5 @@ -namespace Capnp.Rpc +#nullable enable +namespace Capnp.Rpc { static class ResolvingCapabilityExtensions { @@ -19,7 +20,7 @@ { var resolvedCap = await cap.WhenResolved; - endpoint.Resolve(preliminaryId, vine, () => resolvedCap.ConsumedCap); + endpoint.Resolve(preliminaryId, vine, () => resolvedCap.ConsumedCap!); } catch (System.Exception exception) { @@ -42,3 +43,4 @@ } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/Rpc/RpcEngine.cs b/Capnp.Net.Runtime/Rpc/RpcEngine.cs index 316e048..2b6da86 100644 --- a/Capnp.Net.Runtime/Rpc/RpcEngine.cs +++ b/Capnp.Net.Runtime/Rpc/RpcEngine.cs @@ -10,6 +10,7 @@ using System.Text; using System.Threading; using System.Threading.Tasks; +#nullable enable namespace Capnp.Rpc { /// @@ -65,8 +66,8 @@ namespace Capnp.Rpc internal class RpcEndpoint : IEndpoint, IRpcEndpoint { - static readonly ThreadLocal _exportCapTablePostActions = new ThreadLocal(); - static readonly ThreadLocal _tailCall = new ThreadLocal(); + static readonly ThreadLocal _exportCapTablePostActions = new ThreadLocal(); + static readonly ThreadLocal _tailCall = new ThreadLocal(); static readonly ThreadLocal _canDeferCalls = new ThreadLocal(); ILogger Logger { get; } = Logging.CreateLogger(); @@ -145,8 +146,7 @@ namespace Capnp.Rpc Tx(mb.Frame); } - void IRpcEndpoint.Resolve(uint preliminaryId, Skeleton preliminaryCap, - Func resolvedCapGetter) + void IRpcEndpoint.Resolve(uint preliminaryId, Skeleton preliminaryCap, Func resolvedCapGetter) { lock (_reentrancyBlocker) { @@ -232,7 +232,7 @@ namespace Capnp.Rpc return AllocateExport(providedCapability, out first); } - PendingQuestion AllocateQuestion(ConsumedCapability target, SerializerState inParams) + PendingQuestion AllocateQuestion(ConsumedCapability? target, SerializerState? inParams) { lock (_reentrancyBlocker) { @@ -293,7 +293,7 @@ namespace Capnp.Rpc uint q = req.QuestionId; var bootstrap = DynamicSerializerState.CreateForRpc(); - var ans = bootstrap.MsgBuilder.BuildRoot(); + var ans = bootstrap.MsgBuilder!.BuildRoot(); ans.which = Message.WHICH.Return; var ret = ans.Return; @@ -305,7 +305,7 @@ namespace Capnp.Rpc if (bootstrapCap != null) { ret.which = Return.WHICH.Results; - bootstrap.SetCapability(bootstrap.ProvideCapability(LocalCapability.Create(_host.BootstrapCap))); + bootstrap.SetCapability(bootstrap.ProvideCapability(LocalCapability.Create(bootstrapCap))); ret.Results.Content = bootstrap; bootstrapTask = Task.FromResult(bootstrap); @@ -371,12 +371,12 @@ namespace Capnp.Rpc } catch (RpcException exception) { - Logger.LogWarning($"Unable to return call: {exception.InnerException.Message}"); + Logger.LogWarning($"Unable to return call: {exception.InnerException?.Message ?? exception.Message}"); } } - IProvidedCapability cap; - PendingAnswer pendingAnswer = null; + IProvidedCapability? cap; + PendingAnswer? pendingAnswer = null; bool releaseParamCaps = false; void AwaitAnswerAndReply() @@ -414,8 +414,8 @@ namespace Capnp.Rpc } else if (aorcq.Answer != null || aorcq.Counterquestion != _tailCall.Value) { - var results = aorcq.Answer ?? (DynamicSerializerState)(await aorcq.Counterquestion.WhenReturned); - var ret = SetupReturn(results.MsgBuilder); + var results = aorcq.Answer ?? (DynamicSerializerState)(await aorcq.Counterquestion!.WhenReturned); + var ret = SetupReturn(results.MsgBuilder!); switch (req.SendResultsTo.which) { @@ -569,7 +569,7 @@ namespace Capnp.Rpc case MessageTarget.WHICH.PromisedAnswer: { bool exists; - PendingAnswer previousAnswer; + PendingAnswer? previousAnswer; lock (_reentrancyBlocker) { @@ -578,7 +578,7 @@ namespace Capnp.Rpc if (exists) { - previousAnswer.Chain( + previousAnswer!.Chain( false, req.Target.PromisedAnswer, async t => @@ -633,7 +633,7 @@ namespace Capnp.Rpc void ProcessReturn(Return.READER req) { - PendingQuestion question; + PendingQuestion? question; lock (_reentrancyBlocker) { @@ -674,7 +674,7 @@ namespace Capnp.Rpc case Return.WHICH.TakeFromOtherQuestion: { bool exists; - PendingAnswer pendingAnswer; + PendingAnswer? pendingAnswer; lock (_reentrancyBlocker) { @@ -683,7 +683,7 @@ namespace Capnp.Rpc if (exists) { - pendingAnswer.Chain(false, async t => + pendingAnswer!.Chain(false, async t => { try { @@ -747,6 +747,8 @@ namespace Capnp.Rpc lock (_reentrancyBlocker) { var resolvedCap = ImportCap(resolve.Cap); + if (resolvedCap == null) + resolvedCap = LazyCapability.CreateBrokenCap("Failed to resolve this capability"); resolvableCap.ResolveTo(resolvedCap); } break; @@ -935,7 +937,7 @@ namespace Capnp.Rpc { foreach (var cap in results.Caps) { - cap.Release(); + cap?.Release(); } } }); @@ -984,8 +986,8 @@ namespace Capnp.Rpc try { int icount = checked((int)count); - rc.Release(icount); - rc.Cap.Relinquish(icount); + rc!.Release(icount); + rc!.Cap.Relinquish(icount); if (rc.RefCount == 0) { @@ -1171,7 +1173,7 @@ namespace Capnp.Rpc } } - ConsumedCapability ImportCap(CapDescriptor.READER capDesc) + ConsumedCapability? ImportCap(CapDescriptor.READER capDesc) { lock (_reentrancyBlocker) { @@ -1192,8 +1194,7 @@ namespace Capnp.Rpc rcw.Cap.SetTarget(impCap); } - Debug.Assert(impCap != null); - return impCap; + return impCap!; } else { @@ -1236,7 +1237,6 @@ namespace Capnp.Rpc case CapDescriptor.WHICH.ReceiverHosted: if (_exportTable.TryGetValue(capDesc.ReceiverHosted, out var rc)) { - Debug.Assert(rc.Cap != null); return LocalCapability.Create(rc.Cap); } else @@ -1311,9 +1311,9 @@ namespace Capnp.Rpc } } - public IList ImportCapTable(Payload.READER payload) + public IList ImportCapTable(Payload.READER payload) { - var list = new List(); + var list = new List(); if (payload.CapTable != null) { @@ -1341,7 +1341,7 @@ namespace Capnp.Rpc Debug.Assert(_exportCapTablePostActions.Value == null); _exportCapTablePostActions.Value = null; - payload.CapTable.Init(state.MsgBuilder.Caps.Count); + payload.CapTable.Init(state.MsgBuilder!.Caps!.Count); int i = 0; foreach (var cap in state.MsgBuilder.Caps) @@ -1454,7 +1454,7 @@ namespace Capnp.Rpc } catch (RpcException exception) { - Logger.LogWarning($"Unable to release import: {exception.InnerException.Message}"); + Logger.LogWarning($"Unable to release import: {exception.InnerException?.Message ?? exception.Message}"); } } } @@ -1492,12 +1492,12 @@ namespace Capnp.Rpc return inboundEndpoint; } - Skeleton _bootstrapCap; + Skeleton? _bootstrapCap; /// /// Gets or sets the bootstrap capability. /// - public Skeleton BootstrapCap + public Skeleton? BootstrapCap { get => _bootstrapCap; set @@ -1509,3 +1509,4 @@ namespace Capnp.Rpc } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/Rpc/RpcException.cs b/Capnp.Net.Runtime/Rpc/RpcException.cs index 9629cf9..fb288e0 100644 --- a/Capnp.Net.Runtime/Rpc/RpcException.cs +++ b/Capnp.Net.Runtime/Rpc/RpcException.cs @@ -1,4 +1,5 @@ -namespace Capnp.Rpc +#nullable enable +namespace Capnp.Rpc { /// /// Thrown when an RPC-related error condition occurs. @@ -20,3 +21,4 @@ } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/Rpc/RpcUnimplementedException.cs b/Capnp.Net.Runtime/Rpc/RpcUnimplementedException.cs index f700e34..12f3895 100644 --- a/Capnp.Net.Runtime/Rpc/RpcUnimplementedException.cs +++ b/Capnp.Net.Runtime/Rpc/RpcUnimplementedException.cs @@ -1,6 +1,8 @@ -namespace Capnp.Rpc +#nullable enable +namespace Capnp.Rpc { class RpcUnimplementedException : System.Exception { } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/Rpc/Skeleton.cs b/Capnp.Net.Runtime/Rpc/Skeleton.cs index 856f2a1..7bf7801 100644 --- a/Capnp.Net.Runtime/Rpc/Skeleton.cs +++ b/Capnp.Net.Runtime/Rpc/Skeleton.cs @@ -6,6 +6,7 @@ using System.Text; using System.Threading; using System.Threading.Tasks; +#nullable enable namespace Capnp.Rpc { /// @@ -179,8 +180,8 @@ namespace Capnp.Rpc ILogger Logger { get; } = Logging.CreateLogger>(); #endif - Func>[] _methods; - CancellationTokenSource _disposed = new CancellationTokenSource(); + Func>[] _methods = null!; + CancellationTokenSource? _disposed = new CancellationTokenSource(); readonly object _reentrancyBlocker = new object(); int _pendingCalls; @@ -204,7 +205,7 @@ namespace Capnp.Rpc /// /// Gets the underlying capability implementation. /// - protected T Impl { get; private set; } + protected T Impl { get; private set; } = default!; /// /// Gets the ID of the implemented interface. @@ -299,3 +300,4 @@ namespace Capnp.Rpc } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/Rpc/SkeletonAttribute.cs b/Capnp.Net.Runtime/Rpc/SkeletonAttribute.cs index b8b01d6..a5b0600 100644 --- a/Capnp.Net.Runtime/Rpc/SkeletonAttribute.cs +++ b/Capnp.Net.Runtime/Rpc/SkeletonAttribute.cs @@ -1,5 +1,6 @@ using System; +#nullable enable namespace Capnp.Rpc { /// @@ -32,3 +33,4 @@ namespace Capnp.Rpc public Type SkeletonClass { get; } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/Rpc/TcpRpcClient.cs b/Capnp.Net.Runtime/Rpc/TcpRpcClient.cs index 0f3e1a6..a35e7d7 100644 --- a/Capnp.Net.Runtime/Rpc/TcpRpcClient.cs +++ b/Capnp.Net.Runtime/Rpc/TcpRpcClient.cs @@ -10,6 +10,7 @@ using System.Text; using System.Threading; using System.Threading.Tasks; +#nullable enable namespace Capnp.Rpc { /// @@ -44,17 +45,17 @@ namespace Capnp.Rpc readonly RpcEngine _rpcEngine; readonly TcpClient _client; - RpcEngine.RpcEndpoint _inboundEndpoint; - OutboundTcpEndpoint _outboundEndpoint; - FramePump _pump; - Thread _pumpThread; - Action _attachTracerAction; + RpcEngine.RpcEndpoint? _inboundEndpoint; + OutboundTcpEndpoint? _outboundEndpoint; + FramePump? _pump; + Thread? _pumpThread; + Action? _attachTracerAction; /// /// Gets a Task which completes when TCP is connected. Will be /// null until connection is actually requested (either by calling Connect or using appropriate constructor). /// - public Task WhenConnected { get; private set; } + public Task? WhenConnected { get; private set; } async Task ConnectAsync(string host, int port) { @@ -152,8 +153,14 @@ namespace Capnp.Rpc /// /// Bootstrap capability interface /// A proxy for the bootstrap capability + /// Not connected public TProxy GetMain() where TProxy: class { + if (WhenConnected == null) + { + throw new InvalidOperationException("Not connecting"); + } + if (!WhenConnected.IsCompleted) { throw new InvalidOperationException("Connection not yet established"); @@ -164,9 +171,7 @@ namespace Capnp.Rpc throw new InvalidOperationException("Connection not successfully established"); } - Debug.Assert(_inboundEndpoint != null); - - return CapabilityReflection.CreateProxy(_inboundEndpoint.QueryMain()) as TProxy; + return (CapabilityReflection.CreateProxy(_inboundEndpoint!.QueryMain()) as TProxy)!; } /// @@ -178,7 +183,7 @@ namespace Capnp.Rpc try { - if (!WhenConnected.Wait(500)) + if (WhenConnected != null && !WhenConnected.Wait(500)) { Logger.LogError("Unable to join connection task within timeout"); } @@ -214,7 +219,7 @@ namespace Capnp.Rpc _attachTracerAction += () => { - _pump.AttachTracer(tracer); + _pump?.AttachTracer(tracer); }; } @@ -234,33 +239,34 @@ namespace Capnp.Rpc /// /// Gets the number of RPC protocol messages sent by this client so far. /// - public long SendCount => _inboundEndpoint.SendCount; + public long SendCount => _inboundEndpoint?.SendCount ?? 0; /// /// Gets the number of RPC protocol messages received by this client so far. /// - public long RecvCount => _inboundEndpoint.RecvCount; + public long RecvCount => _inboundEndpoint?.RecvCount ?? 0; /// /// Gets the remote port number which this client is connected to, /// or null if the connection is not yet established. /// - public int? RemotePort => ((IPEndPoint)_client.Client?.RemoteEndPoint)?.Port; + public int? RemotePort => ((IPEndPoint)_client.Client.RemoteEndPoint)?.Port; /// /// Gets the local port number which this client using, /// or null if the connection is not yet established. /// - public int? LocalPort => ((IPEndPoint)_client.Client?.LocalEndPoint)?.Port; + public int? LocalPort => ((IPEndPoint)_client.Client.LocalEndPoint)?.Port; /// /// Whether the I/O thread is currently running /// - public bool IsComputing => _pumpThread.ThreadState == System.Threading.ThreadState.Running; + public bool IsComputing => _pumpThread?.ThreadState == System.Threading.ThreadState.Running; /// /// Whether the I/O thread is waiting for data to receive /// - public bool IsWaitingForData => _pump.IsWaitingForData; + public bool IsWaitingForData => _pump?.IsWaitingForData ?? false; } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/Rpc/TcpRpcServer.cs b/Capnp.Net.Runtime/Rpc/TcpRpcServer.cs index 6ac5cb1..9363505 100644 --- a/Capnp.Net.Runtime/Rpc/TcpRpcServer.cs +++ b/Capnp.Net.Runtime/Rpc/TcpRpcServer.cs @@ -6,6 +6,7 @@ using System.Net; using System.Net.Sockets; using System.Threading; +#nullable enable namespace Capnp.Rpc { /// @@ -103,12 +104,12 @@ namespace Capnp.Rpc public FramePump Pump { get; private set; } public OutboundTcpEndpoint OutboundEp { get; private set; } public RpcEngine.RpcEndpoint InboundEp { get; private set; } - public Thread PumpRunner { get; private set; } + public Thread? PumpRunner { get; private set; } public int? LocalPort => ((IPEndPoint)Client.Client.LocalEndPoint)?.Port; public int? RemotePort => ((IPEndPoint)Client.Client.RemoteEndPoint)?.Port; public long RecvCount => InboundEp.RecvCount; public long SendCount => InboundEp.SendCount; - public bool IsComputing => PumpRunner.ThreadState == ThreadState.Running; + public bool IsComputing => PumpRunner?.ThreadState == ThreadState.Running; public bool IsWaitingForData => Pump.IsWaitingForData; public void AttachTracer(IFrameTracer tracer) @@ -165,7 +166,7 @@ namespace Capnp.Rpc connection.Start(); } - connection.PumpRunner.Start(); + connection.PumpRunner!.Start(); } } catch (SocketException) @@ -180,8 +181,13 @@ namespace Capnp.Rpc } } - void SafeJoin(Thread thread) + void SafeJoin(Thread? thread) { + if (thread == null) + { + return; + } + for (int retry = 0; retry < 5; ++retry) { try @@ -313,6 +319,7 @@ namespace Capnp.Rpc /// /// Fires when a new incoming connection was accepted, or when an active connection is closed. /// - public event Action OnConnectionChanged; + public event Action? OnConnectionChanged; } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/Rpc/Vine.cs b/Capnp.Net.Runtime/Rpc/Vine.cs index 47bc10e..d9ce1d3 100644 --- a/Capnp.Net.Runtime/Rpc/Vine.cs +++ b/Capnp.Net.Runtime/Rpc/Vine.cs @@ -4,6 +4,7 @@ using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; +#nullable enable namespace Capnp.Rpc { class Vine : Skeleton @@ -70,3 +71,4 @@ namespace Capnp.Rpc } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/SecurityOptions.cs b/Capnp.Net.Runtime/SecurityOptions.cs index eb0b463..f12f8d3 100644 --- a/Capnp.Net.Runtime/SecurityOptions.cs +++ b/Capnp.Net.Runtime/SecurityOptions.cs @@ -1,4 +1,5 @@ -namespace Capnp +#nullable enable +namespace Capnp { /// /// Provides the security bounds for defeating amplification and stack overflow DoS attacks. @@ -16,3 +17,4 @@ public static int RecursionLimit { get; set; } = 64; } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/SegmentAllocator.cs b/Capnp.Net.Runtime/SegmentAllocator.cs index 36c67ea..f473ba2 100644 --- a/Capnp.Net.Runtime/SegmentAllocator.cs +++ b/Capnp.Net.Runtime/SegmentAllocator.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Text; +#nullable enable namespace Capnp { /// @@ -145,3 +146,4 @@ namespace Capnp } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/SegmentSlice.cs b/Capnp.Net.Runtime/SegmentSlice.cs index 22f5c9d..6b3b403 100644 --- a/Capnp.Net.Runtime/SegmentSlice.cs +++ b/Capnp.Net.Runtime/SegmentSlice.cs @@ -1,5 +1,6 @@ using System; +#nullable enable namespace Capnp { /// @@ -18,3 +19,4 @@ namespace Capnp public int Offset; } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/SerializerExtensions.cs b/Capnp.Net.Runtime/SerializerExtensions.cs index d9d384e..7001ce3 100644 --- a/Capnp.Net.Runtime/SerializerExtensions.cs +++ b/Capnp.Net.Runtime/SerializerExtensions.cs @@ -1,6 +1,7 @@ using System; using System.Runtime.InteropServices; +#nullable enable namespace Capnp { /// @@ -339,3 +340,4 @@ namespace Capnp } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/SerializerState.cs b/Capnp.Net.Runtime/SerializerState.cs index 37d0475..561d27b 100644 --- a/Capnp.Net.Runtime/SerializerState.cs +++ b/Capnp.Net.Runtime/SerializerState.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Runtime.InteropServices; using System.Text; +#nullable enable namespace Capnp { /// @@ -29,10 +30,10 @@ namespace Capnp return s; } - internal MessageBuilder MsgBuilder { get; set; } - internal ISegmentAllocator Allocator => MsgBuilder?.Allocator; - internal List Caps => MsgBuilder?.Caps; - internal SerializerState Owner { get; set; } + internal MessageBuilder? MsgBuilder { get; set; } + internal ISegmentAllocator? Allocator => MsgBuilder?.Allocator; + internal List? Caps => MsgBuilder?.Caps; + internal SerializerState? Owner { get; set; } internal int OwnerSlot { get; set; } internal uint SegmentIndex { get; set; } internal int Offset { get; set; } @@ -43,7 +44,7 @@ namespace Capnp internal ObjectKind Kind { get; set; } internal uint CapabilityIndex { get; set; } - SerializerState[] _linkedStates; + SerializerState[]? _linkedStates; /// /// Constructs an unbound serializer state. @@ -129,7 +130,7 @@ namespace Capnp if (Owner != null) ts.Bind(Owner, OwnerSlot); else - ts.Bind(MsgBuilder); + ts.Bind(MsgBuilder ?? throw Unbound()); return ts; } @@ -152,8 +153,8 @@ namespace Capnp /// public bool IsAllocated => Offset >= 0; - Span SegmentSpan => IsAllocated ? Allocator.Segments[(int)SegmentIndex].Span : Span.Empty; - Span FarSpan(uint index) => Allocator.Segments[(int)index].Span; + Span SegmentSpan => IsAllocated && Allocator != null ? Allocator.Segments[(int)SegmentIndex].Span : Span.Empty; + Span FarSpan(uint index) => Allocator!.Segments[(int)index].Span; /// /// Given this state describes a struct and is allocated, returns the struct's data section. @@ -181,6 +182,9 @@ namespace Capnp } else { + if (Allocator == null) + throw Unbound(); + SegmentIndex = Owner?.SegmentIndex ?? SegmentIndex; Allocator.Allocate(count, SegmentIndex, out var slice, false); SegmentIndex = slice.SegmentIndex; @@ -191,7 +195,7 @@ namespace Capnp } /// - /// Allocates storage for the underlying object. Does nothing if it is already allocated. From the point the object is allocated, its type cannot by changed + /// Allocates storage for the underlying object. Does nothing if it is already allocated. From the point the object is allocated, its type cannot be changed /// anymore (e.g. changing from struct to list, or modifying the struct's section sizes). /// public void Allocate() @@ -250,6 +254,9 @@ namespace Capnp if (!target.IsAllocated) throw new InvalidOperationException("Target must be allocated before a pointer can be built"); + if (MsgBuilder == null) + throw Unbound(); + try { if (SegmentSpan[offset] != 0) @@ -345,7 +352,7 @@ namespace Capnp { WirePointer farPtr = default; - if (Allocator.Allocate(1, target.SegmentIndex, out var landingPadSlice, true)) + if (Allocator!.Allocate(1, target.SegmentIndex, out var landingPadSlice, true)) { farPtr.SetFarPointer(target.SegmentIndex, landingPadSlice.Offset, false); SegmentSpan[offset] = farPtr; @@ -372,7 +379,7 @@ namespace Capnp } } - internal Rpc.ConsumedCapability DecodeCapPointer(int offset) + internal Rpc.ConsumedCapability? DecodeCapPointer(int offset) { if (offset < 0) throw new IndexOutOfRangeException(nameof(offset)); @@ -455,7 +462,7 @@ namespace Capnp throw new InvalidOperationException("This object cannot own pointers to sub-objects"); } - _linkedStates[slot] = target; + _linkedStates![slot] = target; } /// @@ -476,6 +483,7 @@ namespace Capnp } static InvalidOperationException AlreadySet() => new InvalidOperationException("The object type was already set"); + static InvalidOperationException Unbound() => new InvalidOperationException("This state is not bound to a MessageBuilder"); void VerifyNotYetAllocated() { @@ -654,14 +662,21 @@ namespace Capnp /// Trying to obtain the UTF-8 encoding might throw this exception. /// The object type was already set to something different /// UTF-8 encoding exceeds 2^29-2 bytes - protected void WriteText(string text) + protected void WriteText(string? text) { - byte[] srcBytes = Encoding.UTF8.GetBytes(text); - SetListOfValues(8, srcBytes.Length + 1); - var srcSpan = new ReadOnlySpan(srcBytes); - var dstSpan = ListGetBytes(); - dstSpan = dstSpan.Slice(0, dstSpan.Length - 1); - srcSpan.CopyTo(dstSpan); + if (text == null) + { + VerifyNotYetAllocated(); + } + else + { + byte[] srcBytes = Encoding.UTF8.GetBytes(text); + SetListOfValues(8, srcBytes.Length + 1); + var srcSpan = new ReadOnlySpan(srcBytes); + var dstSpan = ListGetBytes(); + dstSpan = dstSpan.Slice(0, dstSpan.Length - 1); + srcSpan.CopyTo(dstSpan); + } } /// @@ -761,7 +776,7 @@ namespace Capnp if (Kind != ObjectKind.Struct && Kind != ObjectKind.ListOfPointers) throw new InvalidOperationException("This is not a struct or list of pointers"); - ref var state = ref _linkedStates[index]; + ref var state = ref _linkedStates![index]; if (state == null) { @@ -784,12 +799,12 @@ namespace Capnp /// Object at given position is not compatible with the desired target serializer type. /// /// is out of bounds. - public TS TryGetPointer(int index) where TS : SerializerState, new() + public TS? TryGetPointer(int index) where TS : SerializerState, new() { if (Kind != ObjectKind.Struct && Kind != ObjectKind.ListOfPointers) throw new InvalidOperationException("This is not a struct or list of pointers"); - var state = _linkedStates[index]; + var state = _linkedStates![index]; if (state == null) return null; @@ -820,7 +835,7 @@ namespace Capnp /// Object at given position is not compatible with the desired target serializer type. /// /// is out of bounds. - public SerializerState TryGetPointer(int index) => TryGetPointer(index); + public SerializerState? TryGetPointer(int index) => TryGetPointer(index); /// /// Reads text from a struct field or list element. @@ -829,7 +844,7 @@ namespace Capnp /// If the underlying object is a list of pointers: Element index /// String to return in case of null /// The decoded text - public string ReadText(int index, string defaultText = null) + public string? ReadText(int index, string? defaultText = null) { var b = BuildPointer(index); @@ -851,7 +866,7 @@ namespace Capnp /// Object at given position was already set. /// /// is out of bounds. - public void WriteText(int index, string text) + public void WriteText(int index, string? text) { BuildPointer(index).WriteText(text); } @@ -886,11 +901,11 @@ namespace Capnp if (Kind != ObjectKind.ListOfStructs) throw new InvalidOperationException("This is not a list of structs"); - ref var state = ref _linkedStates[index]; + ref var state = ref _linkedStates![index]; if (state == null) { - state = new SerializerState(MsgBuilder); + state = new SerializerState(MsgBuilder!); state.SetStruct(StructDataCount, StructPtrCount); state.SegmentIndex = SegmentIndex; state.Offset = Offset + 1 + index * (StructDataCount + StructPtrCount); @@ -913,12 +928,12 @@ namespace Capnp if (Kind != ObjectKind.ListOfStructs) throw new InvalidOperationException("This is not a list of structs"); - ref var state = ref _linkedStates[index]; + ref var state = ref _linkedStates![index]; if (state == null) { state = new TS(); - state.Bind(MsgBuilder); + state.Bind(MsgBuilder!); state.SetStruct(StructDataCount, StructPtrCount); state.SegmentIndex = SegmentIndex; int stride = StructDataCount + StructPtrCount; @@ -942,19 +957,19 @@ namespace Capnp for (int offset = minOffset; offset < maxOffset; offset++) { - ref var state = ref _linkedStates[offset - minOffset]; + ref var state = ref _linkedStates![offset - minOffset]; if (state == null) { state = new TS(); - state.Bind(MsgBuilder); + state.Bind(MsgBuilder!); state.SetStruct(StructDataCount, StructPtrCount); state.SegmentIndex = SegmentIndex; state.Offset = offset; } } - return _linkedStates.LazyListSelect(ts => (TS)ts); + return _linkedStates!.LazyListSelect(ts => (TS)ts); } /// @@ -1240,7 +1255,7 @@ namespace Capnp /// The low-level capability object to provide. /// Index of the given capability in the capability table /// The underlying message builder was not configured for capability table support. - public uint ProvideCapability(Rpc.ConsumedCapability capability) + public uint ProvideCapability(Rpc.ConsumedCapability? capability) { if (Caps == null) throw new InvalidOperationException("Underlying MessageBuilder was not enabled to support capabilities"); @@ -1279,7 +1294,7 @@ namespace Capnp /// /// Index of the given capability in the capability table /// The underlying message builder was not configured for capability table support. - public uint ProvideCapability(object obj) + public uint ProvideCapability(object? obj) { if (obj == null) return ProvideCapability(default(Rpc.ConsumedCapability)); @@ -1352,7 +1367,7 @@ namespace Capnp } } - internal Rpc.ConsumedCapability StructReadRawCap(int index) + internal Rpc.ConsumedCapability? StructReadRawCap(int index) { if (Kind != ObjectKind.Struct && Kind != ObjectKind.Nil) throw new InvalidOperationException("Allowed on structs only"); @@ -1372,7 +1387,7 @@ namespace Capnp /// is out of range. /// The desired interface does not qualify as capability interface () /// This state does not represent a struct. - public T ReadCap(int slot) where T : class + public T? ReadCap(int slot) where T : class { var cap = StructReadRawCap(slot); return Rpc.CapabilityReflection.CreateProxy(cap) as T; @@ -1392,3 +1407,4 @@ namespace Capnp } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/TypeIdAttribute.cs b/Capnp.Net.Runtime/TypeIdAttribute.cs index 098dd03..c21e48c 100644 --- a/Capnp.Net.Runtime/TypeIdAttribute.cs +++ b/Capnp.Net.Runtime/TypeIdAttribute.cs @@ -1,5 +1,6 @@ using System; +#nullable enable namespace Capnp { /// @@ -25,3 +26,4 @@ namespace Capnp public ulong Id { get; } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/UtilityExtensions.cs b/Capnp.Net.Runtime/UtilityExtensions.cs index 745e202..964f89c 100644 --- a/Capnp.Net.Runtime/UtilityExtensions.cs +++ b/Capnp.Net.Runtime/UtilityExtensions.cs @@ -4,8 +4,9 @@ using System.Threading.Tasks; namespace Capnp { - internal static class UtilityExtensions - { +#nullable enable + internal static class UtilityExtensions + { /// /// This method exists until NET Standard 2.1 is released /// @@ -23,7 +24,7 @@ namespace Capnp return true; } #else - public static bool ReplacementTryAdd(this Dictionary thisDict, K key, V value) => thisDict.TryAdd(key, value); + public static bool ReplacementTryAdd(this Dictionary thisDict, K key, V value) where K: struct => thisDict.TryAdd(key, value); #endif /// @@ -40,14 +41,16 @@ namespace Capnp { if (!thisDict.ContainsKey(key)) { - value = default; + value = default!; // OK here since .NET Standard 2.0 does not support (non-)nullability anyway. return false; } value = thisDict[key]; return thisDict.Remove(key); } #else +#pragma warning disable CS8714 public static bool ReplacementTryRemove(this Dictionary thisDict, K key, out V value) => thisDict.Remove(key, out value); +#pragma warning restore CS8714 #endif /// @@ -76,4 +79,5 @@ namespace Capnp public static float ReplacementInt32ToSingleBits(this int value) => BitConverter.Int32BitsToSingle(value); #endif } -} \ No newline at end of file +} +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/WireFrame.cs b/Capnp.Net.Runtime/WireFrame.cs index 0de1f7e..2fbfb76 100644 --- a/Capnp.Net.Runtime/WireFrame.cs +++ b/Capnp.Net.Runtime/WireFrame.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; +#nullable enable namespace Capnp { /// @@ -22,3 +23,4 @@ namespace Capnp } } } +#nullable restore \ No newline at end of file diff --git a/Capnp.Net.Runtime/WirePointer.cs b/Capnp.Net.Runtime/WirePointer.cs index 9b41a67..d3117a8 100644 --- a/Capnp.Net.Runtime/WirePointer.cs +++ b/Capnp.Net.Runtime/WirePointer.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Text; +#nullable enable namespace Capnp { /// @@ -228,3 +229,4 @@ namespace Capnp } } } +#nullable restore \ No newline at end of file