From 3a4b23e28b7f0a109cd4a7bf74c224759c4b32a4 Mon Sep 17 00:00:00 2001 From: erickson Date: Tue, 16 Aug 2005 15:46:54 +0000 Subject: [PATCH] added method to allow waiting for a single client socket instead of waiting on every socket git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@484 9efc2488-bf62-4759-914b-345cdb29e865 --- src/utils/socket_bundle.c | 56 ++++++++++++++++++++++++++++++++++++++++------- src/utils/socket_bundle.h | 4 ++++ 2 files changed, 52 insertions(+), 8 deletions(-) diff --git a/src/utils/socket_bundle.c b/src/utils/socket_bundle.c index c18534f..40d48b9 100644 --- a/src/utils/socket_bundle.c +++ b/src/utils/socket_bundle.c @@ -329,8 +329,35 @@ int socket_connected(int sock_fd) { } +/* this only waits on the server socket and does not handle the actual + data coming in from the client..... XXX */ int socket_wait(socket_manager* mgr, int timeout, int sock_fd) { - return 0; + + int retval = 0; + fd_set read_set; + FD_ZERO( &read_set ); + FD_SET( sock_fd, &read_set ); + + struct timeval tv; + tv.tv_sec = timeout; + tv.tv_usec = 0; + + if( timeout < 0 ) { + + // If timeout is -1, we block indefinitely + if( (retval = select( sock_fd + 1, &read_set, NULL, NULL, NULL)) == -1 ) { + return warning_handler("Call to select interrupted"); + } + + } else if( timeout > 0 ) { /* timeout of 0 means don't block */ + + if( (retval = select( sock_fd + 1, &read_set, NULL, NULL, &tv)) == -1 ) { + return warning_handler( "Call to select interrupted" ); + } + } + + debug_handler("%d active sockets after select()", retval); + return _socket_route_data_id(mgr, sock_fd); } @@ -356,7 +383,7 @@ int socket_wait_all(socket_manager* mgr, int timeout) { tv.tv_sec = timeout; tv.tv_usec = 0; - if( timeout == -1 ) { + if( timeout < 0 ) { // If timeout is -1, there is no timeout passed to the call to select if( (retval = select( max_fd, &read_set, NULL, NULL, NULL)) == -1 ) { @@ -403,11 +430,9 @@ int _socket_route_data( status = -1; break; } - /* does this socket have data? */ if( FD_ISSET( sock_fd, read_set ) ) { - debug_handler("Socket %d active", sock_fd); handled++; @@ -441,13 +466,28 @@ int _socket_route_data( } +int _socket_route_data_id( socket_manager* mgr, int sock_id) { + socket_node* node = socket_find_node(mgr, sock_id); + int status = 0; + + if(node) { + if(node->endpoint == SERVER_SOCKET) + _socket_handle_new_client(mgr, node); + + if(node->endpoint == CLIENT_SOCKET ) + status = _socket_handle_client_data(mgr, node); + + if(status == -1) socket_remove_node(mgr, sock_id); + return 0; + } + + return -1; +} + + int _socket_handle_new_client(socket_manager* mgr, socket_node* node) { if(mgr == NULL || node == NULL) return -1; - //struct sockaddr_in client_addr_in; - //struct sockaddr_un client_addr_un; - //int client_len, new_sock_fd; - int new_sock_fd; new_sock_fd = accept(node->sock_fd, NULL, NULL); if(new_sock_fd < 0) diff --git a/src/utils/socket_bundle.h b/src/utils/socket_bundle.h index 25fc158..133ec7c 100644 --- a/src/utils/socket_bundle.h +++ b/src/utils/socket_bundle.h @@ -101,6 +101,7 @@ void socket_disconnect(socket_manager*, int sock_fd); socket_node* _socket_add_node(socket_manager* mgr, int endpoint, int addr_type, int sock_fd, int parent_id ); +/* XXX This only works if 'sock_fd' is a client socket... */ int socket_wait(socket_manager* mgr, int timeout, int sock_fd); /* waits on all sockets for incoming data. @@ -116,6 +117,9 @@ int socket_wait_all(socket_manager* mgr, int timeout); as it arrives */ int _socket_route_data(socket_manager* mgr, int num_active, fd_set* read_set); +/* routes data from a single known socket */ +int _socket_route_data_id( socket_manager* mgr, int sock_id); + /* utility function for displaying the currently attached sockets */ void _socket_print_list(socket_manager* mgr); -- 2.11.0