Exemple #1
0
 private void ThrowOnNonSuccess(XtResultBase xtResult)
 {
     if (xtResult.IsSuccess == false)
     {
         throw ZeroMqXtSocketException.FromException(xtResult.Exception);
     }
 }
Exemple #2
0
        ///<summary>
        /// necessary indirection for the responsehandler to be used in sync or async fashion
        ///</summary>
        private XtResult SetupResponder <T, TResult>(ResponseHandler <T, TResult> handler, CancellationToken token)
            where T : class, new()
            where TResult : class
        {
            lock (concurrencyToken)
            {
                if (responderIsSetup)
                {
                    throw new ZeroMqXtSocketException("Responder for this instance of Socket exists already. Use a new instance for each server!");
                }
                responderIsSetup   = true;
                respondingIsActive = true;
            }

            poller         = new NetMQ.NetMQPoller();
            responseSocket = new ResponseSocket();
            // handle notifies when the server is set up
            eventHandle = new ManualResetEvent(false);
            // create a new background thread with the response callback

            Exception faultingException = null;

            _ = Task.Run(() =>
            {
                try
                {
                    responseSocket.Bind(_configuration.Address());

                    // add to poller and register handler
                    poller.Add(responseSocket);
                    receiveHandler = async(s, e) => await ResponseHandlerCallback(responseSocket, handler, token);
                    responseSocket.ReceiveReady += receiveHandler;

                    poller.RunAsync();
                }
                catch (Exception exception)
                {
                    faultingException = exception;
                    _configuration.Logger.Log(new ErrorLogMsg(exception.GetType().Name + "-" + exception.Message));
                    Dispose();
                }
                finally
                {
                    // open resetevent after binding to the socket and when the poller is started
                    eventHandle.Set();
                }
            }, token);

            // wait for the Set inside the background thread so we can know at the calling client that the server is set up properly
            eventHandle.WaitOne();
            return(faultingException == null
                   ? XtResult.Success("setup-response")
                   : XtResult.Failed(ZeroMqXtSocketException.FromException(faultingException), "setup-response"));
        }