Sending a SyncRequest from within a SyncRequest and getting a response
See original GitHub issueHey!
I’m in a situation where I would need this to work:
- Client sends a
SyncRequest
to Server - Server requires more info and therefore sends a
SyncRequest
back to Client - Client replies with a
SyncResponse
including the requested info - Server replies to the original
SyncRequest
(from step 1)
Basically: client -> server (extra: -> client -> server) -> client, if that makes sense.
To explain it better, here is some code to illustrate what I’d want to do (fiddle):
using System.Text;
using WatsonTcp;
const string host = "127.0.0.1";
const int port = 1337;
var server = new WatsonTcpServer(host, port);
var client = new WatsonTcpClient(host, port);
server.Callbacks.SyncRequestReceived += request =>
{
Console.WriteLine(2);
var response = server.SendAndWait(1000, request.IpPort, "hello from server!");
Console.WriteLine(4);
return new SyncResponse(request, response.Data);
};
client.Callbacks.SyncRequestReceived += request =>
{
Console.WriteLine(3);
return new SyncResponse(request, "hello back from client!");
};
server.Events.MessageReceived += (_, _) => { };
client.Events.MessageReceived += (_, _) => { };
server.Start();
client.Connect();
Console.WriteLine(1);
var response = client.SendAndWait(1000, "hello from client!");
Console.WriteLine("client received response: " + Encoding.UTF8.GetString(response.Data));
Console.ReadKey(true);
However, this is the output I am getting from this exact code:
1
2
3
Unhandled exception. System.TimeoutException: A response to a synchronous request was not received within the timeout window.
As you can see, the client does not seem to be sending a response back, or at least the server isn’t letting me know about it (stuck at step 3). If I was to guess, and without having read the entire codebase, I would assume this is the intended behavior and is due to a lock()
or SemaphoreSlim
somewhere, preventing concurrent requests/responses?
This would make sense as otherwise, it would mean multiple different data could be received concurrently, mixing everything up. Hopefully I’m not saying sh*t here. Please, correct me if so 😅 Always looking to get more knowledge!
Considering I am right (and even if I am not), then how would I go about getting this to work?
What I thought of is: Instead of sending a SyncRequest
back from within a SyncRequest
, what I could do is make the server reply right away with a SyncResponse
saying something along the lines of "hey! more data is required! please come back with a new SyncRequest containing it!"
, but that is pretty inconvenient and would not work well with my current structure. My question here really is about (issue title): “Sending a SyncRequest from within a SyncRequest and getting a response”, without getting blocked.
So, what can/should I do? Is that even possible at all or should I completely forget about it and instead go with the idea above?
Issue Analytics
- State:
- Created 10 months ago
- Comments:16 (8 by maintainers)
Top GitHub Comments
Thanks Matt! Will be moving over to the other issues shortly! Cheers
Anyway, everything now seems to work just fine. I’ll close this issue so we can move to the next one whenever you have some time.
Thanks a lot for your time collaborating on this, it is very much appreciated. Please keep up the good work! (: