Esempio n. 1
0
        public virtual bool Connect(bool shouldStartReceiving)
        {
            lock (_connectionMutex)
            {
                if (_formatter == null)
                {
                    throw new Exception("Channel formatter is not specified");
                }

                if (_eventListener == null)
                {
                    throw new Exception("There is no channel event listener specified");
                }

                try
                {
                    if (_connection == null)
                    {
                        _connection = new TcpConnection(_sessionType);

                        _connection.SetDataListener(this);

                        if (!string.IsNullOrEmpty(_bindIP))
                        {
                            _connection.Bind(_bindIP);
                        }
                    }

                    if (!_connection.IsConnected)
                    {
                        _connection.Connect(_serverIP, _port);
                    }
                    _forcedDisconnected = false;

                    if (shouldStartReceiving)
                    {
                        StartReceiverThread();
                    }
                    return(true);
                }
                catch (ConnectionException ce)
                {
                    if (_traceProvider != null)
                    {
                        _traceProvider.TraceError(Name + ".Connect", ce.ToString());
                    }
                    throw new ChannelException(ce.Message, ce);
                }
            }
            return(false);
        }
Esempio n. 2
0
        public bool Connect()
        {
            lock (this)
            {
                if (_formatter == null)
                {
                    throw new Exception("Channel formatter is not specified");
                }

                if (_eventListener == null)
                {
                    throw new Exception("There is no channel event listener specified");
                }

                try
                {
                    if (_connection == null)
                    {
                        _connection = new TcpConnection();

                        if (!string.IsNullOrEmpty(_bindIP))
                        {
                            _connection.Bind(_bindIP);
                        }

                        _connection.Connect(_serverIP, _port);

                        _receiverThread = new Thread(new ThreadStart(Run));
                        _receiverThread.IsBackground = true;
                        _receiverThread.Start();
                        return(true);
                    }
                }
                catch (ConnectionException ce)
                {
                    if (_traceProvider != null)
                    {
                        _traceProvider.TraceError(Name + ".Connect", ce.ToString());
                    }
                    throw new ChannelException(ce.Message, ce);
                }
            }
            return(false);
        }