fixed a bug in main-c.c
This commit is contained in:
		| @ -128,16 +128,6 @@ | |||||||
| #	endif | #	endif | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
|  |  | ||||||
| union sockaddr_t |  | ||||||
| { |  | ||||||
| 	struct sockaddr_in in4; |  | ||||||
| #if (HCL_SIZEOF_STRUCT_SOCKADDR_IN6 > 0) |  | ||||||
| 	struct sockaddr_in6 in6; |  | ||||||
| #endif |  | ||||||
| }; |  | ||||||
| typedef union sockaddr_t sockaddr_t; |  | ||||||
|  |  | ||||||
| struct bb_t | struct bb_t | ||||||
| { | { | ||||||
| 	char buf[1024]; | 	char buf[1024]; | ||||||
| @ -251,7 +241,7 @@ struct hcl_server_worker_t | |||||||
| 	hcl_oow_t wid; | 	hcl_oow_t wid; | ||||||
|  |  | ||||||
| 	int sck; | 	int sck; | ||||||
| 	sockaddr_t peeraddr; | 	hcl_sckaddr_t peeraddr; | ||||||
|  |  | ||||||
| 	int claimed; | 	int claimed; | ||||||
| 	 | 	 | ||||||
| @ -2002,7 +1992,7 @@ static HCL_INLINE void release_wid (hcl_server_t* server, hcl_server_worker_t* w | |||||||
| 	worker->wid = HCL_SERVER_WID_INVALID; | 	worker->wid = HCL_SERVER_WID_INVALID; | ||||||
| } | } | ||||||
|  |  | ||||||
| static hcl_server_worker_t* alloc_worker (hcl_server_t* server, int cli_sck, const sockaddr_t* peeraddr) | static hcl_server_worker_t* alloc_worker (hcl_server_t* server, int cli_sck, const hcl_sckaddr_t* peeraddr) | ||||||
| { | { | ||||||
| 	hcl_server_worker_t* worker; | 	hcl_server_worker_t* worker; | ||||||
|  |  | ||||||
| @ -2250,7 +2240,7 @@ int hcl_server_start (hcl_server_t* server, const hcl_bch_t* addrs) | |||||||
| { | { | ||||||
| 	hcl_sckaddr_t srv_addr; | 	hcl_sckaddr_t srv_addr; | ||||||
| 	int srv_fd, sck_fam, optval, xret = 0; | 	int srv_fd, sck_fam, optval, xret = 0; | ||||||
| 	socklen_t srv_len; | 	hcl_scklen_t srv_len; | ||||||
| 	pthread_attr_t thr_attr; | 	pthread_attr_t thr_attr; | ||||||
|  |  | ||||||
| /* TODO: interprete 'addrs' as a command-separated address list  | /* TODO: interprete 'addrs' as a command-separated address list  | ||||||
| @ -2293,9 +2283,9 @@ int hcl_server_start (hcl_server_t* server, const hcl_bch_t* addrs) | |||||||
| 	server->stopreq = 0; | 	server->stopreq = 0; | ||||||
| 	while (!server->stopreq) | 	while (!server->stopreq) | ||||||
| 	{ | 	{ | ||||||
| 		sockaddr_t cli_addr; | 		hcl_sckaddr_t cli_addr; | ||||||
| 		int cli_fd; | 		int cli_fd; | ||||||
| 		socklen_t cli_len; | 		hcl_scklen_t cli_len; | ||||||
| 		pthread_t thr; | 		pthread_t thr; | ||||||
| 		hcl_ntime_t tmout; | 		hcl_ntime_t tmout; | ||||||
| 		hcl_server_worker_t* worker; | 		hcl_server_worker_t* worker; | ||||||
|  | |||||||
| @ -442,13 +442,18 @@ static int handle_logopt (hcl_client_t* client, const hcl_bch_t* str) | |||||||
|  |  | ||||||
| static int start_reply (hcl_client_t* client, hcl_client_reply_type_t type, const hcl_ooch_t* dptr, hcl_oow_t dlen) | static int start_reply (hcl_client_t* client, hcl_client_reply_type_t type, const hcl_ooch_t* dptr, hcl_oow_t dlen) | ||||||
| { | { | ||||||
|  | 	client_xtn_t* client_xtn; | ||||||
|  | 	client_xtn = hcl_client_getxtn(client); | ||||||
|  |  | ||||||
| 	if (dptr) | 	if (dptr) | ||||||
| 	{ | 	{ | ||||||
| 		printf ("GOT SHORT-FORM RESPONSE[%d] with data <<%.*ls>>\n", (int)type, (int)dlen, dptr); | 		/* short-form response - no end_reply will be called */ | ||||||
|  | 		client_xtn->reply_count++; | ||||||
|  | 		fflush (stdout); | ||||||
| 	} | 	} | ||||||
| 	else | 	else | ||||||
| 	{ | 	{ | ||||||
| 		printf ("GOT LONG_FORM RESPONSE[%d]\n", (int)type); | 		/* long-form response */ | ||||||
| 	} | 	} | ||||||
| 	return 0; | 	return 0; | ||||||
| } | } | ||||||
| @ -460,26 +465,24 @@ static int end_reply (hcl_client_t* client, hcl_client_end_reply_state_t state) | |||||||
|  |  | ||||||
| 	if (state == HCL_CLIENT_END_REPLY_STATE_REVOKED) | 	if (state == HCL_CLIENT_END_REPLY_STATE_REVOKED) | ||||||
| 	{ | 	{ | ||||||
| printf (">>>>>>>>>>>>>>>>>>>>>> REPLY revoked....\n"); | 		/* nothing to do here */ | ||||||
| 	} | 	} | ||||||
| 	else | 	else | ||||||
| 	{ | 	{ | ||||||
| 		client_xtn->reply_count++; | 		client_xtn->reply_count++; | ||||||
| printf (">>>>>>>>>>>>>>>>>>>>> REPLY ENDED OK....\n"); | 		fflush (stdout); | ||||||
| 	} | 	} | ||||||
| 	return 0; | 	return 0; | ||||||
| } | } | ||||||
|  |  | ||||||
| static int feed_attr (hcl_client_t* client, const hcl_oocs_t* key, const hcl_oocs_t* val) | static int feed_attr (hcl_client_t* client, const hcl_oocs_t* key, const hcl_oocs_t* val) | ||||||
| { | { | ||||||
| printf ("GOT HEADER ====> [%.*ls] ===> [%.*ls]\n", (int)key->len, key->ptr, (int)val->len, val->ptr); |  | ||||||
| 	return 0; | 	return 0; | ||||||
| } | } | ||||||
|  |  | ||||||
| static int feed_data (hcl_client_t* client, const void* ptr, hcl_oow_t len) | static int feed_data (hcl_client_t* client, const void* ptr, hcl_oow_t len) | ||||||
| { | { | ||||||
| //printf ("GOT DATA>>>>>>>>>[%.*s]>>>>>>>\n", (int)len, ptr); | 	printf ("%.*s", (int)len, ptr); | ||||||
| printf ("%.*s", (int)len, ptr); |  | ||||||
| 	return 0; | 	return 0; | ||||||
| } | } | ||||||
|  |  | ||||||
| @ -490,11 +493,11 @@ static int handle_request (hcl_client_t* client, const char* ipaddr, const char* | |||||||
| 	hcl_sckaddr_t sckaddr; | 	hcl_sckaddr_t sckaddr; | ||||||
| 	hcl_scklen_t scklen; | 	hcl_scklen_t scklen; | ||||||
| 	int sckfam; | 	int sckfam; | ||||||
| 	int sck; | 	int sck = -1; | ||||||
| 	struct iovec iov[10]; | 	struct iovec iov[3]; | ||||||
| 	int index, count; | 	int index, count; | ||||||
|  |  | ||||||
| 	hcl_oow_t xlen, offset; | 	hcl_oow_t used, avail; | ||||||
| 	int x; | 	int x; | ||||||
| 	hcl_bch_t buf[256]; | 	hcl_bch_t buf[256]; | ||||||
| 	ssize_t n; | 	ssize_t n; | ||||||
| @ -507,22 +510,20 @@ static int handle_request (hcl_client_t* client, const char* ipaddr, const char* | |||||||
| 	if (sckfam <= -1)  | 	if (sckfam <= -1)  | ||||||
| 	{ | 	{ | ||||||
| 		fprintf (stderr, "cannot convert ip address - %s\n", ipaddr); | 		fprintf (stderr, "cannot convert ip address - %s\n", ipaddr); | ||||||
| 		return -1; | 		goto oops; | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	sck = socket (sckfam, SOCK_STREAM, 0); | 	sck = socket (sckfam, SOCK_STREAM, 0); | ||||||
| 	if (sck <= -1)  | 	if (sck <= -1)  | ||||||
| 	{ | 	{ | ||||||
| 		fprintf (stderr, "cannot create a socket for %s\n", ipaddr); | 		fprintf (stderr, "cannot create a socket for %s\n", ipaddr); | ||||||
| 		return -1; | 		goto oops; | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  |  | ||||||
| 	if (connect(sck, (struct sockaddr*)&sckaddr, scklen) <= -1) | 	if (connect(sck, (struct sockaddr*)&sckaddr, scklen) <= -1) | ||||||
| 	{ | 	{ | ||||||
| 		fprintf (stderr, "cannot connect to %s\n", ipaddr); | 		fprintf (stderr, "cannot connect to %s\n", ipaddr); | ||||||
| 		close (sck); | 		goto oops; | ||||||
| 		return -1; |  | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	count = 0; | 	count = 0; | ||||||
| @ -565,10 +566,10 @@ static int handle_request (hcl_client_t* client, const char* ipaddr, const char* | |||||||
| 	client_xtn->reply_count = 0; | 	client_xtn->reply_count = 0; | ||||||
|  |  | ||||||
| /* TODO: implement timeout? */ | /* TODO: implement timeout? */ | ||||||
| 	offset = 0; | 	avail = 0; | ||||||
| 	while (client_xtn->reply_count == 0) | 	while (client_xtn->reply_count == 0) | ||||||
| 	{ | 	{ | ||||||
| 		n = read(sck, &buf[offset], HCL_SIZEOF(buf) - offset); /* switch to recv  */ | 		n = read(sck, &buf[avail], HCL_SIZEOF(buf) - avail); /* switch to recv  */ | ||||||
| 		if (n <= -1)  | 		if (n <= -1)  | ||||||
| 		{ | 		{ | ||||||
| 			fprintf (stderr, "Unable to read from %d - %s\n", sck, strerror(n)); | 			fprintf (stderr, "Unable to read from %d - %s\n", sck, strerror(n)); | ||||||
| @ -584,11 +585,12 @@ static int handle_request (hcl_client_t* client, const char* ipaddr, const char* | |||||||
| 			break; | 			break; | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		x = hcl_client_feed(client, buf, n, &xlen); | 		avail += n;; | ||||||
|  | 		x = hcl_client_feed(client, buf, avail, &used); | ||||||
| 		if (x <= -1) goto oops; | 		if (x <= -1) goto oops; | ||||||
|  |  | ||||||
| 		offset = n - xlen; | 		avail -= used; | ||||||
| 		if (offset > 0) memmove (&buf[0], &buf[xlen], offset); | 		if (avail > 0) memmove (&buf[0], &buf[used], avail); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| /* TODO: we can check if the buffer has all been consumed. if not, there is trailing garbage.. */ | /* TODO: we can check if the buffer has all been consumed. if not, there is trailing garbage.. */ | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user