/// <overloads>this method has 3 overloads</overloads> /// <summary> /// Connects to the specified server and port using the specified transport, when the connection fails /// the next server in the list will be used. /// </summary> /// <param name="transport">Transport protocol to use</param> /// <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 new void Connect(IIrcTransportManager transport, string[] addresslist, int port = 0) { _SupportNonRfcLocked = true; ChannelModeMap = new ChannelModeMap(); base.Connect(transport, addresslist, port); // We get here only if the connection is successful if (_UseIrcV3) { _CapNegotiateStart(); } }
/// <summary> /// Connects to the specified transport. The transport must be pre-populated with the host address /// </summary> /// <param name="transport">Transport protocol to use</param> /// <exception cref="CouldNotConnectException">The connection failed</exception> /// <exception cref="AlreadyConnectedException">If there is already an active connection</exception> public override void Connect(IIrcTransportManager transport) { Connect(transport, new string[] { transport.Address }, transport.Port); }
private void dirtyDisconnectScenario(IIrcTransportManager transport, bool reconnect) { runConnectionScenario(transport, async () => { Debug.WriteLine("Waiting for " + killDelay + "ms on secondary thread before destroying connection"); await Task.Delay(killDelay); Debug.WriteLine("Destroying connection"); // Does nothing to 1.2.3.4 but turns ws://1.2.3.4 into 1.2.3.4 string ipNumbers = "/" + transport.Address; ipNumbers = ipNumbers.Substring(ipNumbers.LastIndexOf("/") + 1); destroyTcpConnection(ipNumbers); }, true, reconnect); }
/// <summary> /// Connects to an IRC server with a well-known configuration, runs a connection scenario in another thread /// and checks that everything went according to plan /// </summary> /// <param name="connectionTask">The connection scenario to run in another thread</param> private void runConnectionScenario(IIrcTransportManager transport, Action connectionMethod, bool wantError, bool reconnect, bool connectionExpected = true) { int rx = 0; int gotErrorEvent = 0; int gotDisconnectingEvent = 0; int gotDisconnectedEvent = 0; // Configure connection irc.IdleWorkerInterval = Math.Min(pingInterval, pingTimeout) / 5; irc.PingInterval = pingInterval; irc.PingTimeout = pingTimeout; // Prove we are receiving data irc.OnRawMessage += (s, e) => { Debug.WriteLine("RX: " + e.Data.RawMessage); // Fail the test if we got throttled - change the timeouts or repeat quantities to avoid this Assert.False(e.Data.RawMessage.Contains("throttled"), "Connection was throttled by the remote service - change your timeouts or repeat values to avoid this and re-run the test"); rx++; }; // Prove event flow succeeded irc.OnConnecting += (s, e) => { Debug.WriteLine("OnConnecting fired"); }; irc.OnConnected += (s, e) => { Debug.WriteLine("OnConnected fired"); }; irc.OnRegistered += (s, e) => { Debug.WriteLine("OnRegistered fired"); }; irc.OnConnectionError += (s, e) => { Debug.WriteLine("OnConnectionError fired"); gotErrorEvent++; }; irc.OnDisconnecting += (s, e) => { Debug.WriteLine("OnDisconnecting fired"); gotDisconnectingEvent++; }; irc.OnDisconnected += (s, e) => { Debug.WriteLine("OnDisconnected fired"); gotDisconnectedEvent++; }; irc.OnReconnected += (s, e) => { Debug.WriteLine("OnReconnected fired"); }; irc.OnWriteLine += (s, e) => { Debug.WriteLine("TX: " + e.Line); }; // Run the scenario in another thread once the connection is established irc.OnConnected += (s, e) => { Task.Run(connectionMethod); }; // Enable auto-reconnect if applicable to test enableAutoReconnect(reconnect); // Note how many connection events we're expecting int eventExpectation = irc.AutoReconnect ? maxReconnects + 1 : 1; // Connect Debug.WriteLine("Connecting to IRC server " + transport.Address + ":" + transport.Port); // Wait 60 seconds after each 5 connection attempts to the same server avoid throttling if (irc.Address != lastServer) { totalConnects = 1; lastServer = irc.Address; } else { totalConnects++; } if ((totalConnects >= 8 && !lastServer.Contains("127.0.0.1") && !lastServer.Contains("localhost")) || irc.AutoReconnect) { reconnectDelay = 30000; totalConnects = 1; } else { reconnectDelay = 1000; } // Try to avert throttling exceptions Debug.WriteLine("Waiting for " + reconnectDelay + "ms cooldown period"); Thread.Sleep(reconnectDelay); irc.Connect(transport); irc.Login("SmartIRC", "SmartIrc4net Test Bot"); irc.RfcJoin("#smartirc-test"); // Start processing incoming data Debug.WriteLine("Starting blocking Listen()"); irc.Listen(); Debug.WriteLine("Listen() returns"); Debug.WriteLine(rx + " lines received from IRC server"); // If we get here, we have detected disconnection fairly quickly (within the timeouts) // and disconnected and closed all our threads cleanly // NOTE: Doesn't guarantee the events were received in the right order // Check situation is as it should be Assert.AreEqual(false, irc.IsConnected, "Incorrect value for IsConnected"); Assert.AreEqual(false, irc.IsDisconnecting, "Incorrect value for IsDisconnecting"); Assert.AreEqual(false, irc.IsConnectionError, "Incorrect value for IsConnectionError"); // (error is set to false after disconnect completes) // Check we got all the events the right amount of times Assert.AreEqual(wantError ? eventExpectation : 0, gotErrorEvent, "Wrong number of IsConnectionError events"); Assert.AreEqual(eventExpectation, gotDisconnectingEvent, "Wrong number of IsDisconnecting events"); Assert.AreEqual(eventExpectation, gotDisconnectedEvent, "Wrong number of IsDisconnected events"); // Check we received something if (connectionExpected) { Assert.Greater(rx, 0, "Didn't receive any data from server"); } else { Assert.AreEqual(rx, 0, "Did not expect to receive any data from server"); } }
private void cleanDisconnectScenario(IIrcTransportManager transport, bool reconnect) { runConnectionScenario(transport, async () => { Debug.WriteLine("Waiting for " + killDelay + "ms on secondary thread before closing connection"); await Task.Delay(killDelay); Debug.WriteLine("Closing connection"); if (reconnect && reconnects < maxReconnects) irc.Reconnect(); else irc.Disconnect(); }, false, reconnect); }