Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: socket close properly #609

Merged
merged 3 commits into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions inc/bench.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
#include <sys/ioctl.h>
#include <signal.h>

#define SOCKET_ERROR -1

#elif DARWIN
#include <argp.h>
#include <unistd.h>
Expand All @@ -49,9 +51,22 @@
#include <arpa/inet.h>
#include <sys/time.h>
#include <netdb.h>
#else

#define SOCKET_ERROR -1

#else // WINDOWS
#define _CRT_RAND_S
#include <windows.h>
#include <winsock2.h>
#endif
#pragma comment(lib, "ws2_32.lib")
#define SHUT_WR SD_SEND

typedef unsigned __int32 uint32_t;
// Some old MinGW/CYGWIN distributions don't define this:
#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
#endif // ENABLE_VIRTUAL_TERMINAL_PROCESSING
#endif // LINUX

#include <limits.h>
#include <regex.h>
Expand Down
49 changes: 36 additions & 13 deletions src/benchUtil.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*/

#include <bench.h>

char resEncodingChunk[] = "Encoding: chunked";
Expand Down Expand Up @@ -51,17 +50,6 @@ void ERROR_EXIT(const char *msg) {
}

#ifdef WINDOWS
#define _CRT_RAND_S
#include <windows.h>
#include <winsock2.h>

typedef unsigned __int32 uint32_t;

#pragma comment(lib, "ws2_32.lib")
// Some old MinGW/CYGWIN distributions don't define this:
#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
#endif // ENABLE_VIRTUAL_TERMINAL_PROCESSING

HANDLE g_stdoutHandle;
DWORD g_consoleMode;
Expand Down Expand Up @@ -1160,7 +1148,16 @@ int createSockFd() {
return sockfd;
}

void destroySockFd(int sockfd) {
static void errorPrintSocketMsg(char *msg, int result) {
errorPrint("Socket shutdown failed with error: %d\n",
#ifdef WINDOWS
WSAGetLastError());
#else
result);
#endif
}

static void closeSockFd(int sockfd) {
#ifdef WINDOWS
closesocket(sockfd);
WSACleanup();
Expand All @@ -1169,6 +1166,32 @@ void destroySockFd(int sockfd) {
#endif
}

void destroySockFd(int sockfd) {
// shutdown the connection since no more data will be sent
int result;
result = shutdown(sockfd, SHUT_WR);
if (SOCKET_ERROR == result) {
errorPrintSocketMsg("Socket shutdown failed with error: %d\n", result);
closeSockFd(sockfd);
return;
}
// Receive until the peer closes the connection
do {
int recvbuflen = LARGE_BUFF_LEN;
char recvbuf[LARGE_BUFF_LEN];
result = recv(sockfd, recvbuf, recvbuflen, 0);
if ( result > 0 ) {
debugPrint("Socket bytes received: %d\n", result);
} else if (result == 0) {
infoPrint("Connection closed with result %d\n", result);
} else {
errorPrintSocketMsg("Socket recv failed with error: %d\n", result);
}
} while (result > 0);

closeSockFd(sockfd);
}

FORCE_INLINE void printErrCmdCodeStr(char *cmd, int32_t code, TAOS_RES *res) {
errorPrint("failed to run command %s, code: 0x%08x, reason: %s\n",
cmd, code, taos_errstr(res));
Expand Down