Exemple #1
0
        public void ReceiveMessage(IChannelMessage response)
        {
            IResponse           _response       = (IResponse)response;
            RequestResponsePair reqResponsePair = _requests[_response.RequestId] as RequestResponsePair;
            bool unregister = false;

            if (reqResponsePair != null)
            {
                lock (reqResponsePair)
                {
                    if (reqResponsePair != null)
                    {
                        reqResponsePair.Response = _response;
                        System.Threading.Monitor.Pulse(reqResponsePair);

                        if (reqResponsePair.Listener != null)
                        {
                            reqResponsePair.Listener.OnResponseReceived(response);
                            unregister = true;
                        }
                    }
                }

                if (unregister)
                {
                    lock (_lock)
                    {
                        _requests.Remove(_response.RequestId);
                    }
                }
            }
        }
Exemple #2
0
        public void ChannelDisconnected(string reason)
        {
            lock (_lock)
            {
                Hashtable             requestClone = _requests.Clone() as Hashtable;
                IDictionaryEnumerator ide          = requestClone.GetEnumerator();

                while (ide.MoveNext())
                {
                    RequestResponsePair reqRspPair = ide.Value as RequestResponsePair;

                    if (!reqRspPair.RequestSentOverChannel)
                    {
                        continue;
                    }

                    lock (reqRspPair)
                    {
                        if (_resendRequestOnChannelDisconnect)
                        {
                            //resend the request when channel is disconnected
                            try
                            {
                                if (_channel != null)
                                {
                                    _channel.SendMessage(reqRspPair.Request);
                                }
                            }
                            catch (ChannelException ce)
                            {
                                reqRspPair.ChannelException = ce;
                                System.Threading.Monitor.PulseAll(reqRspPair);

                                if (reqRspPair.Listener != null)
                                {
                                    reqRspPair.Listener.OnError(reqRspPair.ChannelException);
                                }
                            }
                        }
                        else
                        {
                            reqRspPair.ChannelException = new ChannelException(reason);
                            System.Threading.Monitor.PulseAll(reqRspPair);

                            if (reqRspPair.Listener != null)
                            {
                                reqRspPair.Listener.OnError(reqRspPair.ChannelException);
                            }
                        }
                    }
                }
            }
        }
Exemple #3
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);
        }