Skip to content

Commit 3c18e47

Browse files
committed
0.2.0 update
- added `wic_on_buffer_fn` callback for allocating a transmit buffer and indicating when socket is not able to buffer - added buffer type field to `wic_on_buffer_fn` and `wic_on_send_fn` to support buffer prioritisation (i.e. so a pong doesn't disappear) - added `wic_on_handshake_failure_fn` callback to indicate a failed handshake - added `wic_on_close_transport_fn` callback as a dedicated handler for closing the transport - added `wic_on_ping_fn` callback - added `wic_on_pong_fn` callback - removed `wic_send_pong*` interfaces - changed URL schema to be an enum rather than string - added mbed port work in progress - fixed the echo example (was previously closing prematurely)
1 parent 2e58832 commit 3c18e47

27 files changed

Lines changed: 2831 additions & 1294 deletions

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ mbed-os
44
mbed-os.lib
55
*.py
66
*.pyc
7+
.gdbinit
8+
*.jlink
9+
*.py
10+
*.pyc

examples/autobahn/client.c

Lines changed: 80 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -28,44 +28,29 @@
2828
#include <signal.h>
2929
#include <time.h>
3030

31+
bool log_enabled = false;
32+
3133
static void on_close(struct wic_inst *inst, uint16_t code, const char *reason, uint16_t size);
32-
static void on_text(struct wic_inst *inst, bool fin, const char *data, uint16_t size);
33-
static void on_binary(struct wic_inst *inst, bool fin, const void *data, uint16_t size);
34-
static void on_open(struct wic_inst *inst);
35-
static void do_write(struct wic_inst *inst, const void *data, size_t size);
36-
static uint32_t do_random(struct wic_inst *inst);
37-
static void on_text_case_count(struct wic_inst *inst, bool fin, const char *data, uint16_t size);
3834

39-
static int n = 0;
35+
static void on_handshake_failure_handler(struct wic_inst *inst, enum wic_handshake_failure reason);
4036

41-
static void do_client(int *s, struct wic_inst *inst, const struct wic_init_arg *arg)
42-
{
43-
if(!wic_init(inst, arg)){
37+
static bool on_message(struct wic_inst *inst, enum wic_encoding encoding, bool fin, const char *data, uint16_t size);
38+
static bool on_message_case_count(struct wic_inst *inst, enum wic_encoding encoding, bool fin, const char *data, uint16_t size);
4439

45-
ERROR("could not init instance")
46-
exit(EXIT_FAILURE);
47-
}
40+
static void on_open(struct wic_inst *inst);
41+
static void on_send(struct wic_inst *inst, const void *data, size_t size, enum wic_buffer type);
42+
static uint32_t do_random(struct wic_inst *inst);
4843

49-
if(
50-
transport_open_client(
51-
wic_get_url_schema(inst),
52-
wic_get_url_hostname(inst),
53-
wic_get_url_port(inst),
54-
s
55-
)
56-
){
57-
wic_start(inst);
44+
static void on_close_transport(struct wic_inst *inst);
45+
static void *on_buffer(struct wic_inst *inst, size_t min_size, enum wic_buffer type, size_t *max_size);
5846

59-
while(transport_recv(*s, inst));
47+
static void do_client(int *s, struct wic_inst *inst, const struct wic_init_arg *arg);
6048

61-
wic_close(inst);
62-
}
63-
}
49+
static int n = 0;
6450

6551
int main(int argc, char **argv)
6652
{
6753
static struct wic_inst inst;
68-
static uint8_t tx_buffer[UINT16_MAX+100UL];
6954
static uint8_t rx_buffer[UINT16_MAX];
7055
static char url[1000U];
7156

@@ -79,24 +64,24 @@ int main(int argc, char **argv)
7964

8065
arg.rx = rx_buffer;
8166
arg.rx_max = sizeof(rx_buffer);
82-
arg.tx = tx_buffer;
83-
arg.tx_max = sizeof(tx_buffer);
8467
arg.on_open = on_open;
8568
arg.on_close = on_close;
86-
arg.write = do_write;
69+
arg.on_send = on_send;
70+
arg.on_buffer = on_buffer;
71+
arg.on_close_transport = on_close_transport;
72+
arg.on_handshake_failure = on_handshake_failure_handler;
8773
arg.rand = do_random;
8874
arg.app = &s;
8975
arg.role = WIC_ROLE_CLIENT;
9076
arg.url = url;
9177

9278
arg.url = "ws://localhost:9001/getCaseCount?agent=wic";
93-
arg.on_text = on_text_case_count;
79+
arg.on_message = on_message_case_count;
9480

9581
do_client(&s, &inst, &arg);
9682

9783
arg.url = url;
98-
arg.on_text = on_text;
99-
arg.on_binary = on_binary;
84+
arg.on_message = on_message;
10085

10186
for(tc=1; tc <= n; tc++){
10287

@@ -108,56 +93,102 @@ int main(int argc, char **argv)
10893
}
10994

11095
arg.url = "ws://localhost:9001/updateReports?agent=wic";
111-
arg.on_text = NULL;
112-
arg.on_binary = NULL;
113-
96+
arg.on_message = NULL;
97+
11498
do_client(&s, &inst, &arg);
11599

116100
LOG("exiting...")
117101

118102
exit(EXIT_SUCCESS);
119103
}
120104

105+
static void on_handshake_failure_handler(struct wic_inst *inst, enum wic_handshake_failure reason)
106+
{
107+
LOG("websocket handshake failed for reason %d", reason);
108+
}
109+
110+
static void do_client(int *s, struct wic_inst *inst, const struct wic_init_arg *arg)
111+
{
112+
if(!wic_init(inst, arg)){
113+
114+
ERROR("could not init instance")
115+
exit(EXIT_FAILURE);
116+
}
117+
118+
if(
119+
transport_open_client(
120+
wic_get_url_schema(inst),
121+
wic_get_url_hostname(inst),
122+
wic_get_url_port(inst),
123+
s
124+
)
125+
){
126+
if(wic_start(inst) == WIC_STATUS_SUCCESS){
127+
128+
while(transport_recv(*s, inst));
129+
}
130+
else{
131+
132+
transport_close(s);
133+
}
134+
}
135+
}
136+
121137
static void on_close(struct wic_inst *inst, uint16_t code, const char *reason, uint16_t size)
122138
{
123-
transport_close((int *)wic_get_app(inst));
139+
LOG("websocket closed for reason %u %.*s", code, size, reason);
124140
}
125141

126-
static void on_text(struct wic_inst *inst, bool fin, const char *data, uint16_t size)
142+
static bool on_message(struct wic_inst *inst, enum wic_encoding encoding, bool fin, const char *data, uint16_t size)
127143
{
128-
wic_send_text(inst, fin, data, size);
144+
LOG("received %u bytes of %s %s", size, (encoding == WIC_ENCODING_UTF8) ? "text" : "binary", fin ? "(final)" : "");
145+
146+
wic_send(inst, encoding, fin, data, size);
147+
148+
return true;
129149
}
130150

131-
static void on_text_case_count(struct wic_inst *inst, bool fin, const char *data, uint16_t size)
151+
static bool on_message_case_count(struct wic_inst *inst, enum wic_encoding encoding, bool fin, const char *data, uint16_t size)
132152
{
153+
LOG("%.*s", size, data);
154+
133155
if(size > 0){
134156

135157
n = atoi(data);
136158
}
137159

138160
wic_close(inst);
139-
}
140161

141-
static void on_binary(struct wic_inst *inst, bool fin, const void *data, uint16_t size)
142-
{
143-
wic_send_binary(inst, fin, data, size);
162+
return true;
144163
}
145164

146165
static void on_open(struct wic_inst *inst)
147166
{
167+
LOG("websocket is open");
148168
}
149169

150-
static void do_write(struct wic_inst *inst, const void *data, size_t size)
170+
static void on_send(struct wic_inst *inst, const void *data, size_t size, enum wic_buffer type)
151171
{
152-
if(!transport_write(*(int *)wic_get_app(inst), data, size)){
172+
LOG("sending buffer type %d", type);
153173

154-
ERROR("transport_write()")
174+
transport_write(*(int *)wic_get_app(inst), data, size);
175+
}
155176

156-
wic_close(inst);
157-
}
177+
static void on_close_transport(struct wic_inst *inst)
178+
{
179+
transport_close((int *)wic_get_app(inst));
158180
}
159181

160182
static uint32_t do_random(struct wic_inst *inst)
161183
{
162184
return rand();
163185
}
186+
187+
static void *on_buffer(struct wic_inst *inst, size_t min_size, enum wic_buffer type, size_t *max_size)
188+
{
189+
static uint8_t tx[UINT16_MAX+100UL];
190+
191+
*max_size = sizeof(tx);
192+
193+
return (min_size <= sizeof(tx)) ? tx : NULL;
194+
}

examples/autobahn/config/fuzzingserver.json

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@
44
"outdir": "./reports/clients",
55
"cases": ["*"],
66
"exclude-cases": [
7-
"9.3.*",
8-
"9.4.*",
9-
"9.5.*",
10-
"9.6.*",
11-
"9.7.*",
12-
"9.8.*"
13-
],
7+
],
148
"exclude-agent-cases": {}
159
}

