Ejemplo n.º 1
0
        // Handle data received event
        private void OnReceivedData(byte[] buffer, int offset, int size)
        {
            // Decode the raw bytes and appened them to buffer
            dataBuffer += Encoding.UTF8.GetString(buffer, offset, size);

            // Split the string
            var segments           = dataBuffer.Split(MessageEndMark);
            var validSegmentsCount = segments.Length - (dataBuffer.EndsWith(MessageEndMark) ? 0 : 1);

            // Extract request from segments and emit them
            for (int i = 0; i < validSegmentsCount; i++)
            {
                if (segments[i].Length > 0)
                {
                    // Notify the server
                    if (OnSessionMessage != null)
                    {
                        OnSessionMessage.Invoke(segments[i]);
                    }
                }
            }

            // Add last segment to the buffer for further processing if not valid
            dataBuffer = dataBuffer.EndsWith(MessageEndMark) ? string.Empty : segments[segments.Length - 1];
        }
 /// <summary>
 /// Initialize the handler factory.
 /// </summary>
 /// <param name="receiverNamespace"></param>
 /// <param name="receiverPath"></param>
 /// <param name="messageHandler"></param>
 /// <param name="options"></param>
 public SessionMessageAsyncHandlerFactory(string receiverNamespace, string receiverPath, OnSessionMessage messageHandler, OnSessionMessageOptions options)
 {
     _receiverNamespace = receiverNamespace;
     _receiverPath = receiverPath;
     _messageHandler = messageHandler;
     _options = options;
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Initialize the handler factory.
 /// </summary>
 /// <param name="receiverNamespace"></param>
 /// <param name="receiverPath"></param>
 /// <param name="messageHandler"></param>
 /// <param name="options"></param>
 public SessionMessageAsyncHandlerFactory(string receiverNamespace, string receiverPath, OnSessionMessage messageHandler, OnSessionMessageOptions options)
 {
     _receiverNamespace = receiverNamespace;
     _receiverPath      = receiverPath;
     _messageHandler    = messageHandler;
     _options           = options;
 }
Ejemplo n.º 4
0
 public SessionMessageAsyncHandler(string receiverNamespace, string receiverPath, MessageSession session, OnSessionMessage messageHandler, OnSessionMessageOptions options)
 {
     _receiverNamespace = receiverNamespace;
     _receiverPath = receiverPath;
     _session = session;
     _messageHandler = messageHandler;
     _options = options;
 }
Ejemplo n.º 5
0
 public SessionMessageAsyncHandler(string receiverNamespace, string receiverPath, MessageSession session, OnSessionMessage messageHandler, OnSessionMessageOptions options)
 {
     _receiverNamespace = receiverNamespace;
     _receiverPath      = receiverPath;
     _session           = session;
     _messageHandler    = messageHandler;
     _options           = options;
 }
Ejemplo n.º 6
0
        public Task StartAsync(OnSessionMessage messageHandler, OnSessionMessageException exceptionHandler, OnSessionMessageOptions options)
        {
            lock (_initializationLock)
            {
                if (_initialized)
                {
                    throw new MessageReceiverException("Message receiver has already been initialized.");
                }

                if (messageHandler == null)
                {
                    throw new ArgumentNullException("messageHandler");
                }

                if (options == null)
                {
                    options = _defaultOptions;
                }

                // Log.
                ServiceBusEventSource.Log.SessionMessagePumpStart(GetType().Name, Namespace, Path, options.AutoRenewSessionTimeout, options.MaxConcurrentSessions);

                // Initialize the handler options.
                var sessionHandlerOptions = new SessionHandlerOptions();
                sessionHandlerOptions.AutoComplete          = options.AutoComplete;
                sessionHandlerOptions.AutoRenewTimeout      = options.AutoRenewSessionTimeout;
                sessionHandlerOptions.MaxConcurrentSessions = options.MaxConcurrentSessions;
                sessionHandlerOptions.MessageWaitTimeout    = options.MessageWaitTimeout;
                sessionHandlerOptions.ExceptionReceived    += (s, e) =>
                {
                    if (e.Exception != null)
                    {
                        // Log.
                        ServiceBusEventSource.Log.MessagePumpExceptionReceived(Namespace, Path,
                                                                               e.Action, e.Exception);

                        // Handle exception.
                        if (exceptionHandler != null)
                        {
                            exceptionHandler(e.Action, e.Exception);
                        }
                    }
                };

                // Mark receiver as initialized.
                _initialized = true;

                // Start.
                return(OnStartAsync(new SessionMessageAsyncHandlerFactory(Namespace, Path, messageHandler, options), new SessionHandlerOptions
                {
                    AutoComplete = options.AutoComplete,
                    AutoRenewTimeout = options.AutoRenewSessionTimeout,
                    MaxConcurrentSessions = options.MaxConcurrentSessions,
                    MessageWaitTimeout = options.MessageWaitTimeout
                }));
            }
        }
        public Task StartAsync(OnSessionMessage messageHandler, OnSessionMessageException exceptionHandler, OnSessionMessageOptions options)
        {
            lock (_initializationLock)
            {
                if (_initialized)
                    throw new MessageReceiverException("Message receiver has already been initialized.");

                if (messageHandler == null)
                    throw new ArgumentNullException("messageHandler");

                if (options == null)
                    options = new OnSessionMessageOptions();

                // Log.
                ServiceBusEventSource.Log.StartSessionMessageReceiver(GetType().Name, Namespace, Path);

                // Initialize the handler options.
                var sessionHandlerOptions = new SessionHandlerOptions();
                sessionHandlerOptions.AutoComplete = options.AutoComplete;
                sessionHandlerOptions.AutoRenewTimeout = options.AutoRenewSessionTimeout;
                sessionHandlerOptions.MaxConcurrentSessions = options.MaxConcurrentSessions;
                sessionHandlerOptions.MessageWaitTimeout = options.MessageWaitTimeout;
                sessionHandlerOptions.ExceptionReceived += (s, e) => 
                {
                    if (e.Exception != null)
                    {
                        // Log.
                        ServiceBusEventSource.Log.SessionMessageReceiverException(Namespace, Path,
                            null, null, null, e.Action, e.Exception.Message, e.Exception.StackTrace);

                        // Handle exception.
                        if (exceptionHandler != null)
                            exceptionHandler(e.Action, e.Exception);
                    }
                };

                // Mark receiver as initialized.
                _initialized = true;

                // Start.
                return OnStartAsync(new SessionMessageAsyncHandlerFactory(Namespace, Path, messageHandler, options), new SessionHandlerOptions
                {
                    AutoComplete = options.AutoComplete,
                    AutoRenewTimeout = options.AutoRenewSessionTimeout,
                    MaxConcurrentSessions = options.MaxConcurrentSessions,
                    MessageWaitTimeout = options.MessageWaitTimeout
                });
            }
        }
 public static Task StartAsync(this ISessionMessagePump pump, OnSessionMessage messageHandler)
 {
     return(pump.StartAsync(messageHandler, null, null));
 }
 public static Task StartAsync(this ISessionMessagePump pump, OnSessionMessage messageHandler, OnSessionMessageException exception)
 {
     return(pump.StartAsync(messageHandler, exception, null));
 }
 public static Task StartAsync(this ISessionMessagePump pump, OnSessionMessage messageHandler)
 {
     return pump.StartAsync(messageHandler, null, null);
 }
 public static Task StartAsync(this ISessionMessagePump pump, OnSessionMessage messageHandler, OnSessionMessageException exception)
 {
     return pump.StartAsync(messageHandler, exception, null);
 }