public void Timeout() { // Had to pick a routable IP, but one that won't return packets... don't know of a better choice than this var connection = new Connection(new IPEndPoint(IPAddress.Parse("10.230.220.210"), 28015)); connection.ConnectTimeout = TimeSpan.FromSeconds(1); connection.Connect(); }
public void ShouldNotConnectToWrongPort() { var connected = true; var connection = new Connection(new IPEndPoint(IPAddress.Loopback, 7777)); connection.Connect((success) => { connected = success; _completion.Set(); }); _completion.WaitOne(); Assert.IsFalse(connected); }
public void ShouldConnectToListener() { var connected = false; var connection = new Connection(new IPEndPoint(IPAddress.Loopback, 9999)); connection.Connect((success) => { connected = success; _completion.Set(); }); _completion.WaitOne(); Assert.IsTrue(connected); }
public void Cancel() { CancellationTokenSource cts = new CancellationTokenSource(); cts.Cancel(); try { var connection = new Connection(new IPEndPoint(IPAddress.Parse("10.230.220.210"), 28015)); connection.Connect(cts.Token); Assert.Fail("Expected exception"); } catch (AggregateException ex) { Assert.That(ex.InnerException is TaskCanceledException); } }
public void ShouldSendData() { var localConnectionWaiter = new ManualResetEvent(false); var remoteConnectionWaiter = new ManualResetEvent(false); Connection remoteConnection = null; _remote.ConnectionRequested += (sender, args) => { remoteConnection = new Connection(args.Socket); remoteConnectionWaiter.Set(); }; var localConnection = new Connection(new IPEndPoint(IPAddress.Loopback, 9999)); localConnection.Connect(c => localConnectionWaiter.Set()); WaitHandle.WaitAll(new WaitHandle[] { localConnectionWaiter, remoteConnectionWaiter }); localConnectionWaiter.Reset(); remoteConnectionWaiter.Reset(); var remoteBuffer = new Buffer(new byte[1]); remoteConnection.Receive(remoteBuffer, (i, b) => remoteConnectionWaiter.Set()); localConnection.Send(new Buffer(new byte[] { 0xf1 }), (i, b) => localConnectionWaiter.Set()); WaitHandle.WaitAll(new WaitHandle[] { localConnectionWaiter, remoteConnectionWaiter }); Assert.AreEqual(0xf1, remoteBuffer.Segment.Array[0]); }
// IRC COMMANDS private void CreateConnection() { //The hostname of the IRC server string server = serverBox.Text; //The bot's nick on IRC string nick = usernameBox.Text; string password = OAuthBox.Text; //Fire up the Ident server for those IRC networks //silly enough to use it. Identd.Start(nick); //A ConnectionArgs contains all the info we need to establish //our connection with the IRC server and register our bot. //This line uses the simplfied contructor and the default values. //With this constructor the Nick, Real Name, and User name are //all set to the same value. It will use the default port of 6667 and no server //password. ConnectionArgs cargs = new ConnectionArgs(nick, server); cargs.ServerPassword = password; cargs.Port = 6667; //When creating a Connection two additional protocols may be //enabled: CTCP and DCC. In this example we will disable them //both. connection = new Connection(cargs, false, false); //NOTE //We could have created multiple Connections to different IRC servers //and each would process messages simultaneously and independently. //There is no fixed limit on how many Connection can be opened at one time but //it is important to realize that each runs in its own Thread. Also,separate event //handlers are required for each connection, i.e. the //same OnRegistered () handler cannot be used for different connection //instances. try { //Calling Connect() will cause the Connection object to open a //socket to the IRC server and to spawn its own thread. In this //separate thread it will listen for messages and send them to the //Listener for processing. connection.Connect(); Console.WriteLine("Connected."); this.Invoke(new Action(() => this.chatMessage.Text += "[SYSTEM] CONNECTED to " + server + ":6667 #" + channelBox.Text + " with " + nick +"\n")); this.Invoke(new Action(() => this.chart1.Series["User(s) in chat"].Points.AddXY(DateTime.Now.ToString("H:mm"), 1))); this.Invoke(new Action(() => { this.connectButton.Enabled = false; this.infoPanel.Enabled = false; this.topPanel.Enabled = true; this.chatText.Enabled = true; this.chatButton.Enabled = true; EnableTab(this.giveawayPage, true); EnableTab(this.bettingPage, true); EnableTab(this.challengePage, true); EnableTab(this.graphPage, true); EnableTab(this.databasePage, true); currentGiveawayBox.Enabled = false; currentBet.Enabled = false; timer5min = new System.Timers.Timer(1000); timer5min.Elapsed += new ElapsedEventHandler(timerTick); timer5min.Enabled = true; // Enable it nextSync = UnixTimeNow() + (60 * 5); // sync every 5 min nextMin = UnixTimeNow() + 60; })); //The main thread ends here but the Connection's thread is still alive. //We are now in a passive mode waiting for events to arrive. connection.Sender.PublicMessage("#" + channelBox.Text, "HugBotty.com is in da house!"); } catch (Exception e) { Console.WriteLine("Error during connection process."); this.Invoke(new Action(() => this.chatMessage.Text += "[SYSTEM] Error during connection process.\n")); Console.WriteLine(e); Identd.Stop(); } }
/// <summary> /// Sends a message. Looks for the necessary connection. If none exists, a new one will be built /// </summary> /// <param name="msg"></param> public static void SendMessage(ChannelMessage msg) { Connection conn = (Connection) ConnByReceiver[msg.To]; if (conn == null) { String host; int port; msg.SplitReceiver(out host, out port); if (host == null) { // message is directed at a GUID but no connection found // TODO: wait some seconds and retry. Maybe the client reconnects. Console.WriteLine("Message is directed at a GUID but no connection found"); throw new Exception("Message is directed at a GUID but no connection found"); } conn = new Connection(host,port); conn.Connect(); ConnByReceiver[msg.To] = conn; } conn.SendMessage(msg); }