blob: b93da2ef0d2a1b519e9356f6215188dee32cedc0 [file] [log] [blame]
Constantin Kaplinsky32d0d682006-04-16 06:47:16 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 *
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +00003 * 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
Constantin Kaplinsky32d0d682006-04-16 06:47:16 +000024#include <limits.h>
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000025#include <rdr/FdInStream.h>
26#include <rdr/FdOutStream.h>
27#include <rdr/Exception.h>
28
29namespace network {
30
31 class Socket {
32 public:
33 Socket(int fd)
34 : instream(new rdr::FdInStream(fd)),
35 outstream(new rdr::FdOutStream(fd)),
Constantin Kaplinsky32d0d682006-04-16 06:47:16 +000036 ownStreams(true), isShutdown_(false),
37 queryConnection(false) {}
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000038 virtual ~Socket() {
Constantin Kaplinsky32d0d682006-04-16 06:47:16 +000039 if (ownStreams) {
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000040 delete instream;
41 delete outstream;
42 }
43 }
44 rdr::FdInStream &inStream() {return *instream;}
45 rdr::FdOutStream &outStream() {return *outstream;}
46 int getFd() {return outstream->getFd();}
Constantin Kaplinsky32d0d682006-04-16 06:47:16 +000047
48 // if shutdown() is overridden then the override MUST call on to here
49 virtual void shutdown() {isShutdown_ = true;}
50 bool isShutdown() const {return isShutdown_;}
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000051
52 // information about this end of the socket
53 virtual char* getMyAddress() = 0; // a string e.g. "192.168.0.1"
54 virtual int getMyPort() = 0;
55 virtual char* getMyEndpoint() = 0; // <address>::<port>
56
57 // information about the remote end of the socket
58 virtual char* getPeerAddress() = 0; // a string e.g. "192.168.0.1"
59 virtual int getPeerPort() = 0;
60 virtual char* getPeerEndpoint() = 0; // <address>::<port>
61
62 // Is the remote end on the same machine?
63 virtual bool sameMachine() = 0;
64
Constantin Kaplinsky32d0d682006-04-16 06:47:16 +000065 // Was there a "?" in the ConnectionFilter used to accept this Socket?
66 void setRequiresQuery() {queryConnection = true;}
67 bool requiresQuery() const {return queryConnection;}
68
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000069 protected:
Constantin Kaplinsky32d0d682006-04-16 06:47:16 +000070 Socket() : instream(0), outstream(0), ownStreams(false),
71 isShutdown_(false), queryConnection(false) {}
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000072 Socket(rdr::FdInStream* i, rdr::FdOutStream* o, bool own)
Constantin Kaplinsky32d0d682006-04-16 06:47:16 +000073 : instream(i), outstream(o), ownStreams(own),
74 isShutdown_(false), queryConnection(false) {}
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000075 rdr::FdInStream* instream;
76 rdr::FdOutStream* outstream;
Constantin Kaplinsky32d0d682006-04-16 06:47:16 +000077 bool ownStreams;
78 bool isShutdown_;
79 bool queryConnection;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000080 };
81
82 class ConnectionFilter {
83 public:
84 virtual bool verifyConnection(Socket* s) = 0;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000085 };
86
87 class SocketListener {
88 public:
89 SocketListener() : fd(0), filter(0) {}
90 virtual ~SocketListener() {}
91
92 // shutdown() stops the socket from accepting further connections
93 virtual void shutdown() = 0;
94
95 // accept() returns a new Socket object if there is a connection
96 // attempt in progress AND if the connection passes the filter
97 // if one is installed. Otherwise, returns 0.
98 virtual Socket* accept() = 0;
99
Constantin Kaplinsky32d0d682006-04-16 06:47:16 +0000100 // setFilter() applies the specified filter to all new connections
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000101 void setFilter(ConnectionFilter* f) {filter = f;}
102 int getFd() {return fd;}
103 protected:
104 int fd;
105 ConnectionFilter* filter;
106 };
107
108 struct SocketException : public rdr::SystemException {
109 SocketException(const char* text, int err_) : rdr::SystemException(text, err_) {}
110 };
111
112 class SocketServer {
113 public:
114 virtual ~SocketServer() {}
115
Constantin Kaplinsky32d0d682006-04-16 06:47:16 +0000116 // addSocket() tells the server to serve the Socket. The caller
117 // retains ownership of the Socket - the only way for the server
118 // to discard a Socket is by calling shutdown() on it.
119 // outgoing is set to true if the socket was created by connecting out
120 // to another host, or false if the socket was created by accept()ing
121 // an incoming connection.
122 virtual void addSocket(network::Socket* sock, bool outgoing=false) = 0;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000123
Constantin Kaplinsky32d0d682006-04-16 06:47:16 +0000124 // removeSocket() tells the server to stop serving the Socket. The
125 // caller retains ownership of the Socket - the server must NOT
126 // delete the Socket! This call is used mainly to cause per-Socket
127 // resources to be freed.
128 virtual void removeSocket(network::Socket* sock) = 0;
129
130 // processSocketEvent() tells the server there is a Socket read event.
131 // The implementation can indicate that the Socket is no longer active
132 // by calling shutdown() on it. The caller will then call removeSocket()
133 // soon after processSocketEvent returns, to allow any pre-Socket
134 // resources to be tidied up.
135 virtual void processSocketEvent(network::Socket* sock) = 0;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000136
137 // checkTimeouts() allows the server to check socket timeouts, etc. The
Constantin Kaplinsky32d0d682006-04-16 06:47:16 +0000138 // return value is the number of milliseconds to wait before
139 // checkTimeouts() should be called again. If this number is zero then
140 // there is no timeout and checkTimeouts() should be called the next time
141 // an event occurs.
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000142 virtual int checkTimeouts() = 0;
143
Oleg Sheikind87a7a72006-01-12 15:27:04 +0000144 virtual bool getDisable() {return false;};
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000145 };
146
147}
148
149#endif // __NETWORK_SOCKET_H__