]> git.evergreen-ils.org Git - OpenSRF.git/blob - include/opensrf/socket_bundle.h
1. Moved the declaration of socket_node from the header into the
[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;
42 typedef struct socket_node_struct socket_node;
43
44
45 /* Maintains the socket set */
46 struct socket_manager_struct {
47         /* callback for passing up any received data.  sock_fd is the socket
48                 that read the data.  parent_id (if > 0) is the socket id of the 
49                 server that this socket spawned from (i.e. it's a new client connection) */
50         void (*data_received) 
51                 (void* blob, struct socket_manager_struct*, 
52                  int sock_fd, char* data, int parent_id);
53
54         void (*on_socket_closed) (void* blob, int sock_fd);
55
56         socket_node* socket;
57         void* blob;
58 };
59 typedef struct socket_manager_struct socket_manager;
60
61 void socket_manager_free(socket_manager* mgr);
62
63 /* creates a new server socket node and adds it to the socket set.
64         returns socket id on success.  -1 on failure.
65         socket_type is one of INET or UNIX  */
66 int socket_open_tcp_server(socket_manager*, int port, const char* listen_ip );
67
68 int socket_open_unix_server(socket_manager* mgr, const char* path);
69
70 int socket_open_udp_server( socket_manager* mgr, int port, const char* listen_ip );
71
72 /* creates a client TCP socket and adds it to the socket set.
73         returns 0 on success.  -1 on failure.  */
74 int socket_open_tcp_client(socket_manager*, int port, const char* dest_addr);
75
76 /* creates a client UNIX socket and adds it to the socket set.
77         returns 0 on success.  -1 on failure.  */
78 int socket_open_unix_client(socket_manager*, const char* sock_path);
79
80 int socket_open_udp_client( socket_manager* mgr );
81
82 /* sends the given data to the given socket. returns 0 on success, -1 otherwise */
83 int socket_send(int sock_fd, const char* data);
84
85 /* waits at most usecs microseconds for the socket buffer to
86  * be available */
87 int socket_send_timeout( int sock_fd, const char* data, int usecs );
88
89 /* disconnects the node with the given sock_fd and removes
90         it from the socket set */
91 void socket_disconnect(socket_manager*, int sock_fd);
92
93 /* XXX This only works if 'sock_fd' is a client socket... */
94 int socket_wait(socket_manager* mgr, int timeout, int sock_fd);
95
96 /* waits on all sockets for incoming data.  
97         timeout == -1   | block indefinitely
98         timeout == 0    | don't block, just read any available data off all sockets
99         timeout == x    | block for at most x seconds */
100 int socket_wait_all(socket_manager* mgr, int timeout);
101
102 /* utility function for displaying the currently attached sockets */
103 void _socket_print_list(socket_manager* mgr);
104
105 int socket_connected(int sock_fd);
106
107 #ifdef __cplusplus
108 }
109 #endif
110
111 #endif