Skip to content

Commit 899eb0d

Browse files
Copilotithewei
andcommitted
Use CHECK_TIMEOUT after connect and add hsocket.c+htime.c to nslookup target
- Replace inline timeout check with CHECK_TIMEOUT macro by moving variable declarations before the connect: label to avoid crossing initializations with goto. - Add hsocket.c and htime.c to nslookup CMake target for Windows link compatibility (dns.c calls WSAInit from hsocket.c). Co-authored-by: ithewei <26049660+ithewei@users.noreply.github.com>
1 parent 1ac34d6 commit 899eb0d

2 files changed

Lines changed: 10 additions & 14 deletions

File tree

http/client/HttpClient.cpp

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,12 @@ static int http_client_exec(http_client_t* cli, HttpRequest* req, HttpResponse*
330330
}
331331
}
332332

333+
char recvbuf[1024] = {0};
334+
char* data = NULL;
335+
size_t len = 0;
336+
int total_nsend, nsend, nrecv;
337+
total_nsend = nsend = nrecv = 0;
338+
333339
if (connfd <= 0 || cli->host != req->host || cli->port != req->port) {
334340
cli->host = req->host;
335341
cli->port = req->port;
@@ -338,23 +344,13 @@ static int http_client_exec(http_client_t* cli, HttpRequest* req, HttpResponse*
338344
if (connfd < 0) {
339345
return connfd;
340346
}
341-
if (timeout_ms > 0) {
342-
cur_time = gettick_ms();
343-
if (cur_time - start_time >= timeout_ms) {
344-
cli->Close();
345-
return ERR_TASK_TIMEOUT;
346-
}
347-
left_time = timeout_ms - (cur_time - start_time);
348-
}
347+
CHECK_TIMEOUT
349348
}
350349

351350
cli->parser->SubmitRequest(req);
352-
char recvbuf[1024] = {0};
353-
int total_nsend, nsend, nrecv;
354-
total_nsend = nsend = nrecv = 0;
355351
send:
356-
char* data = NULL;
357-
size_t len = 0;
352+
data = NULL;
353+
len = 0;
358354
while (cli->parser->GetSendData(&data, &len)) {
359355
total_nsend = 0;
360356
while (total_nsend < len) {

unittest/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ target_include_directories(objectpool_test PRIVATE .. ../base ../cpputil)
7373
target_link_libraries(objectpool_test -lpthread)
7474

7575
# ------protocol------
76-
add_executable(nslookup nslookup_test.c ../protocol/dns.c)
76+
add_executable(nslookup nslookup_test.c ../protocol/dns.c ../base/hsocket.c ../base/htime.c)
7777
target_include_directories(nslookup PRIVATE .. ../base ../protocol)
7878

7979
add_executable(ping ping_test.c ../protocol/icmp.c ../base/hsocket.c ../base/htime.c)

0 commit comments

Comments
 (0)