2018-05-04 18:52:54 +05:00
|
|
|
#include <fcntl.h>
|
2018-05-04 17:17:49 +05:00
|
|
|
#ifdef __linux__
|
|
|
|
#include "epoll.hpp"
|
|
|
|
typedef nntpchan::ev::EpollLoop LoopImpl;
|
|
|
|
#else
|
|
|
|
#ifdef __freebsd__
|
|
|
|
#include "kqueue.hpp"
|
|
|
|
typedef nntpchan::ev::KqueueLoop LoopImpl;
|
|
|
|
#else
|
2018-05-04 19:08:09 +05:00
|
|
|
#ifdef __netbsd__
|
|
|
|
typedef nntpchan::ev::KqueueLoop LoopImpl;
|
|
|
|
#else
|
2018-05-04 17:17:49 +05:00
|
|
|
#error "unsupported platform"
|
|
|
|
#endif
|
|
|
|
#endif
|
2018-05-04 19:08:09 +05:00
|
|
|
#endif
|
2018-05-04 17:17:49 +05:00
|
|
|
|
|
|
|
namespace nntpchan
|
2018-05-03 20:47:20 +05:00
|
|
|
{
|
2018-05-04 18:52:54 +05:00
|
|
|
namespace ev
|
|
|
|
{
|
|
|
|
bool ev::Loop::BindTCP(const sockaddr * addr, ev::io * handler)
|
|
|
|
{
|
|
|
|
assert(handler->acceptable());
|
|
|
|
socklen_t slen;
|
|
|
|
switch(addr->sa_family)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(bind(fd, addr, slen) == -1)
|
2018-05-04 19:08:09 +05:00
|
|
|
{
|
|
|
|
::close(fd);
|
2018-05-04 18:52:54 +05:00
|
|
|
return false;
|
2018-05-04 19:08:09 +05:00
|
|
|
}
|
|
|
|
|
2018-05-04 18:52:54 +05:00
|
|
|
if (listen(fd, 5) == -1)
|
2018-05-04 19:08:09 +05:00
|
|
|
{
|
|
|
|
::close(fd);
|
2018-05-04 18:52:54 +05:00
|
|
|
return false;
|
2018-05-04 19:08:09 +05:00
|
|
|
}
|
2018-05-04 18:52:54 +05:00
|
|
|
|
2018-05-04 19:08:09 +05:00
|
|
|
handler->fd = fd;
|
2018-05-04 18:52:54 +05:00
|
|
|
return TrackConn(handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Loop::SetNonBlocking(ev::io * handler)
|
|
|
|
{
|
|
|
|
return fcntl(handler->fd, F_SETFL, fcntl(handler->fd, F_GETFL, 0) | O_NONBLOCK) != -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-04 17:17:49 +05:00
|
|
|
ev::Loop * NewMainLoop()
|
2018-05-03 20:47:20 +05:00
|
|
|
{
|
2018-05-04 17:17:49 +05:00
|
|
|
return new LoopImpl;
|
2018-05-03 20:47:20 +05:00
|
|
|
}
|
2018-05-04 17:17:49 +05:00
|
|
|
}
|