From c2a63293d1ca78fa1cd9ea204f72c8dc666a61e8 Mon Sep 17 00:00:00 2001 From: Polynomialdivision Date: Fri, 12 Jun 2020 10:45:33 +0200 Subject: [PATCH] Revert "tcpsocket: implement client_read_cb with ustream_read my wheels are much worse than offical wheels." This reverts commit f7343a053c22f9af1d7a843e04b22bacd4a8ebf1. --- src/network/tcpsocket.c | 62 ++++++++++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 22 deletions(-) diff --git a/src/network/tcpsocket.c b/src/network/tcpsocket.c index 3eb266a..b6b1899 100644 --- a/src/network/tcpsocket.c +++ b/src/network/tcpsocket.c @@ -82,30 +82,48 @@ static void client_to_server_state(struct ustream *s) { static void client_read_cb(struct ustream *s, int bytes) { char *str; int len = 0; - uint32_t final_len = sizeof(uint32_t); - str = malloc(final_len); + uint32_t final_len; + int max_retry = 3, tried = 0; - if ((len = ustream_read(s, str, final_len)) < final_len){//ensure recv sizeof(uint32_t). - fprintf(stderr,"not complete msg, len:%d, expected len:%u\n", len, final_len); - goto out; - } - - final_len = ntohl(*(uint32_t *)str) - sizeof(uint32_t);//the final_len in headder includes header itself - str = realloc(str, final_len); - if ((len = ustream_read(s, str, final_len)) < final_len) {//ensure recv final_len bytes. - fprintf(stderr,"not complete msg, len:%d, expected len:%u\n", len, final_len); - goto out; - } + do { + str = ustream_get_read_buf(s, &len); + if (!str) + break; - if (network_config.use_symm_enc) { - char *dec = gcrypt_decrypt_msg(str, final_len);//len of str is final_len - handle_network_msg(dec); - free(dec); - } else { - handle_network_msg(str);//len of str is final_len - } -out: - free(str); + if (network_config.use_symm_enc) { + final_len = ntohl(*(uint32_t *)str); + if(len < final_len) {//not complete msg, wait for next recv + fprintf(stderr,"not complete msg, len:%d, expected len:%u\n", len, final_len); + if (tried++ == max_retry) { + ustream_consume(s, len); + return;//drop package + } + continue; + } + char *dec = gcrypt_decrypt_msg(str+sizeof(final_len), final_len-sizeof(final_len)); + + handle_network_msg(dec); + free(dec); + ustream_consume(s, final_len);//one msg is processed + tried = 0; + } else { + final_len = ntohl(*(uint32_t *)str); + if(len < final_len){ + fprintf(stderr,"not complete msg, len:%d, expected len:%u\n", len, final_len); + if (tried++ == max_retry) { + ustream_consume(s, len); + return; + } + continue; + } + char* msg = malloc(final_len); + memcpy(msg, str+sizeof(final_len), final_len-sizeof(final_len)); + handle_network_msg(msg); + ustream_consume(s, final_len);//one msg is processed + free(msg); + tried = 0; + } + } while(1); } static void server_cb(struct uloop_fd *fd, unsigned int events) {