]> git.evergreen-ils.org Git - OpenSRF.git/blob - include/opensrf/socket_bundle.h
Several bug fixes:
[OpenSRF.git] / include / opensrf / socket_bundle.h
1 #ifndef SOCKET_BUNDLE_H
2 #define SOCKET_BUNDLE_H
3
4 /**
5         @file socket_bundle.h
6         @brief Header for socket routines.
7
8         These routines cover most of the low-level tedium involved in the use of sockets.
9
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
12         by calls to accept(),
13
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.
17
18         All socket traffic is expected to consist of text; i.e. binary data is not supported.
19 */
20
21 #include <opensrf/utils.h>
22 #include <opensrf/log.h>
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <sys/types.h>
27 #include <errno.h>
28
29
30 //---------------------------------------------------------------
31 // Unix headers
32 //---------------------------------------------------------------
33 #include <unistd.h>
34 #include <sys/time.h>
35 #include <sys/stat.h>
36 #include <fcntl.h>
37 #include <sys/socket.h>
38 #include <arpa/inet.h>
39 #include <netdb.h>
40 #include <netinet/in.h>
41 #include <netinet/tcp.h>
42 #include <sys/un.h>
43
44 #include <signal.h>
45
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49
50 /* models a single socket connection */
51 struct socket_node;
52 typedef struct socket_node_struct socket_node;
53
54
55 /* Maintains the socket set */
56 /**
57         @brief Manages a collection of sockets.
58 */
59 struct socket_manager_struct {
60         /** @brief Callback for passing any received data up to the calling code.
61         Parameters:
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.
67         */
68         void (*data_received) (
69                 void* blob,
70                 struct socket_manager_struct* mgr,
71                 int sock_fd,
72                 char* data,
73                 int parent_id
74         );
75
76         /** @brief Callback for closing the socket.
77         Parameters:
78         - @em blob Opaque pointer from the calling code.
79         - @em sock_fd File descriptor of the socket that was closed.
80         */
81         void (*on_socket_closed) (  
82                 void* blob,
83                 int sock_fd
84         );
85
86         socket_node* socket;       /**< Linked list of managed sockets. */
87         void* blob;                /**< Opaque pointer from the calling code .*/
88 };
89 typedef struct socket_manager_struct socket_manager;
90
91 void socket_manager_free(socket_manager* mgr);
92
93 int socket_open_tcp_server(socket_manager*, int port, const char* listen_ip );
94
95 int socket_open_unix_server(socket_manager* mgr, const char* path);
96
97 int socket_open_udp_server( socket_manager* mgr, int port, const char* listen_ip );
98
99 int socket_open_tcp_client(socket_manager*, int port, const char* dest_addr);
100
101 int socket_open_unix_client(socket_manager*, const char* sock_path);
102
103 int socket_open_udp_client( socket_manager* mgr );
104
105 int socket_send(int sock_fd, const char* data);
106
107 int socket_send_timeout( int sock_fd, const char* data, int usecs );
108
109 void socket_disconnect(socket_manager*, int sock_fd);
110
111 int socket_wait(socket_manager* mgr, int timeout, int sock_fd);
112
113 int socket_wait_all(socket_manager* mgr, int timeout);
114
115 void _socket_print_list(socket_manager* mgr);
116
117 int socket_connected(int sock_fd);
118
119 #ifdef __cplusplus
120 }
121 #endif
122
123 #endif