Beispiel #1
0
        /// <summary>
        /// Starts dispatching the messages and won't yield until
        /// the client has stopped or has disconnected.
        /// </summary>
        /// <returns></returns>
        protected async Task StartDispatchingAsync([NotNull] IManagedNetworkClient <TOutgoingPayloadType, TIncomingPayloadType> client)
        {
            if (client == null)
            {
                throw new ArgumentNullException(nameof(client));
            }

            try
            {
                IPeerRequestSendService <TOutgoingPayloadType> requestService = new PayloadInterceptMessageSendService <TOutgoingPayloadType>(client, client);

                if (!client.isConnected && Logger.IsWarnEnabled)
                {
                    Logger.Warn($"The client was not connected before dispatching started.");
                }

                //TODO: Read the next message before awaiting the result of the dispatch message handling.
                while (client.isConnected && !CancelTokenSource.IsCancellationRequested)                 //if we exported we should reading messages
                {
                    NetworkIncomingMessage <TIncomingPayloadType> message = await client.ReadMessageAsync(CancelTokenSource.Token)
                                                                            .ConfigureAwaitFalse();

                    //Supress and continue reading
                    try
                    {
                        //We don't do anything with the result. We should hope someone registered
                        //a default handler to deal with this situation
                        bool result = await Handlers.TryHandleMessage(MessageContextFactory.Create(client, client, requestService), message)
                                      .ConfigureAwaitFalse();
                    }
                    catch (Exception e)
                    {
                        if (Logger.IsInfoEnabled)
                        {
                            Logger.Info($"Error: {e.Message}\n\n Stack Trace: {e.StackTrace}");
                        }
                    }
                }
            }
            catch (Exception e)
            {
                if (Logger.IsInfoEnabled)
                {
                    Logger.Info($"Error: {e.Message}\n\n Stack Trace: {e.StackTrace}");
                }

                throw;
            }
            finally
            {
                if (Logger.IsInfoEnabled)
                {
                    Logger.Info("Network client stopped reading.");
                }

                OnClientStoppedHandlingMessages();
            }
        }
Beispiel #2
0
        /// <summary>
        /// Starts dispatching the messages and won't yield until
        /// the client has stopped or has disconnected.
        /// </summary>
        /// <returns></returns>
        protected async Task StartDispatchingAsync()
        {
            try
            {
                RequestService = new PayloadInterceptMessageSendService <TOutgoingPayloadType>(Client, Client);

                if (!Client.isConnected && Logger.IsWarnEnabled)
                {
                    Logger.Warn($"The client {name} was not connected before dispatching started.");
                }

                while (Client.isConnected && !isClientExported)                //if we exported we should reading messages
                {
                    if (Logger.IsDebugEnabled)
                    {
                        Logger.Debug("Reading message.");
                    }

                    NetworkIncomingMessage <TIncomingPayloadType> message = await Client.ReadMessageAsync(CancelTokenSource.Token)
                                                                            .ConfigureAwait(true);

                    //Supress and continue reading
                    try
                    {
                        //We don't do anything with the result. We should hope someone registered
                        //a default handler to deal with this situation
                        bool result = await Handlers.TryHandleMessage(MessageContextFactory.Create(Client, Client, RequestService), message)
                                      .ConfigureAwait(true);
                    }
                    catch (Exception e)
                    {
                        if (Logger.IsDebugEnabled)
                        {
                            Logger.Debug($"Error: {e.Message}\n\n Stack Trace: {e.StackTrace}");
                        }
                    }
                }
            }
            catch (Exception e)
            {
                if (Logger.IsDebugEnabled)
                {
                    Logger.Debug($"Error: {e.Message}\n\n Stack Trace: {e.StackTrace}");
                }

                throw;
            }

            if (Logger.IsDebugEnabled)
            {
                Logger.Debug("Network client stopped reading.");
            }
        }