Skip to content

Commit

Permalink
Special return code for "socket closed"
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
rom1v committed Feb 21, 2013
1 parent f93642c commit 8d2ce4b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
4 changes: 4 additions & 0 deletions mdp_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ int overlay_mdp_recv(int mdp_sockfd, overlay_mdp_frame *mdp, int port, int *ttl)

/* Check if reply available */
ssize_t len = recvwithttl(mdp_sockfd,(unsigned char *)mdp, sizeof(overlay_mdp_frame),ttl,recvaddr,&recvaddrlen);
if (len == 0) {
/* Socket is closed. */
return -2; /* would be better to always return len */
}

recvaddr_un=(struct sockaddr_un *)recvaddr;
/* Null terminate received address so that the stat() call below can succeed */
Expand Down
15 changes: 11 additions & 4 deletions mdp_jni.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}

Expand Down

0 comments on commit 8d2ce4b

Please sign in to comment.