public async Task HandleMessage(Client client, INetworkStreamWrapper networkStream, byte[] bytes) { ICustomMessageHandler customMessageHandler = GetMessageHandler(client.Protocol); MessageInput messageInput = new MessageInput { Client = client, NetworkStream = networkStream, DataMessage = new DataMessage(bytes, client.Protocol.SplitMessageBy) }; logger.LogTrace( $"{client.Protocol}: received {HexUtil.ConvertHexStringArrayToHexString(messageInput.DataMessage.Hex)}"); int connectionMessageId = await connectionService.AddMessage(client.DeviceConnection.Id, messageInput.DataMessage.HexString); try { List <Location> locations = customMessageHandler.ParseRange(messageInput)?.ToList(); if (locations != null && locations.Any()) { // TODO refactor this await connectionService.SetDeviceId(client); await locationService.AddRange(locations, connectionMessageId); } } catch (Exception e) { logger.LogCritical(e, $"{customMessageHandler.GetType()}: Error parsing {messageInput.DataMessage.Hex} "); } }
private ICustomMessageHandler GetMessageHandler(IProtocol protocol) { Type type = typeof(ICustomMessageHandler <>).MakeGenericType(protocol.GetType()); ICustomMessageHandler customMessageHandler = (ICustomMessageHandler)serviceProvider.GetService(type); if (customMessageHandler == null) { throw new Exception($"There is no message handler implemented for {protocol}"); } return(customMessageHandler); }
public ProtocolTester(IProtocol protocol, ICustomMessageHandler customMessageHandler) { cancellationTokenSource = new CancellationTokenSource(); TotalParsedLocations = new List <Location>(); Client = new Client { Protocol = protocol }; SetupLocationService(); SetupStreamHandler(customMessageHandler); SetupNetworkStreamMock(); }
public RaknetSession(ConnectionInfo connectionInfo, RaknetConnection packetSender, IPEndPoint endPoint, short mtuSize, ICustomMessageHandler messageHandler = null) { Log.Debug($"Create session for {endPoint}"); _packetSender = packetSender; ConnectionInfo = connectionInfo; CustomMessageHandler = messageHandler ?? new DefaultMessageHandler(); EndPoint = endPoint; MtuSize = mtuSize; InactivityTimeout = 30000; ResendThreshold = 10; _cancellationToken = new CancellationTokenSource(); }
public RakSession(ConnectionInfo connectionInfo, IPacketSender packetSender, IPEndPoint endPoint, short mtuSize, ICustomMessageHandler messageHandler = null) { Log.Debug($"Create session for {endPoint}"); _packetSender = packetSender; ConnectionInfo = connectionInfo; CustomMessageHandler = messageHandler ?? new DefaultMessageHandler(); EndPoint = endPoint; MtuSize = mtuSize; InactivityTimeout = Config.GetProperty("InactivityTimeout", 8500); ResendThreshold = Config.GetProperty("ResendThreshold", 10); _cancellationToken = new CancellationTokenSource(); //_tickerHighPrecisionTimer = new HighPrecisionTimer(10, SendTick, true); }