Beispiel #1
0
        public void handleClient()
        {
            connected = client.Poll(1000, SelectMode.SelectRead);

            while (true)
            {
                connected = client.Poll(1000, SelectMode.SelectRead);
                if (connected && client.Available == 0)
                {
                    break;
                }

                // receive
                if (client.Available > 0)
                {
                    StateObject obj = Receive(client);
                    handleRequest(obj);
                }
            }
            Console.WriteLine("Cliente " + ServerDispatcher.get().ServerSockets.Find(u => u.Socket == client).Alias + " desconectou ");
            client.Shutdown(SocketShutdown.Both);
            client.Disconnect(false);
            ServerDispatcher.get().ServerSockets.Remove(user);
            alertQuit();
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            iniciaGrupos();

            IPHostEntry ipHostInfo    = Dns.GetHostEntry("localhost");
            IPAddress   ipAddress     = ipHostInfo.AddressList[1];
            IPEndPoint  localEndPoint = new IPEndPoint(ipAddress, 5442);

            listener = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            listener.Bind(localEndPoint);
            listener.Listen(100);

            ServerDispatcher dispatcher = ServerDispatcher.get();

            Console.WriteLine("Escutando conexões...");
            while (true)
            {
                Socket sock = listener.Accept();
                //sock.SendBufferSize = 1024 * 500;
                sock.Blocking = false;
                User u = new User(sock);
                dispatcher.ServerSockets.Add(u);
                new Task(new SocketTask(u).getTask()).Start();
            }
        }
Beispiel #3
0
        public String listUsers()
        {
            List <dynamic> users_ = new List <dynamic>(), groups_ = new List <dynamic>();

            foreach (User item in ServerDispatcher.get().ServerSockets)
            {
                if (item.ID == user.ID)
                {
                    continue;
                }
                users_.Add(new
                {
                    id     = item.ID,
                    alias  = item.Alias,
                    status = item.status,
                    PKEY   = Convert.ToBase64String(item.PKEY)
                });
            }


            foreach (Group group in ServerDispatcher.get().Rooms)
            {
                RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
                rsa.ImportCspBlob(user.PKEY);

                byte[] encryptedKey = rsa.Encrypt(group.des.Key, false);
                byte[] encryptedIV  = rsa.Encrypt(group.des.IV, false);

                groups_.Add(new {
                    id          = group.ID,
                    alias       = group.Alias,
                    leader      = group.leader,
                    subscribers = group.Subscribers.Select(x => x.ID).ToList(),
                    PKEY        = Convert.ToBase64String(group.rsa.ExportCspBlob(false)),
                    key         = Convert.ToBase64String(encryptedKey),
                    iv          = Convert.ToBase64String(encryptedIV)
                });
            }
            dynamic handshake = new
            {
                users   = users_,
                rooms   = groups_,
                command = DispatcherCodes.CONNECT
            };
            string str = format(handshake);

            return(str);
        }
Beispiel #4
0
        public void alertQuit()
        {
            var payload = new
            {
                user    = user.ID,
                command = DispatcherCodes.QUIT
            };
            List <Socket> sockets = ServerDispatcher.get().ServerSockets.Select(s => s.Socket).ToList();

            Socket.Select(null, sockets, null, 1000);
            foreach (Socket sock in sockets)
            {
                send(format(payload), sock);
                sendDone.WaitOne();
            }
        }
Beispiel #5
0
 static void iniciaGrupos()
 {
     ServerDispatcher.get().Rooms.Add(new Group()
     {
         ID     = "group#00",
         Alias  = "Política",
         leader = null,
         des    = new DESCryptoServiceProvider(),
         rsa    = new RSACryptoServiceProvider(),
     });
     ServerDispatcher.get().Rooms.Add(new Group()
     {
         ID     = "group#01",
         Alias  = "Assuntos Gerais",
         des    = new DESCryptoServiceProvider(),
         rsa    = new RSACryptoServiceProvider(),
         leader = null
     });
 }
Beispiel #6
0
        public void alertNewUser()
        {
            var payload = new
            {
                alias   = user.Alias,
                id      = user.ID,
                status  = user.status,
                PKEY    = Convert.ToBase64String(user.PKEY),
                command = DispatcherCodes.NEW_USER,
                groups  = ServerDispatcher.get().Rooms.Where(v => v.Subscribers.Exists(x => x.ID == user.ID)).Select(x => x.ID).ToList()
            };
            List <User> users = ServerDispatcher.get().ServerSockets.Where(s => s.ID != user.ID).ToList();

            foreach (User u in users)
            {
                send(format(payload), u.Socket);
                sendDone.WaitOne();
            }
        }
Beispiel #7
0
        public void handleRequest(StateObject obj)
        {
            dynamic data = obj.data;

            switch ((int)data["command"])
            {
            case (int)DispatcherCodes.CONNECT:
                user.ID     = data["id"];
                user.Alias  = data["alias"];
                user.status = UserStatus.DISPONIVEL;
                user.PKEY   = Convert.FromBase64String(data["PKEY"]);

                ServerDispatcher.get().Rooms.Find(x => x.ID == "group#00").Subscribers.Add(user);
                ServerDispatcher.get().Rooms.Find(x => x.ID == "group#01").Subscribers.Add(user);

                send(listUsers());
                sendDone.WaitOne();
                alertNewUser();
                break;

            case (int)DispatcherCodes.DISCONNECT:
                break;

            case (int)DispatcherCodes.SEND:

                if ((bool)data["isGroup"])
                {
                    Group         group   = ServerDispatcher.get().Rooms.Find(s => s.ID == data["id"]);
                    List <Socket> sockets = new List <Socket>(ServerDispatcher.get().ServerSockets.Select(s => s.Socket));

                    foreach (Socket item in sockets)
                    {
                        dynamic message = new
                        {
                            message = data["message"],
                            id      = group.ID,
                            sender  = user.ID,
                            command = DispatcherCodes.SEND,
                        };
                        send(serializer.Serialize(message), item);
                        sendDone.WaitOne();
                    }
                }
                else
                {
                    User    sendto  = ServerDispatcher.get().ServerSockets.Find(x => x.ID == data["id"]);
                    dynamic message = new
                    {
                        id      = user.ID,
                        sender  = sendto.ID,
                        command = DispatcherCodes.SEND,
                        message = data["message"],
                        key     = data["key"],
                        iv      = data["iv"]
                    };
                    send(serializer.Serialize(message), sendto.Socket);
                    sendDone.WaitOne();
                    send(serializer.Serialize(message), client);
                    sendDone.WaitOne();
                }
                break;
            }
        }
Beispiel #8
0
 public void releaseClient()
 {
     client.Disconnect(false);
     ServerDispatcher.get().ServerSockets.Remove(user);
 }