dev 优化ssocket.h
This commit is contained in:
parent
c911153cca
commit
d42fcd471d
@ -42,6 +42,10 @@
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#define _SOCKET_TEST_IP4 "127.0.0.1"
|
||||
#define _SOCKET_TEST_IP6 "::1"
|
||||
#define _SOCKET_TEST_PORT 26789
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
@ -82,8 +86,11 @@ 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);
|
||||
|
||||
#define _SOCKET_TEST_IP "127.0.0.1"
|
||||
#define _SOCKET_TEST_PORT 26789
|
||||
static inline int make_server_sock_ex(SOCKET* sock, const char* server_ip, unsigned short port,
|
||||
struct addrinfo** resaddr);
|
||||
static inline int make_client_sock_ex(SOCKET* sock, const char* connect_ip, unsigned short port,
|
||||
struct addrinfo** resaddr);
|
||||
|
||||
static inline int _getaddrinfo(struct addrinfo* inf, struct addrinfo** resaddr,
|
||||
const char* ip, const char* port);
|
||||
static inline int _socket(SOCKET* sock, struct addrinfo* resaddr);
|
||||
@ -135,6 +142,7 @@ static inline int _sock_eai_res_map(int res) {
|
||||
// need to using freeaddrinfo to free <resaddr>
|
||||
static inline int _getaddrinfo(struct addrinfo* inf, struct addrinfo** resaddr,
|
||||
const char* ip, const char* port) {
|
||||
if (resaddr == NULL) { return ERR_SOCK_NULLPTR; }
|
||||
int res = getaddrinfo(ip, port, inf, resaddr);
|
||||
if (res != 0) {
|
||||
return -(_sock_eai_res_map(res));
|
||||
@ -143,17 +151,6 @@ static inline int _getaddrinfo(struct addrinfo* inf, struct addrinfo** resaddr,
|
||||
}
|
||||
|
||||
static inline int _socket(SOCKET* sock, struct addrinfo* resaddr) {
|
||||
#ifdef WIN_PART
|
||||
WSADATA wsaData;
|
||||
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
|
||||
return -ERR_SOCK_WSAStartup;
|
||||
}
|
||||
|
||||
if (HIBYTE(wsaData.wVersion) != 2 || \
|
||||
LOBYTE(wsaData.wVersion) != 2) {
|
||||
return -ERR_SOCK_DETERMINE_AGREEMENT;
|
||||
}
|
||||
#endif
|
||||
for (struct addrinfo* p = resaddr; p != NULL; p = p->ai_next) {
|
||||
if (*sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) {
|
||||
if (*sock == INVALID_SOCKET) {
|
||||
@ -240,20 +237,13 @@ RES:
|
||||
|
||||
static inline int sock_accpet(SOCKET sock,SOCKET* client, char** accept_ip, unsigned short* port) {
|
||||
int res = 0;
|
||||
struct sockaddr_storage inf;
|
||||
//struct addrinfo inf;
|
||||
//res = _accept(sock, client, &inf);
|
||||
//if (res != ERR_SOCK_SUCCESS) return res;
|
||||
//if (accept_ip == NULL) return ERR_SOCK_SUCCESS;
|
||||
socklen_t addrLen = sizeof(struct sockaddr_storage);
|
||||
|
||||
struct addrinfo inf;
|
||||
if (client == NULL) {
|
||||
return -ERR_SOCK_NULLPTR;
|
||||
}
|
||||
*client = accept(sock, (struct sockaddr*)&inf, &addrLen);
|
||||
if (*client == INVALID_SOCKET) {
|
||||
return -ERR_SOCK_ACCEPT;
|
||||
}
|
||||
res = _accept(sock, client, &inf);
|
||||
if (res != ERR_SOCK_SUCCESS) return res;
|
||||
if (accept_ip == NULL) return ERR_SOCK_SUCCESS;
|
||||
|
||||
char* hostname = (char*)malloc(32);
|
||||
char* servInfo = (char*)malloc(8);
|
||||
@ -279,36 +269,21 @@ 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) {
|
||||
#ifdef WIN_PART
|
||||
WSADATA wsaData;
|
||||
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
|
||||
return -ERR_SOCK_WSAStartup;
|
||||
}
|
||||
|
||||
if (HIBYTE(wsaData.wVersion) != 2 || \
|
||||
LOBYTE(wsaData.wVersion) != 2) {
|
||||
return -ERR_SOCK_DETERMINE_AGREEMENT;
|
||||
}
|
||||
#endif
|
||||
int res = ERR_SOCK_SUCCESS;
|
||||
char buf[8] = { 0 };
|
||||
sprintf(buf, "%u", port);
|
||||
struct addrinfo inf = { 0 };
|
||||
struct addrinfo *resaddr = NULL;
|
||||
res = _getaddrinfo(&inf, &resaddr, server_ip, buf);
|
||||
if (res != ERR_SOCK_SUCCESS) goto RES;
|
||||
|
||||
res = _socket(sock, resaddr);
|
||||
if (res != ERR_SOCK_SUCCESS) goto RES;
|
||||
|
||||
res = _bindlisten(*sock, resaddr);
|
||||
if (res != ERR_SOCK_SUCCESS) goto RES;
|
||||
RES:
|
||||
struct addrinfo* resaddr = NULL;
|
||||
int res = make_server_sock_ex(sock, server_ip, port, &resaddr);
|
||||
if (resaddr) freeaddrinfo(resaddr);
|
||||
return res;
|
||||
}
|
||||
|
||||
static inline int make_client_sock(SOCKET* sock, const char* connect_ip, unsigned short port) {
|
||||
struct addrinfo* resaddr = NULL;
|
||||
int res = make_client_sock_ex(sock, connect_ip, port, &resaddr);
|
||||
if (resaddr) freeaddrinfo(resaddr);
|
||||
return res;
|
||||
}
|
||||
|
||||
static inline int make_server_sock_ex(SOCKET* sock, const char* server_ip, unsigned short port,
|
||||
struct addrinfo** resaddr) {
|
||||
#ifdef WIN_PART
|
||||
WSADATA wsaData;
|
||||
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
|
||||
@ -324,22 +299,44 @@ static inline int make_client_sock(SOCKET* sock, const char* connect_ip, unsigne
|
||||
char buf[8] = { 0 };
|
||||
sprintf(buf, "%u", port);
|
||||
struct addrinfo inf = { 0 };
|
||||
struct addrinfo* resaddr = NULL;
|
||||
res = _getaddrinfo(&inf, &resaddr, connect_ip, buf);
|
||||
if (res != ERR_SOCK_SUCCESS) goto RES;
|
||||
res = _getaddrinfo(&inf, resaddr, server_ip, buf);
|
||||
if (res != ERR_SOCK_SUCCESS) return res;
|
||||
|
||||
res = _socket(sock, resaddr);
|
||||
if (res != ERR_SOCK_SUCCESS) goto RES;
|
||||
res = _socket(sock, *resaddr);
|
||||
if (res != ERR_SOCK_SUCCESS) return res;
|
||||
|
||||
if(connect_ip) {
|
||||
res = _connect(*sock, resaddr);
|
||||
if (res != ERR_SOCK_SUCCESS) goto RES;
|
||||
}
|
||||
RES:
|
||||
if (resaddr) freeaddrinfo(resaddr);
|
||||
return res;
|
||||
res = _bindlisten(*sock, *resaddr);
|
||||
if (res != ERR_SOCK_SUCCESS) return res;
|
||||
}
|
||||
|
||||
static inline int make_client_sock_ex(SOCKET* sock, const char* connect_ip, unsigned short port,
|
||||
struct addrinfo** resaddr) {
|
||||
#ifdef WIN_PART
|
||||
WSADATA wsaData;
|
||||
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
|
||||
return -ERR_SOCK_WSAStartup;
|
||||
}
|
||||
|
||||
if (HIBYTE(wsaData.wVersion) != 2 || \
|
||||
LOBYTE(wsaData.wVersion) != 2) {
|
||||
return -ERR_SOCK_DETERMINE_AGREEMENT;
|
||||
}
|
||||
#endif
|
||||
int res = ERR_SOCK_SUCCESS;
|
||||
char buf[8] = { 0 };
|
||||
sprintf(buf, "%u", port);
|
||||
struct addrinfo inf = { 0 };
|
||||
res = _getaddrinfo(&inf, resaddr, connect_ip, buf);
|
||||
if (res != ERR_SOCK_SUCCESS) return res;
|
||||
|
||||
res = _socket(sock, *resaddr);
|
||||
if (res != ERR_SOCK_SUCCESS) return res;
|
||||
|
||||
if (connect_ip) {
|
||||
res = _connect(*sock, *resaddr);
|
||||
if (res != ERR_SOCK_SUCCESS) return res;
|
||||
}
|
||||
}
|
||||
static inline void close_sock(SOCKET sock) {
|
||||
if(sock == -1) return;
|
||||
#if WIN_PART
|
||||
|
@ -85,7 +85,7 @@ void acceptfunc(void* param) {
|
||||
int main()
|
||||
{
|
||||
char Buf[128];
|
||||
int res = make_server_sock(&sfd, "::1", _SOCKET_TEST_PORT);
|
||||
int res = make_server_sock(&sfd, _SOCKET_TEST_IP6, _SOCKET_TEST_PORT);
|
||||
if(res != 0) {
|
||||
printf("server start error\npress enter to continue");
|
||||
getchar();
|
||||
|
Loading…
x
Reference in New Issue
Block a user