websocket first version works
[src/app-framework-binder.git] / src / websock.h
1 /*
2  * Copyright 2016 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 struct iovec;
25
26 #define STATUS_CODE_UNSET                0
27 #define STATUS_CODE_OK                1000
28 #define STATUS_CODE_GOING_AWAY        1001
29 #define STATUS_CODE_PROTOCOL_ERROR    1002
30 #define STATUS_CODE_RESERVED          1004      /* Protocol 8: frame too large */
31 #define STATUS_CODE_INVALID_UTF8      1007
32 #define STATUS_CODE_POLICY_VIOLATION  1008
33 #define STATUS_CODE_MESSAGE_TOO_LARGE 1009
34 #define STATUS_CODE_INTERNAL_ERROR    1011
35
36 struct websock_itf {
37         ssize_t (*writev) (void *, const struct iovec *, int);
38         ssize_t (*readv) (void *, const struct iovec *, int);
39         void (*disconnect) (void *);
40
41         void (*on_ping) (void *);
42         void (*on_pong) (void *);
43         void (*on_close) (void *, uint16_t code, size_t size);
44         void (*on_text) (void *, int last, size_t size);
45         void (*on_binary) (void *, int last, size_t size);
46         void (*on_continue) (void *, int last, size_t size);
47 };
48
49 struct websock;
50
51 void websock_close(struct websock *ws);
52 void websock_close_code(struct websock *ws, uint16_t code);
53
54 void websock_ping(struct websock *ws);
55 void websock_pong(struct websock *ws);
56 void websock_text(struct websock *ws, const char *text, size_t length);
57 void websock_binary(struct websock *ws, const void *data, size_t length);
58
59 ssize_t websock_read(struct websock *ws, void *buffer, size_t size);
60 void websock_drop(struct websock *ws);
61
62 int websock_dispatch(struct websock *ws);
63
64 struct websock *websock_create(const struct websock_itf *itf, void *closure);
65 void websock_destroy(struct websock *ws);
66