TEMP_FAILURE_RETRY宏的應用(很好用)

 
The GNU library provides a convenient way to retry a call after a temporary failure, with the macro TEMP_FAILURE_RETRY: — Macro: TEMP_FAILURE_RETRY (expression)
This macro evaluates expression once, and examines its value as type long int. If the value equals -1, that indicates a failure and errno should be set to show what kind of failure. If it fails and reports error code EINTR, TEMP_FAILURE_RETRY evaluates it again, and over and over until the result is not a temporary failure. The value returned by TEMP_FAILURE_RETRY is whatever value expression produced.

舉個例子(etc.)TEMP_FAILURE_RETRY(::accept(sock, (struct sockaddr*)addr, &len));
它的功能: 不斷地從套接口中接收客戶端的地址知道成功爲止返回客戶端的可用套接口.

順便寫一個利用epoll技術來接收數據的routine
int accept(struct sockaddr_in *addr)
{
socklen_t len = sizeof(struct sockaddr_in);
bzero(addr, sizeof(struct sockaddr_in));
struct epoll_event ev;
int rc = epoll_wait(kdpfd, &ev, 1, 2000);//這裏kdpfd is the library function epoll_create(
           int size)的reture value.Here is called by epoll_create(1).You can man it.
if(1 == rc && (ev.events & EPOLLIN))
   return TEMP_FAILURE_RETRY(::accept(sock, (struct sockaddr*)addr, &len));
return -1;
}
發佈了25 篇原創文章 · 獲贊 0 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章