From f8dbc120c055fb61fa79f901cc2974d049d04f4f Mon Sep 17 00:00:00 2001 From: Nicolas Noble Date: Tue, 16 Jul 2013 11:14:34 -0700 Subject: Split the Socket class into Selectable, in order to let it work with other non-socket file descriptors. --- includes/Selectable.h | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 includes/Selectable.h (limited to 'includes/Selectable.h') diff --git a/includes/Selectable.h b/includes/Selectable.h new file mode 100644 index 0000000..0cfae0c --- /dev/null +++ b/includes/Selectable.h @@ -0,0 +1,49 @@ +#pragma once + +#include +#include +#include +#include +#include + +namespace Balau { + +class Selectable : public Handle { + public: + ~Selectable(); + virtual ssize_t read(void * buf, size_t count) throw (GeneralException); + virtual ssize_t write(const void * buf, size_t count) throw (GeneralException); + virtual bool isClosed(); + virtual bool isEOF(); + + bool gotR() { return m_evtR->gotSignal(); } + bool gotW() { return m_evtW->gotSignal(); } + + class SelectableEvent : public Events::BaseEvent { + public: + SelectableEvent(int fd, int evt = ev::READ | ev::WRITE) : m_task(NULL) { Printer::elog(E_SELECT, "Got a new SelectableEvent at %p", this); m_evt.set(this); m_evt.set(fd, evt); } + virtual ~SelectableEvent() { Printer::elog(E_SELECT, "Destroying a SelectableEvent at %p", this); m_evt.stop(); } + void stop() { Printer::elog(E_SELECT, "Stopping a SelectableEvent at %p", this); reset(); m_evt.stop(); } + private: + void evt_cb(ev::io & w, int revents) { Printer::elog(E_SELECT, "Got a libev callback on a SelectableEvent at %p", this); doSignal(); } + virtual void gotOwner(Task * task); + + ev::io m_evt; + Task * m_task = NULL; + }; + + protected: + Selectable() { } + void setFD(int fd) throw (GeneralException); + void internalClose() { m_fd = -1; } + int getFD() { return m_fd; } + virtual ssize_t recv(int sockfd, void *buf, size_t len, int flags) = 0; + virtual ssize_t send(int sockfd, const void *buf, size_t len, int flags) = 0; + + SelectableEvent * m_evtR = NULL, * m_evtW = NULL; + + private: + int m_fd = -1; +}; + +}; -- cgit v1.2.3