Skip to content

Commit 2e58832

Browse files
committed
updates
1 parent a735602 commit 2e58832

2 files changed

Lines changed: 125 additions & 55 deletions

File tree

port/mbed/wic_client.cpp

Lines changed: 106 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ const uint32_t WICClient::socket_open_flag = 1U;
99

1010
WICClient::WICClient(NetworkInterface &interface) :
1111
interface(interface),
12-
condition(mutex)
12+
condition(mutex),
13+
on_text_cb(nullptr),
14+
on_binary_cb(nullptr),
15+
on_open_cb(nullptr),
16+
on_close_cb(nullptr)
1317
{
1418
init_arg = {0};
1519

@@ -30,10 +34,11 @@ WICClient::WICClient(NetworkInterface &interface) :
3034

3135
init_arg.role = WIC_ROLE_CLIENT;
3236

33-
3437
/* these will block on flags until needed */
3538
writer_thread.start(callback(this, &WICClient::writer_task));
3639
reader_thread.start(callback(this, &WICClient::reader_task));
40+
41+
event_thread.start(callback(&events, &EventQueue::dispatch_forever));
3742
}
3843

3944
/* static methods *****************************************************/
@@ -68,28 +73,48 @@ WICClient::handle_rand(struct wic_inst *self)
6873
void
6974
WICClient::handle_open(struct wic_inst *self)
7075
{
71-
printf("handling open\n");
76+
WICClient *obj = to_obj(self);
77+
78+
obj->events.cancel(obj->timeout_id);
79+
80+
if(obj->on_open_cb){
81+
82+
obj->on_open_cb();
83+
}
7284
}
7385

7486
void
7587
WICClient::handle_close(struct wic_inst *self, uint16_t code, const char *reason, uint16_t size)
7688
{
77-
printf("handling close event\n");
78-
7989
WICClient *obj = to_obj(self);
80-
obj->sock.close();
90+
obj->sock.close();
91+
92+
if(obj->on_close_cb){
93+
94+
obj->on_close_cb(code, reason, size);
95+
}
8196
}
8297

8398
void
8499
WICClient::handle_text(struct wic_inst *self, bool fin, const char *data, uint16_t size)
85100
{
86-
printf("handling binary event\n");
101+
WICClient *obj = to_obj(self);
102+
103+
if(obj->on_text_cb){
104+
105+
obj->on_text_cb(fin, data, size);
106+
}
87107
}
88108

89109
void
90110
WICClient::handle_binary(struct wic_inst *self, bool fin, const void *data, uint16_t size)
91111
{
92-
printf("handling binary event\n");
112+
WICClient *obj = to_obj(self);
113+
114+
if(obj->on_binary_cb){
115+
116+
obj->on_binary_cb(fin, data, size);
117+
}
93118
}
94119

95120
/* protected instance methods *****************************************/
@@ -117,58 +142,63 @@ WICClient::do_close(bool &done)
117142
condition.notify_all();
118143
}
119144

