diff options
Diffstat (limited to 'common/network/Socket.h')
-rw-r--r-- | common/network/Socket.h | 54 |
1 files changed, 31 insertions, 23 deletions
diff --git a/common/network/Socket.h b/common/network/Socket.h index 39ac39b4..bfda8a57 100644 --- a/common/network/Socket.h +++ b/common/network/Socket.h @@ -30,24 +30,22 @@ namespace network { + void initSockets(); + + bool isSocketListening(int sock); + class Socket { public: - Socket(int fd) - : instream(new rdr::FdInStream(fd)), - outstream(new rdr::FdOutStream(fd)), - isShutdown_(false), - queryConnection(false) {} - virtual ~Socket() { - delete instream; - delete outstream; - } + Socket(int fd); + virtual ~Socket(); + rdr::FdInStream &inStream() {return *instream;} rdr::FdOutStream &outStream() {return *outstream;} int getFd() {return outstream->getFd();} - // if shutdown() is overridden then the override MUST call on to here - virtual void shutdown() {isShutdown_ = true;} - bool isShutdown() const {return isShutdown_;} + void shutdown(); + bool isShutdown() const; + virtual bool cork(bool enable) = 0; // information about the remote end of the socket @@ -55,15 +53,15 @@ namespace network { virtual char* getPeerEndpoint() = 0; // <address>::<port> // Was there a "?" in the ConnectionFilter used to accept this Socket? - void setRequiresQuery() {queryConnection = true;} - bool requiresQuery() const {return queryConnection;} + void setRequiresQuery(); + bool requiresQuery() const; protected: - Socket() : instream(0), outstream(0), - isShutdown_(false), queryConnection(false) {} - Socket(rdr::FdInStream* i, rdr::FdOutStream* o) - : instream(i), outstream(o), - isShutdown_(false), queryConnection(false) {} + Socket(); + + void setFd(int fd); + + private: rdr::FdInStream* instream; rdr::FdOutStream* outstream; bool isShutdown_; @@ -78,22 +76,32 @@ namespace network { class SocketListener { public: - SocketListener() : fd(0), filter(0) {} - virtual ~SocketListener() {} + SocketListener(int fd); + virtual ~SocketListener(); // shutdown() stops the socket from accepting further connections - virtual void shutdown() = 0; + void shutdown(); // accept() returns a new Socket object if there is a connection // attempt in progress AND if the connection passes the filter // if one is installed. Otherwise, returns 0. - virtual Socket* accept() = 0; + Socket* accept(); virtual int getMyPort() = 0; // setFilter() applies the specified filter to all new connections void setFilter(ConnectionFilter* f) {filter = f;} int getFd() {return fd;} + + protected: + SocketListener(); + + void listen(int fd); + + // createSocket() should create a new socket of the correct class + // for the given file descriptor + virtual Socket* createSocket(int fd) = 0; + protected: int fd; ConnectionFilter* filter; |