Inheritance: System.Net.Sockets.TcpClient
Beispiel #1
0
        /// <overloads>this method has 2 overloads</overloads>
        /// <summary>
        /// Connects to the specified server and port, when the connection fails
        /// the next server in the list will be used.
        /// </summary>
        /// <param name="addresslist">List of servers to connect to</param>
        /// <param name="port">Portnumber to connect to</param>
        /// <exception cref="CouldNotConnectException">The connection failed</exception>
        /// <exception cref="AlreadyConnectedException">If there is already an active connection</exception>
        public void Connect(string[] addresslist, int port)
        {
            if (_IsConnected)
            {
                throw new AlreadyConnectedException("Already connected to: " + Address + ":" + Port);
            }

            _ConnectTries++;
#if LOG4NET
            Logger.Connection.Info(String.Format("connecting... (attempt: {0})",
                                                 _ConnectTries));
#endif
            _AddressList = (string[])addresslist.Clone();
            _Port        = port;

            if (OnConnecting != null)
            {
                OnConnecting(this, EventArgs.Empty);
            }
            try {
                System.Net.IPAddress ip = System.Net.Dns.Resolve(Address).AddressList[0];
                _TcpClient         = new IrcTcpClient();
                _TcpClient.NoDelay = true;
                _TcpClient.Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, 1);
                // set timeout, after this the connection will be aborted
                _TcpClient.ReceiveTimeout = _SocketReceiveTimeout * 1000;
                _TcpClient.SendTimeout    = _SocketSendTimeout * 1000;
                _TcpClient.Connect(ip, port);

                Stream stream = _TcpClient.GetStream();
#if NET_2_0
                if (_UseSsl)
                {
                    SslStream sslStream = new SslStream(stream, false, delegate {
                        return(true);
                    });
                    sslStream.AuthenticateAsClient(Address);
                    stream = sslStream;
                }
#endif
                _Reader = new StreamReader(stream, _Encoding);
                _Writer = new StreamWriter(stream, _Encoding);

                if (_Encoding.GetPreamble().Length > 0)
                {
                    // HACK: we have an encoding that has some kind of preamble
                    // like UTF-8 has a BOM, this will confuse the IRCd!
                    // Thus we send a \r\n so the IRCd can safely ignore that
                    // garbage.
                    _Writer.WriteLine();
                }

                // Connection was succeful, reseting the connect counter
                _ConnectTries = 0;

                // updating the connection error state, so connecting is possible again
                IsConnectionError = false;
                _IsConnected      = true;

                // lets power up our threads
                _ReadThread.Start();
                _WriteThread.Start();
                _IdleWorkerThread.Start();

#if LOG4NET
                Logger.Connection.Info("connected");
#endif
                if (OnConnected != null)
                {
                    OnConnected(this, EventArgs.Empty);
                }
            } catch (Exception e) {
                if (_Reader != null)
                {
                    try {
                        _Reader.Close();
                    } catch (ObjectDisposedException) {
                    }
                }
                if (_Writer != null)
                {
                    try {
                        _Writer.Close();
                    } catch (ObjectDisposedException) {
                    }
                }
                if (_TcpClient != null)
                {
                    _TcpClient.Close();
                }
                _IsConnected      = false;
                IsConnectionError = true;

#if LOG4NET
                Logger.Connection.Info("connection failed: " + e.Message);
#endif
                if (_AutoRetry &&
                    _ConnectTries <= 3)
                {
                    if (OnAutoConnectError != null)
                    {
                        OnAutoConnectError(this, new AutoConnectErrorEventArgs(Address, Port, e));
                    }
#if LOG4NET
                    Logger.Connection.Debug("delaying new connect attempt for " + _AutoRetryDelay + " sec");
#endif
                    Thread.Sleep(_AutoRetryDelay * 1000);
                    _NextAddress();
                    Connect(_AddressList, _Port);
                }
                else
                {
                    throw new CouldNotConnectException("Could not connect to: " + Address + ":" + Port + " " + e.Message, e);
                }
            }
        }
