protected override async Task OnClientMessageAsync(ConnectionDataMessage connectionDataMessage)
 {
     if (connectionDataMessage.TracingId != null)
     {
         MessageLog.ReceiveMessageFromService(Logger, connectionDataMessage);
     }
     if (_clientConnectionManager.ClientConnections.TryGetValue(connectionDataMessage.ConnectionId, out var connection))
     {
         try
         {
             var payload = connectionDataMessage.Payload;
             Log.WriteMessageToApplication(Logger, payload.Length, connectionDataMessage.ConnectionId);
             await connection.WriteMessageAsync(payload);
         }
         catch (Exception ex)
         {
             Log.FailToWriteMessageToApplication(Logger, connectionDataMessage, ex);
         }
     }
     else
     {
         // Unexpected error
         Log.ReceivedMessageForNonExistentConnection(Logger, connectionDataMessage);
     }
 }
Exemple #2
0
        public override Task SendAllExceptAsync(string methodName, object[] args, IReadOnlyList <string> excludedIds, CancellationToken cancellationToken = default)
        {
            if (IsInvalidArgument(methodName))
            {
                throw new ArgumentException(NullOrEmptyStringErrorMessage, nameof(methodName));
            }

            var message = new BroadcastDataMessage(excludedIds, SerializeAllProtocols(methodName, args)).WithTracingId();

            if (message.TracingId != null)
            {
                MessageLog.StartToBroadcastMessage(Logger, message);
            }
            return(WriteAsync(message));
        }
 private async Task WriteCoreAsync <T>(T message, Func <T, Task> task) where T : ServiceMessage, IMessageWithTracingId
 {
     try
     {
         await task(message);
     }
     catch (Exception ex)
     {
         MessageLog.FailedToSendMessage(Logger, message, ex);
         throw;
     }
     if (message.TracingId != null)
     {
         MessageLog.SucceededToSendMessage(Logger, message);
     }
 }
        private async Task <bool> WriteAckableCoreAsync <T>(T message, Func <T, Task <bool> > task) where T : ServiceMessage, IMessageWithTracingId
        {
            try
            {
                var result = await task(message);

                if (message.TracingId != null)
                {
                    MessageLog.SucceededToSendMessage(Logger, message);
                }
                return(result);
            }
            catch (Exception ex)
            {
                MessageLog.FailedToSendMessage(Logger, message, ex);
                throw;
            }
        }
Exemple #5
0
        public override Task SendConnectionsAsync(IReadOnlyList <string> connectionIds, string methodName, object[] args, CancellationToken cancellationToken = default)
        {
            if (IsInvalidArgument(connectionIds))
            {
                throw new ArgumentException(NullOrEmptyStringErrorMessage, nameof(connectionIds));
            }

            if (IsInvalidArgument(methodName))
            {
                throw new ArgumentException(NullOrEmptyStringErrorMessage, nameof(methodName));
            }

            var message = new MultiConnectionDataMessage(connectionIds, SerializeAllProtocols(methodName, args)).WithTracingId();

            if (message.TracingId != null)
            {
                MessageLog.StartToSendMessageToConnections(Logger, message);
            }
            return(WriteAsync(message));
        }
Exemple #6
0
        public override Task RemoveFromGroupAsync(string connectionId, string groupName, CancellationToken cancellationToken = default)
        {
            if (IsInvalidArgument(connectionId))
            {
                throw new ArgumentException(NullOrEmptyStringErrorMessage, nameof(connectionId));
            }

            if (IsInvalidArgument(groupName))
            {
                throw new ArgumentException(NullOrEmptyStringErrorMessage, nameof(groupName));
            }

            var message = new LeaveGroupWithAckMessage(connectionId, groupName).WithTracingId();

            if (message.TracingId != null)
            {
                MessageLog.StartToRemoveConnectionFromGroup(Logger, message);
            }
            return(WriteAckableMessageAsync(message));
        }
