Ejemplo n.º 1
0
        public void Request(Request request)
        {
            if (!connected)
            {
                connected = Connect();
                if (!connected)
                {
                    throw new ApplicationException("Pool not connected.");
                }
            }
            PooledSocket socket = Acquire();

            if (socket != null)
            {
                socket.Send(request);
            }
            else
            {
                Log.WriteLog("NTSock: Acquire null socket");
            }
        }
Ejemplo n.º 2
0
        public void BeginRequest(Request request, WaitCallback callback, object context)
        {
            if (!connected)
            {
                connected = Connect();
                if (!connected)
                {
                    throw new ApplicationException("Pool not connected.");
                }
            }
            var socket = Acquire();

            if (socket != null)
            {
                if (callback != null)
                {
                    request.Id = Interlocked.Increment(ref mailId);
                    waitingCallbackLock.EnterWriteLock();
                    try
                    {
                        waitingCallback.Add(request.Id, callback);
                        if (context != null)
                        {
                            waitingContext.Add(request.Id, context);
                        }
                    }
                    finally
                    {
                        waitingCallbackLock.ExitWriteLock();
                    }
                }
                socket.Send(request);
            }
            else
            {
                Log.WriteLog("NTSock: Acquire null socket");
            }
        }
Ejemplo n.º 3
0
        public T Request <T>(Request request)
        {
            if (!connected)
            {
                connected = Connect();
                if (!connected)
                {
                    throw new ApplicationException("Pool not connected.");
                }
            }
            var socket = Acquire();

            if (socket != null)
            {
                request.Id = Interlocked.Increment(ref mailId);
                ManualResetEvent waitEvent;
                waitHandlesLock.EnterWriteLock();
                try
                {
                    waitEvent = new ManualResetEvent(false);
                    waitHandles.Add(request.Id, new SynchResult()
                    {
                        WaitEvent = waitEvent
                    });
                }
                finally
                {
                    waitHandlesLock.ExitWriteLock();
                }
                socket.Send(request);
                waitEvent.WaitOne(waitTimeSpan, false);
                waitHandlesLock.EnterWriteLock();
                try
                {
                    SynchResult result;
                    waitHandles.TryGetValue(request.Id, out result);
                    if (result != null)
                    {
                        waitHandles.Remove(request.Id);
                        var response = result.Response;
                        if (response != null)
                        {
                            if (response is ExceptionResponse)
                            {
                                throw new ApplicationException(((ExceptionResponse)response).Exception);
                            }
                            return(response.GetResult() is T ? (T)response.GetResult() : default(T));
                        }
                        else
                        {
                            return(default(T));
                        }
                    }
                    else
                    {
                        Log.WriteLog("Cannot find result for request: " + request);
                        return(default(T));
                    }
                }
                finally
                {
                    waitHandlesLock.ExitWriteLock();
                }
            }
            else
            {
                Log.WriteLog("NTSock: Acquire null socket");
            }
            return(default(T));
        }
Ejemplo n.º 4
0
		internal Response Excute(Request request)
		{
			var requestType = request.requestDescription;
			IExecutor executor = null;
			servicesLock.EnterReadLock();
			try
			{
				methodMaps.TryGetValue(requestType, out executor);
			}
			finally
			{
				servicesLock.ExitReadLock();
			}
			Response response;
			if (executor != null)
			{
				try
				{
					response = executor.Execute(request);
					response.Id = request.Id;	
				}
				catch(Exception e)
				{
					executorLog.WriteLog("Exception when invoke type : " + executor.GetType().FullName + ", with exception: " + e);
					response = new ExceptionResponse(request.Id, e);
				}
			}
			else
			{
				response = new ExceptionResponse(request.Id, new ServiceNotFoundException(request.ToString()));
			}
			return response;
		}