Exemple #1
0
        public void ReceiveResponseAsync <T>(string requestId,
                                             IResponseListener <T> responseListener) where T : class
        {
            if (responseListener == null)
            {
                throw new ArgumentException("responseListener must be not null");
            }

            Task.Run(() => responseListener.ResponseReceived(requestId, ReceiveResponseAsync <T>(requestId).Result));
        }
Exemple #2
0
        private object SendRequest(IRequest request, bool waitForResponse, IResponseListener listener)
        {
            IResponse response = null;

            if (request.NoResponse)
            {
                _channel.SendMessage(request);

                return(response);
            }

            request.RequestId = GenerateRequestId();
            bool lockReacquired             = false;
            RequestResponsePair reqRespPair = new RequestResponsePair();

            reqRespPair.Listener = listener;

            lock (_lock)
            {
                reqRespPair.Request = request;

                if (!_requests.Contains(request.RequestId))
                {
                    _requests.Add(request.RequestId, reqRespPair);
                }
            }
            bool unregiserRequst = waitForResponse;

            lock (reqRespPair)
            {
                try
                {
                    _channel.SendMessage(request);
                    reqRespPair.RequestSentOverChannel = true;


                    if (waitForResponse)
                    {
                        lockReacquired = System.Threading.Monitor.Wait(reqRespPair, _requestTimeout);
                    }
                    else
                    {
                        return(null);
                    }
                }
                catch (ChannelException e)
                {
                    if (unregiserRequst)
                    {
                        lock (_lock)
                        {
                            _requests.Remove(request.RequestId);
                        }
                    }

                    if (listener != null)
                    {
                        listener.OnError(e);
                    }
                    throw;
                }
                finally
                {
                    if (unregiserRequst)
                    {
                        lock (_lock)
                        {
                            _requests.Remove(request.RequestId);
                        }
                    }
                }
            }

            if (!lockReacquired)
            {
                throw new Alachisoft.NosDB.Common.Exceptions.TimeoutException("Request has timed out. Did not receive response from " + _channel.Server);
            }

            if (reqRespPair.ChannelException != null)
            {
                throw reqRespPair.ChannelException;
            }

            response = reqRespPair.Response as IResponse;

            if (response != null && response.Error != null)
            {
                throw response.Error;
            }

            return(response);
        }
Exemple #3
0
 /// <summary>
 /// ctor
 /// </summary>
 /// <param name="comListener"></param>
 public NativeResponseListener(IResponseListener comListener, Session session)
 {
     mComListener = comListener;
     mSession     = session;
 }