-
Notifications
You must be signed in to change notification settings - Fork 63
/
connection.h
119 lines (78 loc) · 2.02 KB
/
connection.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
*
* filename: connection.h
* summary: websocket connection
* author: caosiyang
* email: [email protected]
*
*/
#ifndef CONNECTION_H
#define CONNECTION_H
#include <stdlib.h>
#include <iostream>
#include <event2/event.h>
#include <event2/bufferevent.h>
#include "websocket.h"
using namespace std;
typedef void(*websocket_cb)(void*);
typedef struct {
websocket_cb cb;
void *cbarg;
} ws_cb_unit;
typedef struct websocket_connection {
struct bufferevent *bev;
string ws_req_str;
string ws_resp_str;
enum Step step;
uint64_t ntoread;
frame_t *frame; //current frame
ws_cb_unit handshake_cb_unit;
ws_cb_unit frame_recv_cb_unit;
ws_cb_unit write_cb_unit;
ws_cb_unit close_cb_unit;
ws_cb_unit ping_cb_unit;
} ws_conn_t;
//callback type
enum CBTYPE {
HANDSHAKE,
FRAME_RECV,
WRITE,
CLOSE,
PING
};
//
// following functions are for library-users
//
//create a websocket connection
ws_conn_t *ws_conn_new();
//destroy a websocket connection
void ws_conn_free(ws_conn_t *conn);
//set callback
//MUST set: frame_read_cb, write_cb, close_cb
void ws_conn_setcb(ws_conn_t *conn, enum CBTYPE cbtype, websocket_cb cb, void *cbarg);
//websocket serve start
void ws_serve_start(ws_conn_t *conn);
//websocket serve exit
void ws_serve_exit(ws_conn_t *conn);
//send a frame
int32_t send_a_frame(ws_conn_t *conn, const frame_buffer_t *fb);
//
// following functions are for internal
//
//accept the websocket request
void accept_websocket_request(ws_conn_t *conn);
//respond the websocket request
void respond_websocket_request(ws_conn_t *conn);
//receive a frame
void frame_recv_loop(ws_conn_t *conn);
//request read callback
void request_read_cb(struct bufferevent *bev, void *ctx);
//response write callback
void response_write_cb(struct bufferevent *bev, void *ctx);
//frame read callback
void frame_read_cb(struct bufferevent *bev, void *ctx);
//websocket write callback
void write_cb(struct bufferevent *bev, void *ctx);
//connection close callback
void close_cb(struct bufferevent *bev, short what, void *ctx);
#endif