static void Server_State(Server s, bool open) { if (open) { Reconnect = 0; Console.WriteLine("Server listening."); } else { //TODO: Clean up server state. if (Reconnect == 4) //Try reconnecting 3 times then exit. Environment.Exit(0); Console.WriteLine("Server disconnected. Reconnecting in 20 seconds."); System.Threading.Thread.Sleep(20000); Reconnect += 1; Listener.Listen(Port); } }
static void Client_Read(Server s, Client c, byte[] e) { try { object[] values = Packer.Deserialize(e); ClientPacket packet = (ClientPacket)values[0]; if (c.Value.Authenticated) { switch (packet) { case ClientPacket.Channel: HandleChannelPacket(c, (byte)values[1]); break; case ClientPacket.ChatMessage: HandleChatPacket(c, (string)values[1]); break; } } else { switch (packet) { case ClientPacket.SignIn: HandleSignInPacket(c, (string)values[1], (string)values[2]); break; case ClientPacket.Register: HandleRegisterPacket(c, (string)values[1], (string)values[2]); break; } } } catch (Exception ex) { Console.WriteLine(ex.ToString()); c.Disconnect(); } }
static void Main(string[] args) { SQL = new MySqlConnection(); SQL.ConnectionString = "server=localhost;uid=auxilium;pwd=123456;database=auxilium"; SQL.Open(); Channels = new string[] { "Lounge", "VB.NET", "C#" }; Packer = new Pack(); Listener = new Server(); Listener.Size = 2048; Listener.Client_Read += Client_Read; Listener.MaxConnections = 10000; Listener.Client_State += Client_State; Listener.Server_State += Server_State; Listener.Listen(Port); while (true) { string str = string.Empty; if (Console.ReadKey().Key == ConsoleKey.Escape) break; else if (!string.IsNullOrWhiteSpace(str = Console.ReadLine())) ProcessCommand(str); } }
static void Client_State(Server s, Client c, bool open) { if (open) { c.Value = new UserState(); } else { if (c.Value.Authenticated) { byte[] data = Packer.Serialize((byte)ServerPacket.UserLeave, c.Value.UserID, c.Value.Username); Broadcast(c.Value.Channel, data); } } }
static void Main(string[] args) { SQLQueue = new MySqlConnection[10]; RandomSQL = new Random(new Guid().GetHashCode()); string connectionString = "server=localhost;uid=auxilium;pwd=123456;database=auxilium"; for (int i = 0; i < SQLQueue.Length; i++) { MySqlConnection msc = new MySqlConnection(); SQLQueue[i] = msc; try { msc.ConnectionString = connectionString; msc.Open(); } catch (Exception) { Console.Write("An error occured connecting to the database.\nPlease enter your credentials.\n\nUsername: "******"Password: "******"server=localhost;uid={0};pwd={1};database=auxilium", username, password)); msc.Open(); } } EmailClient = new SmtpClient { Host = "smtp.gmail.com", Port = 587, EnableSsl = true, DeliveryMethod = SmtpDeliveryMethod.Network, UseDefaultCredentials = false }; LetsDoThis(); Channels = new string[] { "Lounge" }; MOTD = GetMOTD(); Packer = new Pack(); Listener = new Server {BufferSize = 2048, MaxConnections = 1000}; Listener.ClientRead += ClientRead; Listener.ClientState += ClientState; Listener.ServerState += ServerState; Listener.Listen(Port); //Create the chat monitor timer. ChatMonitor = new Timer(Monitor, null, 1000, 240000); //240,000 = 4 Minutes LastBackup = DateTime.Now; string console = string.Empty; while (true) { char c = Console.ReadKey().KeyChar; if (c == Convert.ToChar(ConsoleKey.Escape)) { break; } if (c == Convert.ToChar(ConsoleKey.Enter)) { CheckCommand(null, console); console = string.Empty; } else { console += c; } } }
static void ClientState(Server s, Client c, bool open) { if (open) { c.Value = new UserState(); if (!string.IsNullOrEmpty(MOTD)) { byte[] data = Packer.Serialize((byte)ServerPacket.MOTD, MOTD); c.Send(data); } } else { if (c.Value.Authenticated) { byte[] data = Packer.Serialize((byte)ServerPacket.UserLeave, c.Value.UserId); Broadcast(c.Value.Channel, data); AwardPoints(c); //Let's save the users data. MySqlCommand q = new MySqlCommand(string.Empty, SQL); q.CommandText = "UPDATE users SET Points=@Points,Rank=@Rank,Mute=@Mute WHERE Username=@Username;"; q.Parameters.AddWithValue("@Points", c.Value.Points); q.Parameters.AddWithValue("@Rank", c.Value.Rank); q.Parameters.AddWithValue("@Mute", c.Value.Mute); q.Parameters.AddWithValue("@Username", c.Value.Username); //If it fails there isn't much we can do about it. q.ExecuteNonQuery(); } } }
static void ClientRead(Server s, Client c, byte[] e) { try { //Anti-flood measures. if (c.Value.IsFlooding()) { c.Disconnect(); return; } object[] values = Packer.Deserialize(e); if (values == null) return; ClientPacket packet = (ClientPacket)values[0]; if (c.Value.Authenticated && c.Value.Verified) { switch (packet) { case ClientPacket.Channel: HandleWakeup(c, true); //Supress the packet send here since we are changing rooms anyways. HandleChannelPacket(c, (byte)values[1]); break; case ClientPacket.ChatMessage: HandleWakeup(c); HandleChatPacket(c, (string)values[1]); break; case ClientPacket.PM: HandleWakeup(c); HandlePMPacket(c, (string)values[1], (string)values[2], (string)values[3], values.Length == 5 ? (ushort)values[4] : default(ushort)); break; case ClientPacket.KeepAlive: HandleKeepAlivePacket(c); break; case ClientPacket.News: HandleNewsPacket(c); break; case ClientPacket.ViewProfile: HandleViewProfilePacket(c, (string)values[1]); break; case ClientPacket.EditProfile: HandleEditProfilePacket(c, (string)values[1], (string)values[2], (string)values[3]); break; case ClientPacket.AuthCode: HandleAuthCodePacket(c, (string) values[1]); break; } } else { switch (packet) { case ClientPacket.SignIn: HandleSignInPacket(c, (string)values[1], (string)values[2]); break; case ClientPacket.Register: HandleRegisterPacket(c, (string)values[1], (string)values[2], (string)values[3]); break; case ClientPacket.ResendVerification: HandleResendVerificationPacket(c); break; case ClientPacket.AuthCode: HandleAuthCodePacket(c, (string) values[1]); break; } } } catch (Exception ex) { Console.WriteLine(ex.ToString()); c.Disconnect(); } }