// This subroutine sends a response to the sender. private void ReplyToSender(string strMessage, UserConnection sender) { sender.SendData(strMessage); }
// This subroutine is used a background listener thread to allow reading incoming // messages without lagging the user interface. private void DoListen() { try { // Listen for new connections. listener = new TcpListener(System.Net.IPAddress.Any, _serverPort); listener.Start(); do { // Create a new user connection using TcpClient returned by // TcpListener.AcceptTcpClient() UserConnection client = new UserConnection(listener.AcceptTcpClient()); // Create an event handler to allow the UserConnection to communicate // with the window. client.LineReceived += new LineReceiveEventHandler(OnLineReceived); //AddHandler client.LineReceived, AddressOf OnLineReceived; }while(true); } catch(Exception ex){ System.Console.WriteLine(ex.ToString()); } }
// This is the event handler for the UserConnection when it receives a full line. // Parse the cammand and parameters and take appropriate action. private void OnLineReceived(UserConnection sender, string data) { string[] dataArray; // Message parts are divided by "|" Break the string into an array accordingly. // Basically what happens here is that it is possible to get a flood of data during // the lock where we have combined commands and overflow // to simplify this proble, all I do is split the response by char 13 and then look // at the command, if the command is unknown, I consider it a junk message // and dump it, otherwise I act on it // dataArray = data.Split((char) 13); dataArray = data.Split((char) 124); // dataArray(0) is the command. switch( dataArray[0]) { case "CONNECT": ConnectUser(dataArray[1], sender); DataManager(dataArray[1], data); break; case "DISCONNECT": DisconnectUser(sender.Name); DataManager(sender.Name, data); break; default: DataManager(sender.Name, data); break; } }
// This subroutine checks to see if username already exists in the clients // Hashtable. if it does, send a REFUSE message, otherwise confirm with a JOIN. private void ConnectUser(string userName, UserConnection sender) { if (clients.Contains(userName)) { ReplyToSender(EConnectionResponse.ConnectionRefused.ToString(), sender); } else { sender.Name=userName; clients.Add(userName, sender); System.Console.WriteLine("CONNECTED: "+sender.Name); ReplyToSender(EConnectionResponse.ConnectionAccepted.ToString(), sender); } }