private Task HandleClient(IProgress <Tuple <Guid, Exception> > exceptionHandler, CancellationToken ct, TcpClient clientToListen, Guid clientId) { Task listeningTask = new Task(() => { BinaryReader reader = null; BinaryWriter writer = null; NetworkStream socketStream = null; MessageModel response = null; MessageModel request = null; while (!ct.IsCancellationRequested) { try { var waitForData = new Task <string>(() => { socketStream = clientToListen.GetStream(); reader = new BinaryReader(socketStream, Encoding.UTF8, true); writer = new BinaryWriter(socketStream, Encoding.UTF8, true); try { return(reader.ReadString()); } catch when(ct.IsCancellationRequested) { return(null); } }); waitForData.Start(); waitForData.Wait(ct); if (waitForData.IsCompleted) { var reqRaw = waitForData.Result; request = MessageCustomXmlConverter.ToModel(reqRaw); response = MessageRouting(ref request, clientId); writer.Write(MessageCustomXmlConverter.ToXml(response)); writer.Flush(); } } catch (Exception e) { exceptionHandler.Report(new Tuple <Guid, Exception>(clientId, e)); } finally { //reader?.Close(); //writer?.Close(); } } socketStream.Close(); }); return(listeningTask); }
public override async Task <ReplyModel> SendAsync(MessageModel message, bool encrypted = true) { MessageModel request = PrepareMessage(message, encrypted); byte[] requestBody = Encoding.UTF8.GetBytes(MessageCustomXmlConverter.ToXml(request)); var requestProps = mqChannel.CreateBasicProperties(); requestProps.CorrelationId = request.TicketId.ToString(); requestProps.ReplyTo = callbackQueue; await Task.Run(() => { mqChannel.BasicPublish(exchange: "", routingKey: rpcQueueName, mandatory: true, basicProperties: requestProps, body: requestBody); }); pendingRequests.Add(request.TicketId, null); return(await Task.Run(() => { while (true) { lock (pendingRequests) { if (pendingRequests.ContainsKey(request.TicketId)) { var response = pendingRequests[request.TicketId]; if (response != null) { return response; } } else { return null; } } } })); }
public override async Task <ReplyModel> SendAsync(MessageModel message, bool encrypted = true) { ReplyModel response = null; if (CheckConnection()) { var socketStream = client.GetStream(); { BinaryWriter writer = null; BinaryReader reader = null; try { writer = new BinaryWriter(socketStream, Encoding.UTF8, true); reader = new BinaryReader(socketStream, Encoding.UTF8, true); MessageModel request = PrepareMessage(message, encrypted); await Task.Run(() => { writer.Write(MessageCustomXmlConverter.ToXml(request)); writer.Flush(); }); response = await Task.Run(() => { return((ReplyModel)MessageCustomXmlConverter.ToModel(reader.ReadString())); }); } catch (Exception e) { OnConnectionError(this, e); } finally { writer.Close(); reader.Close(); } } } return(response); }
private void InitializeConsumer(IModel channel) { mqConsumer = new EventingBasicConsumer(channel); mqConsumer.Received += (sender, ea) => { string requestBody = Encoding.UTF8.GetString(ea.Body); var requestProps = ea.BasicProperties; MessageModel requestModel = MessageCustomXmlConverter.ToModel(requestBody); ReplyModel responseModel = MessageRouting(ref requestModel, requestModel.SenderId); var responseProps = channel.CreateBasicProperties(); responseProps.CorrelationId = requestProps.CorrelationId; byte[] responseBody = Encoding.UTF8.GetBytes(MessageCustomXmlConverter.ToXml(responseModel)); channel.BasicPublish(exchange: "", routingKey: requestProps.ReplyTo, basicProperties: responseProps, body: responseBody); channel.BasicAck(ea.DeliveryTag, false); }; }