Пример #1
0
        public async Task Disconnect(TransportManager transportManager, CancellationToken ct = default)
        {
            _logger.Information("Handling disconnection");

            await(OnDisconnected?.Invoke(_clientId) ?? Task.CompletedTask);
            var olehMessage = Message.Serialize(new OlehMessage());
            await Task.WhenAny(
                transportManager.Get(ProtocolType.Tcp).SendServerMessageAsync(olehMessage),
                transportManager.Get(ProtocolType.Udp).SendServerMessageAsync(olehMessage));
        }
Пример #2
0
        public async Task Connect(TransportManager transportManager, CancellationToken ct = default)
        {
            _logger.Information("Handling connection handshake");

            transportManager.OnConnected += OnTransportConnected;
            transportManager.OnReceived  += OnTransportReceived;

            var tcpTransport = transportManager.Get(ProtocolType.Tcp);
            var udpTransport = transportManager.Get(ProtocolType.Udp);

            await tcpTransport.StartAsync(ct);

            await _asyncEvent.WaitAsync(ct);

            _logger.Verbose("TCP Transport connected; sending Helo");

            _receiveCts = new TaskCompletionSource <HeloAckMessage>();
            await tcpTransport.SendServerMessageAsync(Message.Serialize(new HeloMessage()));

            var ack = await _receiveCts.Task;

            _logger.Verbose("Received TCP HeloAck");

            await udpTransport.StartAsync(ct);

            await _asyncEvent.WaitAsync(ct);

            _logger.Verbose("UDP Transport connected; sending Helo");

            _receiveCts = new TaskCompletionSource <HeloAckMessage>();
            await udpTransport.SendServerMessageAsync(Message.Serialize(new HeloMessage {
                ClientId = ack.ClientId
            }));

            ack = await _receiveCts.Task;

            _logger.Verbose("Received UDP HeloAck");

            transportManager.OnConnected -= OnTransportConnected;
            transportManager.OnReceived  -= OnTransportReceived;

            _clientId = ack.ClientId;
            await tcpTransport.SendServerMessageAsync(Message.Serialize(new ClientReadyMessage {
                ClientId = _clientId
            }));

            _logger.Information("Connection established with {RemoteEndPoint}; assigned {ClientId:s}", _serverEndPoint, _clientId);

            await(OnConnected?.Invoke(_clientId) ?? Task.CompletedTask);
        }