2828#include <signal.h>
2929#include <time.h>
3030
31+ bool log_enabled = false;
32+
3133static 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
6551int 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+
121137static 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
146165static 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
160182static 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+ }
0 commit comments