@@ -9,7 +9,11 @@ const uint32_t WICClient::socket_open_flag = 1U;
99
1010WICClient::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)
6873void
6974WICClient::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
7486void
7587WICClient::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
8398void
8499WICClient::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
89109void
90110WICClient::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+
120159void
121160WICClient::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
174204void
@@ -193,9 +223,9 @@ WICClient::do_send_binary(bool &done, bool &retval, bool fin, const void *value,
193223}
194224
195225void
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
201231void
@@ -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+ }
0 commit comments