Exemple #1
0
        public void GetSetOpt()
        {
            var url = UrlIpc();

            using (var rep = Factory.ReplierCreate(url).Unwrap().CreateAsyncContext(Factory).Unwrap() as ICtx)
            {
                // Get a value, set a new value, get back the new value
                Assert.Equal(0, rep.GetCtxOpt(NNG_OPT_RECVTIMEO, out nng_duration recvTimeout));
                var newResvTimeout = new nng_duration(recvTimeout);
                ++newResvTimeout.TimeMs;
                Assert.Equal(0, rep.SetCtxOpt(NNG_OPT_RECVTIMEO, newResvTimeout));
                rep.GetCtxOpt(NNG_OPT_RECVTIMEO, out nng_duration nextRecvTimeout);
                Assert.Equal(newResvTimeout.TimeMs, nextRecvTimeout.TimeMs);
            }
        }
Exemple #2
0
        public void GetSetOpt()
        {
            var url = UrlIpc();

            using (var socket = Factory.ReplierOpen().ThenListen(url).Unwrap())
                using (var rep = socket.CreateAsyncContext(Factory).Unwrap())
                {
                    var ctx = (rep as ICtx).Ctx;
                    // Get a value, set a new value, get back the new value
                    Assert.Equal(0, ctx.GetOpt(NNG_OPT_RECVTIMEO, out nng_duration recvTimeout));
                    var newResvTimeout = new nng_duration(recvTimeout);
                    ++newResvTimeout.TimeMs;
                    Assert.Equal(0, ctx.SetOpt(NNG_OPT_RECVTIMEO, newResvTimeout));
                    ctx.GetOpt(NNG_OPT_RECVTIMEO, out nng_duration nextRecvTimeout);
                    Assert.Equal(newResvTimeout.TimeMs, nextRecvTimeout.TimeMs);
                }
        }
Exemple #3
0
 public static extern int nng_dialer_setopt_ms(nng_dialer dialer, string name, nng_duration value);
Exemple #4
0
 public int GetOpt(string name, out nng_duration data)
 {
     return(nng_getopt_ms(NngSocket, name, out data));
 }
Exemple #5
0
 public int GetOpt(string name, out nng_duration data)
 => nng_ctx_getopt_ms(NngCtx, name, out data);
Exemple #6
0
 public int GetOpt(string name, out nng_duration data)
 => nng_stream_listener_get_ms(NngStreamListener, name, out data);
Exemple #7
0
 public int GetOpt(string name, out nng_duration data)
 => nng_stream_get_ms(NngStream, name, out data);
Exemple #8
0
 public int GetOpt(string name, out nng_duration data)
 {
     return(nng_ctx_getopt_ms(NngCtx, name, out data));
 }
Exemple #9
0
 public static extern int nng_ctx_getopt_ms(nng_ctx ctx, string name, out nng_duration data);
Exemple #10
0
 public static extern int nng_pipe_getopt_ms(nng_pipe pipe, string opt, out nng_duration val);
Exemple #11
0
 public void SetTimeout(nng_duration timeout)
 {
     nng_aio_set_timeout(aioHandle, timeout);
 }
Exemple #12
0
 public int SetOpt(string name, nng_duration data)
 => nng_ctx_setopt_ms(NativeNngStruct, name, data);
Exemple #13
0
 public static extern Int32 nng_getopt_ms(nng_socket socket, string name, out nng_duration value);
Exemple #14
0
 public int SetOpt(string name, nng_duration data)
 => nng_setopt_ms(NngSocket, name, data);
Exemple #15
0
 public int GetOpt(string name, out nng_duration data)
 => nng_getopt_ms(NngSocket, name, out data);
