private static void ClientDisconnected(object sender, TcpClient client) { try { if (clients.Count != 0) { int index = clients.IndexOf(client); if (index >= 0 && index < manager.usernames.Count) { Console.WriteLine(manager.currentTimeStamp() + " User disconnected: " + manager.usernames[index]); manager.usernames.RemoveAt(index); } else { Console.WriteLine(manager.currentTimeStamp() + " User disconnected. Could not delete username at index " + index); manager.username = ""; } clients.Remove(client); } } catch (Exception ex) { Consolemanager.error("user disconnect", ex); } }
private static void ClientConnected(object sender, TcpClient client) { try { clients.Add(client); } catch (Exception ex) { Consolemanager.error("client connected", ex); } }
public void BroadcastMessage(string data) { try { Program.server.BroadcastLine(username + "$" + data); } catch (Exception ex) { Consolemanager.error("broadcast message", ex); } }
private void connectClient(string[] data, Message e) { try { string username = data[1]; Boolean isAlreadyConnected = usernames.Contains(username); usernames.Add(username); if (isAlreadyConnected) { e.ReplyLine($"$kck${username}$3"); return; } int index = usernames.IndexOf(username); string ip = Program.clients[index].Client.RemoteEndPoint.ToString().Split(':')[0]; if (Filemanager.isBanned(username) || Filemanager.isIpBanned(ip)) { e.ReplyLine($"$kck${username}$1"); return; } else if (useWhitelist && !Filemanager.isOnWhitelist(username)) { e.ReplyLine($"$kck${username}$2"); return; } else { if (Program.isFirstConnection) { //Console.WriteLine("First connection, fetching Data..."); e.ReplyLine("$gad"); Program.isFirstConnection = false; } else { //Console.WriteLine("Not first connection."); e.ReplyLine("$rad"); sendAllData(e); } Console.WriteLine(currentTimeStamp() + " User connected: " + username); } } catch (Exception ex) { Consolemanager.error("connect client", ex); } }
private void sendAllData(Message e) { try { foreach (Column c in columns) { e.ReplyLine("$ccl$" + c.name + "$" + c.id.ToString()); foreach (string[] s in c.Flightstrips) { string toSend = ""; Array.ForEach(s, x => toSend += (x + "$")); e.ReplyLine("$cfs$" + toSend); } } } catch (Exception ex) { Consolemanager.error("send all data", ex); } }
private static void SetupServer() { char changePort = 'x'; string stringPort = string.Empty; Console.Title = "FlightStrip Manager Server"; Console.WriteLine("Setup Server:"); Console.WriteLine("Standard port is set to " + port + "."); while (!(changePort == 'y' || changePort == 'n')) { Console.Write("Do you want to use a custom port? (y/n): "); changePort = Console.ReadKey().KeyChar; if (changePort == 'y') { Console.WriteLine(); Console.Write("Desired port: "); stringPort = Console.ReadLine(); if (stringPort != "") { try { port = Int32.Parse(stringPort); Console.WriteLine("Port set to " + port); } catch (FormatException e) { Console.WriteLine("Input wasn't a number. Port set to 13000"); } } } else if (changePort == 'n') { Console.WriteLine(); Console.WriteLine("Port set to " + port); } else { Console.WriteLine(); } } Console.WriteLine("Setting up server..."); try { startServer(); Console.WriteLine(); Console.WriteLine("Server is reachable under the following IP-addresses:"); string hostName = Dns.GetHostName(); for (int i = 0; i < Dns.GetHostEntry(hostName).AddressList.Length; i++) { string ipAddress = Dns.GetHostEntry(hostName).AddressList[i].ToString(); if (!ipAddress.Contains(":")) { Console.WriteLine(ipAddress); } } Console.WriteLine(); Console.WriteLine("To list all commands, type: help"); Console.WriteLine("Setup finished. Server started at " + DateTime.Now.ToLongTimeString()); Console.WriteLine("---------------------------------------------"); while (true) { string line = Console.ReadLine(); if (!String.IsNullOrWhiteSpace(line)) { string[] commands = line.Split(' '); switch (commands[0]) { case "help": Consolemanager.help(); break; case "msg": Consolemanager.msg(commands); break; case "msguser": Consolemanager.msguser(commands); break; case "listuser": Consolemanager.listuser(); break; case "kick": Consolemanager.kick(commands, 0); break; case "kickall": Consolemanager.kickall(); break; case "ban": Consolemanager.ban(commands); break; case "banip": Consolemanager.banip(commands); break; case "unban": Consolemanager.unban(commands); break; case "listbanned": Consolemanager.listbanned(); break; case "usewl": Consolemanager.usewl(commands); break; case "addtowl": Consolemanager.addtowl(commands); break; case "removefromwl": Consolemanager.removefromwl(commands); break; case "listwl": Consolemanager.listwl(); break; case "recoverfrom": Consolemanager.recoverfrom(commands); break; case "reset": Consolemanager.reset(); break; default: Consolemanager.notvalid(); break; } } } } catch (SocketException e) { Console.WriteLine("Error occured:"); Console.WriteLine(e); } }
public void processReceivedData(string receivedData, Message e) { try { string[] splittedString = receivedData.Split('$'); if (splittedString.Length > 2) { username = splittedString[0]; splittedString = splittedString.Skip(1).ToArray(); switch (splittedString[0]) { case "con": connectClient(splittedString, e); break; case "gad": Debug.WriteLine("GAD"); sendAllData(e); break; case "ccl": createColumn(splittedString, e); break; case "rcl": removeColumn(splittedString.Skip(1).ToArray()); break; case "cfs": createFlightstrip(splittedString.Skip(1).ToArray(), e); break; case "rfs": removeFlightstrip(splittedString.Skip(1).ToArray()); break; case "edt": editFlightstrip(splittedString.Skip(1).ToArray()); break; case "efs": editFlightstripStatus(splittedString.Skip(1).ToArray()); break; case "mov": moveFlightstrip(splittedString.Skip(1).ToArray(), e); break; case "gco": sendClientOverview(e); break; case "pos": editFlightstripPosition(splittedString.Skip(1).ToArray()); break; } } } catch (Exception ex) { Consolemanager.error("process data", ex); } }