// Constructor public Server(int port = 9996) : base() { channels = new List <Channel>(); //clientList = new List<ServerClient>(); try { Administration.LoadServerKeys(); StartTick(); string[] channelString = File.ReadAllLines(Environment.CurrentDirectory + "/serverlayout.txt"); string defaultChan = channelString.First(); defaultChannel = short.Parse(defaultChan); if (defaultChannel > channelString.Count() - 1 || defaultChannel < 1) { Console.WriteLine("Default channel " + defaultChannel + " not found, reverting to first channel."); defaultChannel = 1; } foreach (string stringChannel in channelString.Skip(1)) { this.channels.Add(new Channel(stringChannel)); } } catch (FileNotFoundException) { Console.WriteLine("serverlayout.txt file not found -- creating default channels."); channels.Add(new Channel("Main")); channels.Add(new Channel("Channel 1")); channels.Add(new Channel("Channel 2")); } Console.WriteLine("Packet Handlers"); packetHandler.AddPacketHandler(Messages.VOICE, new Action <Packet>(HandleVoicePacket)); packetHandler.AddPacketHandler(Messages.CHAT, new Action <Packet>(HandleChatPacket)); packetHandler.AddPacketHandler(Messages.CONNECT, new Action <Packet>(HandleConnectPacket)); packetHandler.AddPacketHandler(Messages.CONNECTED, new Action <Packet>(HandleConnectedPacket)); packetHandler.AddPacketHandler(Messages.KEEPALIVE, new Action <Packet>(HandleKeepAlivePacket)); packetHandler.AddPacketHandler(Messages.DISCONNECT, new Action <Packet>(HandleDisconnectPacket)); packetHandler.AddPacketHandler(Messages.RECIEVED, new Action <Packet>(HandleRecievedPacket)); packetHandler.AddPacketHandler(Messages.JOINCHANNEL, new Action <Packet>(HandleJoinChannelPacket)); packetHandler.AddPacketHandler(Messages.GETKEY, new Action <Packet>(HandleNewKeyPacket)); packetHandler.AddPacketHandler(Messages.SETKEY, new Action <Packet>(HandleSetKeyPacket)); packetHandler.AddPacketHandler(Messages.SERVERMESSAGE, new Action <Packet>(HandleServerMessagePacket)); this.port = port; // Start listening for incoming packets and requests StartListen(port); var host = Dns.GetHostEntry(Dns.GetHostName()); Console.WriteLine(" ~ Server Initialized on " + Dns.GetHostName()); foreach (var ip in host.AddressList) { if (ip.AddressFamily == AddressFamily.InterNetwork) { Console.WriteLine(" > " + ip); } } }
// Stop the server public void Stop(string message = "Shutting Down") { Console.WriteLine("Server Shutting Down"); Administration.SaveServerKeys(); SendToClients(0, new Packet(Messages.SHUTDOWN, Encoding.ASCII.GetBytes(message))); Disconnect(); }
public void SetKey(string key) { if (Administration.KeyExists(key)) { this._key = key; _isAdmin = Administration.IsAdmin(key); Send(new Packet(Messages.SETADMIN, BitConverter.GetBytes(_isAdmin))); Console.WriteLine("SETKEY: " + this.name + ", KEY: " + this._key + ", ISADMIN: " + this._isAdmin.ToString()); } else { NewKey(); } }
public void SetAdmin(bool state) { if (state) { Administration.SetAdmin(_key); } else { Administration.RemoveAdmin(_key); } Send(new Packet(Messages.SETADMIN, BitConverter.GetBytes(state))); _isAdmin = true; }
static void Main(string[] args) { Console.CursorVisible = false; server = new Server(); do { string command = ""; ConsoleKeyInfo lastKey; do { lastKey = Console.ReadKey(true); // If key was delete, remove last char of string. if (command != "" && (lastKey.Key == ConsoleKey.Backspace || lastKey.Key == ConsoleKey.Delete)) { command = command.Remove(command.Length - 1); } else if (lastKey.Key != ConsoleKey.Enter) { command += lastKey.KeyChar; } WriteLastLine(command); } while (lastKey.Key != ConsoleKey.Enter); // Type the command in the 'normal' area, and clear the bottom line. Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine(command); Console.ForegroundColor = ConsoleColor.Gray; WriteLastLine(new string(' ', Console.BufferWidth)); //command = command.ToLower(); string[] inputs = command.Split(' '); DoCommand(inputs[0], inputs); } while (server.IsListening); Administration.SaveServerKeys(); Console.WriteLine("Exited"); Console.ReadLine(); }
public void NewKey() { _key = Administration.AddUserKey(); Console.WriteLine("SETKEY: " + this.name + ", KEY: " + this._key + ", ISADMIN: " + this._isAdmin.ToString()); Send(new Packet(Messages.SETKEY, Encoding.ASCII.GetBytes(_key))); }
protected override void Tick() { Administration.SaveServerKeys(); }