memgraph/include/io/network/server.hpp

42 lines
867 B
C++
Raw Normal View History

2016-03-14 04:51:04 +08:00
#pragma once
#include "io/network/stream_reader.hpp"
2016-03-14 04:51:04 +08:00
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:
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
void on_close_event(Epoll::Event &event) { ::close(event.data.fd); }
2016-03-14 04:51:04 +08:00
void on_error_event(Epoll::Event &event) { ::close(event.data.fd); }
2016-03-14 04:51:04 +08:00
void on_data_event(Epoll::Event &event)
2016-08-02 05:14:09 +08:00
{
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
template <class... Args>
void on_exception_event(Epoll::Event &event, Args &&... args)
{
// TODO: Do something about it
}
2016-08-02 05:14:09 +08:00
protected:
Epoll::Event event;
Socket socket;
2016-03-14 04:51:04 +08:00
};
}