本文整理汇总了C++中TCPSocket::set_noblock方法的典型用法代码示例。如果您正苦于以下问题:C++ TCPSocket::set_noblock方法的具体用法?C++ TCPSocket::set_noblock怎么用?C++ TCPSocket::set_noblock使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TCPSocket
的用法示例。
在下文中一共展示了TCPSocket::set_noblock方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: run
void EpollReactor::run()
{
epoll_event ev, events[10000];
int _epf = epoll_create(10000);
int sock = _server->sock();
ev.data.fd = sock;
ev.events = EPOLLIN;
epoll_ctl(_epf, EPOLL_CTL_ADD, sock, &ev);
if (_buffer_size < 1024 || _buffer_size > 1024 * 1024 * 100) _buffer_size = 1024;
char* buffer = new char[_buffer_size];
char* buf;
size_t len = 0;
int r = 0;
int peer;
sockaddr_in addr;
socklen_t sock_len;
TCPSocket peersocket;
while(1) {
buf = buffer;
int n = epoll_wait(_epf, events, 10000, -1);
if (n > 0) {
//t = clock();
for(int i = 0; i < n; ++i) {
if(events[i].data.fd == sock) {
// process accept
peer = accept(sock, (sockaddr*)&addr, &sock_len);
if (peer == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
perror ("accept by others");
continue;
}
perror("accept error");
return;
}
r = _server->handle()->handle_handshake(peer, buf, _buffer_size);
if (r == -1){ ::close(peer); continue; }
peersocket.sock(peer);
peersocket.set_noblock();
ev.data.fd = peer;
ev.events = EPOLLIN;
epoll_ctl(_epf, EPOLL_CTL_ADD, peer, &ev);
perror ("epoll_ctr has issue? :");
cout << "new socket:" << peer << endl << flush;
} else {
r = _server->handle()->handle_input(events[i].data.fd, buf, _buffer_size);
if (r == -1) {
epoll_ctl(_epf, EPOLL_CTL_DEL, events[i].data.fd, &ev);
::close(events[i].data.fd);
cout << "close socket:" << peer << endl << flush;
continue;
}
}
}
//cout << n << "cost: " << clock() -t << endl;
}
}
}