Archived
1
0
This repository has been archived on 2023-08-12. You can view files and clone it, but cannot push or open issues or pull requests.
nntpchan/contrib/backends/nntpchan-daemon/libnntpchan/event.cpp

78 lines
1.4 KiB
C++
Raw Permalink Normal View History

2018-05-04 18:52:54 +05:00
#include <fcntl.h>
2018-05-06 17:50:23 +05:00
#include <cstdlib>
2018-05-06 17:56:20 +05:00
#include <cassert>
#include <sys/types.h>
#include <sys/socket.h>
2018-05-06 18:01:17 +05:00
#include <sys/un.h>
2018-05-06 17:57:54 +05:00
#include <netinet/in.h>
2018-05-06 17:17:32 +05:00
2018-05-06 17:49:48 +05:00
constexpr std::size_t ev_buffsz = 512;
2018-05-06 17:17:32 +05:00
#ifdef __linux__
#include "epoll.hpp"
2018-05-06 17:17:32 +05:00
typedef nntpchan::ev::EpollLoop<ev_buffsz> LoopImpl;
#else
2018-05-06 17:48:44 +05:00
#ifdef __FreeBSD__
#include "kqueue.hpp"
2018-05-06 17:17:32 +05:00
typedef nntpchan::ev::KqueueLoop<ev_buffsz> LoopImpl;
#else
2018-05-04 19:08:09 +05:00
#ifdef __netbsd__
2018-05-06 17:17:32 +05:00
typedef nntpchan::ev::KqueueLoop<ev_buffsz> LoopImpl;
2018-05-04 19:08:09 +05:00
#else
#error "unsupported platform"
#endif
#endif
2018-05-04 19:08:09 +05:00
#endif
2018-05-06 17:10:20 +05:00
namespace nntpchan
2018-05-03 20:47:20 +05:00
{
2018-05-06 17:10:20 +05:00
namespace ev
{
bool ev::Loop::BindTCP(const sockaddr *addr, ev::io *handler)
{
assert(handler->acceptable());
socklen_t slen;
switch (addr->sa_family)
2018-05-04 18:52:54 +05:00
{
2018-05-06 17:10:20 +05:00
case AF_INET:
slen = sizeof(sockaddr_in);
break;
case AF_INET6:
slen = sizeof(sockaddr_in6);
break;
case AF_UNIX:
slen = sizeof(sockaddr_un);
break;
default:
return false;
}
int fd = socket(addr->sa_family, SOCK_STREAM | SOCK_NONBLOCK, 0);
if (fd == -1)
{
return false;
2018-05-04 18:52:54 +05:00
}
2018-05-06 17:10:20 +05:00
if (bind(fd, addr, slen) == -1)
{
::close(fd);
return false;
}
2018-05-04 18:52:54 +05:00
2018-05-06 17:10:20 +05:00
if (listen(fd, 5) == -1)
2018-05-03 20:47:20 +05:00
{
2018-05-06 17:10:20 +05:00
::close(fd);
return false;
2018-05-03 20:47:20 +05:00
}
2018-05-06 17:10:20 +05:00
handler->fd = fd;
return TrackConn(handler);
}
bool Loop::SetNonBlocking(ev::io *handler)
{
return fcntl(handler->fd, F_SETFL, fcntl(handler->fd, F_GETFL, 0) | O_NONBLOCK) != -1;
}
}
ev::Loop *NewMainLoop() { return new LoopImpl; }
}