if(node->addr && !strcmp(node->addr,addr)) {
js->client = node->next;
+ debug_handler("Removing the first jserver client");
socket_disconnect(js->mgr, node->id);
_free_jclient_node(node);
return;
while(node) {
if(node->addr && !strcmp(node->addr,addr)) {
tail_node->next = node->next;
+ debug_handler("Removing a jserver client");
socket_disconnect(js->mgr, node->id);
_free_jclient_node(node);
return;
int _socket_route_data(
socket_manager* mgr, int num_active, fd_set* read_set) {
- socket_node* node = mgr->socket;
- int handled = 0;
-
- while(node && (handled < num_active)) {
+ if(mgr == NULL) return -1;
- int sock_fd = node->sock_fd;
- /* does this socket have data? */
- if( FD_ISSET( sock_fd, read_set ) ) {
+ /* come back here if someone yanks a socket_node from beneath us */
+ while(1) {
- debug_handler("Socket %d active", sock_fd);
- handled++;
- FD_CLR(sock_fd, read_set);
-
- if(node->endpoint == SERVER_SOCKET)
- _socket_handle_new_client(mgr, node);
+ socket_node* node = mgr->socket;
+ int handled = 0;
+ int status = 0;
+
+ while(node && (handled < num_active)) {
+
+ int sock_fd = node->sock_fd;
+
+ /* does this socket have data? */
+ if( FD_ISSET( sock_fd, read_set ) ) {
+
+ debug_handler("Socket %d active", sock_fd);
+ handled++;
+ FD_CLR(sock_fd, read_set);
+
+ if(node->endpoint == SERVER_SOCKET)
+ _socket_handle_new_client(mgr, node);
+
+ if(node->endpoint == CLIENT_SOCKET )
+ status = _socket_handle_client_data(mgr, node);
+
+ /* someone may have yanked a socket_node out from under
+ us...start over with the first socket */
+ if(status == -1) {
+ debug_handler("Backtracking back to start of loop because "
+ "of -1 return code from _socket_handle_client_data()");
+ }
+ }
- if(node->endpoint == CLIENT_SOCKET )
- _socket_handle_client_data(mgr, node);
+ if(status == -1) break;
+ node = node->next;
} // is_set
-
- node = node->next;
- } // while(node)
+ if(status == 0) break;
+ if(status == -1) status = 0;
+ }
return 0;
}
}
if(read_bytes == 0) { /* socket closed by client */
- if(mgr->on_socket_closed)
+ if(mgr->on_socket_closed) {
mgr->on_socket_closed(mgr->blob, sock_fd);
+ return -1;
+ }
}
return 0;