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