1

I'm using hybrid connections to request data from a listener. If I can write and read to the connection, how can I know that the response I've read from the connection matches the request I've given it? For example:

private HybridConnectionClient _client = new HybridConnectionClient(***);

public override async Task<RelayResponse> SendAsync(RelayRequest request)
{
    var stream = await _client.CreateConnectionAsync();
    var writer = new StreamWriter(stream) { AutoFlush = true };
    var reader = new StreamReader(stream);

    var reqestSerialized = JsonConvert.SerializeObject(request);

    await writer.WriteLineAsync(reqestSerialized);

    string responseSerialized = await reader.ReadLineAsync();
    var response = JsonConvert.DeserializeObject<RelayResponse>(responseSerialized);

    return response;
}

If the listener on this connection is reading and responding to many requests at the same time, is there anyway to know that the next Readline() we do on the client side to get the response is the one that is associated with the request? Or is that something that has to be managed?

zola25
  • 1,774
  • 6
  • 24
  • 44

1 Answers1

0

Understanding a bit more about azure relay hybrid connections, I understand this now.

There isn't really any concept of a synchronous request/response in the framework, but if you use a new connection for each request, and respond on that same connection in the listener, you can be sure the response is for the request you sent.

Spawn a new connection for each request, then make sure the response is written to that connection. So looking at Microsoft's listener example code, whenever listener.AcceptConnectionAsync() fires do all the message response on relayConnection, then go back to waiting at await listener.AcceptConnectionAsync();

while (true)
{
     var relayConnection = await listener.AcceptConnectionAsync();
     if (relayConnection == null)
     {
         break;
     }

     ProcessMessagesOnConnection(relayConnection, cts);
}
zola25
  • 1,774
  • 6
  • 24
  • 44