Ejemplo n.º 1
0
    public void Execute()
    {
        Debug.Log("Build test connection.");
        _clientConnection.Connect("127.0.0.1", 6543);
        Debug.Log("Creating streams.");

        Debug.Log("Write login packages");

        string username = UsernameField.text;
        string password = PasswordField.text;

        _packageParser.ParsePackageToStream(new LoginRequestPackage
        {
            Username = username,
            Password = password,
        }, _clientConnection.Writer);

        Debug.Log("Receive Login Response Packages");
        var packageData = _packageParser.ParsePackageFromStream(_clientConnection.Reader);

        Debug.Log($"Received Login Response Package TYPE: {packageData.GetType()} result: {(packageData as LoginResponsePackage).IsValid}");

        MenuManager menuManager = GetComponentInChildren <MenuManager>(true);

        menuManager.RaceSelectionMenu();
    }
        private async void ReceivePackage()
        {
            try {
                logger.LogInformation("NetworkService.ReceivePackage thread successfully started");
                while (IsRunning)
                {
                    await Task.Delay(1);

                    if (IsRunning)
                    {
                        lock (locker) {
                            if (++receivePackageIterationCounter == 6000)
                            {
                                receivePackageIterationCounter = 0;
                                foreach (var client in clients)
                                {
                                    try {
                                        // packageParser.ParsePackageToStream(new KeepAlivePackage(), client.Writer);
                                    }
                                    catch (Exception e) {
                                        invalidConnections.Add(client); // TODO: Set this to use time and keep alive every 5/6 seconds
                                        clients.Remove(client);
                                        logger.LogInformation($"KEEP_ALIVE Exception client: {client.ConnectionId}");
                                    }
                                }
                            }
                            if (invalidConnections.Count > 0)
                            {
                                foreach (var client in invalidConnections)
                                {
                                    clients.Remove(client);
                                }
                                invalidConnections.Clear();
                            }
                        }
                    }
                    var clientConnArr = clients.ToArray();
                    foreach (var client in clientConnArr)
                    {
                        if (client.AvailableBytes > 0)
                        {
                            logger.LogInformation($"Package from client {client.ConnectionId}");
                            var package = packageParser.ParsePackageFromStream(client.Reader);
                            packageDispatcher.DispatchPackage(client, package);
                        }
                    }
                }
            }finally {
                logger.LogInformation("Server stopped...");
            }
        }
Ejemplo n.º 3
0
        private async void OnPackageReceive(object obj)
        {
            try
            {
                _logger.LogInformation("NetworkService.ReceiveThread successfully started.");
                while (Running)
                {
                    await Task.Delay(1);

                    if (Running)
                    {
                        lock (AddRemoveLocker)
                        {
                            if (++_packageCounter == 1000)
                            {
                                _packageCounter = 0;
                                foreach (ClientConnection client in _clients)
                                {
                                    try
                                    {
                                        _packageParser.ParsePackageToStream(new KeepAllivePackage(), client.Writer);
                                    }
                                    catch (Exception e)
                                    {
                                        //_invalidConnections.Add(client);
                                        _clients.Remove(client);
                                        _logger.LogWarning($"NetworkServices.OnPackageReceive KEEP ALIVE Exception: {e.Message}");
                                    }
                                }
                            }

                            foreach (ClientConnection client in _clients)
                            {
                                if (0 < client.AvailableBytes)
                                {
                                    _logger.LogInformation($"Package from client {client.ConnectionId}");
                                    PackageBase package = _packageParser.ParsePackageFromStream(client.Reader);
                                    _packageDispatcher.DispatchPackage(package, client);
                                }
                            }
                        }
                    }
                }
            }
            finally
            {
                _logger.LogError("NetworkServices stopped.");
            }
        }
    private void HandlePackageInput(object obj)
    {
        Package package = packageParser.ParsePackageFromStream(clientConnection.Reader);

        IncomingPackage?.Invoke(this, new IncomingPackageArgs(package));
        foreach (var keyValuePair in waitingThread)
        {
            if (!keyValuePair.Key.Equals(package.GetType().Name))
            {
                continue;
            }
            keyValuePair.Value.Item2.Value = package;
            keyValuePair.Value.Item1.Set();
        }
    }