Example #1
0
        protected override void AioCallback(IntPtr argument)
        {
            var res = Unit.Ok;

            switch (State)
            {
            case AsyncState.Recv:
                res = Aio.GetResult();
                if (res.IsErr())
                {
                    State = AsyncState.Init;
                    tcs.TrySetNngError(res.Err());
                    return;
                }
                State = AsyncState.Init;
                nng_msg msg     = Aio.GetMsg();
                var     message = Factory.CreateMessage(msg);
                tcs.TrySetResult(NngResult <T> .Ok(message));
                break;

            case AsyncState.Init:
            default:
                tcs.TrySetException(new Exception(State.ToString()));
                break;
            }
        }
Example #2
0
        public static NngResult <IRepReqAsyncContext <T> > Create(IMessageFactory <T> factory, IRepSocket socket)
        {
            var context = new RepAsyncCtx <T> {
                Factory = factory, socket = socket
            };
            var res = context.InitAio();

            if (res.IsOk())
            {
                //TODO: when get default interface methods in C#8 move this to ICtx
                var ctx = NngCtx.Create(socket);
                if (ctx.IsOk())
                {
                    context.Ctx = ctx.Ok();
                    // Start receive loop
                    context.AioCallback(IntPtr.Zero);
                    return(NngResult <IRepReqAsyncContext <T> > .Ok(context));
                }
                return(NngResult <IRepReqAsyncContext <T> > .Err(ctx.Err()));
            }
            else
            {
                return(NngResult <IRepReqAsyncContext <T> > .Fail(res.Err()));
            }
        }
Example #3
0
        public NngResult <IMessage> RecvMsg(Defines.NngFlag flags = default)
        {
            nng_msg message;
            var     res = nng_recvmsg(NngSocket, out message, flags);

            return(NngResult <IMessage> .OkThen(res, () => new Message(message)));
        }
Example #4
0
        public NngResult <INngMsg> RecvMsg(Defines.NngFlag flags = default)
        {
            nng_msg message;
            var     res = nng_recvmsg(NativeNngStruct, out message, flags);

            return(NngResult <INngMsg> .OkThen(res, () => new NngMsg(message)));
        }
        protected override void AioCallback(IntPtr argument)
        {
            var res = 0;

            switch (State)
            {
            case AsyncState.Recv:
                res = nng_aio_result(aioHandle);
                if (res != 0)
                {
                    State = AsyncState.Init;
                    tcs.TrySetNngError(res);
                    return;
                }
                State = AsyncState.Init;
                nng_msg msg     = nng_aio_get_msg(aioHandle);
                var     message = Factory.CreateMessage(msg);
                tcs.TrySetResult(NngResult <T> .Ok(message));
                break;

            case AsyncState.Init:
            default:
                tcs.TrySetException(new Exception(State.ToString()));
                break;
            }
        }
Example #6
0
 public static void TrySetNngError <T>(this CancellationTokenTaskSource <NngResult <T> > socket, int error)
 {
     if (error == 0)
     {
         return;
     }
     socket.TrySetResult(NngResult <T> .Fail(error));
 }
Example #7
0
 public static void TrySetNngError <T>(this TaskCompletionSource <NngResult <T> > socket, int error)
 {
     if (error == 0)
     {
         return;
     }
     socket.TrySetResult(NngResult <T> .Fail(error));
 }
        public static NngResult <ISendReceiveAsyncContext <T> > Create(IMessageFactory <T> factory, ISocket socket)
        {
            var context = new SendReceiveAsyncContext <T> {
                Factory = factory, Socket = socket
            };
            var res = context.InitAio();

            return(NngResult <ISendReceiveAsyncContext <T> > .OkIfZero(res, context));
        }
Example #9
0
        public static NngResult <INngCtx> Create(ISocket socket)
        {
            var res = nng_ctx_open(out var ctx, socket.NngSocket);

            if (res != 0)
            {
                return(NngResult <INngCtx> .Fail(res));
            }
            return(NngResult <INngCtx> .Ok(new AsyncCtx { NngCtx = ctx }));
        }
Example #10
0
        /// <summary>
        /// Create a surveyor socket
        /// </summary>
        /// <returns>The open.</returns>
        public static NngResult <ISurveyorSocket> Open()
        {
            int res = nng_surveyor0_open(out var socket);

            if (res != 0)
            {
                return(NngResult <ISurveyorSocket> .Fail(res));
            }
            return(NngResult <ISurveyorSocket> .Ok(new SurveyorSocket { NngSocket = socket }));
        }
