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