Ejemplo n.º 1
0
        private MainMessage RoomExit(ulong msgId, ulong clientId, RoomExit roomExit)
        {
            _log.Debug("In RoomExit method.");
            int   roomId = (int)roomExit.RoomId;
            ulong?userId = _api.Services.User.GetUserIdByClientId(clientId);

            if (!userId.HasValue || roomId >= _roomList.Count || _roomList[roomId] == null)
            {
                return(ISystemService.CreateErrorMessage(msgId, 0, 0, "Unable to match client ID to any user."));
            }
            lock (_roomList[roomId])
            {
                int idx = _roomList[roomId].Players.FindIndex(x => x == userId.Value);
                if (idx < 0)
                {
                    return(ISystemService.CreateErrorMessage(msgId, 0, 0, "Your client is not connected to this room."));
                }
                Room room = _roomList[roomId];
                room.Players.RemoveAt(idx);
                room.LastActivity = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
                OnUserDisconnected(userId.Value, room.Id, "Disconnected");
                _user2Room[userId.Value] = null;
                _log.Info($"User with id '{userId.Value}' exited room '{room.Name}'.");
            }
            return(ISystemService.CreateOkMessage(msgId));
        }
Ejemplo n.º 2
0
 private MainMessage RoomListServer(ulong msgId, int serverId, RoomList roomList)
 {
     if (!_roomsPerServer.ContainsKey(serverId))
     {
         _roomsPerServer[serverId] = new List <Room>();
     }
     lock (_roomsPerServer[serverId])
     {
         _roomsPerServer[serverId].Clear();
         _roomsPerServer[serverId].AddRange(roomList.RoomList_.Select(x => new Room(x)));
     }
     roomList.RoomList_.AsParallel().ForAll(x => x.Players.AsParallel().ForAll(y => _user2Room[y] = x.RoomId));
     return(ISystemService.CreateOkMessage(msgId));
 }
Ejemplo n.º 3
0
        private MainMessage HandleStatMsg(MainMessage msg)
        {
            _log.Debug("In HandleStatMsg method");
            uint serverId = msg.ServerId;

            if (serverId >= computingServers.Count)
            {
                _log.Error("Unknown Computing Server.");
                return(ISystemService.CreateErrorMessage(msg.MsgId, 0, 0, "Unknown Computing Server."));
            }
            StatMsg statMsg = msg.SystemMsg.StatMsg;

            computingServers[(int)serverId].cpuUsage     = statMsg.CpuUsage;
            computingServers[(int)serverId].ramUsage     = statMsg.MemoryUsed;
            computingServers[(int)serverId].lastResponse = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
            _log.Debug($"server {serverId} status: CPU: {statMsg.CpuUsage}%, RAM: {statMsg.MemoryUsed} MB");
            return(ISystemService.CreateOkMessage((uint)msg.MsgId));
        }
Ejemplo n.º 4
0
        private MainMessage HandleHiMsg(HiMsg msg)
        {
            this._log.Debug("Received Hi message.");
            if (msg.Version != VrLifeServer.VERSION)
            {
                this._log.Debug("Not compatiable version of client.");
                return(ISystemService.CreateErrorMessage(0, 0, 0, "Not compatiable version"));
            }
            MainMessage response = ISystemService.CreateOkMessage();

            response.ServerId = (uint)computingServers.Count;
            this._log.Debug($"Sending {response.ServerId} as a new ServerID.");
            computingServers.Add(
                new ComputingServer {
                id           = response.ServerId, cores = msg.Threads,
                memory       = msg.Memory, address = new IPEndPoint(msg.Address, msg.Port),
                lastResponse = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
            return(response);
        }
Ejemplo n.º 5
0
        private MainMessage HandleUserRequest(MainMessage msg)
        {
            _log.Debug("In HandleUserRequest method.");
            try
            {
                // TODO needs enhancemnets
                UserRequestMsg userRequest = msg.UserMngMsg.UserMsg.UserRequestMsg;
                switch (userRequest.UserRequestTypeCase)
                {
                // create new User
                case UserRequestMsg.UserRequestTypeOneofCase.CreateQuery:
                    _log.Debug("Creating new user: "******"User sucessfuly created.");
                    return(new MainMessage {
                        UserMngMsg = userMngMsg
                    });

                // List users (Username LIKE '%msg.username%')
                case UserRequestMsg.UserRequestTypeOneofCase.ListQuery:
                    _log.Debug("Looking for user like: " + userRequest.ListQuery.Username);
                    UserDetailMsg   userDetail = userRequest.ListQuery;
                    UserDetailMsg[] users      = User.List(userDetail)
                                                 .Select(x => new UserDetailMsg {
                        Password = "", UserId = x.Id, Username = x.Username
                    })
                                                 .ToArray();
                    UserListMsg userList = new UserListMsg();
                    userList.Users.AddRange(users);
                    UserMngMsg userMngMsg1 = new UserMngMsg();
                    userMngMsg1.UserMsg = new UserMsg {
                        UserListMsg = userList
                    };
                    _log.Debug($"Found {users.Length} records.");
                    return(new MainMessage {
                        UserMngMsg = userMngMsg1
                    });

                // Change password, where old and new password are in same filed, separated by newline
                case UserRequestMsg.UserRequestTypeOneofCase.UpdateQuery:
                    _log.Debug("Changing password to user: "******"Password sucessfuly changed.");
                    return(ISystemService.CreateOkMessage(msg.MsgId));

                // show user by its Id
                case UserRequestMsg.UserRequestTypeOneofCase.UserIdDetail:
                    _log.Debug($"Looking for user with id {userRequest.UserIdDetail}");
                    User          user2       = User.Get(userRequest.UserIdDetail);
                    UserDetailMsg userDetail1 = new UserDetailMsg();
                    userDetail1.UserId   = user2.Id;
                    userDetail1.Password = "";
                    userDetail1.Username = user2.Username;
                    UserMsg userMsg1 = new UserMsg();
                    userMsg1.UserDetailMsg = userDetail1;
                    return(new MainMessage {
                        UserMngMsg = new UserMngMsg {
                            UserMsg = userMsg1
                        }
                    });

                case UserRequestMsg.UserRequestTypeOneofCase.UserByClientId:
                    _log.Debug($"Looking for user with client id {userRequest.UserByClientId}");
                    if (msg.SenderIdCase != MainMessage.SenderIdOneofCase.ServerId)
                    {
                        return(ISystemService.CreateErrorMessage(msg.MsgId, 0, 0, "Not enough permissions."));
                    }
                    ulong clientId = userRequest.UserByClientId;
                    if (clientId >= (ulong)_clientMachines.Count || _clientMachines[(int)clientId] == null)
                    {
                        return(ISystemService.CreateErrorMessage(msg.MsgId, 0, 0, "Unable to find user with this client ID"));
                    }
                    MainMessage respUserDetail = new MainMessage();
                    respUserDetail.UserMngMsg         = new UserMngMsg();
                    respUserDetail.UserMngMsg.UserMsg = new UserMsg();
                    respUserDetail.UserMngMsg.UserMsg.UserDetailMsg = _clientMachines[(int)clientId].ToMessage();
                    return(respUserDetail);

                default:
                    throw new ErrorMsgException("This message cannot be empty.");
                }
            }
            catch (Exception ex)
            {
                _log.Error(ex);
                return(ISystemService.CreateErrorMessage(0, 0, 0, ex.Message));
            }
        }