/// <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(); } }
/// <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."); } }
public void ParseWords() { var synonymService = new SynonymService(); var messageContextFactory = new MessageContextFactory(synonymService); MessageContext messageContext = messageContextFactory.Create(new TestInboundMessage("Hello World. Hello World.")); messageContext.IdentifiedAreaIds.Count.ShouldBeEquivalentTo(0); messageContext.IdentifiedComponentIds.Count.ShouldBeEquivalentTo(0); messageContext.IdentifiedComponentStates.Count.ShouldBeEquivalentTo(0); messageContext.Words.Count.ShouldBeEquivalentTo(3); }
public void IdentifyAreaIds() { var synonymService = new SynonymService(); synonymService.AddSynonymsForArea(new AreaId("Office"), "Büro"); var messageContextFactory = new MessageContextFactory(synonymService); MessageContext messageContext = messageContextFactory.Create(new TestInboundMessage("Hello World. Büro.")); messageContext.IdentifiedAreaIds.Count.ShouldBeEquivalentTo(1); messageContext.IdentifiedComponentIds.Count.ShouldBeEquivalentTo(0); messageContext.IdentifiedComponentStates.Count.ShouldBeEquivalentTo(0); messageContext.Words.Count.ShouldBeEquivalentTo(4); }
public IPeerMessageContext <GameClientPacketPayload> CreateMessageContext() { return(MessageContextFactory.Create(ConnectionService, SendService, this)); }