Exemple #7
0
        public override Task SendUserAsync(string userId, string methodName, object[] args, CancellationToken cancellationToken = default)
        {
            if (IsInvalidArgument(userId))
            {
                throw new ArgumentException(NullOrEmptyStringErrorMessage, nameof(userId));
            }

            if (IsInvalidArgument(methodName))
            {
                throw new ArgumentException(NullOrEmptyStringErrorMessage, nameof(methodName));
            }

            var message = new UserDataMessage(userId, SerializeAllProtocols(methodName, args)).WithTracingId();

            if (message.TracingId != null)
            {
                MessageLog.StartToSendMessageToUser(Logger, message);
            }
            return(WriteAsync(message));
        }
Exemple #8
0
        public override Task SendGroupAsync(string groupName, string methodName, object[] args, CancellationToken cancellationToken = default)
        {
            if (IsInvalidArgument(groupName))
            {
                throw new ArgumentException(NullOrEmptyStringErrorMessage, nameof(groupName));
            }

            if (IsInvalidArgument(methodName))
            {
                throw new ArgumentException(NullOrEmptyStringErrorMessage, nameof(methodName));
            }

            var message = new GroupBroadcastDataMessage(groupName, null, SerializeAllProtocols(methodName, args)).WithTracingId();

            if (message.TracingId != null)
            {
                MessageLog.StartToBroadcastMessageToGroup(Logger, message);
            }
            return(WriteAsync(message));
        }
 private ServiceMessage CreateMessage(string connectionId, string methodName, object[] args, ClientConnectionContext serviceConnectionContext)
 {
     if (serviceConnectionContext.Protocol != null)
     {
         var message = new ConnectionDataMessage(connectionId, SerializeProtocol(serviceConnectionContext.Protocol, methodName, args)).WithTracingId();
         if (message.TracingId != null)
         {
             MessageLog.StartToSendMessageToConnection(Logger, message);
         }
         return(message);
     }
     else
     {
         var message = new MultiConnectionDataMessage(new[] { connectionId }, SerializeAllProtocols(methodName, args)).WithTracingId();
         if (message.TracingId != null)
         {
             MessageLog.StartToSendMessageToConnections(Logger, message);
         }
         return(message);
     }
 }
        public override async Task SendConnectionAsync(string connectionId, string methodName, object[] args, CancellationToken cancellationToken = default)
        {
            if (IsInvalidArgument(connectionId))
            {
                throw new ArgumentException(NullOrEmptyStringErrorMessage, nameof(connectionId));
            }

            if (IsInvalidArgument(methodName))
            {
                throw new ArgumentException(NullOrEmptyStringErrorMessage, nameof(methodName));
            }

            if (_clientConnectionManager.ClientConnections.TryGetValue(connectionId, out var serviceConnectionContext))
            {
                var message = new MultiConnectionDataMessage(new[] { connectionId }, SerializeAllProtocols(methodName, args)).WithTracingId();
                if (message.TracingId != null)
                {
                    MessageLog.StartToSendMessageToConnections(Logger, message);
                }

                try
                {
                    // Write directly to this connection
                    await serviceConnectionContext.ServiceConnection.WriteAsync(message);

                    if (message.TracingId != null)
                    {
                        MessageLog.SucceededToSendMessage(Logger, message);
                    }
                    return;
                }
                catch (Exception ex)
                {
                    MessageLog.FailedToSendMessage(Logger, message, ex);
                    throw;
                }
            }

            await base.SendConnectionAsync(connectionId, methodName, args, cancellationToken);
        }
Exemple #11
0
        public override Task SendGroupsAsync(IReadOnlyList <string> groupNames, string methodName, object[] args, CancellationToken cancellationToken = default)
        {
            if (IsInvalidArgument(groupNames))
            {
                throw new ArgumentException(NullOrEmptyStringErrorMessage, nameof(groupNames));
            }

            if (IsInvalidArgument(methodName))
            {
                throw new ArgumentException(NullOrEmptyStringErrorMessage, nameof(methodName));
            }

            var message = new MultiGroupBroadcastDataMessage(groupNames, SerializeAllProtocols(methodName, args)).WithTracingId();

            if (message.TracingId != null)
            {
                MessageLog.StartToBroadcastMessageToGroups(Logger, message);
            }
            // Send this message from a random service connection because this message involves of multiple groups.
            // Unless we send message for each group one by one, we can not guarantee the message order for all groups.
            return(WriteAsync(message));
        }