2016-03-14 04:51:04 +08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "stream_reader.hpp"
|
|
|
|
|
|
|
|
namespace io
|
|
|
|
{
|
|
|
|
|
2016-08-02 05:14:09 +08:00
|
|
|
template <class Derived>
|
|
|
|
class Server : public EventListener<Derived>
|
2016-03-14 04:51:04 +08:00
|
|
|
{
|
|
|
|
public:
|
2016-08-02 05:14:09 +08:00
|
|
|
Server(Socket&& socket) : socket(std::forward<Socket>(socket))
|
2016-03-14 04:51:04 +08:00
|
|
|
{
|
2016-08-02 05:14:09 +08:00
|
|
|
event.data.fd = this->socket;
|
|
|
|
event.events = EPOLLIN | EPOLLET;
|
2016-03-14 04:51:04 +08:00
|
|
|
|
2016-08-02 05:14:09 +08:00
|
|
|
this->listener.add(this->socket, &event);
|
|
|
|
}
|
2016-03-14 04:51:04 +08:00
|
|
|
|
2016-08-02 05:14:09 +08:00
|
|
|
void on_close_event(Epoll::Event& event)
|
|
|
|
{
|
|
|
|
::close(event.data.fd);
|
|
|
|
}
|
2016-03-14 04:51:04 +08:00
|
|
|
|
2016-08-02 05:14:09 +08:00
|
|
|
void on_error_event(Epoll::Event& event)
|
|
|
|
{
|
|
|
|
::close(event.data.fd);
|
|
|
|
}
|
2016-03-14 04:51:04 +08:00
|
|
|
|
2016-08-02 05:14:09 +08:00
|
|
|
void on_data_event(Epoll::Event& event)
|
|
|
|
{
|
|
|
|
if(UNLIKELY(socket != event.data.fd))
|
|
|
|
return;
|
2016-03-14 04:51:04 +08:00
|
|
|
|
2016-08-02 05:14:09 +08:00
|
|
|
this->derived().on_connect();
|
2016-03-14 04:51:04 +08:00
|
|
|
}
|
2016-08-02 05:14:09 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
Epoll::Event event;
|
|
|
|
Socket socket;
|
2016-03-14 04:51:04 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|