Example #11
0
        public static NngResult <INngCtx> Create(INngSocket socket)
        {
            var res = nng_ctx_open(out var ctx, socket.NativeNngStruct);

            if (res != 0)
            {
                return(NngResult <INngCtx> .Fail(res));
            }
            return(NngResult <INngCtx> .Ok(new NngCtx { NativeNngStruct = ctx }));
        }
Example #12
0
        public static NngResult <INngAio> Create(AioCallback callback)
        {
            // Make a copy to ensure an automatically created delegate doesn't get GC'd while native code
            // is still using it:
            // https://stackoverflow.com/questions/6193711/call-has-been-made-on-garbage-collected-delegate-in-c
            // https://docs.microsoft.com/en-us/cpp/dotnet/how-to-marshal-callbacks-and-delegates-by-using-cpp-interop
            var res = nng_aio_alloc(out var aioHandle, callback, IntPtr.Zero);

            return(NngResult <INngAio> .OkThen(res, () => new NngAio { aioHandle = aioHandle, gcHandle = GCHandle.Alloc(callback) }));
        }
Example #13
0
        public static NngResult <INngStreamListener> Alloc(string addr)
        {
            var listener = nng_stream_listener.Null;
            var res      = nng_stream_listener_alloc(ref listener, addr);

            return(NngResult <INngStreamListener> .OkThen(res, () => new StreamListener()
            {
                listener = listener
            }));
        }
Example #14
0
        /// <summary>
        /// Create a respondent socket
        /// </summary>
        /// <returns>The open.</returns>
        public static NngResult <IRespondentSocket> Open()
        {
            int res = nng_respondent0_open(out var socket);

            if (res != 0)
            {
                return(NngResult <IRespondentSocket> .Fail(res));
            }
            return(NngResult <IRespondentSocket> .Ok(new RespondentSocket { NngSocket = socket }));
        }
Example #15
0
        public static NngResult <INngStreamDialer> Alloc(string addr)
        {
            var dialer = nng_stream_dialer.Null;
            var res    = nng_stream_dialer_alloc(ref dialer, addr);

            return(NngResult <INngStreamDialer> .OkThen(res, () => new StreamDialer()
            {
                dialer = dialer
            }));
        }
Example #16
0
        /// <summary>
        /// Create a pair socket
        /// </summary>
        /// <returns>The open.</returns>
        public static NngResult <IPairSocket> Open()
        {
            int res = nng_pair1_open(out var socket);

            if (res != 0)
            {
                return(NngResult <IPairSocket> .Fail(res));
            }
            return(NngResult <IPairSocket> .Ok(new Pair1Socket { NngSocket = socket }));
        }
Example #17
0
 public NngResult <T> Into <T>(T value)
 {
     if (IsOk())
     {
         return(NngResult <T> .Ok(value));
     }
     else
     {
         return(IntoErr <T>());
     }
 }
Example #18
0
 /// <summary>
 /// Create a result of another type.  If this is a success uses.
 /// If this is a fail, contains the same fail value.
 /// </summary>
 /// <param name="func">Function to convert success value to another type</param>
 /// <typeparam name="T"></typeparam>
 /// <returns></returns>
 public NngResult <T> Into <T>(Func <T> func)
 {
     if (IsOk())
     {
         return(NngResult <T> .Ok(func()));
     }
     else
     {
         return(IntoErr <T>());
     }
 }
Example #19
0
        /// <summary>
        /// Create a reply socket
        /// </summary>
        /// <returns>The open.</returns>
        public static INngResult <IRepSocket> Open()
        {
            var res = nng_rep0_open(out var socket);

            if (res != 0)
            {
                return(NngResult.Fail <IRepSocket>(res));
            }
            return(NngResult.Ok <IRepSocket>(new RepSocket {
                NngSocket = socket
            }));
        }
Example #20
0
        /// <summary>
        /// Create a publish socket
        /// </summary>
        /// <returns>The open.</returns>
        public static INngResult <IPubSocket> Open()
        {
            int res = nng_pub0_open(out var socket);

            if (res != 0)
            {
                return(NngResult.Fail <IPubSocket>(res));
            }
            return(NngResult.Ok <IPubSocket>(new PubSocket {
                NngSocket = socket
            }));
        }
Example #21
0
        public NngResult <IMemory> RecvZeroCopy(Defines.NngFlag flags = default)
        {
            // Unconditionally set NNG_FLAG_ALLOC for "zero-copy" receive
            flags = flags | Defines.NngFlag.NNG_FLAG_ALLOC;
            var ptr  = IntPtr.Zero;
            var size = UIntPtr.Zero;
            var res  = nng_recv(NngSocket, ref ptr, ref size, flags);

            return(NngResult <IMemory> .OkThen(res, () => {
                System.Diagnostics.Debug.Assert(ptr != default && size != default);
                return Alloc.Create(ptr, size);
            }));
        }
