private async Task <bool> Connect()
        {
            var endPoint = Config.Instance.AuthAPI.EndPoint;

            try
            {
                await _client.ConnectAsync(endPoint)
                .ConfigureAwait(false);
            }
            catch (SocketException ex)
            {
                if (ex.SocketErrorCode != SocketError.ConnectionRefused)
                {
                    Logger.Error(ex, $"Failed to connect authserver on endpoint {endPoint}. Retrying on next update.");
                }
                else
                {
                    Logger.Error($"Failed to connect authserver on endpoint {endPoint}. Retrying on next update.");
                }
                return(false);
            }
            catch (Exception ex)
            {
                Logger.Error(ex, $"Failed to connect authserver on endpoint {endPoint}. Retrying on next update.");
                return(false);
            }

            return(true);
        }
Example #2
0
    private IEnumerator Start()
    {
        _client = FlexiSocket.Create("::1", 1366, Protocols.BodyLengthPrefix); //ipv6
        yield return(new WaitForSeconds(1));                                   // wait for server to startup since bot server and clients are in the same scene

        using (var connect = _client.ConnectAsync())
        {
            yield return(connect);

            if (!connect.IsSuccessful)
            {
                Debug.LogException(connect.Exception);
                yield break;
            }
            Debug.Log("Connected", this);
        }

        while (_client.IsConnected)
        {
            using (var receive = _client.ReceiveAsync())
            {
                yield return(receive);

                if (!receive.IsSuccessful)
                {
                    if (receive.Exception != null)
                    {
                        Debug.LogException(receive.Exception);
                    }
                    if (receive.Error != SocketError.Success)
                    {
                        Debug.LogError(receive.Error);
                    }
                    _client.Close();
                    yield break;
                }

                Debug.Log("Client received: " + Encoding.UTF8.GetString(receive.Data), this);
            }

            var send = _client.SendAsync("Hey I've got your message");
            yield return(send);

            if (!send.IsSuccessful)
            {
                if (send.Exception != null)
                {
                    Debug.LogException(send.Exception);
                }
                if (send.Error != SocketError.Success)
                {
                    Debug.LogError(send.Error);
                }
                _client.Close();
                yield break;
            }
            Debug.Log("Message sent", this);
            GameObject.CreatePrimitive(PrimitiveType.Cylinder);
        }
    }
Example #3
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            var(success, errorMessage) = await Helpers.Retry(async() =>
            {
                await client.ConnectAsync(conf.CsIP, conf.CsPort, stoppingToken);
                return(true);
            }, ConnectRetries, RetryIntervalMs, stoppingToken);

            if (!success)
            {
                logger.Error($"No connection could be made. Error: {errorMessage}");
                lifetime.StopApplication();
                return;
            }

            // Increment semafor initial count to 1
            synchronizationContext.SemaphoreSlim.Release();

            // Wait for player service to pick up semaphore
            while (synchronizationContext.SemaphoreSlim.CurrentCount > 0 && !stoppingToken.IsCancellationRequested)
            {
                await Task.Delay(200);
            }

            // Block until joinTheGame is sent
            await synchronizationContext.SemaphoreSlim.WaitAsync(stoppingToken);

            // Wait for JoinTheGame response
            (bool receivedMessage, Message message) = await client.ReceiveAsync(stoppingToken);

            if (!receivedMessage)
            {
                logger.Error($"Did not receive JoinTheGame response. Error: {errorMessage}");
                lifetime.StopApplication();
                return;
            }

            while (!stoppingToken.IsCancellationRequested && receivedMessage)
            {
                bool sended = await queue.SendAsync(message, stoppingToken);

                if (!sended)
                {
                    logger.Warning($"SocketService| Message id: {message.MessageID} has been lost");
                }
                (receivedMessage, message) = await client.ReceiveAsync(stoppingToken);
            }

            message = new Message(MessageID.CSDisconnected, -1, new EmptyAnswerPayload());
            await queue.SendAsync(message, stoppingToken);
        }
        public async Task InitAsync()
        {
            _sendLock.Wait();
            try
            {
                _boltProtocol = await _client.ConnectAsync(RoutingContext).ConfigureAwait(false);
            }
            finally
            {
                _sendLock.Release();
            }

            await _boltProtocol.LoginAsync(this, _userAgent, _authToken).ConfigureAwait(false);
        }
Example #5
0
        public async ValueTask Start()
        {
            if (_isRunning)
            {
                _logger.LogInformation("PushHandleService is running!");
                return;
            }
            Initialize();
            _performanceService.Start();

            await _client.ConnectAsync();

            _isRunning = true;
        }
Example #6
0
        public async ValueTask Start()
        {
            if (_isRunning)
            {
                _logger.LogInformation("MessageSendService is running!");
                return;
            }
            Initialize();
            _performanceService.Start();

            await _client.ConnectAsync();

            _isRunning = true;
            for (int i = 0; i < _option.SendThread; i++)
            {
                SendMessageTask();
            }
        }
Example #7
0
        private async Task ConnectInternalAsync()
        {
            _connectCancellationToken = new CancellationTokenSource();
            Socket.SetCancellationToken(_connectCancellationToken.Token);
            if (Socket is IWebSocketClient webSocketClient)
            {
                CurrentServer = await GetWebSocketConnectionServerAsync().ConfigureAwait(false);

                await NetLog.InfoAsync($"Connecting to WebSocket {CurrentServer}").ConfigureAwait(false);

                await webSocketClient.ConnectAsync(new Uri($"wss://{CurrentServer.ToString()}/cmsocket/")).ConfigureAwait(false);
            }
            else
            {
                CurrentServer = await GetConnectionServerAsync().ConfigureAwait(false);

                await NetLog.InfoAsync($"Connecting to endpoint {CurrentServer}").ConfigureAwait(false);

                await Socket.ConnectAsync(CurrentServer.GetIPEndPoint()).ConfigureAwait(false);
            }
        }
Example #8
0
        protected override async Task RunService(CancellationToken stoppingToken)
        {
            var(success, errorMessage) = await Helpers.Retry(async() =>
            {
                await client.ConnectAsync(conf.CsIP, conf.CsPort, stoppingToken);
                return(true);
            }, ConnectRetries, RetryIntervalMs, stoppingToken);

            if (!success)
            {
                logger.Error($"No connection could be made. Error: {errorMessage}");
                lifetime.StopApplication();
                return;
            }

            (bool receivedMessage, Message message) = await client.ReceiveAsync(stoppingToken);

            while (!stoppingToken.IsCancellationRequested && receivedMessage)
            {
                bool sended = await queue.SendAsync(message, stoppingToken);

                if (!sended)
                {
                    logger.Warning($"SocketService| Message id: {message.MessageID} has been lost");
                }
                (receivedMessage, message) = await client.ReceiveAsync(stoppingToken);
            }
            await client.CloseAsync(stoppingToken);

            message = new Message()
            {
                AgentID   = -1,
                MessageID = MessageID.CSDisconnected,
                Payload   = new EmptyPayload()
            };
            await queue.SendAsync(message, stoppingToken);
        }
Example #9
0
 public void Connect()
 {
     StartCoroutine(client.ConnectAsync());
 }
        public async Task InitAsync()
        {
            _boltProtocol = await _client.ConnectAsync().ConfigureAwait(false);

            await _boltProtocol.AuthenticateAsync(this, _userAgent, _authToken).ConfigureAwait(false);
        }