summaryrefslogtreecommitdiff
path: root/plugins/websocket/ws_proto.h
blob: 5a1cb23ecf78f4c7dfac988567f2add2dd919cfa (plain)
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
#ifndef HYBBX_WS_PROTO_H
#define HYBBX_WS_PROTO_H

#include "hybbx/types.h"

#include <stddef.h>
#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

#define HYBBX_WS_HANDSHAKE_MAX 4096u
#define HYBBX_WS_FRAME_PAYLOAD_MAX 4096u

typedef void (*hybbx_ws_data_cb)(void *ctx, const uint8_t *data, size_t len);

typedef struct hybbx_ws_connection {
    int fd;
    void *tls;
    int established;
    uint8_t rx_buf[HYBBX_WS_FRAME_PAYLOAD_MAX + 16];
    size_t rx_len;
} hybbx_ws_connection_t;

void hybbx_ws_connection_init(hybbx_ws_connection_t *ws, int fd);

/** Attach TLS session (opaque SSL*) after accept; plain ws when NULL. */
void hybbx_ws_connection_set_tls(hybbx_ws_connection_t *ws, void *tls);

/** Release TLS resources; does not close @c fd. */
void hybbx_ws_connection_cleanup(hybbx_ws_connection_t *ws);

/**
 * Perform server-side HTTP Upgrade handshake.
 * @p path must match the request URI path (e.g. /hybbx) when non-empty.
 */
hybbx_result_t hybbx_ws_server_handshake(hybbx_ws_connection_t *ws,
                                         const char *path);

/**
 * Read WebSocket frames; invoke @p on_data for each complete text/binary
 * payload from the client. Returns HYBBX_ERR_IO on disconnect.
 */
hybbx_result_t hybbx_ws_read_frames(hybbx_ws_connection_t *ws,
                                    hybbx_ws_data_cb on_data,
                                    void *ctx);

/** Send one server text frame (unmasked). */
hybbx_result_t hybbx_ws_write_text(hybbx_ws_connection_t *ws,
                                   const char *data, size_t len);

/** Send one server ping frame (unmasked, empty payload). */
hybbx_result_t hybbx_ws_ping(hybbx_ws_connection_t *ws);

/** Send WebSocket close frame. */
hybbx_result_t hybbx_ws_close(hybbx_ws_connection_t *ws);

#ifdef __cplusplus
}
#endif

#endif /* HYBBX_WS_PROTO_H */
git clone -b <branch> https://cgit.mode42.com/<repo>.git
git clone -b <branch> git://cgit.mode42.com/<repo>.git

info@mode42.com