/// <summary> /// Main Thread of the server /// </summary> /// <param name="args"></param> static void Main(string[] args) { Thread cmdLoop = new Thread(CommandLoop); // cmdLoop.IsBackground = true; cmdLoop.Start(); server.Start(); Login_Helper.InitializeDB(); Log.InitiateLog(); //Console.ReadLine(); }
private static void CommandLoop() { while (true) { string cmd = Console.ReadLine(); string[] cmdArgs = cmd.Split(' '); if (cmdArgs[0].ToLower() == "ban" && cmdArgs.Length >= 1) { Login_Helper.BanUser(cmdArgs[1]); string msg = ""; for (int i = 2; i < cmdArgs.Length; i++) { msg += cmdArgs[i] + " "; } server.BanUser(cmdArgs[1], msg); Log.Warning("Server Issued Ban To " + cmdArgs[1]); } else if (cmdArgs[0].ToLower() == "clear") { Log.Warning("Clear cmd Called"); Console.Clear(); server.drawLogo(); } else if (cmdArgs[0].ToLower() == "list") { server.ListUsers(); Log.Warning("List cmd Called"); } else if (cmdArgs[0].ToLower() == "admins") { //lists all admins online currently } else if (cmdArgs[0].ToLower() == "give" && cmdArgs.Length >= 1) { // } else if (cmdArgs[0].ToLower() == "reboot") { server.RebootServer(); Log.Warning("SERVER CMD REBOOT CALLED"); } else if (cmdArgs[0].ToLower() == "unban" && cmdArgs.Length >= 1) { Login_Helper.UnBanUser(cmdArgs[1]); Log.Warning("Server Unbanned User " + cmdArgs[1]); } else if (cmdArgs[0].ToLower() == "kick" && cmdArgs.Length >= 2) { Log.Warning("Server Kicked User " + cmdArgs[1]); string msg = ""; for (int i = 2; i < cmdArgs.Length; i++) { msg += cmdArgs[i] + " "; } server.KickUser(cmdArgs[1], msg); } else if (cmdArgs[0].ToLower() == "send" && cmdArgs.Length >= 2) { Log.Warning("Server Sent Message To User " + cmdArgs[1]); string msg = ""; for (int i = 2; i < cmdArgs.Length; i++) { msg += cmdArgs[i] + " "; } server.SendUserMsg(cmdArgs[1], msg); } } }
private void ReceiveCallback(IAsyncResult ar) { //better to initailize these outside the try catch to make them accessable throughout method Socket socket = (Socket)ar.AsyncState; ProcessCodes code; string[] msg; int received; string text; byte[] dataBuff; //Everythings done in a try catch in case the client unexpectedly disconnects so it doesnt crash the server try { received = socket.EndReceive(ar); Users user = userList.Find(i => i.IP == socket.RemoteEndPoint.ToString()); Admins admin = adminList.Find(i => i.IP == socket.RemoteEndPoint.ToString()); dataBuff = new byte[received]; Array.Copy(g_buffer, dataBuff, received); text = Encoding.ASCII.GetString(dataBuff); msg = text.Split(' '); if ((user != null) && (user.Name != null && user.Name != "")) { Log.Success("Received msg from " + user.Name); sendAdminsLog("GREEN " + "Received msg from " + user.Name); } else if ((admin != null) && (admin.Name != null && admin.Name != "") && (msg[1] == "KICK" || msg[1] == "BAN" || msg[1] == "SENDMSG")) { Log.AdminConnect(admin.Name + " Issued a " + msg[1]); sendAdminsLog("GREEN " + "Received msg from " + admin.Name); } } //Everytime a user disconnects this is called catch (SocketException) { int msgtype = 0; string rep = ""; //creates a user object for regular users Users user = userList.Find(i => i.IP == socket.RemoteEndPoint.ToString()); //creates an admin object for admin user Admins admin = adminList.Find(i => i.IP == socket.RemoteEndPoint.ToString()); if (user != null && user.Name != null && user.Name != "") { //set user to logged off Log.Warning(user.Name + " Has Forcefully Disconnected!"); msgtype = 1; Login_Helper.UpdateUser(user.Name); } else if (admin != null && admin.Name != null && admin.Name != "") { //sets admin to log off Log.Warning("ADMIN: " + admin.Name + " Has Forcefully Disconnected!"); msgtype = 3; Login_Helper.UpdateUser(admin.Name); } else { Log.Error("Random Client Has Forcefully Disconnected! " + socket.RemoteEndPoint.ToString()); rep = socket.RemoteEndPoint.ToString(); msgtype = 2; } var ItemToRemove = userList.SingleOrDefault(i => i.IP == socket.RemoteEndPoint.ToString()); var AdminToRemove = adminList.SingleOrDefault(i => i.IP == socket.RemoteEndPoint.ToString()); socket.Close(); //clientSockets.Remove(socket); if (ItemToRemove != null) { userList.Remove(ItemToRemove); } if (AdminToRemove != null) { adminList.Remove(AdminToRemove); } if (msgtype == 2) { sendAdminsLog("RED " + "Random Client Has Forcefully Disconnected! " + rep); } else if (msgtype == 1) { sendAdminsLog("YELLOW " + user.Name + " Has Forcefully Disconnected!"); } else if (msgtype == 3) { sendAdminsLog("YELLOW " + admin.Name + " Has Forcefully Disconnected!"); } return; } //incase the code given is not an actual code so we dont run into formatting problems and the server crashes try { code = (ProcessCodes)UInt16.Parse(msg[0]); } catch (System.FormatException e) { var ItemToRemove = userList.SingleOrDefault(i => i.IP == socket.RemoteEndPoint.ToString()); var AdminToRemove = adminList.SingleOrDefault(i => i.IP == socket.RemoteEndPoint.ToString()); //socket.Close(); //clientSockets.Remove(socket); if (ItemToRemove != null) { userList.Remove(ItemToRemove); } if (AdminToRemove != null) { adminList.Remove(AdminToRemove); } Log.Error("Error connecting user, possibly malicaious " + socket.RemoteEndPoint); sendAdminsLog("RED " + "Error connecting user, possibly malicaious " + socket.RemoteEndPoint); return; } switch (code)//first item is always code { case ProcessCodes.Login: { ErrorCodes tempCode = Login_Helper.doLogin(msg[1], msg[2]); byte[] data = Encoding.ASCII.GetBytes(Convert.ToString(tempCode)); if (tempCode == ErrorCodes.Login_Success) { Log.Success(msg[1] + " Has Logged In!"); sendAdminsLog("GREEN " + msg[1] + " Has Logged In!"); if (Login_Helper.checkAdmin(msg[1])) { adminList.Add(new Admins(userList.Find(i => i.IP == socket.RemoteEndPoint.ToString()).IP, userList.Find(i => i.IP == socket.RemoteEndPoint.ToString()).clientSocket)); adminList.Find(i => i.IP == socket.RemoteEndPoint.ToString()).Name = msg[1]; var toRemove = userList.Single(i => i.IP == socket.RemoteEndPoint.ToString()); userList.Remove(toRemove); } else { userList.Find(i => i.IP == socket.RemoteEndPoint.ToString()).Name = msg[1]; } IPEndPoint ipAdd = socket.RemoteEndPoint as IPEndPoint; Login_Helper.UpdateUser(ipAdd.ToString(), msg[1], 1); } socket.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(SendCallback), socket); socket.BeginReceive(g_buffer, 0, g_buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), socket); } break; case ProcessCodes.Version: { if (clientVersion.Equals(msg[1])) { Log.Success("Client versions matched, cleared for login"); sendAdminsLog("GREEN " + "Client versions matched, cleared for login"); byte[] data = Encoding.ASCII.GetBytes(Convert.ToString(ErrorCodes.Version_Success.ToString())); socket.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(SendCallback), socket); socket.BeginReceive(g_buffer, 0, g_buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), socket); } else { Log.Warning("Client versions do match closing client"); sendAdminsLog("YELLOW " + "Client versions do match closing client"); byte[] data = Encoding.ASCII.GetBytes(Convert.ToString(ErrorCodes.Error.ToString()) + " " + clientVersion); socket.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(SendCallback), socket); socket.BeginReceive(g_buffer, 0, g_buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), socket); } } break; case ProcessCodes.Command: { string message = ""; for (int i = 3; i < msg.Length; i++) { message += msg[i] + " "; } if (msg[1] == "KICK") { KickUser(msg[2], message); socket.BeginReceive(g_buffer, 0, g_buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), socket); } else if (msg[1] == "BAN") { BanUser(msg[2], message); socket.BeginReceive(g_buffer, 0, g_buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), socket); } else if (msg[1] == "SENDMSG") { SendUserMsg(msg[2], message); socket.BeginReceive(g_buffer, 0, g_buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), socket); } else if (msg[1] == "SENDMSGALL") { SendUserMsgAll(message); socket.BeginReceive(g_buffer, 0, g_buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), socket); } else if (msg[1] == "UPDATEUSERS") { sendUpdatedUsers(); socket.BeginReceive(g_buffer, 0, g_buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), socket); } } break; default: { ProcessError(ar); } break; } }