Exemple #16
0
 public int SetOpt(string name, nng_duration data)
 => nng_dialer_setopt_ms(NngDialer, name, data);
        async Task DoContexts(string url)
        {
            const int NumSurveyors        = 1;
            const int NumResponders       = 2;
            var       readyToDial         = new AsyncBarrier(NumSurveyors + NumResponders);
            var       readyToSend         = new AsyncBarrier(NumSurveyors + NumResponders);
            var       ready               = readyToSend.WaitAsync();
            var       numSurveyorReceive  = new AsyncCountdownEvent(NumSurveyors);
            var       numResponderReceive = new AsyncCountdownEvent(NumSurveyors);

            using (var surveySocket = Factory.SurveyorOpen().ThenListen(url).Unwrap())
                using (var respondSocket = Factory.RespondentOpen().ThenDial(url).Unwrap())
                {
                    var duration = new nng_duration {
                        TimeMs = DefaultTimeoutMs
                    };
                    // Send() is not cancelable so need it to timeout
                    surveySocket.SetOpt(nng.Native.Defines.NNG_OPT_SENDTIMEO, new nng_duration {
                        TimeMs = 50
                    });
                    surveySocket.SetOpt(nng.Native.Defines.NNG_OPT_RECVTIMEO, nng_duration.Infinite);
                    surveySocket.SetOpt(Native.Defines.NNG_OPT_SURVEYOR_SURVEYTIME, nng_duration.Infinite);
                    respondSocket.SetOpt(nng.Native.Defines.NNG_OPT_SENDTIMEO, new nng_duration {
                        TimeMs = 50
                    });

                    var cts   = new CancellationTokenSource();
                    var tasks = new List <Task>();
                    for (var i = 0; i < NumSurveyors; ++i)
                    {
                        var id   = i;
                        var task = Task.Run(async() =>
                        {
                            using (var ctx = surveySocket.CreateAsyncContext(Factory).Unwrap())
                            {
                                ctx.Ctx.SetOpt(Native.Defines.NNG_OPT_RECVTIMEO, nng_duration.Infinite);
                                ctx.Ctx.SetOpt(Native.Defines.NNG_OPT_SURVEYOR_SURVEYTIME, nng_duration.Infinite);

                                await readyToDial.SignalAndWait();
                                await readyToSend.SignalAndWait();

                                // Send survey and receive responses
                                var survey = Factory.CreateMessage();
                                var val    = (uint)rng.Next();
                                survey.Append(val);
                                //Assert.Equal(0, survey.Header.Append((uint)(0x8000000 | i))); // Protocol header contains "survey ID"
                                (await ctx.Send(survey)).Unwrap();
                                while (!cts.IsCancellationRequested)
                                {
                                    try
                                    {
                                        var response = (await ctx.Receive(cts.Token)).Unwrap();
                                        response.Trim(out uint respVal);
                                        Assert.Equal(val, respVal);
                                        if (numSurveyorReceive.Signal() == 0)
                                        {
                                            break;
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        Console.Error.WriteLine(ex.ToString());
                                        throw ex;
                                    }
                                }
                            }
                        });
                        tasks.Add(task);
                    }

                    for (var i = 0; i < NumResponders; ++i)
                    {
                        var id   = i;
                        var task = Task.Run(async() =>
                        {
                            await readyToDial.SignalAndWait();
                            using (var ctx = respondSocket.CreateAsyncContext(Factory).Unwrap())
                            {
                                // Receive survey and send response
                                try
                                {
                                    // Receive is async, give it a chance to start before signaling we are ready.
                                    // This to avoid race where surveyor sends before it actually starts receiving
                                    var recvFuture = ctx.Receive(cts.Token);
                                    await WaitShort();
                                    await readyToSend.SignalAndWait();
                                    var survey = (await recvFuture).Unwrap();
                                    await Task.Delay(10); // Make sure surveyor has a chance to start receiving
                                    (await ctx.Send(survey)).Unwrap();
                                    numResponderReceive.Signal();
                                    await numSurveyorReceive.WaitAsync();
                                    cts.Cancel(); // Cancel any responders still receiving
                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine(ex.ToString());
                                    throw ex;
                                }
                            }
                        });
                        tasks.Add(task);
                    }
                    await Task.WhenAny(ready, Task.WhenAll(tasks));

                    await Util.CancelAfterAssertwait(tasks, cts);

                    Assert.Equal(0, numSurveyorReceive.Count);
                    Assert.Equal(0, numResponderReceive.Count);
                }
        }
Exemple #18
0
 public int GetOpt(string name, out nng_duration data)
 => nng_listener_getopt_ms(NngListener, name, out data);
Exemple #19
0
 public static extern int nng_ctx_setopt_ms(nng_ctx ctx, string name, nng_duration value);
Exemple #20
0
 public int SetOpt(string name, nng_duration data)
 => nng_listener_setopt_ms(NngListener, name, data);
Exemple #21
0
 public int SetOpt(string name, nng_duration data)
 {
     return(nng_ctx_setopt_ms(NngCtx, name, data));
 }
Exemple #22
0
 public static extern int nng_listener_getopt_ms(nng_listener listener, string name, out nng_duration data);
Exemple #23
0
 public int SetOpt(string name, nng_duration data)
 => nng_stream_set_ms(NngStream, name, data);
Exemple #24
0
 public static extern int nng_listener_setopt_ms(nng_listener listener, string name, nng_duration value);
Exemple #25
0
 public int SetOpt(string name, nng_duration data)
 => nng_stream_listener_set_ms(NngStreamListener, name, data);
Exemple #26
0
 public int GetOpt(string name, out nng_duration data)
 => nng_pipe_getopt_ms(NativeNngStruct, name, out data);
Exemple #27
0
 public int SetOpt(string name, nng_duration data)
 => nng_ctx_setopt_ms(NngCtx, name, data);
Exemple #28
0
 public int GetOpt(string name, out nng_duration data)
 => nng_pipe_getopt_ms(NngPipe, name, out data);
Exemple #29
0
 public int SetOpt(string name, nng_duration data)
 {
     return(nng_setopt_ms(NngSocket, name, data));
 }
Exemple #30
0
 public int GetOpt(string name, out nng_duration data)
 => nng_dialer_getopt_ms(NngDialer, name, out data);