本文整理汇总了C++中Dispatcher::getEpoll方法的典型用法代码示例。如果您正苦于以下问题:C++ Dispatcher::getEpoll方法的具体用法?C++ Dispatcher::getEpoll怎么用?C++ Dispatcher::getEpoll使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Dispatcher
的用法示例。
在下文中一共展示了Dispatcher::getEpoll方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: runtime_error
TcpListener::TcpListener(Dispatcher& dispatcher, const Ipv4Address& addr, uint16_t port) : dispatcher(&dispatcher) {
std::string message;
listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (listener == -1) {
message = "socket failed, " + lastErrorMessage();
} else {
int flags = fcntl(listener, F_GETFL, 0);
if (flags == -1 || fcntl(listener, F_SETFL, flags | O_NONBLOCK) == -1) {
message = "fcntl failed, " + lastErrorMessage();
} else {
int on = 1;
if (setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &on, sizeof on) == -1) {
message = "setsockopt failed, " + lastErrorMessage();
} else {
sockaddr_in address;
address.sin_family = AF_INET;
address.sin_port = htons(port);
address.sin_addr.s_addr = htonl( addr.getValue());
if (bind(listener, reinterpret_cast<sockaddr *>(&address), sizeof address) != 0) {
message = "bind failed, " + lastErrorMessage();
} else if (listen(listener, SOMAXCONN) != 0) {
message = "listen failed, " + lastErrorMessage();
} else {
epoll_event listenEvent;
listenEvent.events = 0;
listenEvent.data.ptr = nullptr;
if (epoll_ctl(dispatcher.getEpoll(), EPOLL_CTL_ADD, listener, &listenEvent) == -1) {
message = "epoll_ctl failed, " + lastErrorMessage();
} else {
context = nullptr;
return;
}
}
}
}
int result = close(listener);
assert(result != -1);
}
throw std::runtime_error("TcpListener::TcpListener, " + message);
}
示例2: runtime_error
TcpListener::TcpListener(Dispatcher& dispatcher, const std::string& address, uint16_t port) : dispatcher(&dispatcher) {
listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (listener == -1) {
std::cerr << "socket failed, errno=" << errno << std::endl;
} else {
int flags = fcntl(listener, F_GETFL, 0);
if (flags == -1 || fcntl(listener, F_SETFL, flags | O_NONBLOCK) == -1) {
std::cerr << "fcntl() failed errno=" << errno << std::endl;
} else {
sockaddr_in address;
address.sin_family = AF_INET;
address.sin_port = htons(port);
address.sin_addr.s_addr = INADDR_ANY;
if (bind(listener, reinterpret_cast<sockaddr*>(&address), sizeof address) != 0) {
std::cerr << "bind failed, errno=" << errno << std::endl;
} else if (listen(listener, SOMAXCONN) != 0) {
std::cerr << "listen failed, errno=" << errno << std::endl;
} else {
epoll_event listenEvent;
listenEvent.data.fd = listener;
listenEvent.events = 0;
listenEvent.data.ptr = nullptr;
if (epoll_ctl(dispatcher.getEpoll(), EPOLL_CTL_ADD, listener, &listenEvent) == -1) {
std::cerr << "epoll_ctl() failed, errno=" << errno << '.' << std::endl;
} else {
stopped = false;
context = nullptr;
return;
}
}
}
if (close(listener) == -1) {
std::cerr << "close failed, errno=" << errno << std::endl;
}
}
throw std::runtime_error("TcpListener::TcpListener");
}