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).
This commit is contained in:
Romain Vimont (®om) 2013-02-21 16:30:13 +01:00 committed by Jeremy Lakeman
parent 927a35d879
commit 6783656179
2 changed files with 12 additions and 5 deletions

View File

@ -160,7 +160,7 @@ int overlay_mdp_recv(int mdp_sockfd, overlay_mdp_frame *mdp, int port, int *ttl)
mdp->packetTypeAndFlags = 0; mdp->packetTypeAndFlags = 0;
len = recvwithttl(mdp_sockfd, (unsigned char *)mdp, sizeof(overlay_mdp_frame), ttl, (struct sockaddr *)&recvaddr, &recvaddrlen); len = recvwithttl(mdp_sockfd, (unsigned char *)mdp, sizeof(overlay_mdp_frame), ttl, (struct sockaddr *)&recvaddr, &recvaddrlen);
if (len <= 0) 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 // If the received address overflowed the buffer, then it cannot have come from the server, whose
// address always fits within a struct sockaddr_un. // address always fits within a struct sockaddr_un.

View File

@ -300,6 +300,8 @@ Java_org_servalproject_servald_mdp_MeshSocket__1receive(JNIEnv * env,
jbyteArray jbuf, jsid; jbyteArray jbuf, jsid;
jbyte *sid; jbyte *sid;
overlay_mdp_frame mdp; overlay_mdp_frame mdp;
int res;
char *msg;
/* fd = this.fd; */ /* fd = this.fd; */
fd = (*env)->GetIntField(env, this, f_meshsocket_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); length = (*env)->GetIntField(env, mdppack, f_meshpacket_length);
/* Receive data. */ /* Receive data. */
if (overlay_mdp_recv(fd, &mdp, localport, &ttl)) { if (res = (overlay_mdp_recv(fd, &mdp, localport, &ttl))) {
(*env)->ThrowNew(env, cl_meshsocketexception, if (res == -2) {
"Cannot receive data from servald"); /* Socket is closed. */
WHY("Cannot receive data from servald"); msg = "Socket closed";
} else {
msg = "Cannot receive data from servald";
}
(*env)->ThrowNew(env, cl_meshsocketexception, msg);
WHY(msg);
return; return;
} }