Example #22
0
        public static INngResult <IRepReqAsyncContext <T> > Create(IMessageFactory <T> factory, ISocket socket)
        {
            var ctx = new RepAsyncCtx <T>();
            var res = ctx.Init(factory, socket, ctx.callback);

            if (res == 0)
            {
                // Start receive loop
                ctx.callback(IntPtr.Zero);
                return(NngResult.Ok <IRepReqAsyncContext <T> >(ctx));
            }
            else
            {
                return(NngResult.Fail <IRepReqAsyncContext <T> >(res));
            }
        }
Example #23
0
        public static NngResult <INngStream> From(INngAio aio)
        {
            var ptr = aio.GetOutput(0);

            if (ptr == IntPtr.Zero)
            {
                return(NngResult <INngStream> .Fail(-1));
            }
            else
            {
                var stream = new Stream {
                    stream = new nng_stream(ptr)
                };
                return(NngResult <INngStream> .Ok(stream));
            }
        }
Example #24
0
 public NngResult <UIntPtr> Recv(ref IMemory buffer, Defines.NngFlag flags = default)
 {
     if (flags.HasFlag(Defines.NngFlag.NNG_FLAG_ALLOC))
     {
         var res = RecvZeroCopy(flags);
         return(res.Into <UIntPtr>(() => res.Ok().Length));
     }
     else
     {
         if (buffer == null || buffer.Length == UIntPtr.Zero)
         {
             return(NngResult <UIntPtr> .Err(Defines.NngErrno.EMSGSIZE));
         }
         var ptr  = buffer.Ptr;
         var size = buffer.Length;
         var res  = nng_recv(NngSocket, ref ptr, ref size, flags);
         return(NngResult <UIntPtr> .OkIfZero(res, size));
     }
 }
Example #25
0
        public static NngResult <IReqRepAsyncContext <T> > Create(IMessageFactory <T> factory, ISocket socket)
        {
            var context = new ReqAsyncCtx <T> {
                Factory = factory, Socket = socket
            };
            var res = context.InitAio();

            if (res == 0)
            {
                //TODO: when get default interface methods in C#8 move this to ICtx
                var ctx = AsyncCtx.Create(socket);
                if (ctx.IsOk())
                {
                    context.Ctx = ctx.Ok();
                    return(NngResult <IReqRepAsyncContext <T> > .Ok(context));
                }
                return(NngResult <IReqRepAsyncContext <T> > .Err(ctx.Err()));
            }
            return(NngResult <IReqRepAsyncContext <T> > .Fail(res));
        }
Example #26
0
        public static NngResult <ISurveyorAsyncContext <T> > Create(IMessageFactory <T> factory, ISendRecvSocket socket)
        {
            var context = new SurveyAsyncContext <T> {
                Factory = factory, socket = socket
            };
            var res = context.InitAio();

            if (res.IsOk())
            {
                //TODO: when get default interface methods in C#8 move this to ICtx
                var ctx = NngCtx.Create(socket);
                if (ctx.IsOk())
                {
                    context.Ctx = ctx.Ok();
                    return(NngResult <ISurveyorAsyncContext <T> > .Ok(context));
                }
                return(NngResult <ISurveyorAsyncContext <T> > .Err(ctx.Err()));
            }
            else
            {
                return(NngResult <ISurveyorAsyncContext <T> > .Fail(res.Err()));
            }
        }
Example #27
0
        /// <summary>
        /// Create a subscribe socket
        /// </summary>
        /// <returns>The open.</returns>
        public static NngResult <ISubSocket> Open()
        {
            var res = nng_sub0_open(out var socket);

            return(NngResult <ISubSocket> .OkThen(res, () => new SubSocket { NativeNngStruct = socket }));
        }
Example #28
0
 public static void TrySetNngResult <T>(this CancellationTokenTaskSource <NngResult <T> > socket, T message)
 {
     socket.TrySetResult(NngResult <T> .Ok(message));
 }
Example #29
0
 public static void TrySetNngResult <T>(this TaskCompletionSource <NngResult <T> > socket, T message)
 {
     socket.TrySetResult(NngResult <T> .Ok(message));
 }
Example #30
0
        public static NngResult <IStatRoot> GetStatSnapshot()
        {
            var res = nng_stats_get(out nng_stat statsp);

            return(NngResult <IStatRoot> .OkThen(res, () => new StatRoot { NngStat = statsp }));
        }