]> git.evergreen-ils.org Git - Evergreen.git/blob - OpenSRF/src/utils/socket_bundle.h
moving to UNIVERSAL::require to suck in implementation modules
[Evergreen.git] / OpenSRF / src / utils / socket_bundle.h
1 #include "utils.h"
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <sys/types.h>
6 #include <errno.h>
7
8 #include "utils.h"
9 #include "log.h"
10
11 //---------------------------------------------------------------
12 // Unix headers
13 //---------------------------------------------------------------
14 #include <unistd.h>
15 #include <sys/time.h>
16 #include <sys/stat.h>
17 #include <fcntl.h>
18 #include <sys/socket.h>
19 #include <arpa/inet.h>
20 #include <netdb.h>
21 #include <netinet/in.h>
22 #include <netinet/tcp.h>
23 #include <sys/un.h>
24
25 #include <signal.h>
26
27 #ifndef SOCKET_BUNDLE_H
28 #define SOCKET_BUNDLE_H
29
30
31 #define SERVER_SOCKET                   1
32 #define CLIENT_SOCKET                   2
33
34 #define INET 10 
35 #define UNIX 11 
36
37 /* buffer used to read from the sockets */
38 #define RBUFSIZE 1024 
39
40
41 /* models a single socket connection */
42 struct socket_node_struct {
43         int endpoint;           /* SERVER_SOCKET or CLIENT_SOCKET */
44         int addr_type;          /* INET or UNIX */
45         int sock_fd;
46         int parent_id;          /* if we're a new client for a server socket, 
47                                                                 this points to the server socket we spawned from */
48         struct socket_node_struct* next;
49 };
50 typedef struct socket_node_struct socket_node;
51
52
53 /* Maintains the socket set */
54 struct socket_manager_struct {
55         /* callback for passing up any received data.  sock_fd is the socket
56                 that read the data.  parent_id (if > 0) is the socket id of the 
57                 server that this socket spawned from (i.e. it's a new client connection) */
58         void (*data_received) 
59                 (void* blob, struct socket_manager_struct*, 
60                  int sock_fd, char* data, int parent_id);
61
62         void (*on_socket_closed) (void* blob, int sock_fd);
63
64         socket_node* socket;
65         void* blob;
66 };
67 typedef struct socket_manager_struct socket_manager;
68
69 void socket_manager_free(socket_manager* mgr);
70
71 /* creates a new server socket node and adds it to the socket set.
72         returns socket id on success.  -1 on failure.
73         socket_type is one of INET or UNIX  */
74 int socket_open_tcp_server(socket_manager*, int port, char* listen_ip );
75
76 int socket_open_unix_server(socket_manager* mgr, char* path);
77
78 int socket_open_udp_server( socket_manager* mgr, int port, char* listen_ip );
79
80 /* creates a client TCP socket and adds it to the socket set.
81         returns 0 on success.  -1 on failure.  */
82 int socket_open_tcp_client(socket_manager*, int port, char* dest_addr);
83
84 /* creates a client UNIX socket and adds it to the socket set.
85         returns 0 on success.  -1 on failure.  */
86 int socket_open_unix_client(socket_manager*, char* sock_path);
87
88 int socket_open_udp_client( socket_manager* mgr, int port, char* dest_addr);
89
90 /* returns the socket_node with the given sock_fd */
91 socket_node* socket_find_node(socket_manager*, int sock_fd);
92
93 /* removes the node with the given sock_fd from the list and frees it */
94 void socket_remove_node(socket_manager*, int sock_fd);
95
96
97 /* sends the given data to the given socket. returns 0 on success, -1 otherwise */
98 int socket_send(int sock_fd, const char* data);
99
100 /* disconnects the node with the given sock_fd and removes
101         it from the socket set */
102 void socket_disconnect(socket_manager*, int sock_fd);
103
104 /* allocates and inserts a new socket node into the nodeset.
105         if parent_id is positive and non-zero, it will be set */
106 socket_node*  _socket_add_node(socket_manager* mgr, 
107                 int endpoint, int addr_type, int sock_fd, int parent_id );
108
109 /* XXX This only works if 'sock_fd' is a client socket... */
110 int socket_wait(socket_manager* mgr, int timeout, int sock_fd);
111
112 /* waits on all sockets for incoming data.  
113         timeout == -1   | block indefinitely
114         timeout == 0    | don't block, just read any available data off all sockets
115         timeout == x    | block for at most x seconds */
116 int socket_wait_all(socket_manager* mgr, int timeout);
117
118 /* iterates over the sockets in the set and handles active sockets.
119         new sockets connecting to server sockets cause the creation
120         of a new socket node.
121         Any new data read is is passed off to the data_received callback
122         as it arrives */
123 int _socket_route_data(socket_manager* mgr, int num_active, fd_set* read_set);
124
125 /* routes data from a single known socket */
126 int _socket_route_data_id( socket_manager* mgr, int sock_id);
127
128 /* utility function for displaying the currently attached sockets */
129 void _socket_print_list(socket_manager* mgr);
130
131 int socket_connected(int sock_fd);
132
133
134 int _socket_handle_new_client(socket_manager* mgr, socket_node* node);
135 int _socket_handle_client_data(socket_manager* mgr, socket_node* node);
136
137
138 #endif