Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2004 RealVNC Ltd. All Rights Reserved. |
| 2 | * |
| 3 | * This is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This software is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this software; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 16 | * USA. |
| 17 | */ |
| 18 | |
| 19 | // -=- Socket.h - abstract base-class for any kind of network stream/socket |
| 20 | |
| 21 | #ifndef __NETWORK_SOCKET_H__ |
| 22 | #define __NETWORK_SOCKET_H__ |
| 23 | |
| 24 | #include <rdr/FdInStream.h> |
| 25 | #include <rdr/FdOutStream.h> |
| 26 | #include <rdr/Exception.h> |
| 27 | |
| 28 | namespace network { |
| 29 | |
| 30 | class Socket { |
| 31 | public: |
| 32 | Socket(int fd) |
| 33 | : instream(new rdr::FdInStream(fd)), |
| 34 | outstream(new rdr::FdOutStream(fd)), |
| 35 | own_streams(true) {} |
| 36 | virtual ~Socket() { |
| 37 | if (own_streams) { |
| 38 | delete instream; |
| 39 | delete outstream; |
| 40 | } |
| 41 | } |
| 42 | rdr::FdInStream &inStream() {return *instream;} |
| 43 | rdr::FdOutStream &outStream() {return *outstream;} |
| 44 | int getFd() {return outstream->getFd();} |
| 45 | virtual void shutdown() = 0; |
| 46 | |
| 47 | // information about this end of the socket |
| 48 | virtual char* getMyAddress() = 0; // a string e.g. "192.168.0.1" |
| 49 | virtual int getMyPort() = 0; |
| 50 | virtual char* getMyEndpoint() = 0; // <address>::<port> |
| 51 | |
| 52 | // information about the remote end of the socket |
| 53 | virtual char* getPeerAddress() = 0; // a string e.g. "192.168.0.1" |
| 54 | virtual int getPeerPort() = 0; |
| 55 | virtual char* getPeerEndpoint() = 0; // <address>::<port> |
| 56 | |
| 57 | // Is the remote end on the same machine? |
| 58 | virtual bool sameMachine() = 0; |
| 59 | |
| 60 | protected: |
| 61 | Socket() : instream(0), outstream(0), own_streams(false) {} |
| 62 | Socket(rdr::FdInStream* i, rdr::FdOutStream* o, bool own) |
| 63 | : instream(i), outstream(o), own_streams(own) {} |
| 64 | rdr::FdInStream* instream; |
| 65 | rdr::FdOutStream* outstream; |
| 66 | bool own_streams; |
| 67 | }; |
| 68 | |
| 69 | class ConnectionFilter { |
| 70 | public: |
| 71 | virtual bool verifyConnection(Socket* s) = 0; |
| 72 | virtual bool queryUserAcceptConnection(Socket*) {return false;} |
| 73 | }; |
| 74 | |
| 75 | class SocketListener { |
| 76 | public: |
| 77 | SocketListener() : fd(0), filter(0) {} |
| 78 | virtual ~SocketListener() {} |
| 79 | |
| 80 | // shutdown() stops the socket from accepting further connections |
| 81 | virtual void shutdown() = 0; |
| 82 | |
| 83 | // accept() returns a new Socket object if there is a connection |
| 84 | // attempt in progress AND if the connection passes the filter |
| 85 | // if one is installed. Otherwise, returns 0. |
| 86 | virtual Socket* accept() = 0; |
| 87 | |
| 88 | void setFilter(ConnectionFilter* f) {filter = f;} |
| 89 | int getFd() {return fd;} |
| 90 | protected: |
| 91 | int fd; |
| 92 | ConnectionFilter* filter; |
| 93 | }; |
| 94 | |
| 95 | struct SocketException : public rdr::SystemException { |
| 96 | SocketException(const char* text, int err_) : rdr::SystemException(text, err_) {} |
| 97 | }; |
| 98 | |
| 99 | class SocketServer { |
| 100 | public: |
| 101 | virtual ~SocketServer() {} |
| 102 | |
| 103 | // addClient() tells the server to manage the socket. |
| 104 | // If the server can't manage the socket, it must shutdown() it. |
| 105 | virtual void addClient(network::Socket* sock) = 0; |
| 106 | |
| 107 | // processSocketEvent() tells the server there is a socket read event. |
| 108 | // If there is an error, or the socket has been closed/shutdown then |
| 109 | // the server MUST delete the socket AND return false. |
| 110 | virtual bool processSocketEvent(network::Socket* sock) = 0; |
| 111 | |
| 112 | // checkTimeouts() allows the server to check socket timeouts, etc. The |
| 113 | // return value is the number of milliseconds to wait before |
| 114 | // checkTimeouts() should be called again. If this number is zero then |
| 115 | // there is no timeout and checkTimeouts() should be called the next time |
| 116 | // an event occurs. |
| 117 | virtual int checkTimeouts() = 0; |
| 118 | |
| 119 | // soonestTimeout() is a function to help work out the soonest of several |
| 120 | // timeouts. |
| 121 | static void soonestTimeout(int* timeout, int newTimeout) { |
| 122 | if (newTimeout && (!*timeout || newTimeout < *timeout)) |
| 123 | *timeout = newTimeout; |
| 124 | } |
Oleg Sheikin | d87a7a7 | 2006-01-12 15:27:04 +0000 | [diff] [blame^] | 125 | virtual bool getDisable() {return false;}; |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | } |
| 129 | |
| 130 | #endif // __NETWORK_SOCKET_H__ |