blob: 13b12d1d03ade162b4227fb5e7d8a3a8ce46f2a8 [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 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 <limits.h>
25#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)),
36 ownStreams(true), isShutdown_(false),
37 queryConnection(false) {}
38 virtual ~Socket() {
39 if (ownStreams) {
40 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();}
47
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_;}
51
52 // information about this end of the socket
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000053 virtual int getMyPort() = 0;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000054
55 // information about the remote end of the socket
56 virtual char* getPeerAddress() = 0; // a string e.g. "192.168.0.1"
57 virtual int getPeerPort() = 0;
58 virtual char* getPeerEndpoint() = 0; // <address>::<port>
59
60 // Is the remote end on the same machine?
61 virtual bool sameMachine() = 0;
62
63 // Was there a "?" in the ConnectionFilter used to accept this Socket?
64 void setRequiresQuery() {queryConnection = true;}
65 bool requiresQuery() const {return queryConnection;}
66
67 protected:
68 Socket() : instream(0), outstream(0), ownStreams(false),
69 isShutdown_(false), queryConnection(false) {}
70 Socket(rdr::FdInStream* i, rdr::FdOutStream* o, bool own)
71 : instream(i), outstream(o), ownStreams(own),
72 isShutdown_(false), queryConnection(false) {}
73 rdr::FdInStream* instream;
74 rdr::FdOutStream* outstream;
75 bool ownStreams;
76 bool isShutdown_;
77 bool queryConnection;
78 };
79
80 class ConnectionFilter {
81 public:
82 virtual bool verifyConnection(Socket* s) = 0;
83 };
84
85 class SocketListener {
86 public:
87 SocketListener() : fd(0), filter(0) {}
88 virtual ~SocketListener() {}
89
90 // shutdown() stops the socket from accepting further connections
91 virtual void shutdown() = 0;
92
93 // accept() returns a new Socket object if there is a connection
94 // attempt in progress AND if the connection passes the filter
95 // if one is installed. Otherwise, returns 0.
96 virtual Socket* accept() = 0;
97
98 // setFilter() applies the specified filter to all new connections
99 void setFilter(ConnectionFilter* f) {filter = f;}
100 int getFd() {return fd;}
101 protected:
102 int fd;
103 ConnectionFilter* filter;
104 };
105
106 struct SocketException : public rdr::SystemException {
107 SocketException(const char* text, int err_) : rdr::SystemException(text, err_) {}
108 };
109
110 class SocketServer {
111 public:
112 virtual ~SocketServer() {}
113
114 // addSocket() tells the server to serve the Socket. The caller
115 // retains ownership of the Socket - the only way for the server
116 // to discard a Socket is by calling shutdown() on it.
117 // outgoing is set to true if the socket was created by connecting out
118 // to another host, or false if the socket was created by accept()ing
119 // an incoming connection.
120 virtual void addSocket(network::Socket* sock, bool outgoing=false) = 0;
121
122 // removeSocket() tells the server to stop serving the Socket. The
123 // caller retains ownership of the Socket - the server must NOT
124 // delete the Socket! This call is used mainly to cause per-Socket
125 // resources to be freed.
126 virtual void removeSocket(network::Socket* sock) = 0;
127
Pierre Ossmand408ca52016-04-29 14:26:05 +0200128 // processSocketReadEvent() tells the server there is a Socket read event.
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000129 // The implementation can indicate that the Socket is no longer active
130 // by calling shutdown() on it. The caller will then call removeSocket()
131 // soon after processSocketEvent returns, to allow any pre-Socket
132 // resources to be tidied up.
Pierre Ossmand408ca52016-04-29 14:26:05 +0200133 virtual void processSocketReadEvent(network::Socket* sock) = 0;
134
135 // processSocketReadEvent() tells the server there is a Socket write event.
136 // This is only necessary if the Socket has been put in non-blocking
137 // mode and needs this callback to flush the buffer.
138 virtual void processSocketWriteEvent(network::Socket* sock) = 0;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000139
140 // checkTimeouts() allows the server to check socket timeouts, etc. The
141 // return value is the number of milliseconds to wait before
142 // checkTimeouts() should be called again. If this number is zero then
143 // there is no timeout and checkTimeouts() should be called the next time
144 // an event occurs.
145 virtual int checkTimeouts() = 0;
146
147 virtual bool getDisable() {return false;};
148 };
149
150}
151
152#endif // __NETWORK_SOCKET_H__