Beispiel #1
0
        public BaseUdpClient(
            IPEndPoint endPoint,
            IMessageSource messageSource,
            IMessageDispatcher messageDispatcher)
            : base(endPoint)
        {
            Session = GetSession(endPoint);

            _messageSource     = messageSource;
            _messageDispatcher = messageDispatcher;
            _logger            = Log.ForContext <BaseUdpClient>();

            // TODO: Remove byte array allocation
            _messageDispatcher.OnSent += (session, buffer) =>
            {
                var data = buffer.ToArray();
                _logger.Verbose(
                    "Handling OnSent " +
                    $"(EndPoint='{session.EndPoint}', " +
                    $"Data='{BitConverter.ToString(data)}')."
                    );
                SendAsync(session.EndPoint, data);
            };
            _messageSource.Subscribe <AcknowledgeMessage>((session, message) =>
            {
                _messageDispatcher.Acknowledge(message.ResponseId, message.MessageHandled);
                return(Task.CompletedTask);
            });
            _messageSource.Subscribe((session, message) =>
            {
                if (message is IReliableRequest reliableRequest)
                {
                    _messageDispatcher.Send(session, new AcknowledgeMessage()
                    {
                        ResponseId     = reliableRequest.RequestId,
                        MessageHandled = true
                    });
                }
                return(Task.CompletedTask);
            });
        }
Beispiel #2
0
 protected void Register <TMessage>(MessageHandler <TMessage> messageHandler)
     where TMessage : class, IMessage =>
 _messageSource.Subscribe(messageHandler);