Ejemplo n.º 1
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);
            }
        }
Ejemplo n.º 2
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;
            }
        }