Ejemplo n.º 1
0
        private async void ReceivePackage()
        {
            try
            {
                logger.LogInformation("NetworkService.receivePackage thread sucessfully started");
                while (Running)
                {
                    await Task.Delay(1);

                    if (Running)
                    {
                        lock (AddRemoveLocker)
                        {
                            if (++recievedPackageInterationCounter == 1000)
                            {
                                recievedPackageInterationCounter = 0;
                                foreach (var client in ClientConnections)
                                {
                                    try
                                    {
                                        packageParser.ParserPackageToStream(new KeepAlivePackage(), client.Writer);
                                    }
                                    catch (Exception)
                                    {
                                        invalidConnections.Add(client);
                                        logger.LogInformation("KeepAlive Exception");
                                    }
                                }
                            }
                            if (invalidConnections.Count > 0)
                            {
                                foreach (var conn in invalidConnections)
                                {
                                    ClientConnections.Remove(conn);
                                }
                                invalidConnections.Clear();
                            }
                        }
                        var clientConArr = ClientConnections.ToArray();
                        foreach (var client in clientConArr)
                        {
                            if (client.AvailableBytes > 0)
                            {
                                logger.LogInformation($"Package from Client {client.ConnectionId}");
                                var package = packageParser.ParserPackageFromStream(client.Reader);
                                packageDispatcher.DispatchPackage(package, client);
                            }
                        }
                    }
                }
            }
            finally
            {
                logger.LogInformation("Server Stopped");
            }
        }
        public void HandleLogin(NetworkConnection connection, LoginRequestPackage parsedObjectData)
        {
            logger.LogInformation("Login Request");
            logger.LogInformation($"Login DATA User: {parsedObjectData.Username} PW: {parsedObjectData.Password}");

            if (!UserRepo.UserExists(parsedObjectData.Username))
            {
                return;
            }
            (var passwordOK, int userId) = UserRepo.PasswordOK(parsedObjectData.Username, parsedObjectData.Password);
            if (!passwordOK)
            {
                logger.LogInformation("Wrong Password");
                return;
            }
            else
            {
                packageParser.ParserPackageToStream(new LoginResponsePackage
                {
                    IsValid = true
                }, connection.Writer);
            }
        }