public static void sendNotificationToAll(string notification)
 {
     foreach (var item in ListOfActiveUsers)
     {
         SocketReadWrite.writeToSocket(item.Value, Encoding.ASCII.GetBytes(notification));
     }
 }
 public static void sendNotificationToOtherSide(string Username, string Message)
 {
     foreach (var item in ServerSideSessionManager.ListOfActiveUsers)
     {
         if (item.Key == Username)
         {
             SocketReadWrite.writeToSocket(item.Value, Encoding.ASCII.GetBytes(Message));
             break;
         }
     }
 }
        //notify All active users about new Logins
        public static void notifyAllUsers()
        {
            string notification = "notificationOfUsers ";

            foreach (String name in ListOfActiveUsers.Keys)
            {
                notification += name + " ";
            }

            foreach (Socket sck in ListOfActiveUsers.Values)
            {
                Console.WriteLine("Sending notification to " + sck.RemoteEndPoint);
                SocketReadWrite.writeToSocket(sck, Encoding.Default.GetBytes(notification));
                System.Threading.Thread.Sleep(10);
            }
        }
 public static void sendMessageToOtherSide(string id, string to, string message)
 {
     foreach (var aSession in listOfSessions)
     {
         //if the id is found and is not empty or null
         if (!id.Contains("\0\0") && !(id.Trim() == ""))
         {
             if (aSession.Key == Int32.Parse(id))
             {
                 foreach (var aUser in ListOfActiveUsers)
                 {
                     if (aUser.Key == to)
                     {
                         SocketReadWrite.writeToSocket(aUser.Value, Encoding.ASCII.GetBytes(message));
                         break;
                     }
                 }
             } //#endif
         }
     }         //#outer-foreach
 }
Esempio n. 5
0
        void connectionsHandler(Object objSocket)
        {
            byte[] response;
            Socket socket = (Socket)objSocket;

            do
            {
                if (!(socket.Available > 0))
                {
                    Thread.Sleep(200);
                }

                string strData = SocketReadWrite.readFromSocket(socket);
                //  socket.Receive()

                if (strData.Trim() == "" || strData.Contains("\0") && !(strData.StartsWith("Incoming")))
                {
                    continue;
                }
                //split message into parts to find out whether it is a login, signup or newSession request
                String[] substrings = strData.Split(':');


                //If it is a signUp request
                if (substrings[0] == "signUp")
                {
                    if (ManageUsers.createNewUser(substrings))
                    {
                        response = Encoding.Default.GetBytes("true");
                        //listOfUsers.Add(substrings[1], accepted); //add name of user to Dictionary
                        ServerSideSessionManager.ListOfActiveUsers.Add(substrings[1], accepted);
                    }
                    else
                    {
                        response = Encoding.Default.GetBytes("false");
                        SocketReadWrite.writeToSocket(socket, response);
                        return;
                    }
                    SocketReadWrite.writeToSocket(socket, response);
                    //   notifyAllUsers();
                    ServerSideSessionManager.notifyAllUsers();
                }
                //If it is a login request
                else if (substrings[0] == "login")
                {
                    if (ManageUsers.authenticateUser(substrings))
                    {
                        //Check Whether user already logged in or not
                        if (ServerSideSessionManager.ListOfActiveUsers.ContainsKey(substrings[1]))
                        {
                            response = Encoding.Default.GetBytes("userAlreadyLoggedIn");
                            SocketReadWrite.writeToSocket(socket, response);
                            return;
                        }
                        response = Encoding.Default.GetBytes("true");
                        // listOfUsers.Add(substrings[1], accepted);
                        ServerSideSessionManager.ListOfActiveUsers.Add(substrings[1], accepted);
                        SocketReadWrite.writeToSocket(socket, response);
                    }
                    else
                    {
                        response = Encoding.Default.GetBytes("false");
                        SocketReadWrite.writeToSocket(socket, response);
                        return;
                    }
                    ServerSideSessionManager.notifyAllUsers();
                }
                //If there is a new end Session
                else if (substrings[0] == "newSessionStarted")
                {
                    Console.WriteLine("New Session Started" + substrings[1] + substrings[2]);

                    //int bal = Int32.Parse(substrings[3]); //from--to
                    ServerSideSessionManager.listOfSessions.Add(Int32.Parse(substrings[3]), substrings[1] + ":" + substrings[2]);
                    //Send To server for registration of the session on other side
                    ServerSideSessionManager.sendNotificationToOtherSide(substrings[2], strData);
                }
                //incomplete
                else if (substrings[0] == "Incoming")
                {
                    //parse Destination of file
                    int fileDestination = Int32.Parse(substrings[1]);
                    ///// SocketReadWrite.writeToSocket(socket,);
                    byte[] bytes = Encoding.ASCII.GetBytes(substrings[2]);;
                }
                else if (substrings[0].Trim() == "Logout")
                {
                    ServerSideSessionManager.ListOfActiveUsers.Remove(substrings[1]);
                    ServerSideSessionManager.notifyAllUsers();
                    ServerSideSessionManager.sendNotificationToAll(strData);
                }
                else if (substrings[0].Trim() == "ClosedSesion")
                {
                    //TODO:Send Closing Message To other Client
                }
                else
                //It is a message to be sent
                {
                    ServerSideSessionManager.sendMessageToOtherSide(substrings[0], substrings[2], strData);
                }
            } while (true);
        }