Пример #1
0
 private void SendResponseToAllClients(Response response)
 {
     foreach (Socket client in _clients.Values)
     {
         DataTransferHelper.SendResponseToClient(client, response);
     }
 }
Пример #2
0
        private void ClientLoop(object o)
        {
            Socket temp_client = ((IDSocket)o).Handle_Socket;
            User   client_user = new User(((IDSocket)o).Handle_ID, null, null);

            try
            {
                while (true)
                {
                    Request client_request = DataTransferHelper.GetRequestFromClient(temp_client);

                    switch (client_request.RequestType)
                    {
                    case "registration":
                    {
                        RegistrationRequest registrationRequest = client_request as RegistrationRequest;

                        if (!IsUserNameExists(registrationRequest.UserName))
                        {
                            AddUser(new User(GetNewUserID(), registrationRequest.UserName, registrationRequest.Password));
                            RegistrationResponse registrationResponse = new RegistrationResponse();
                            DataTransferHelper.SendResponseToClient(temp_client, registrationResponse);
                        }
                        else
                        {
                            RegistrationResponse registrationResponse = new RegistrationResponse()
                            {
                                Ok    = false,
                                Error = "user_name_exists"
                            };
                            DataTransferHelper.SendResponseToClient(temp_client, registrationResponse);
                        }

                        break;
                    }

                    case "auth":
                    {
                        AuthRequest authRequest = client_request as AuthRequest;

                        if (IsUserNameExists(authRequest.UserName))
                        {
                            User temp_user = GetUserByName(authRequest.UserName);
                            if (authRequest.Password == temp_user.Password)
                            {
                                if (IsBannedUser(temp_user.ID))
                                {
                                    BanResponse banResponse = new BanResponse();
                                    DataTransferHelper.SendResponseToClient(temp_client, banResponse);
                                }
                                else
                                {
                                    if (IsConnectedUser(temp_user.ID))
                                    {
                                        AuthResponse authResponse = new AuthResponse(temp_user);
                                        authResponse.Error = "user_is_connected";
                                        authResponse.Ok    = false;
                                        DataTransferHelper.SendResponseToClient(temp_client, authResponse);
                                    }
                                    else
                                    {
                                        AuthResponse authResponse = new AuthResponse(temp_user);
                                        DataTransferHelper.SendResponseToClient(temp_client, authResponse);
                                        temp_user.IsConnected = true;
                                        RaisePropertyChangedEvent("UserDataBase");
                                        RaisePropertyChangedEvent("ConnectedUsers");
                                        _clients.Add(temp_user.ID, temp_client);
                                        _clients.Remove(client_user.ID);
                                        client_user.ID       = temp_user.ID;
                                        client_user.Name     = temp_user.Name;
                                        client_user.Password = temp_user.Password;

                                        for (int i = 0; i < ConnectedUsers.Count(); i++)
                                        {
                                            if (ConnectedUsers.ElementAt(i).ID == client_user.ID)
                                            {
                                                continue;
                                            }
                                            UserSignInResponse userSignInResponse = new UserSignInResponse(ConnectedUsers.ElementAt(i));
                                            DataTransferHelper.SendResponseToClient(temp_client, userSignInResponse);
                                        }
                                        for (int i = 0; i < _clients.Count(); i++)
                                        {
                                            if (_clients.ElementAt(i).Key == client_user.ID)
                                            {
                                                continue;
                                            }
                                            UserSignInResponse userSignInResponse = new UserSignInResponse(client_user);
                                            DataTransferHelper.SendResponseToClient(_clients.ElementAt(i).Value, userSignInResponse);
                                        }
                                    }
                                }
                            }
                            else
                            {
                                AuthResponse authResponse = new AuthResponse(null)
                                {
                                    Ok    = false,
                                    Error = "invalid_password"
                                };
                                DataTransferHelper.SendResponseToClient(temp_client, authResponse);
                            }
                        }
                        else
                        {
                            AuthResponse authResponse = new AuthResponse(null)
                            {
                                Ok    = false,
                                Error = "user_name_not_exists"
                            };
                            DataTransferHelper.SendResponseToClient(temp_client, authResponse);
                        }

                        break;
                    }

                    case "signout":
                    {
                        GetUserByID(client_user.ID).IsConnected = false;
                        throw new Exception();
                    }

                    case "message":
                    {
                        MessageResponse messageResponse = new MessageResponse((client_request as MessageRequest).Message);
                        SendResponseToAllClients(messageResponse);
                        break;
                    }

                    default:
                        break;
                    }
                }
            }
            catch (Exception e)
            {
            }
            finally
            {
                temp_client?.Close();
                _clients.Remove(client_user.ID);
                if (client_user != null)
                {
                    User temp_user = GetUserByID(client_user.ID);
                    if (temp_user != null)
                    {
                        temp_user.IsConnected = false;
                    }
                    UserSignOutResponse userSignOutResponse = new UserSignOutResponse(client_user);
                    SendResponseToAllClients(userSignOutResponse);
                }
                RaisePropertyChangedEvent("UserDataBase");
                RaisePropertyChangedEvent("ConnectedUsers");
            }
        }
Пример #3
0
 private void SendResponseToUserByUserID(String user_id, Response response)
 {
     DataTransferHelper.SendResponseToClient(GetClientByUserID(user_id), response);
 }