[nanomsg] Re: Asynchronous (raw) sockets in nanomsg

  • From: Martin Sustrik <sustrik@xxxxxxxxxx>
  • To: nanomsg@xxxxxxxxxxxxx
  • Date: Mon, 18 Nov 2013 20:35:21 +0100

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 18/11/13 20:22, Paul Colomiets wrote:
> Hi Martin,
> 
> On Mon, Nov 18, 2013 at 9:08 PM, Martin Sustrik
> <sustrik@xxxxxxxxxx> wrote:
>> Ok. Let me give an example:
>> 
>> // Open the connection to server. s = nn_socket (AF_SP, NN_REQ); 
>> nn_connect (s, "tcp://myserver:5555");
>> 
>> // Send first request. Associate it with dummy request context
>> (simple int). const int first = 1; nn_req_send (s, &first, "ABC",
>> 3, 0);
>> 
>> //  Second request. Give it a different dummy context. const int
>> second = 2; nn_req_send (s, &second, "DEF", 3, 0);
>> 
>> //  Wait for a reply. int *context; char reply [32]; nn_req_recv
>> (s, &context, buf, sizeof (buf), 0);
>> 
>> //  Check which reply was received. if (context == &first)
> 
> 1. Shouldn't be compared by value? In case of comparison by
> pointer it's possible to make the following mistake:
> 
> int request_id = 1; nn_req_send(s, &request_id..); req->request_id
> = request_id; return req;

It's just a dummy context. In real world I would expect the code to
look something like:

struct mycontext {
   int a;
   int b;
   int c;
}

mycontext context1 = {1, 2, 3};
nn_send (s, &context1, "ABC", 3, 0);

mycontext *ctx;
nn_recv (s, &ctx, buf, sizeof (buf), 0);
if (ctx == &context1) ...

etc.

> 2. How to know that request is timed out?
> 
> rc = nn_req_recv(s, &context, buf, sizeof(buf), 0); if(rc < 0 &&
> errno == ETIMEDOUT) { printf("request %d timed out\n", *context); 
> }
> 
> Is it ok that context is filled even if error code received?
> Probably only certain errors will have request id filled in?

Looks ugly, but it's technically viable.

The other option, of course, is not to have timeouts and let the use
cancel the requests:

nn_req_cancel (s, &context1);

Martin
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQEcBAEBAgAGBQJSimv5AAoJENTpVjxCNN9Y37kIAJD03P8A8S/8016mYLthcQ3C
9eM+BdcOMki3HW+Kr2UPwNuuxncLpNHOtlIizvwULdSNFx1HoI+Noeo0gt9Vwy3E
eVc4M3EqH9V9eb6xATmhlh91kpZBxVrYBqSuRqvbTl5pFKied9S3iCULCa17Ynu/
vqBBuydtJQNtIEZIpU7O+JcypQ1Qr+iJCYLtIuf8NucrZBVFbiUuQc/YdZsE/kej
FYhI228qWKQXhFbYk8A7239HoRtloOP0K94PhjbsbU2llhPkvL6ipmntM170NPne
YT2EdJExppA4nVa72vkQRJ8UPGIiga1hZMOaPkxoWnCZzSjGSdKExuQm51WqCIA=
=6YWz
-----END PGP SIGNATURE-----

Other related posts: