/// <summary>
 /// Custom constructor.
 /// </summary>
 /// <param name="throttleLimit">throttleLimit the maximum number of results that can be expected at any given time.</param>
 public ResultHolderResultQueue(int throttleLimit)
 {
     _results = new PriorityBlockingQueue <IResultHolder>(throttleLimit, new ResultHolderComparer());
     _waits   = new Semaphore(throttleLimit, throttleLimit);
 }
Пример #2
0
 internal TimeBasedTaskScheduler(SystemNanoClock clock, ThreadPoolManager pools)
 {
     this._clock   = clock;
     this._pools   = pools;
     _delayedTasks = new PriorityBlockingQueue <ScheduledJobHandle>(42, _deadlineComparator);
 }
Пример #3
0
        public bool Connect(string host, int port, long timeout)
        {
            lock (_InitializationLock)
            {
                if (IsConnected())
                    Close();

                try
                {
                    _Socket = new TcpClient();

                    _RequestQueue = new PriorityBlockingQueue<IRequest>();
                    _RequestQueue.Start();
                    _OngoingRequests = new ConcurrentDictionary<int, IRequest>();

                    IAsyncResult result = _Socket.BeginConnect(host, port, null, null);
                    bool connected = result.AsyncWaitHandle.WaitOne((int)timeout);

                    if (connected)
                    {
                        _Socket.EndConnect(result);
                        _Socket.ReceiveTimeout = (int)timeout;

                        //notify connection change
                        if (null != _ConnectionListener)
                            _ConnectionListener.OnGazeApiConnectionStateChanged(_Socket.Connected);

                        _IncomingStreamHandler = new IncomingStreamHandler(_Socket, _ResponseListener, _ConnectionListener, _OngoingRequests, this);
                        _IncomingStreamHandler.Start();

                        _OutgoingStreamHandler = new OutgoingStreamHandler(_Socket, _RequestQueue, _OngoingRequests, _ConnectionListener, this);
                        _OutgoingStreamHandler.Start();

                        return true;
                    }
                }
                catch (SocketException se)
                {
                    Debug.WriteLine("Unable to open socket. Is Tracker Server running? Exception: " + se.Message);
                }
                catch (Exception e)
                {
                    Debug.WriteLine("Exception while establishing socket connection. Is Tracker Server running? Exception: " + e.Message);
                }

                Close();

                return false;
            }
        }
Пример #4
0
 public OutgoingStreamHandler(
     TcpClient socket,
     PriorityBlockingQueue<IRequest> queue,
     ConcurrentDictionary<int, IRequest> requests, 
     IGazeApiConnectionListener connectionListener, 
     GazeApiManager networkLayer)
 {
     _Socket = socket;
     _ConnectionListener = connectionListener;
     _NetworkLayer = networkLayer;
     _OutQueue = queue;
     _OngoingRequests = requests;
 }
Пример #5
0
        public void Close()
        {
            lock (_InitializationLock)
            {
                try
                {
                    if (null != _Socket)
                    {
                        _Socket.Close();
                        _Socket = null;
                    }

                    if (null != _IncomingStreamHandler)
                    {
                        _IncomingStreamHandler.Stop();
                        _IncomingStreamHandler = null;
                    }

                    if (null != _OutgoingStreamHandler)
                    {
                        _OutgoingStreamHandler.Stop();
                        _OutgoingStreamHandler = null;
                    }

                    //notify connection change
                    if (null != _ConnectionListener)
                        _ConnectionListener.OnGazeApiConnectionStateChanged(false);

                    //We cancel queued requests
                    if (null != _RequestQueue)
                    {
                        CancelAllRequests();
                        _RequestQueue.Stop();
                    }
                    _RequestQueue = null;

                    //We make sure we cancel currently ongoing requests
                    if (null != _OngoingRequests)
                    {
                        IEnumerator<KeyValuePair<int, IRequest>> reqs = _OngoingRequests.GetEnumerator();

                        while(reqs.MoveNext())
                        {
                            reqs.Current.Value.Cancel();
                        }
                        _OngoingRequests.Clear();
                    }
                    _OngoingRequests = null;

                }
                catch (Exception e)
                {
                    Debug.WriteLine("Error closing socket: " + e.Message);
                }
            }
        }