145+
void
146+
WICClient::do_close_with_reason(bool &done, uint16_t code, const char *reason, uint16_t size)
147+
{
148+
wic_close_with_reason(&inst, code, reason, size);
149+
done = true;
150+
condition.notify_all();
151+
}
152+
153+
void
154+
WICClient::do_timeout(bool &done)
155+
{
156+
do_close(done);
157+
}
158+
120159
void
121160
WICClient::do_open(bool &done, bool &retval)
122161
{
123162
SocketAddress a;
163+
nsapi_error_t err;
124164

125-
sock.open(&interface);
165+
if(!wic_init(&inst, &init_arg)){
126166

127-
interface.gethostbyname(wic_get_url_hostname(&inst), &a);
167+
done = true;
168+
return;
169+
}
170+
171+
err = interface.gethostbyname(wic_get_url_hostname(&inst), &a);
172+
173+
if(err != NSAPI_ERROR_OK){
174+
175+
done = true;
176+
return;
177+
}
178+
179+
sock.open(&interface);
128180

129181
a.set_port(wic_get_url_port(&inst));
130182

131-
nsapi_error_t err = sock.connect(a);
183+
err = sock.connect(a);
132184

133185
if(err != NSAPI_ERROR_OK){
134186

135-
switch(err){
136-
case NSAPI_ERROR_DNS_FAILURE:
137-
case NSAPI_ERROR_TIMEOUT:
138-
case NSAPI_ERROR_CONNECTION_TIMEOUT:
139-
//ThisThread::sleep_for(5000);
140-
break;
141-
default:
142-
//ThisThread::sleep_for(5000);
143-
break;
144-
}
145-
146-
//requeue?
147-
//or stay in this event and loop
187+
sock.close();
188+
done = true;
189+
return;
148190
}
149191

150-
/* this should never fail at this point */
151-
{
152-
bool ok = wic_init(&inst, &init_arg);
153-
assert(ok);
154-
}
155-
156-
/* this might fail if something weird happens like not
157-
* enough memory for all the headers
158-
*
159-
* */
160-
{
161-
bool ok = wic_start(&inst);
162-
assert(ok);
192+
if(!wic_start(&inst)){
193+
194+
sock.close();
195+
done = true;
196+
return;
163197
}
164198

165199
flags.set(socket_open_flag);
166200

167-
/* now we actually have to wait for the thign to open */
168-
169-
170-
done = true;
171-
retval = true;
201+
timeout_id = events.call_in(5000, callback(this, &WICClient::do_timeout), done);
172202
}
173203

174204
void
@@ -193,9 +223,9 @@ WICClient::do_send_binary(bool &done, bool &retval, bool fin, const void *value,
193223
}
194224

195225
void
196-
WICClient::do_signal_socket_error()
226+
WICClient::do_signal_socket_error(uint16_t code)
197227
{
198-
228+
wic_close_with_reason(&inst, code, NULL, 0U);
199229
}
200230

201231
void
@@ -215,7 +245,7 @@ WICClient::writer_task(void)
215245

216246
if(sock.send(buf->data, buf->size) != NSAPI_ERROR_OK){
217247

218-
//signal failure to event loop, finalize thread
248+
events.call(callback(this, &WICClient::do_signal_socket_error), WIC_CLOSE_PROTOCOL_ERROR);
219249
}
220250

221251
output.free(buf);
@@ -243,7 +273,7 @@ WICClient::reader_task(void)
243273

244274
if(retval < 0){
245275

246-
// signal failure to event loop
276+
events.call(callback(this, &WICClient::do_signal_socket_error), WIC_CLOSE_PROTOCOL_ERROR);
247277

248278
input.free(buf);
249279
break;
@@ -252,7 +282,7 @@ WICClient::reader_task(void)
252282
buf->size = retval;
253283

254284
input.put(buf);
255-
queue.event(this, &WICClient::do_parse);
285+
events.call(this, &WICClient::do_parse);
256286
}
257287

258288
flags.clear(socket_open_flag);
@@ -269,7 +299,7 @@ WICClient::open(const char *url)
269299

270300
mutex.lock();
271301

272-
do_open(done, retval);
302+
events.call(callback(this, &WICClient::do_open), done, retval);
273303

274304
while(!done){
275305

@@ -288,7 +318,7 @@ WICClient::close()
288318

289319
bool done = false;
290320

291-
do_close(done);
321+
events.call(callback(this, &WICClient::do_close), done);
292322

293323
while(!done){
294324

@@ -332,7 +362,7 @@ WICClient::text(bool fin, const char *value, uint16_t size)
332362

333363
mutex.lock();
334364

335-
do_send_text(done, retval, fin, value, size);
365+
events.call(callback(this, &WICClient::do_send_text), done, retval, fin, value, size);
336366

337367
while(!done){
338368

@@ -358,8 +388,8 @@ WICClient::binary(bool fin, const void *value, uint16_t size)
358388

359389
mutex.lock();
360390

361-
do_send_binary(done, retval, fin, value, size);
362-
391+
events.call(callback(this, &WICClient::do_send_binary), done, retval, fin, value, size);
392+
363393
while(!done){
364394

365395
condition.wait();
@@ -375,3 +405,27 @@ WICClient::is_open()
375405
{
376406
return(wic_get_state(&inst) == WIC_STATE_OPEN);
377407
}
408+
409+
void
410+
WICClient::on_text(Callback<void(bool,const char *, uint16_t)> handler)
411+
{
412+
on_text_cb = handler;
413+
}
414+
415+
void
416+
WICClient::on_binary(Callback<void(bool,const void *, uint16_t)> handler)
417+
{
418+
on_binary_cb = handler;
419+
}
420+
421+
void
422+
WICClient::on_open(Callback<void()> handler)
423+
{
424+
on_open_cb = handler;
425+
}
426+
427+
void
428+
WICClient::on_close(Callback<void(uint16_t, const char *, uint16_t)> handler)
429+
{
430+
on_close_cb = handler;
431+
}

port/mbed/wic_client.hpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ class WICClient {
77

88
static const uint32_t socket_open_flag;
99

10+
int timeout_id;
11+
1012
uint8_t tx[1012U];
1113
uint8_t rx[1000U];
1214

1315
NetworkInterface &interface;
1416
TCPSocket sock;
1517
Mutex mutex;
1618
ConditionVariable condition;
17-
EventQueue queue;
19+
EventQueue events;
1820
EventFlags flags;
1921

2022
/* two very similar structures so we can have
@@ -35,10 +37,16 @@ class WICClient {
3537

3638
Thread writer_thread;
3739
Thread reader_thread;
40+
Thread event_thread;
3841

3942
struct wic_inst inst;
4043
struct wic_init_arg init_arg;
4144

45+
Callback<void(bool,const char *, uint16_t)> on_text_cb;
46+
Callback<void(bool,const void *, uint16_t)> on_binary_cb;
47+
Callback<void()> on_open_cb;
48+
Callback<void(uint16_t, const char *, uint16_t)> on_close_cb;
49+
4250
/* get WICClient instance back from wic_inst */
4351
static WICClient *to_obj(struct wic_inst *self);
4452

@@ -54,10 +62,12 @@ class WICClient {
5462
void do_parse();
5563
void do_open(bool &done, bool &retval);
5664
void do_close(bool &done);
65+
void do_close_with_reason(bool &done, uint16_t code, const char *reason, uint16_t size);
5766
void do_tick();
5867
void do_send_text(bool &done, bool &retval, bool fin, const char *value, uint16_t size);
5968
void do_send_binary(bool &done, bool &retval, bool fin, const void *value, uint16_t size);
60-
void do_signal_socket_error();
69+
void do_signal_socket_error(uint16_t code);
70+
void do_timeout(bool &done);
6171

6272
/* these run as threads */
6373
void writer_task(void);
@@ -84,5 +94,11 @@ class WICClient {
8494
bool binary(bool fin, const void *value, uint16_t size);
8595

8696
/* return true if websocket is open */
87-
bool is_open();
97+
bool is_open();
98+
99+
/* set callback to receive text messages */
100+
void on_text(Callback<void(bool,const char *, uint16_t)> handler);
101+
void on_binary(Callback<void(bool,const void *, uint16_t)> handler);
102+
void on_close(Callback<void(uint16_t,const char *, uint16_t)> handler);
103+
void on_open(Callback<void()> handler);
88104
};

0 commit comments

Comments
 (0)