public void StartService()
        {
            //запуск каждые 2 минуты.
            Thread.Sleep(90000);
            foreach (var connectUser in Startup.ConnectedUsers)
            {
                try
                {
                    var request = new SocketRequest <int>();
                    request.Command  = "check";
                    request.Data     = 1;
                    request.TypeData = "chk";
                    var bytes = request.Serialize();
                    connectUser.Client.GetStream().Write(bytes, 0, bytes.Length);
                }
                catch (Exception e)
                {
                    //Пользователь был отключен от сервера.

                    Startup.ConnectedUsers.Remove(connectUser); //отключаем
                }
            }
        }
Example #2
0
        private void ExcecuteCommand <T>(Models.ICommand command, SocketRequest <T> request)
        {
            _logger.Debug($"Выполнение команды {request.Command}...");

            Task.Run(() =>
            {
                byte[] bytes;

                ResponseCommand resp;
                User user = null;

                if (request.Token is null)
                {
                    if (request.Command == "log" || request.Command == "reg")
                    {
                    }
                    else
                    {
                        var response      = new SocketRequest <long>();
                        response.Command  = request.Command;
                        response.TypeData = "error";
                        response.Data     = 1;

                        bytes = response.Serialize();

                        _stream.Write(bytes, 0, bytes.Length);
                        _logger.Debug("Команда выполнена.");
                        return;
                    }
                }
                else
                {
                    using (var db = new ServerData())
                    {
                        user = db.Users.FirstOrDefault(u => u.Token == request.Token);
                    }


                    if (user is null && (request.Command != "reg" || request.Command != "log"))
                    {
                        //todo: проверка на правильный токен..
                        _logger.Debug("f");
                    }

                    //Проверка на онлайн
                    if (Startup.ConnectedUsers.Any(c => c.User.UserId != user?.UserId))
                    {
                        //Пользователя нет в сети.
                        Startup.ConnectedUsers.Add(new ConnectUser()
                        {
                            LastCheck = DateTime.Now,
                            Client    = _client,
                            User      = user,
                            Session   = this
                        });
                    }
                }


                resp = command.Execute(request.Data, user, _logger);

                switch (resp.TypeData)
                {
                case "reg":
                    var response      = new SocketRequest <RegisterResponse>();
                    response.TypeData = resp.TypeData;
                    response.Token    = request.Token;
                    response.Data     = (RegisterResponse)resp.Data;
                    bytes             = response.Serialize();
                    break;

                case "log":
                    var response1      = new SocketRequest <LoginResponse>();
                    response1.TypeData = resp.TypeData;
                    response1.Token    = request.Token;
                    response1.Data     = (LoginResponse)resp.Data;
                    bytes = response1.Serialize();
                    break;

                case "msg.snd":
                    var response2      = new SocketRequest <SendResponse>();
                    response2.TypeData = resp.TypeData;
                    response2.Token    = request.Token;
                    response2.Data     = (SendResponse)resp.Data;
                    bytes = response2.Serialize();
                    break;

                case "msg.getChat":
                    var response3      = new SocketRequest <GetChatResponse>();
                    response3.TypeData = resp.TypeData;
                    response3.Token    = request.Token;
                    response3.Data     = (GetChatResponse)resp.Data;
                    bytes = response3.Serialize();
                    break;

                case "msg.get":
                    var response4      = new SocketRequest <GetResponseProxy>();
                    response4.TypeData = resp.TypeData;
                    response4.Token    = request.Token;
                    response4.Data     = (GetResponseProxy)resp.Data;
                    bytes = response4.Serialize();
                    break;

                case "usr.info":
                    var response5      = new SocketRequest <User>();
                    response5.TypeData = resp.TypeData;
                    response5.Token    = request.Token;
                    response5.Data     = (User)resp.Data;
                    bytes = response5.Serialize();
                    break;

                case "error":
                    var response6      = new SocketRequest <int>();
                    response6.TypeData = resp.TypeData;
                    response6.Token    = request.Token;
                    response6.Data     = (int)resp.Data;
                    bytes = response6.Serialize();
                    break;

                default:
                    new Exception("default");
                    bytes = null;
                    break;
                }
                _stream.Write(bytes, 0, bytes.Length);
                _logger.Debug("Команда выполнена.");
            });
        }