Ejemplo n.º 1
0
        public void SendUserMsg(string username, string message)
        {
            string msg   = ProcessCodes.Message.ToString() + " " + message;
            Users  user  = userList.Find(i => i.Name == username);
            Admins admin = adminList.Find(i => i.Name == username);

            if (user != null)
            {
                Socket socket = user.clientSocket;
                byte[] data   = Encoding.ASCII.GetBytes(msg);
                socket.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(SendCallback), socket);
            }
            else if (admin != null)
            {
                Socket socket = admin.clientSocket;
                byte[] data   = Encoding.ASCII.GetBytes(msg);
                socket.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(SendCallback), socket);
            }
        }
Ejemplo n.º 2
0
        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;
            }
        }