tcpsocket: implement client_read_cb with ustream_read my wheels are much worse than offical wheels.

This commit is contained in:
twy_2000 2020-06-11 16:38:07 +08:00 committed by Polynomialdivision
parent 292ccb01f2
commit f7343a053c

View file

@ -82,48 +82,30 @@ static void client_to_server_state(struct ustream *s) {
static void client_read_cb(struct ustream *s, int bytes) { static void client_read_cb(struct ustream *s, int bytes) {
char *str; char *str;
int len = 0; int len = 0;
uint32_t final_len; uint32_t final_len = sizeof(uint32_t);
int max_retry = 3, tried = 0; str = malloc(final_len);
do { if ((len = ustream_read(s, str, final_len)) < final_len){//ensure recv sizeof(uint32_t).
str = ustream_get_read_buf(s, &len); fprintf(stderr,"not complete msg, len:%d, expected len:%u\n", len, final_len);
if (!str) goto out;
break; }
if (network_config.use_symm_enc) { final_len = ntohl(*(uint32_t *)str) - sizeof(uint32_t);//the final_len in headder includes header itself
final_len = ntohl(*(uint32_t *)str); str = realloc(str, final_len);
if(len < final_len) {//not complete msg, wait for next recv 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); fprintf(stderr,"not complete msg, len:%d, expected len:%u\n", len, final_len);
if (tried++ == max_retry) { goto out;
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); if (network_config.use_symm_enc) {
free(dec); char *dec = gcrypt_decrypt_msg(str, final_len);//len of str is final_len
ustream_consume(s, final_len);//one msg is processed handle_network_msg(dec);
tried = 0; free(dec);
} else { } else {
final_len = ntohl(*(uint32_t *)str); handle_network_msg(str);//len of str is final_len
if(len < final_len){ }
fprintf(stderr,"not complete msg, len:%d, expected len:%u\n", len, final_len); out:
if (tried++ == max_retry) { free(str);
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) { static void server_cb(struct uloop_fd *fd, unsigned int events) {