Update copyright dates
[src/app-framework-binder.git] / src / websock.h
1 /*
2  * Copyright (C) 2015-2020 "IoT.bzh"
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /*
18  * This work is a far adaptation of apache-websocket:
19  *   origin:  https://github.com/disconnect/apache-websocket
20  *   commit:  cfaef071223f11ba016bff7e1e4b7c9e5df45b50
21  *   Copyright 2010-2012 self.disconnect (APACHE-2)
22  */
23
24 #pragma once
25
26 struct iovec;
27
28 #define WEBSOCKET_CODE_OK                1000
29 #define WEBSOCKET_CODE_GOING_AWAY        1001
30 #define WEBSOCKET_CODE_PROTOCOL_ERROR    1002
31 #define WEBSOCKET_CODE_CANT_ACCEPT       1003
32 #define WEBSOCKET_CODE_RESERVED          1004
33 #define WEBSOCKET_CODE_NOT_SET           1005
34 #define WEBSOCKET_CODE_ABNORMAL          1006
35 #define WEBSOCKET_CODE_INVALID_UTF8      1007
36 #define WEBSOCKET_CODE_POLICY_VIOLATION  1008
37 #define WEBSOCKET_CODE_MESSAGE_TOO_LARGE 1009
38 #define WEBSOCKET_CODE_INTERNAL_ERROR    1011
39
40 struct websock_itf {
41         ssize_t (*writev) (void *, const struct iovec *, int);
42         ssize_t (*readv) (void *, const struct iovec *, int);
43
44         void (*on_ping) (void *, size_t size); /* optional, if not NULL, responsible of pong */
45         void (*on_pong) (void *, size_t size); /* optional */
46         void (*on_close) (void *, uint16_t code, size_t size);
47         void (*on_text) (void *, int last, size_t size);
48         void (*on_binary) (void *, int last, size_t size);
49         void (*on_continue) (void *, int last, size_t size);
50         int (*on_extension) (void *, int last, int rsv1, int rsv2, int rsv3, int opcode, size_t size);
51
52         void (*on_error) (void *, uint16_t code, const void *data, size_t size); /* optional */
53 };
54
55 struct websock;
56
57 extern int websock_close_empty(struct websock *ws);
58 extern int websock_close(struct websock *ws, uint16_t code, const void *data, size_t length);
59 extern int websock_error(struct websock *ws, uint16_t code, const void *data, size_t length);
60
61 extern int websock_ping(struct websock *ws, const void *data, size_t length);
62 extern int websock_pong(struct websock *ws, const void *data, size_t length);
63 extern int websock_text(struct websock *ws, int last, const void *text, size_t length);
64 extern int websock_text_v(struct websock *ws, int last, const struct iovec *iovec, int count);
65 extern int websock_binary(struct websock *ws, int last, const void *data, size_t length);
66 extern int websock_binary_v(struct websock *ws, int last, const struct iovec *iovec, int count);
67 extern int websock_continue(struct websock *ws, int last, const void *data, size_t length);
68 extern int websock_continue_v(struct websock *ws, int last, const struct iovec *iovec, int count);
69
70 extern ssize_t websock_read(struct websock *ws, void *buffer, size_t size);
71 extern int websock_drop(struct websock *ws);
72
73 extern int websock_dispatch(struct websock *ws, int loop);
74
75 extern struct websock *websock_create_v13(const struct websock_itf *itf, void *closure);
76 extern void websock_destroy(struct websock *ws);
77
78 extern void websock_set_default_max_length(size_t maxlen);
79 extern void websock_set_max_length(struct websock *ws, size_t maxlen);
80
81 extern const char *websocket_explain_error(uint16_t code);