From 5e506c69d2fdb8eb5bb08af9a0ef670665dd8419 Mon Sep 17 00:00:00 2001 From: zzy-linux <2450266535@qq.com> Date: Wed, 25 Oct 2023 23:11:08 +0800 Subject: [PATCH] =?UTF-8?q?dev=20=E5=87=86=E5=A4=87v1.0.4,=E5=8F=91?= =?UTF-8?q?=E7=8E=B0tthread.h=E9=87=8D=E5=A4=8D=E5=8C=85=E5=90=ABbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/ssocket.h | 83 +++++++++++++++++++++++--------------------- include/tthread.h | 19 ++++------ test/client/client.c | 4 ++- test/server/server.c | 7 ++-- version.txt | 10 +++--- 5 files changed, 65 insertions(+), 58 deletions(-) diff --git a/include/ssocket.h b/include/ssocket.h index ab5d0ce..9b44d00 100644 --- a/include/ssocket.h +++ b/include/ssocket.h @@ -2,11 +2,12 @@ #define _SSOCKET_H_ #include +#include #include -#define SSOCKET_VERSION_MAJOR 1 -#define SSOCKET_VERSION_MINOR 0 -#define SSOCKET_VERSION_PATCH 2 +// #define SSOCKET_VERSION_MAJOR 1 +// #define SSOCKET_VERSION_MINOR 0 +// #define SSOCKET_VERSION_PATCH #include "sysenv.h" #if _OS_WIN @@ -18,9 +19,9 @@ #endif #if WIN_PART -#include -#include -#include +#include +#include +#include #define strcasecmp _stricmp #define strncasecmp _strnicmp #define socklen_t int @@ -29,14 +30,11 @@ #include #include #include -#include #include #define SOCKET long long int #define INVALID_SOCKET (SOCKET)(~0) #endif -#include "tthread.h" - #ifndef ZZY_SLEEP #define ZZY_SLEEP #if WIN_PART @@ -87,7 +85,6 @@ static inline int make_client_sock(SOCKET* sock, const char* connect_ip, unsigne static inline void close_sock(SOCKET sock); static inline void out_sock_err(FILE* output, int errcode); static inline void get_sock_err(char* buff_128, size_t buff_len, int errcode); -static inline void sock_thread(void(*thread_func)(void*), void* data); #define _SOCKET_TEST_IP "127.0.0.1" #define _SOCKET_TEST_PORT 6789 @@ -101,23 +98,24 @@ static inline int make_sock_tcp4(SOCKET* sock) { return _sock(sock, AF_INET, SOC static inline int make_sock_tcp6(SOCKET* sock) { return _sock(sock, AF_INET6, SOCK_STREAM); } static inline int make_sock_udp4(SOCKET* sock) { return _sock(sock, AF_INET, SOCK_DGRAM); } static inline int make_sock_udp6(SOCKET* sock) { return _sock(sock, AF_INET6, SOCK_DGRAM); } +//不推荐 static inline int make_sock(SOCKET* sock) { return make_sock_tcp4(sock); }; static inline int _sock(SOCKET* sock, int af, int type) { #ifdef WIN_PART WSADATA wsaData; if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { - return ERR_SOCK_WSAStartup; + return -ERR_SOCK_WSAStartup; } if (HIBYTE(wsaData.wVersion) != 2 || \ LOBYTE(wsaData.wVersion) != 2) { - return ERR_SOCK_DETERMINE_AGREEMENT; + return -ERR_SOCK_DETERMINE_AGREEMENT; } #endif *sock = socket(af, type, 0); if (*sock == INVALID_SOCKET) { - return ERR_SOCK_CREATE_SOCKET; + return -ERR_SOCK_CREATE_SOCKET; } return ERR_SOCK_SUCCESS; } @@ -141,19 +139,19 @@ static inline int _socket(SOCKET* sock, const char* ip, const char* port) { struct addrinfo* res_addr = NULL; int res = getaddrinfo(ip, port, &inf, &res_addr); if (res != 0) { - return _sock_eai_res_map(res); + return -(_sock_eai_res_map(res)); } for (struct addrinfo* p = res_addr; p != NULL; p = p->ai_next) { if (*sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) { if (*sock == INVALID_SOCKET) { - return ERR_SOCK_CREATE_SOCKET; + return -ERR_SOCK_CREATE_SOCKET; } freeaddrinfo(res_addr); - return ERR_SOCK_SUCCESS; + return -ERR_SOCK_SUCCESS; } } freeaddrinfo(res_addr); - return ERR_SOCK_CONNECT; + return -ERR_SOCK_CONNECT; } static inline int _connect(SOCKET sock, const char* ip, const char* port) { @@ -170,7 +168,7 @@ static inline int _connect(SOCKET sock, const char* ip, const char* port) { } } freeaddrinfo(res_addr); - return ERR_SOCK_CONNECT; + return -ERR_SOCK_CONNECT; } static inline int _bind(SOCKET sock, const char* ip, const char* port) { @@ -178,7 +176,7 @@ static inline int _bind(SOCKET sock, const char* ip, const char* port) { struct addrinfo* res_addr = NULL; int res = getaddrinfo(ip, port, &inf, &res_addr); if (res != 0) { - return ERR_SOCK_ADDRINFO; + return -ERR_SOCK_ADDRINFO; } for (struct addrinfo* p = res_addr; p != NULL; p = p->ai_next) { if (bind(sock, p->ai_addr, p->ai_addrlen) == 0) { @@ -187,7 +185,7 @@ static inline int _bind(SOCKET sock, const char* ip, const char* port) { } } freeaddrinfo(res_addr); - return ERR_SOCK_CONNECT; + return -ERR_SOCK_CONNECT; } static inline int _bindlisten(SOCKET sock, const char* ip, const char* port) { @@ -196,7 +194,7 @@ static inline int _bindlisten(SOCKET sock, const char* ip, const char* port) { if (listen(sock, SOMAXCONN) == 0) { return ERR_SOCK_SUCCESS; } - return ERR_SOCK_LISTEN; + return -ERR_SOCK_LISTEN; } return res; } @@ -218,16 +216,16 @@ static inline int sock_accpet(SOCKET sock,SOCKET* client, char** accept_ip, unsi socklen_t addrLen = sizeof(struct sockaddr_in); if(client == NULL) { - return ERR_SOCK_NULLPTR; + return -ERR_SOCK_NULLPTR; } *client = accept(sock,(struct sockaddr*) &caddr, &addrLen); if (*client == -1) { - return ERR_SOCK_ACCEPT; + return -ERR_SOCK_ACCEPT; } char* ip = (char*)malloc(32); if(ip == NULL) { - return ERR_SOCK_MALLOC; + return -ERR_SOCK_MALLOC; } int buffLen = strlen( inet_ntoa(caddr.sin_addr) ); @@ -244,7 +242,10 @@ static inline int sock_accpet(SOCKET sock,SOCKET* client, char** accept_ip, unsi } static inline int make_server_sock(SOCKET* sock, const char* server_ip, unsigned short port) { - int res = make_sock(sock); + char buf[8] = { 0 }; + sprintf(buf, "%u", port); + + int res = _socket(sock, server_ip, buf); if(res != ERR_SOCK_SUCCESS) { return res; } @@ -258,7 +259,10 @@ static inline int make_server_sock(SOCKET* sock, const char* server_ip, unsigned } static inline int make_client_sock(SOCKET* sock, const char* connect_ip, unsigned short port) { - int res = make_sock(sock); + char buf[8] = { 0 }; + sprintf(buf, "%u", port); + + int res = _socket(sock, connect_ip, buf); if(res != ERR_SOCK_SUCCESS) { return res; } @@ -274,9 +278,7 @@ static inline int make_client_sock(SOCKET* sock, const char* connect_ip, unsigne } static inline void close_sock(SOCKET sock) { - if(sock == -1) { - return; - } + if(sock == -1) return; #if WIN_PART closesocket(sock); #elif LINUX_PART @@ -296,14 +298,26 @@ static inline void get_sock_err(char* buff_128, size_t buff_len, int errcode) { // ERR_SOCK_SUCCESS, // ERR_SOCK_MALLOC, // ERR_SOCK_NULLPTR, + // ERR_SOCK_WSAStartup, // ERR_SOCK_DETERMINE_AGREEMENT, // ERR_SOCK_CREATE_SOCKET, // ERR_SOCK_BIND, // ERR_SOCK_LISTEN, // ERR_SOCK_ACCEPT, - // ERR_SOCK_CONNECT, + + // ERR_SOCK_ADDRINFO, + // //getaddrinfo + // ERR_SOCK_EAI_MAPPING_NOT_EXIST, + // ERR_SOCK_EAI_AGAIN, + // ERR_SOCK_EAI_BADFLAGS, + // ERR_SOCK_EAI_FAIL, + // ERR_SOCK_EAI_FAMILY, + // ERR_SOCK_EAI_MEMORY, + // ERR_SOCK_EAI_NONAME, + // ERR_SOCK_EAI_SERVICE, + // ERR_SOCK_EAI_SOCKTYPE, // }; // https://learn.microsoft.com/zh-cn/windows/win32/api/ws2tcpip/nf-ws2tcpip-getaddrinfo?redirectedfrom=MSDN // https://learn.microsoft.com/en-gb/windows/win32/api/ws2tcpip/nf-ws2tcpip-getaddrinfo?redirectedfrom=MSDN @@ -320,15 +334,6 @@ static inline void get_sock_err(char* buff_128, size_t buff_len, int errcode) { sprintf(buff_128, "%3d", errcode); } -static inline void sock_thread(void(*thread_func)(void*), void* data) { -#if WIN_PART - CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)thread_func, data, 0, NULL); -#elif LINUX_PART - pthread_t pid; - pthread_create(&pid, 0, (void*(*)(void*))thread_func, data); -#endif -} - #ifdef __cplusplus } #endif diff --git a/include/tthread.h b/include/tthread.h index bd91755..2b2fb2d 100644 --- a/include/tthread.h +++ b/include/tthread.h @@ -2,15 +2,11 @@ #define _TTHREAD_H_ #include "sysenv.h" - #include #include + #if _OS_WIN #include -//#include -//#include -//#include - typedef DWORD TID; typedef CRITICAL_SECTION MUTEX; typedef CONDITION_VARIABLE COND; @@ -36,9 +32,9 @@ typedef pthread_cond_t COND; #endif #endif -#ifdef __cplusplus -extern "C" { -#endif +// #ifdef __cplusplus +// extern "C" { +// #endif enum { ERR_THREAD_SUCCESS, @@ -131,7 +127,6 @@ static inline TID thread_self(void) { #elif _OS_LINUX return pthread_self(); #endif - } // mutex part @@ -296,8 +291,8 @@ static inline int tvsnprintf(MUTEX* mutex, char* const buffer, return res; } -#ifdef __cplusplus -} -#endif +// #ifdef __cplusplus +// } +// #endif #endif // _TTHREAD_H_ \ No newline at end of file diff --git a/test/client/client.c b/test/client/client.c index 2314db4..1652ffe 100644 --- a/test/client/client.c +++ b/test/client/client.c @@ -2,6 +2,8 @@ #include #include +// #include +#include SOCKET cfd; void receive_message(void* param) @@ -37,7 +39,7 @@ int main(int argc, char** argv) } printf("conncet server success\n"); - sock_thread(receive_message, NULL); + thread_create(NULL, receive_message, NULL); while (1) { fgets(Buf, sizeof(Buf), stdin); diff --git a/test/server/server.c b/test/server/server.c index 6ed3c80..3544318 100644 --- a/test/server/server.c +++ b/test/server/server.c @@ -3,6 +3,9 @@ #include #include +#define _THREAD_H_ +#include + #define CLIENT_SIZE 32 SOCKET sfd; SOCKET cfds[CLIENT_SIZE]; @@ -69,7 +72,7 @@ void acceptfunc(void* param) { cfds[i] = sock; printf("\n", ip, port, sock); free(ip); - sock_thread(receive_message, (void*)psock); + thread_create(NULL, receive_message, (void*)psock); goto CONTINUE; } } @@ -91,7 +94,7 @@ int main() return 0; } printf("server start...\n"); - sock_thread(acceptfunc, NULL); + thread_create(NULL, acceptfunc, NULL); while(1) { fgets(Buf, sizeof(Buf), stdin); printf("%s", Buf); diff --git a/version.txt b/version.txt index 4227e71..fb23de9 100644 --- a/version.txt +++ b/version.txt @@ -1,10 +1,12 @@ -v1.0.1 ssocket +v1.0.1 dev 提供基础的windows与linux的跨平台tcp-ipv4协议 -v1.0.2 ssocket +v1.0.2 dev 使用getaddrinfo重写部分内容,为未来兼容tcp/udp-ipv4/ipv6做准备 添加make_sock,make_sock_tcp4,make_sock_tcp6,make_sock_udp4,make_sock_udp6函数 添加若干内置函数(以_func形式) 兼容老版本 -v1.0.3 ssocket +v1.0.3 dev 使用多头文件分离的方法将系统环境,线程,套接字分成三个文档 - 暂时兼容老版本,将在下个版本取消对前版本的兼容性 \ No newline at end of file + 暂时兼容老版本,将在下个版本取消对前版本的兼容性 +v1.0.4 alpha + 通过了Windows和Linux的基础测试(不完全), \ No newline at end of file