/// <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); } } }
/// <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); } } }