Beispiel #2
0
        /// <overloads>this method has 2 overloads</overloads>
        /// <summary>
        /// Connects to the specified server and port, when the connection fails
        /// the next server in the list will be used.
        /// </summary>
        /// <param name="addresslist">List of servers to connect to</param>
        /// <param name="port">Portnumber to connect to</param>
        /// <exception cref="CouldNotConnectException">The connection failed</exception>
        /// <exception cref="AlreadyConnectedException">If there is already an active connection</exception>
        public void Connect(string[] addresslist, int port)
        {
            if (_IsConnected) {
                throw new AlreadyConnectedException("Already connected to: " + Address + ":" + Port);
            }

            _ConnectTries++;
            #if LOG4NET
            Logger.Connection.Info(String.Format("connecting... (attempt: {0})",
                                                 _ConnectTries));
            #endif
            _AddressList = (string[])addresslist.Clone();
            _Port = port;

            if (OnConnecting != null) {
                OnConnecting(this, EventArgs.Empty);
            }
            try {
                System.Net.IPAddress ip = System.Net.Dns.Resolve(Address).AddressList[0];
                _TcpClient = new IrcTcpClient();
                _TcpClient.NoDelay = true;
                _TcpClient.Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, 1);
                // set timeout, after this the connection will be aborted
                _TcpClient.ReceiveTimeout = _SocketReceiveTimeout * 1000;
                _TcpClient.SendTimeout = _SocketSendTimeout * 1000;
                _TcpClient.Connect(ip, port);

                Stream stream = _TcpClient.GetStream();
            #if NET_2_0
                if (_UseSsl) {
                    SslStream sslStream = new SslStream(stream, false, delegate {
                        return true;
                    });
                    sslStream.AuthenticateAsClient(Address);
                    stream = sslStream;
                }
            #endif
                _Reader = new StreamReader(stream, _Encoding);
                _Writer = new StreamWriter(stream, _Encoding);

                if (_Encoding.GetPreamble().Length > 0) {
                    // HACK: we have an encoding that has some kind of preamble
                    // like UTF-8 has a BOM, this will confuse the IRCd!
                    // Thus we send a \r\n so the IRCd can safely ignore that
                    // garbage.
                    _Writer.WriteLine();
                }

                // Connection was succeful, reseting the connect counter
                _ConnectTries = 0;

                // updating the connection error state, so connecting is possible again
                IsConnectionError = false;
                _IsConnected = true;

                // lets power up our threads
                _ReadThread.Start();
                _WriteThread.Start();
                _IdleWorkerThread.Start();

            #if LOG4NET
                Logger.Connection.Info("connected");
            #endif
                if (OnConnected != null) {
                    OnConnected(this, EventArgs.Empty);
                }
            } catch (Exception e) {
                if (_Reader != null) {
                    try {
                        _Reader.Close();
                    } catch (ObjectDisposedException) {
                    }
                }
                if (_Writer != null) {
                    try {
                        _Writer.Close();
                    } catch (ObjectDisposedException) {
                    }
                }
                if (_TcpClient != null) {
                    _TcpClient.Close();
                }
                _IsConnected = false;
                IsConnectionError = true;

            #if LOG4NET
                Logger.Connection.Info("connection failed: "+e.Message);
            #endif
                if (_AutoRetry &&
                    _ConnectTries <= 3) {
                    if (OnAutoConnectError != null) {
                        OnAutoConnectError(this, new AutoConnectErrorEventArgs(Address, Port, e));
                    }
            #if LOG4NET
                    Logger.Connection.Debug("delaying new connect attempt for "+_AutoRetryDelay+" sec");
            #endif
                    Thread.Sleep(_AutoRetryDelay * 1000);
                    _NextAddress();
                    Connect(_AddressList, _Port);
                } else {
                    throw new CouldNotConnectException("Could not connect to: "+Address+":"+Port+" "+e.Message, e);
                }
            }
        }
Beispiel #3
0
 public void Connect(string[] addresslist, int port)
 {
     if (this._IsConnected)
     {
         throw new AlreadyConnectedException("Already connected to: " + this.Address + ":" + (object)this.Port);
     }
     ++this._ConnectTries;
     this._AddressList = (string[])addresslist.Clone();
     this._Port        = port;
     if (this.OnConnecting != null)
     {
         this.OnConnecting((object)this, EventArgs.Empty);
     }
     try
     {
         IPAddress address = Dns.GetHostEntry(this.Address).AddressList[0];
         this._TcpClient         = new IrcTcpClient();
         this._TcpClient.NoDelay = true;
         this._TcpClient.Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, 1);
         this._TcpClient.ReceiveTimeout = this._SocketReceiveTimeout * 1000;
         this._TcpClient.SendTimeout    = this._SocketSendTimeout * 1000;
         this._TcpClient.Connect(address, port);
         Stream stream = (Stream)this._TcpClient.GetStream();
         this._Reader = new StreamReader(stream, this._Encoding);
         this._Writer = new StreamWriter(stream, this._Encoding);
         if (this._Encoding.GetPreamble().Length > 0)
         {
             this._Writer.WriteLine();
         }
         this._ConnectTries     = 0;
         this.IsConnectionError = false;
         this._IsConnected      = true;
         this._ReadThread.Start();
         this._WriteThread.Start();
         this._IdleWorkerThread.Start();
         if (this.OnConnected == null)
         {
             return;
         }
         this.OnConnected((object)this, EventArgs.Empty);
     }
     catch (Exception ex1)
     {
         if (this._Reader != null)
         {
             try
             {
                 this._Reader.Close();
             }
             catch (ObjectDisposedException ex2)
             {
             }
         }
         if (this._Writer != null)
         {
             try
             {
                 this._Writer.Close();
             }
             catch (ObjectDisposedException ex2)
             {
             }
         }
         if (this._TcpClient != null)
         {
             this._TcpClient.Close();
         }
         this._IsConnected      = false;
         this.IsConnectionError = true;
         if (this._AutoRetry && this._ConnectTries <= 3)
         {
             if (this.OnAutoConnectError != null)
             {
                 this.OnAutoConnectError((object)this, new AutoConnectErrorEventArgs(this.Address, this.Port, ex1));
             }
             Thread.Sleep(this._AutoRetryDelay * 1000);
             this._NextAddress();
             this.Connect(this._AddressList, this._Port);
         }
         else
         {
             throw new CouldNotConnectException("Could not connect to: " + this.Address + ":" + (object)this.Port + " " + ex1.Message, ex1);
         }
     }
 }
Beispiel #4
0
        /// <overloads>this method has 2 overloads</overloads>
        /// <summary>
        /// Connects to the specified server and port, when the connection fails
        /// the next server in the list will be used.
        /// </summary>
        /// <param name="addresslist">List of servers to connect to</pararm>
        /// <param name="port">Portnumber to connect to</pararm>
        /// <exception cref="CouldNotConnectException">The connection failed</exceptio>
        /// <exception cref="AlreadyConnectedException">If there is already an active connection</exceptio>
        public void Connect(string[] addresslist, int port)
        {
            if (_IsConnected) {
                throw new AlreadyConnectedException("Already connected to: "+Address+":"+Port);
            }

            #if LOG4NET
            Logger.Connection.Info("connecting...");
            #endif
            _ConnectTries++;
            _AddressList = (string[])addresslist.Clone();
            _Port = port;

            if (OnConnecting != null) {
                OnConnecting(this, EventArgs.Empty);
            }
            try {
                System.Net.IPAddress ip = System.Net.Dns.Resolve(Address).AddressList[0];
                _TcpClient = new IrcTcpClient();
                _TcpClient.NoDelay = true;
                _TcpClient.Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, 1);
                // set timeout, after this the connection will be aborted
                _TcpClient.ReceiveTimeout = _SocketReceiveTimeout*1000;
                _TcpClient.SendTimeout = _SocketSendTimeout*1000;
                _TcpClient.Connect(ip, port);

                _Reader = new StreamReader(_TcpClient.GetStream(), Encoding);
                _Writer = new StreamWriter(_TcpClient.GetStream(), Encoding);

                // Connection was succeful, reseting the connect counter
                _ConnectTries = 0;

                // updating the connection error state, so connecting is possible again
                IsConnectionError = false;
                _IsConnected = true;

                // lets power up our threads
                _ReadThread.Start();
                _WriteThread.Start();

            #if LOG4NET
                Logger.Connection.Info("connected");
            #endif
                if (OnConnected != null) {
                    OnConnected(this, EventArgs.Empty);
                }
            } catch (Exception e) {
                if (_Reader != null) {
                    _Reader.Close();
                }
                if (_Writer != null) {
                    _Writer.Close();
                }
                if (_TcpClient != null) {
                    _TcpClient.Close();
                }
                _IsConnected = false;
                IsConnectionError = true;
            #if LOG4NET
                Logger.Connection.Info("connection failed: "+e.Message);
            #endif
                if (_AutoRetry &&
                    (_ConnectTries <= 3)) {
            #if LOG4NET
                    Logger.Connection.Debug("delaying new connect attempt for "+_AutoRetryDelay+" sec");
            #endif
                    Thread.Sleep(_AutoRetryDelay*1000);
                    _NextAddress();
                    Connect(_AddressList, _Port);
                } else {
                    throw new CouldNotConnectException("Could not connect to: "+Address+":"+Port+" "+e.Message, e);
                }
            }
        }
