]> git.evergreen-ils.org Git - OpenSRF.git/blob - include/opensrf/socket_bundle.h
rolling back latest change because of macro variable oddities. this macro will event...
[OpenSRF.git] / include / opensrf / socket_bundle.h
1 #ifndef SOCKET_BUNDLE_H
2 #define SOCKET_BUNDLE_H
3
4 #include <opensrf/utils.h>
5 #include <opensrf/log.h>
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <sys/types.h>
10 #include <errno.h>
11
12
13 //---------------------------------------------------------------
14 // Unix headers
15 //---------------------------------------------------------------
16 #include <unistd.h>
17 #include <sys/time.h>
18 #include <sys/stat.h>
19 #include <fcntl.h>
20 #include <sys/socket.h>
21 #include <arpa/inet.h>
22 #include <netdb.h>
23 #include <netinet/in.h>
24 #include <netinet/tcp.h>
25 #include <sys/un.h>
26
27 #include <signal.h>
28
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32
33 #define SERVER_SOCKET                   1
34 #define CLIENT_SOCKET                   2
35
36 #define INET 10 
37 #define UNIX 11 
38
39
40 /* models a single socket connection */
41 struct socket_node_struct {
42         int endpoint;           /* SERVER_SOCKET or CLIENT_SOCKET */
43         int addr_type;          /* INET or UNIX */
44         int sock_fd;
45         int parent_id;          /* if we're a new client for a server socket, 
46                                                                 this points to the server socket we spawned from */
47         struct socket_node_struct* next;
48 };
49 typedef struct socket_node_struct socket_node;
50
51
52 /* Maintains the socket set */
53 struct socket_manager_struct {
54         /* callback for passing up any received data.  sock_fd is the socket
55                 that read the data.  parent_id (if > 0) is the socket id of the 
56                 server that this socket spawned from (i.e. it's a new client connection) */
57         void (*data_received) 
58                 (void* blob, struct socket_manager_struct*, 
59                  int sock_fd, char* data, int parent_id);
60
61         void (*on_socket_closed) (void* blob, int sock_fd);
62
63         socket_node* socket;
64         void* blob;
65 };
66 typedef struct socket_manager_struct socket_manager;
67
68 void socket_manager_free(socket_manager* mgr);
69
70 /* creates a new server socket node and adds it to the socket set.
71         returns socket id on success.  -1 on failure.
72         socket_type is one of INET or UNIX  */
73 int socket_open_tcp_server(socket_manager*, int port, const char* listen_ip );
74
75 int socket_open_unix_server(socket_manager* mgr, const char* path);
76
77 int socket_open_udp_server( socket_manager* mgr, int port, const char* listen_ip );
78
79 /* creates a client TCP socket and adds it to the socket set.
80         returns 0 on success.  -1 on failure.  */
81 int socket_open_tcp_client(socket_manager*, int port, const char* dest_addr);
82
83 /* creates a client UNIX socket and adds it to the socket set.
84         returns 0 on success.  -1 on failure.  */
85 int socket_open_unix_client(socket_manager*, const char* sock_path);
86
87 int socket_open_udp_client( socket_manager* mgr, int port, const char* dest_addr);
88
89 /* sends the given data to the given socket. returns 0 on success, -1 otherwise */
90 int socket_send(int sock_fd, const char* data);
91
92 /* waits at most usecs microseconds for the socket buffer to
93  * be available */
94 int socket_send_timeout( int sock_fd, const char* data, int usecs );
95
96 /* disconnects the node with the given sock_fd and removes
97         it from the socket set */
98 void socket_disconnect(socket_manager*, int sock_fd);
99
100 /* XXX This only works if 'sock_fd' is a client socket... */
101 int socket_wait(socket_manager* mgr, int timeout, int sock_fd);
102
103 /* waits on all sockets for incoming data.  
104         timeout == -1   | block indefinitely
105         timeout == 0    | don't block, just read any available data off all sockets
106         timeout == x    | block for at most x seconds */
107 int socket_wait_all(socket_manager* mgr, int timeout);
108
109 /* utility function for displaying the currently attached sockets */
110 void _socket_print_list(socket_manager* mgr);
111
112 int socket_connected(int sock_fd);
113
114 #ifdef __cplusplus
115 }
116 #endif
117
118 #endif