From 6783656179244e75bb4fb47c5e690d4e82ea7166 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Vimont=20=28=C2=AEom=29?= Date: Thu, 21 Feb 2013 16:30:13 +0100 Subject: [PATCH] Special return code for "socket closed" Blocking recv calls return a special return code (-2) for notifying the socket is closed (this information is useful for MDP socket users). This implementation is unsatisfactory: return code should instead be consistent with recvfrom(), that is always returning the length of received data (0 for socket closed). --- mdp_client.c | 2 +- mdp_jni.c | 15 +++++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/mdp_client.c b/mdp_client.c index 56f9f9ae..dcd673f2 100644 --- a/mdp_client.c +++ b/mdp_client.c @@ -160,7 +160,7 @@ int overlay_mdp_recv(int mdp_sockfd, overlay_mdp_frame *mdp, int port, int *ttl) mdp->packetTypeAndFlags = 0; len = recvwithttl(mdp_sockfd, (unsigned char *)mdp, sizeof(overlay_mdp_frame), ttl, (struct sockaddr *)&recvaddr, &recvaddrlen); if (len <= 0) - return -1; // no packet received + return -2; // no packet received // If the received address overflowed the buffer, then it cannot have come from the server, whose // address always fits within a struct sockaddr_un. diff --git a/mdp_jni.c b/mdp_jni.c index 02f24c8a..6f1dd2be 100644 --- a/mdp_jni.c +++ b/mdp_jni.c @@ -300,6 +300,8 @@ Java_org_servalproject_servald_mdp_MeshSocket__1receive(JNIEnv * env, jbyteArray jbuf, jsid; jbyte *sid; overlay_mdp_frame mdp; + int res; + char *msg; /* fd = this.fd; */ fd = (*env)->GetIntField(env, this, f_meshsocket_fd); @@ -311,10 +313,15 @@ Java_org_servalproject_servald_mdp_MeshSocket__1receive(JNIEnv * env, length = (*env)->GetIntField(env, mdppack, f_meshpacket_length); /* Receive data. */ - if (overlay_mdp_recv(fd, &mdp, localport, &ttl)) { - (*env)->ThrowNew(env, cl_meshsocketexception, - "Cannot receive data from servald"); - WHY("Cannot receive data from servald"); + if (res = (overlay_mdp_recv(fd, &mdp, localport, &ttl))) { + if (res == -2) { + /* Socket is closed. */ + msg = "Socket closed"; + } else { + msg = "Cannot receive data from servald"; + } + (*env)->ThrowNew(env, cl_meshsocketexception, msg); + WHY(msg); return; }