Beispiel #5
0
        /// <overloads>this method has 2 overloads</overloads>
        /// <summary>
        /// Connects to the specified server and port, when the connection fails
        /// the next server in the list will be used.
        /// </summary>
        /// <param name="addresslist">List of servers to connect to</pararm>
        /// <param name="port">Portnumber to connect to</pararm>
        /// <exception cref="CouldNotConnectException">The connection failed</exceptio>
        /// <exception cref="AlreadyConnectedException">If there is already an active connection</exceptio>
        public void Connect(string[] addresslist, int port)
        {
            if (_IsConnected)
            {
                throw new AlreadyConnectedException("Already connected to: " + Address + ":" + Port);
            }

#if LOG4NET
            Logger.Connection.Info("connecting...");
#endif
            _ConnectTries++;
            _AddressList = (string[])addresslist.Clone();
            _Port        = port;

            if (OnConnecting != null)
            {
                OnConnecting(this, EventArgs.Empty);
            }
            try {
                System.Net.IPAddress ip = System.Net.Dns.Resolve(Address).AddressList[0];
                _TcpClient         = new IrcTcpClient();
                _TcpClient.NoDelay = true;
                _TcpClient.Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, 1);
                // set timeout, after this the connection will be aborted
                _TcpClient.ReceiveTimeout = _SocketReceiveTimeout * 1000;
                _TcpClient.SendTimeout    = _SocketSendTimeout * 1000;
                _TcpClient.Connect(ip, port);

                _Reader = new StreamReader(_TcpClient.GetStream(), Encoding);
                _Writer = new StreamWriter(_TcpClient.GetStream(), Encoding);

                // Connection was succeful, reseting the connect counter
                _ConnectTries = 0;

                // updating the connection error state, so connecting is possible again
                IsConnectionError = false;
                _IsConnected      = true;

                // lets power up our threads
                _ReadThread.Start();
                _WriteThread.Start();

#if LOG4NET
                Logger.Connection.Info("connected");
#endif
                if (OnConnected != null)
                {
                    OnConnected(this, EventArgs.Empty);
                }
            } catch (Exception e) {
                if (_Reader != null)
                {
                    _Reader.Close();
                }
                if (_Writer != null)
                {
                    _Writer.Close();
                }
                if (_TcpClient != null)
                {
                    _TcpClient.Close();
                }
                _IsConnected      = false;
                IsConnectionError = true;
#if LOG4NET
                Logger.Connection.Info("connection failed: " + e.Message);
#endif
                if (_AutoRetry &&
                    (_ConnectTries <= 3))
                {
#if LOG4NET
                    Logger.Connection.Debug("delaying new connect attempt for " + _AutoRetryDelay + " sec");
#endif
                    Thread.Sleep(_AutoRetryDelay * 1000);
                    _NextAddress();
                    Connect(_AddressList, _Port);
                }
                else
                {
                    throw new CouldNotConnectException("Could not connect to: " + Address + ":" + Port + " " + e.Message, e);
                }
            }
        }