前言:行動是治癒拖延的良藥,現代人所謂的拖延症都是對自己懶惰的藉口。
一直說要看看libevent源碼,卻又一直拖着忙着,不知道在忙些什麼。我說在公司看着那一堆亂七八糟的源碼都看下去了,爲什麼不能花點時間來研讀下別人的優秀的源碼呢?同學說那是因爲在公司那是被動的,而這個是主動的,想想也挺有道理。。
這篇文章是看libevent的第一篇記錄,libevent是基於信號來分發消息,對於信號的描述結構
typedef void (*ev_sighandler_t)(int);
/* Data structure for the default signal-handling implementation in signal.c
*/
struct evsig_info {
/* Event watching ev_signal_pair[1] */
struct event ev_signal;
/* Socketpair used to send notifications from the signal handler */
evutil_socket_t ev_signal_pair[2];
/* True iff we've added the ev_signal event yet. */
int ev_signal_added;
/* Count of the number of signals we're currently watching. */
int ev_n_signals_added;
/* Array of previous signal handler objects before Libevent started
* messing with them. Used to restore old signal handlers. */
#ifdef _EVENT_HAVE_SIGACTION
struct sigaction **sh_old;
#else
ev_sighandler_t **sh_old;
#endif
/* Size of sh_old. */
int sh_old_max;
};
其中裏面有個成員變量 ev_signal_pair[2]; 在初始化時將它設爲了一組連接着的socket。在linux下使用的是socketpair函數,
int socketpair(int d, int type, int protocol, int sv[2]);
而windows下沒有這個函數,是手動實現的。這個socketpair的用途爲
1. 這對套接字可以用於全雙工通信,每一個套接字既可以讀也可以寫。例如,可以往sv[0]中寫,從sv[1]中讀;或者從sv[1]中寫,從sv[0]中讀;
2. 如果往一個套接字(如sv[0])中寫入後,再從該套接字讀時會阻塞,只能在另一個套接字中(sv[1])上讀成功;
3. 讀、寫操作可以位於同一個進程,也可以分別位於不同的進程,如父子進程。如果是父子進程時,一般會功能分離,一個進程用來讀,一個用來寫。因爲文件描述副sv[0]和sv[1]是進程共享的,所以讀的進程要關閉寫描述符, 反之,寫的進程關閉讀描述符。
藉此博客的一張圖https://www.cnblogs.com/big-xuyue/p/4098578.html
實現原理我還沒有看到,此文主要說windows下socketpair的實現。
#include<WinSock2.h>
#include<Windows.h>
#pragma comment(lib,"ws2_32.lib")
#include<iostream>
using namespace std;
void EVUTIL_SET_SOCKET_ERROR(int code)
{
cout<<"error code"<<code<<endl;
}
int evutil_ersatz_socketpair(int family, int type, int protocol, SOCKET fd[2])
{
/* This code is originally from Tor. Used with permission. */
/* This socketpair does not work when localhost is down. So
* it's really not the same thing at all. But it's close enough
* for now, and really, when localhost is down sometimes, we
* have other problems too.
*/
#ifdef WIN32
#define ERR(e) WSA##e
#else
#define ERR(e) e
#endif
SOCKET listener = -1;
SOCKET connector = -1;
SOCKET acceptor = -1;
struct sockaddr_in listen_addr;
struct sockaddr_in connect_addr;
int size;
int saved_errno = -1;
if (protocol
|| (family != AF_INET
#ifdef AF_UNIX
&& family != AF_UNIX
#endif
)) {
EVUTIL_SET_SOCKET_ERROR(ERR(EAFNOSUPPORT));
return -1;
}
if (!fd) {
EVUTIL_SET_SOCKET_ERROR(ERR(EINVAL));
return -1;
}
listener = socket(AF_INET, type, 0);
if (listener < 0)
return -1;
memset(&listen_addr, 0, sizeof(listen_addr));
listen_addr.sin_family = AF_INET;
listen_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
listen_addr.sin_port = 0; /* kernel chooses port. */
if (bind(listener, (struct sockaddr *) &listen_addr, sizeof(listen_addr))
== -1)
goto tidy_up_and_fail;
if (listen(listener, 1) == -1)
goto tidy_up_and_fail;
connector = socket(AF_INET, type, 0);
if (connector < 0)
goto tidy_up_and_fail;
/* We want to find out the port number to connect to. */
size = sizeof(connect_addr);
//獲取監聽地址
if (getsockname(listener, (struct sockaddr *) &connect_addr, &size) == -1)
goto tidy_up_and_fail;
if (size != sizeof(connect_addr))
goto abort_tidy_up_and_fail;
//connector連接到listener上
if (connect(connector, (struct sockaddr *) &connect_addr,
sizeof(connect_addr)) == -1)
goto tidy_up_and_fail;
size = sizeof(listen_addr);
//listener接受連接,此時connector與acceptor是一對已連接的socket pair
acceptor = accept(listener, (struct sockaddr *) &listen_addr, &size);
if (acceptor < 0)
goto tidy_up_and_fail;
if (size != sizeof(listen_addr))
goto abort_tidy_up_and_fail;
/* 判斷兩個socket之間的地址,端口,協議是否相同 */
if (getsockname(connector, (struct sockaddr *) &connect_addr, &size) == -1)
goto tidy_up_and_fail;
if (size != sizeof(connect_addr)
|| listen_addr.sin_family != connect_addr.sin_family
|| listen_addr.sin_addr.s_addr != connect_addr.sin_addr.s_addr
|| listen_addr.sin_port != connect_addr.sin_port)
goto abort_tidy_up_and_fail;
closesocket(listener);//不需要再監聽了
fd[0] = connector;
fd[1] = acceptor;
/**
* 此時就已經創建了兩個連接起來的socket,即可以實現進程間的通信
*/
return 0;
abort_tidy_up_and_fail:
saved_errno = ERR(ECONNABORTED);
tidy_up_and_fail:
if (saved_errno < 0)
;//saved_errno = EVUTIL_SOCKET_ERROR();
if (listener != -1)
closesocket(listener);
if (connector != -1)
closesocket(connector);
if (acceptor != -1)
closesocket(acceptor);
EVUTIL_SET_SOCKET_ERROR( saved_errno);
return -1;
#undef ERR
}
int main() {
WSADATA wsa;
WSAStartup(MAKEWORD(2, 2), &wsa);
SOCKET fd[2];
if (evutil_ersatz_socketpair(AF_INET, SOCK_STREAM, 0, fd))
goto end;
char buf[1024] = { 0 };
send(fd[0], "hello", 6, 0);
int len = recv(fd[1], buf, 1024, 0);
if (len)
{
buf[len] = '\0';
cout << "收到" << buf << endl;
strcat(buf, " word");
int r=send(fd[0], buf, strlen(buf), 0);
}
char buf2[1024] = { 0 };
len = recv(fd[1], buf2, 1024, 0);
if (len)
{
buf2[len] = '\0';
cout << "2收到" << buf2 << endl;
}
end:
system("pause");
return 1;
}
代碼中首先定義了一個listener來監聽連接,然後有一個connecter去主動連接,然後調用accept後返回一個accepter,如果沒有發生異常問題,那此時connecter與accepter是一組可以相互通信的socket。最後通過參數fd返回函數調用處實參。