private async ValueTask OnNewConnection(NewConnectionEventArgs e)
        {
            MessageHello.Deserialize(e.HandshakeData, out int announcementVersion, out int id, out Language language);

            _logger.LogDebug("Client requested announcement (version: {Version}, id: {Id}, language: {Language})", announcementVersion, id, language);

            if (announcementVersion != 2)
            {
                await e.Connection.Disconnect("Unsupported announcement version");

                return;
            }

            var @event = new AnnouncementRequestEvent(id, language);
            await _eventManager.CallAsync(@event);

            var response = @event.Response;

            if (response.UseCached)
            {
                using var writer = MessageWriter.Get(MessageType.Reliable);
                Message00UseCache.Serialize(writer);
                await e.Connection.SendAsync(writer);

                _logger.LogDebug("Sent UseCache response");
            }

            if (response.Announcement != null)
            {
                using var writer = MessageWriter.Get(MessageType.Reliable);
                Announcement announcement = response.Announcement.Value;
                Message01Update.Serialize(writer, announcement.Id, announcement.Message);
                await e.Connection.SendAsync(writer);

                _logger.LogDebug("Sent ({Id}) {Message}", announcement.Id, announcement.Message);
            }

            if (response.FreeWeekendState != FreeWeekendState.NotFree)
            {
                using var writer = MessageWriter.Get(MessageType.Reliable);
                Message02SetFreeWeekend.Serialize(writer, response.FreeWeekendState);
                await e.Connection.SendAsync(writer);

                _logger.LogDebug("Sent {FreeWeekendState} weekend state", response.FreeWeekendState);
            }

            await e.Connection.Disconnect(null);
        }
Beispiel #2
0
        private async ValueTask OnNewConnection(NewConnectionEventArgs e)
        {
            MessageHello.Deserialize(e.HandshakeData, out var announcementVersion, out var id, out var language);

            if (announcementVersion != 2)
            {
                await e.Connection.Disconnect("Unsupported announcement version");

                return;
            }

            var @event = new AnnouncementRequestEvent(id, language);
            await _eventManager.CallAsync(@event);

            var response = @event.Response;

            if (response.UseCached)
            {
                using var writer = MessageWriter.Get(MessageType.Reliable);
                Message00UseCache.Serialize(writer);
                await e.Connection.SendAsync(writer);
            }

            if (response.Announcement != null)
            {
                using var writer = MessageWriter.Get(MessageType.Reliable);
                var announcement = response.Announcement.Value;
                Message01Update.Serialize(writer, announcement.Id, announcement.Message);
                await e.Connection.SendAsync(writer);
            }

            if (response.FreeWeekendState != FreeWeekendState.NotFree)
            {
                using var writer = MessageWriter.Get(MessageType.Reliable);
                Message02SetFreeWeekend.Serialize(writer, response.FreeWeekendState);
                await e.Connection.SendAsync(writer);
            }

            await e.Connection.Disconnect(null);
        }