The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.

Please select the desired protocol below to get the URL.

This URL has Read-Only access.

Statistics
| Branch: | Revision:

main_repo / deps / liboi / oi_socket.h @ 40c0f755

History | View | Annotate | Download (2.63 KB)

1 40c0f755 Ryan
#include <netdb.h>
2
#include <ev.h>
3
#include <oi_queue.h>
4
#include <oi_error.h>
5
#include <oi_buf.h>
6
7
#ifndef oi_socket_h
8
#define oi_socket_h
9
#ifdef __cplusplus
10
extern "C" {
11
#endif 
12
13
#ifndef HAVE_GNUTLS
14
# define HAVE_GNUTLS 0
15
#endif
16
#if HAVE_GNUTLS
17
# include <gnutls/gnutls.h>
18
#endif
19
20
typedef struct oi_server  oi_server;
21
typedef struct oi_socket  oi_socket;
22
23
void oi_server_init          (oi_server *, int backlog);
24
 int oi_server_listen        (oi_server *, struct addrinfo *addrinfo);
25
void oi_server_attach        (oi_server *, struct ev_loop *loop);
26
void oi_server_detach        (oi_server *);
27
void oi_server_close         (oi_server *); 
28
29
void oi_socket_init          (oi_socket *, float timeout);
30
 int oi_socket_pair          (oi_socket *a, oi_socket *b); /* TODO */
31
 int oi_socket_connect       (oi_socket *, struct addrinfo *addrinfo);
32
void oi_socket_attach        (oi_socket *, struct ev_loop *loop);
33
void oi_socket_detach        (oi_socket *);
34
void oi_socket_read_start    (oi_socket *);
35
void oi_socket_read_stop     (oi_socket *);
36
void oi_socket_reset_timeout (oi_socket *);
37
void oi_socket_write         (oi_socket *, oi_buf *);
38
void oi_socket_write_simple  (oi_socket *, const char *str, size_t len);
39
void oi_socket_write_eof     (oi_socket *);
40
void oi_socket_close         (oi_socket *);
41
#if HAVE_GNUTLS
42
void oi_socket_set_secure_session (oi_socket *, gnutls_session_t);
43
#endif
44
45
struct oi_server {
46
  /* read only */
47
  int fd;
48
  int backlog;
49
  struct ev_loop *loop;
50
  unsigned listening:1;
51
52
  /* private */
53
  ev_io connection_watcher;
54
55
  /* public */
56
  oi_socket* (*on_connection) (oi_server *, struct sockaddr *remote_addr, socklen_t remove_addr_len);
57
  void       (*on_error)      (oi_server *, struct oi_error e);
58
  void *data;
59
};
60
61
struct oi_socket {
62
  /* read only */
63
  int fd;
64
  struct ev_loop *loop;
65
  oi_server *server;
66
  oi_queue out_stream;
67
  size_t written;
68
  unsigned connected:1;
69
  unsigned secure:1;
70
  unsigned wait_for_secure_hangup:1;
71
72
  /* if these are NULL then it means that end of the socket is closed. */
73
  int (*read_action)  (oi_socket *);
74
  int (*write_action) (oi_socket *);
75
76
  /* private */  
77
  ev_io write_watcher;
78
  ev_io read_watcher;
79
  ev_timer timeout_watcher;
80
#if HAVE_GNUTLS
81
  gnutls_session_t session;
82
#endif
83
  
84
  /* public */
85
  size_t chunksize; /* the maximum chunk that on_read() will return */
86
  void (*on_connect)   (oi_socket *);
87
  void (*on_read)      (oi_socket *, const void *buf, size_t count);
88
  void (*on_drain)     (oi_socket *);
89
  void (*on_error)     (oi_socket *, struct oi_error e);
90
  void (*on_close)     (oi_socket *);
91
  void (*on_timeout)   (oi_socket *);
92
  void *data;
93
};
94
95
#ifdef __cplusplus
96
}
97
#endif 
98
#endif /* oi_socket_h */