examples/autobahn/log.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424

2525
#include <stdio.h>
2626

27-
#define LOG(...) do{printf("%s: ", __FILE__);printf(__VA_ARGS__);printf("\n");fflush(stdout);}while(0);
28-
#define ERROR(...) do{fprintf(stderr, "error: ");fprintf(stderr, __VA_ARGS__);fprintf(stderr, "\n");fflush(stderr);}while(0);
27+
extern bool log_enabled;
28+
29+
#define LOG(...) do{if(log_enabled){printf(__VA_ARGS__);printf("\n");fflush(stdout);}}while(0);
30+
#define ERROR(...) do{fprintf(stderr, "%s: ", __FILE__);fprintf(stderr, "error: ");fprintf(stderr, __VA_ARGS__);fprintf(stderr, "\n");fflush(stderr);}while(0);
2931

3032
#endif

examples/autobahn/makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ CFLAGS += -D'WIC_PORT_INCLUDE="port.h"'
1818
SRC := $(notdir $(wildcard $(DIR_ROOT)/src/*.c)) transport.c
1919
OBJ := $(SRC:.c=.o)
2020

21-
all: $(addprefix bin/, client server)
21+
all: $(addprefix bin/, client)
2222

2323
bin/client: $(addprefix build/,$(OBJ) client.o)
2424
$(CC) $(LDFLAGS) $^ -o $@

0 commit comments

Comments
 (0)