@@ -120,11 +120,13 @@ static int register_signal_handler() {
120120}
121121
122122// Register connection FD with epoll, associated with this `conn`
123- static bool update_conn_data_registrations (conn_data * conn , bool create ) {
123+ static bool update_conn_data_registrations (conn_data * conn , bool create ) {
124124 struct epoll_event transport_event ;
125125 transport_event .events = conn -> event_mask ;
126126 transport_event .data .ptr = conn ;
127- if (epoll_ctl (conn -> epoll_fd , create ? EPOLL_CTL_ADD : EPOLL_CTL_MOD , conn -> fd , & transport_event ) < 0 ) {
127+ if (epoll_ctl (
128+ conn -> epoll_fd , create ? EPOLL_CTL_ADD : EPOLL_CTL_MOD , conn -> fd , & transport_event
129+ ) < 0 ) {
128130 perror ("epoll_ctl (transport)" );
129131 return false;
130132 } else {
@@ -152,10 +154,10 @@ static size_t read_cb(void *userdata, hyper_context *ctx, uint8_t *buf, size_t b
152154 }
153155
154156 if (!(conn -> event_mask & EPOLLIN )) {
155- conn -> event_mask |= EPOLLIN ;
156- if (!update_conn_data_registrations (conn , false)) {
157- return HYPER_IO_ERROR ;
158- }
157+ conn -> event_mask |= EPOLLIN ;
158+ if (!update_conn_data_registrations (conn , false)) {
159+ return HYPER_IO_ERROR ;
160+ }
159161 }
160162
161163 conn -> read_waker = hyper_context_waker (ctx );
@@ -182,10 +184,10 @@ static size_t write_cb(void *userdata, hyper_context *ctx, const uint8_t *buf, s
182184 }
183185
184186 if (!(conn -> event_mask & EPOLLOUT )) {
185- conn -> event_mask |= EPOLLOUT ;
186- if (!update_conn_data_registrations (conn , false)) {
187- return HYPER_IO_ERROR ;
188- }
187+ conn -> event_mask |= EPOLLOUT ;
188+ if (!update_conn_data_registrations (conn , false)) {
189+ return HYPER_IO_ERROR ;
190+ }
189191 }
190192
191193 conn -> write_waker = hyper_context_waker (ctx );
@@ -201,15 +203,15 @@ static conn_data *create_conn_data(int epoll, int fd) {
201203 conn -> write_waker = NULL ;
202204
203205 if (!update_conn_data_registrations (conn , true)) {
204- free (conn );
205- return NULL ;
206+ free (conn );
207+ return NULL ;
206208 }
207209
208210 return conn ;
209211}
210212
211213static void free_conn_data (void * userdata ) {
212- conn_data * conn = (conn_data * )userdata ;
214+ conn_data * conn = (conn_data * )userdata ;
213215
214216 // Disassociate with the epoll
215217 if (epoll_ctl (conn -> epoll_fd , EPOLL_CTL_DEL , conn -> fd , NULL ) < 0 ) {
@@ -244,18 +246,18 @@ static hyper_io *create_io(conn_data *conn) {
244246}
245247
246248typedef struct service_userdata_s {
247- char host [128 ];
248- char port [8 ];
249- const hyper_executor * executor ;
249+ char host [128 ];
250+ char port [8 ];
251+ const hyper_executor * executor ;
250252} service_userdata ;
251253
252- static service_userdata * create_service_userdata () {
253- return (service_userdata * )calloc (1 , sizeof (service_userdata ));
254+ static service_userdata * create_service_userdata () {
255+ return (service_userdata * )calloc (1 , sizeof (service_userdata ));
254256}
255257
256- static void free_service_userdata (void * userdata ) {
257- service_userdata * cast_userdata = (service_userdata * )userdata ;
258- free (cast_userdata );
258+ static void free_service_userdata (void * userdata ) {
259+ service_userdata * cast_userdata = (service_userdata * )userdata ;
260+ free (cast_userdata );
259261}
260262
261263static int print_each_header (
@@ -272,23 +274,23 @@ static int print_body_chunk(void *userdata, const hyper_buf *chunk) {
272274 return HYPER_ITER_CONTINUE ;
273275}
274276
275- static int send_each_body_chunk (void * userdata , hyper_context * ctx , hyper_buf * * chunk ) {
276- int * chunk_count = (int * )userdata ;
277- if (* chunk_count > 0 ) {
278- unsigned char data [4096 ];
279- memset (data , '0' + (* chunk_count % 10 ), sizeof (data ));
280- * chunk = hyper_buf_copy (data , sizeof (data ));
281- (* chunk_count )-- ;
282- } else {
283- * chunk = NULL ;
284- }
285- return HYPER_POLL_READY ;
277+ static int send_each_body_chunk (void * userdata , hyper_context * ctx , hyper_buf * * chunk ) {
278+ int * chunk_count = (int * )userdata ;
279+ if (* chunk_count > 0 ) {
280+ unsigned char data [4096 ];
281+ memset (data , '0' + (* chunk_count % 10 ), sizeof (data ));
282+ * chunk = hyper_buf_copy (data , sizeof (data ));
283+ (* chunk_count )-- ;
284+ } else {
285+ * chunk = NULL ;
286+ }
287+ return HYPER_POLL_READY ;
286288}
287289
288290static void server_callback (
289291 void * userdata , hyper_request * request , hyper_response_channel * channel
290292) {
291- service_userdata * service_data = (service_userdata * )userdata ;
293+ service_userdata * service_data = (service_userdata * )userdata ;
292294 printf ("Request from %s:%s\n" , service_data -> host , service_data -> port );
293295
294296 // Print out various properties of the request.
@@ -323,10 +325,10 @@ static void server_callback(
323325 hyper_headers * req_headers = hyper_request_headers (request );
324326 hyper_headers_foreach (req_headers , print_each_header , NULL );
325327
326- if (!strcmp ((char * )method , "POST" ) || !strcmp ((char * )method , "PUT" )) {
328+ if (!strcmp ((char * )method , "POST" ) || !strcmp ((char * )method , "PUT" )) {
327329 // ...consume the request body
328- hyper_body * body = hyper_request_body (request );
329- hyper_task * task = hyper_body_foreach (body , print_body_chunk , NULL , NULL );
330+ hyper_body * body = hyper_request_body (request );
331+ hyper_task * task = hyper_body_foreach (body , print_body_chunk , NULL , NULL );
330332 hyper_executor_push (service_data -> executor , task );
331333 }
332334
@@ -336,22 +338,18 @@ static void server_callback(
336338 // Build a response
337339 hyper_response * response = hyper_response_new ();
338340 hyper_response_set_status (response , 200 );
339- hyper_headers * rsp_headers = hyper_response_headers (response );
341+ hyper_headers * rsp_headers = hyper_response_headers (response );
340342 hyper_headers_set (
341- rsp_headers ,
342- (unsigned char * )"Cache-Control" ,
343- 13 ,
344- (unsigned char * )"no-cache" ,
345- 8
343+ rsp_headers , (unsigned char * )"Cache-Control" , 13 , (unsigned char * )"no-cache" , 8
346344 );
347345
348- if (!strncmp ((char * )method , "GET" , method_len )) {
346+ if (!strncmp ((char * )method , "GET" , method_len )) {
349347 // ...add a body
350- hyper_body * body = hyper_body_new ();
348+ hyper_body * body = hyper_body_new ();
351349 hyper_body_set_data_func (body , send_each_body_chunk );
352- int * chunk_count = (int * )malloc (sizeof (int ));
350+ int * chunk_count = (int * )malloc (sizeof (int ));
353351 * chunk_count = 1000 ;
354- hyper_body_set_userdata (body , (void * )chunk_count , free );
352+ hyper_body_set_userdata (body , (void * )chunk_count , free );
355353 hyper_response_set_body (response , body );
356354 }
357355
@@ -412,7 +410,7 @@ int main(int argc, char *argv[]) {
412410 // Configure the server HTTP/2 stack
413411 hyper_http2_serverconn_options * http2_opts = hyper_http2_serverconn_options_new (exec );
414412 hyper_http2_serverconn_options_keep_alive_interval (http2_opts , 5 ); // 5 seconds
415- hyper_http2_serverconn_options_keep_alive_timeout (http2_opts , 5 ); // 5 seconds
413+ hyper_http2_serverconn_options_keep_alive_timeout (http2_opts , 5 ); // 5 seconds
416414
417415 while (1 ) {
418416 while (1 ) {
@@ -424,7 +422,7 @@ int main(int argc, char *argv[]) {
424422 if (hyper_task_type (task ) == HYPER_TASK_ERROR ) {
425423 printf ("hyper task failed with error!\n" );
426424
427- hyper_error * err = hyper_task_value (task );
425+ hyper_error * err = hyper_task_value (task );
428426 printf ("error code: %d\n" , hyper_error_code (err ));
429427 uint8_t errbuf [256 ];
430428 size_t errlen = hyper_error_print (err , errbuf , sizeof (errbuf ));
@@ -492,7 +490,9 @@ int main(int argc, char *argv[]) {
492490 perror ("getnameinfo" );
493491 printf ("New incoming connection from (unknown)\n" );
494492 } else {
495- printf ("New incoming connection from (%s:%s)\n" , userdata -> host , userdata -> port );
493+ printf (
494+ "New incoming connection from (%s:%s)\n" , userdata -> host , userdata -> port
495+ );
496496 }
497497
498498 // Set non-blocking
@@ -546,26 +546,26 @@ int main(int argc, char *argv[]) {
546546 // Existing transport socket, poke the wakers or close the socket
547547 conn_data * conn = events [n ].data .ptr ;
548548 if (events [n ].events & EPOLLIN ) {
549- if (conn -> read_waker ) {
550- hyper_waker_wake (conn -> read_waker );
551- conn -> read_waker = NULL ;
552- } else {
553- conn -> event_mask &= ~EPOLLIN ;
554- if (!update_conn_data_registrations (conn , false)) {
555- epoll_ctl (conn -> epoll_fd , EPOLL_CTL_DEL , conn -> fd , NULL );
549+ if (conn -> read_waker ) {
550+ hyper_waker_wake (conn -> read_waker );
551+ conn -> read_waker = NULL ;
552+ } else {
553+ conn -> event_mask &= ~EPOLLIN ;
554+ if (!update_conn_data_registrations (conn , false)) {
555+ epoll_ctl (conn -> epoll_fd , EPOLL_CTL_DEL , conn -> fd , NULL );
556+ }
556557 }
557- }
558558 }
559559 if (events [n ].events & EPOLLOUT ) {
560- if (conn -> write_waker ) {
561- hyper_waker_wake (conn -> write_waker );
562- conn -> write_waker = NULL ;
563- } else {
564- conn -> event_mask &= ~EPOLLOUT ;
565- if (!update_conn_data_registrations (conn , false)) {
566- epoll_ctl (conn -> epoll_fd , EPOLL_CTL_DEL , conn -> fd , NULL );
560+ if (conn -> write_waker ) {
561+ hyper_waker_wake (conn -> write_waker );
562+ conn -> write_waker = NULL ;
563+ } else {
564+ conn -> event_mask &= ~EPOLLOUT ;
565+ if (!update_conn_data_registrations (conn , false)) {
566+ epoll_ctl (conn -> epoll_fd , EPOLL_CTL_DEL , conn -> fd , NULL );
567+ }
567568 }
568- }
569569 }
570570 }
571571 }
0 commit comments