1 #ifndef SOCKET_BUNDLE_H
2 #define SOCKET_BUNDLE_H
6 @brief Header for socket routines.
8 These routines cover most of the low-level tedium involved in the use of sockets.
10 They support UDP, TCP, and UNIX domain sockets, for both clients and servers. That's six
11 different combinations. They also support the spawning of sockets from a listener
14 Support for UPD is nominal at best. UDP sockets normally involve the use of calls to
15 recvfrom() or sendto(), but neither of those functions appears here. In practice the
16 functions for opening UDP sockets are completely unused at this writing.
18 All socket traffic is expected to consist of text; i.e. binary data is not supported.
21 #include <opensrf/utils.h>
22 #include <opensrf/log.h>
26 #include <sys/types.h>
30 //---------------------------------------------------------------
32 //---------------------------------------------------------------
37 #include <sys/socket.h>
38 #include <arpa/inet.h>
40 #include <netinet/in.h>
41 #include <netinet/tcp.h>
50 /* models a single socket connection */
52 typedef struct socket_node_struct socket_node;
55 /* Maintains the socket set */
57 @brief Manages a collection of sockets.
59 struct socket_manager_struct {
60 /** @brief Callback for passing any received data up to the calling code.
62 - @em blob Opaque pointer from the calling code.
63 - @em mgr Pointer to the socket_manager that manages the socket.
64 - @em sock_fd File descriptor of the socket that read the data.
65 - @em data Pointer to the data received.
66 - @em parent_id (if > 0) listener socket from which the data socket was spawned.
68 void (*data_received) (
70 struct socket_manager_struct* mgr,
76 /** @brief Callback for closing the socket.
78 - @em blob Opaque pointer from the calling code.
79 - @em sock_fd File descriptor of the socket that was closed.
81 void (*on_socket_closed) (
86 socket_node* socket; /**< Linked list of managed sockets. */
87 void* blob; /**< Opaque pointer from the calling code .*/
89 typedef struct socket_manager_struct socket_manager;
91 void socket_manager_free(socket_manager* mgr);
93 int socket_open_tcp_server(socket_manager*, int port, const char* listen_ip );
95 int socket_open_unix_server(socket_manager* mgr, const char* path);
97 int socket_open_udp_server( socket_manager* mgr, int port, const char* listen_ip );
99 int socket_open_tcp_client(socket_manager*, int port, const char* dest_addr);
101 int socket_open_unix_client(socket_manager*, const char* sock_path);
103 int socket_open_udp_client( socket_manager* mgr );
105 int socket_send(int sock_fd, const char* data);
107 int socket_send_timeout( int sock_fd, const char* data, int usecs );
109 void socket_disconnect(socket_manager*, int sock_fd);
111 int socket_wait(socket_manager* mgr, int timeout, int sock_fd);
113 int socket_wait_all(socket_manager* mgr, int timeout);
115 void _socket_print_list(socket_manager* mgr);
117 int socket_connected(int sock_fd);