1 | /*
|
---|
2 | * synergy -- mouse and keyboard sharing utility
|
---|
3 | * Copyright (C) 2002 Chris Schoeneman
|
---|
4 | *
|
---|
5 | * This package is free software; you can redistribute it and/or
|
---|
6 | * modify it under the terms of the GNU General Public License
|
---|
7 | * found in the file COPYING that should have accompanied this file.
|
---|
8 | *
|
---|
9 | * This package is distributed in the hope that it will be useful,
|
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
12 | * GNU General Public License for more details.
|
---|
13 | */
|
---|
14 |
|
---|
15 | #ifndef IARCHNETWORK_H
|
---|
16 | #define IARCHNETWORK_H
|
---|
17 |
|
---|
18 | #include "IInterface.h"
|
---|
19 | #include "stdstring.h"
|
---|
20 |
|
---|
21 | class CArchThreadImpl;
|
---|
22 | typedef CArchThreadImpl* CArchThread;
|
---|
23 |
|
---|
24 | /*!
|
---|
25 | \class CArchSocketImpl
|
---|
26 | \brief Internal socket data.
|
---|
27 | An architecture dependent type holding the necessary data for a socket.
|
---|
28 | */
|
---|
29 | class CArchSocketImpl;
|
---|
30 |
|
---|
31 | /*!
|
---|
32 | \var CArchSocket
|
---|
33 | \brief Opaque socket type.
|
---|
34 | An opaque type representing a socket.
|
---|
35 | */
|
---|
36 | typedef CArchSocketImpl* CArchSocket;
|
---|
37 |
|
---|
38 | /*!
|
---|
39 | \class CArchNetAddressImpl
|
---|
40 | \brief Internal network address data.
|
---|
41 | An architecture dependent type holding the necessary data for a network
|
---|
42 | address.
|
---|
43 | */
|
---|
44 | class CArchNetAddressImpl;
|
---|
45 |
|
---|
46 | /*!
|
---|
47 | \var CArchNetAddress
|
---|
48 | \brief Opaque network address type.
|
---|
49 | An opaque type representing a network address.
|
---|
50 | */
|
---|
51 | typedef CArchNetAddressImpl* CArchNetAddress;
|
---|
52 |
|
---|
53 | //! Interface for architecture dependent networking
|
---|
54 | /*!
|
---|
55 | This interface defines the networking operations required by
|
---|
56 | synergy. Each architecture must implement this interface.
|
---|
57 | */
|
---|
58 | class IArchNetwork : public IInterface {
|
---|
59 | public:
|
---|
60 | //! Supported address families
|
---|
61 | enum EAddressFamily {
|
---|
62 | kUNKNOWN,
|
---|
63 | kINET,
|
---|
64 | };
|
---|
65 |
|
---|
66 | //! Supported socket types
|
---|
67 | enum ESocketType {
|
---|
68 | kDGRAM,
|
---|
69 | kSTREAM
|
---|
70 | };
|
---|
71 |
|
---|
72 | //! Events for \c poll()
|
---|
73 | /*!
|
---|
74 | Events for \c poll() are bitmasks and can be combined using the
|
---|
75 | bitwise operators.
|
---|
76 | */
|
---|
77 | enum {
|
---|
78 | kPOLLIN = 1, //!< Socket is readable
|
---|
79 | kPOLLOUT = 2, //!< Socket is writable
|
---|
80 | kPOLLERR = 4, //!< The socket is in an error state
|
---|
81 | kPOLLNVAL = 8 //!< The socket is invalid
|
---|
82 | };
|
---|
83 |
|
---|
84 | //! A socket query for \c poll()
|
---|
85 | class CPollEntry {
|
---|
86 | public:
|
---|
87 | //! The socket to query
|
---|
88 | CArchSocket m_socket;
|
---|
89 |
|
---|
90 | //! The events to query for
|
---|
91 | /*!
|
---|
92 | The events to query for can be any combination of kPOLLIN and
|
---|
93 | kPOLLOUT.
|
---|
94 | */
|
---|
95 | unsigned short m_events;
|
---|
96 |
|
---|
97 | //! The result events
|
---|
98 | unsigned short m_revents;
|
---|
99 | };
|
---|
100 |
|
---|
101 | //! @name manipulators
|
---|
102 | //@{
|
---|
103 |
|
---|
104 | //! Create a new socket
|
---|
105 | /*!
|
---|
106 | The socket is an opaque data type.
|
---|
107 | */
|
---|
108 | virtual CArchSocket newSocket(EAddressFamily, ESocketType) = 0;
|
---|
109 |
|
---|
110 | //! Copy a socket object
|
---|
111 | /*!
|
---|
112 | Returns a reference to to socket referred to by \c s.
|
---|
113 | */
|
---|
114 | virtual CArchSocket copySocket(CArchSocket s) = 0;
|
---|
115 |
|
---|
116 | //! Release a socket reference
|
---|
117 | /*!
|
---|
118 | Deletes the given socket object. This does not destroy the socket
|
---|
119 | the object referred to until there are no remaining references for
|
---|
120 | the socket.
|
---|
121 | */
|
---|
122 | virtual void closeSocket(CArchSocket s) = 0;
|
---|
123 |
|
---|
124 | //! Close socket for further reads
|
---|
125 | /*!
|
---|
126 | Calling this disallows future reads on socket \c s.
|
---|
127 | */
|
---|
128 | virtual void closeSocketForRead(CArchSocket s) = 0;
|
---|
129 |
|
---|
130 | //! Close socket for further writes
|
---|
131 | /*!
|
---|
132 | Calling this disallows future writes on socket \c s.
|
---|
133 | */
|
---|
134 | virtual void closeSocketForWrite(CArchSocket s) = 0;
|
---|
135 |
|
---|
136 | //! Bind socket to address
|
---|
137 | /*!
|
---|
138 | Binds socket \c s to the address \c addr.
|
---|
139 | */
|
---|
140 | virtual void bindSocket(CArchSocket s, CArchNetAddress addr) = 0;
|
---|
141 |
|
---|
142 | //! Listen for connections on socket
|
---|
143 | /*!
|
---|
144 | Causes the socket \c s to begin listening for incoming connections.
|
---|
145 | */
|
---|
146 | virtual void listenOnSocket(CArchSocket s) = 0;
|
---|
147 |
|
---|
148 | //! Accept connection on socket
|
---|
149 | /*!
|
---|
150 | Accepts a connection on socket \c s, returning a new socket for the
|
---|
151 | connection and filling in \c addr with the address of the remote
|
---|
152 | end. \c addr may be NULL if the remote address isn't required.
|
---|
153 | The original socket \c s is unaffected and remains in the listening
|
---|
154 | state. The new socket shares most of the properties of \c s except
|
---|
155 | it's not in the listening state and it's connected. Returns NULL
|
---|
156 | if there are no pending connection requests.
|
---|
157 | */
|
---|
158 | virtual CArchSocket acceptSocket(CArchSocket s, CArchNetAddress* addr) = 0;
|
---|
159 |
|
---|
160 | //! Connect socket
|
---|
161 | /*!
|
---|
162 | Connects the socket \c s to the remote address \c addr. Returns
|
---|
163 | true if the connection succeed immediately, false if the connection
|
---|
164 | is in progress, and throws if the connection failed immediately.
|
---|
165 | If it returns false, \c pollSocket() can be used to wait on the
|
---|
166 | socket for writing to detect when the connection finally succeeds
|
---|
167 | or fails.
|
---|
168 | */
|
---|
169 | virtual bool connectSocket(CArchSocket s, CArchNetAddress addr) = 0;
|
---|
170 |
|
---|
171 | //! Check socket state
|
---|
172 | /*!
|
---|
173 | Tests the state of \c num sockets for readability and/or writability.
|
---|
174 | Waits up to \c timeout seconds for some socket to become readable
|
---|
175 | and/or writable (or indefinitely if \c timeout < 0). Returns the
|
---|
176 | number of sockets that were readable (if readability was being
|
---|
177 | queried) or writable (if writablility was being queried) and sets
|
---|
178 | the \c m_revents members of the entries. \c kPOLLERR and \c kPOLLNVAL
|
---|
179 | are set in \c m_revents as appropriate. If a socket indicates
|
---|
180 | \c kPOLLERR then \c throwErrorOnSocket() can be used to determine
|
---|
181 | the type of error. Returns 0 immediately regardless of the \c timeout
|
---|
182 | if no valid sockets are selected for testing.
|
---|
183 |
|
---|
184 | (Cancellation point)
|
---|
185 | */
|
---|
186 | virtual int pollSocket(CPollEntry[], int num, double timeout) = 0;
|
---|
187 |
|
---|
188 | //! Unblock thread in pollSocket()
|
---|
189 | /*!
|
---|
190 | Cause a thread that's in a pollSocket() call to return. This
|
---|
191 | call may return before the thread is unblocked. If the thread is
|
---|
192 | not in a pollSocket() call this call has no effect.
|
---|
193 | */
|
---|
194 | virtual void unblockPollSocket(CArchThread thread) = 0;
|
---|
195 |
|
---|
196 | //! Read data from socket
|
---|
197 | /*!
|
---|
198 | Read up to \c len bytes from socket \c s in \c buf and return the
|
---|
199 | number of bytes read. The number of bytes can be less than \c len
|
---|
200 | if not enough data is available. Returns 0 if the remote end has
|
---|
201 | disconnected and/or there is no more queued received data.
|
---|
202 | */
|
---|
203 | virtual size_t readSocket(CArchSocket s, void* buf, size_t len) = 0;
|
---|
204 |
|
---|
205 | //! Write data from socket
|
---|
206 | /*!
|
---|
207 | Write up to \c len bytes to socket \c s from \c buf and return the
|
---|
208 | number of bytes written. The number of bytes can be less than
|
---|
209 | \c len if the remote end disconnected or the internal buffers fill
|
---|
210 | up.
|
---|
211 | */
|
---|
212 | virtual size_t writeSocket(CArchSocket s,
|
---|
213 | const void* buf, size_t len) = 0;
|
---|
214 |
|
---|
215 | //! Check error on socket
|
---|
216 | /*!
|
---|
217 | If the socket \c s is in an error state then throws an appropriate
|
---|
218 | XArchNetwork exception.
|
---|
219 | */
|
---|
220 | virtual void throwErrorOnSocket(CArchSocket s) = 0;
|
---|
221 |
|
---|
222 | //! Turn Nagle algorithm on or off on socket
|
---|
223 | /*!
|
---|
224 | Set socket to send messages immediately (true) or to collect small
|
---|
225 | messages into one packet (false). Returns the previous state.
|
---|
226 | */
|
---|
227 | virtual bool setNoDelayOnSocket(CArchSocket, bool noDelay) = 0;
|
---|
228 |
|
---|
229 | //! Turn address reuse on or off on socket
|
---|
230 | /*!
|
---|
231 | Allows the address this socket is bound to to be reused while in the
|
---|
232 | TIME_WAIT state. Returns the previous state.
|
---|
233 | */
|
---|
234 | virtual bool setReuseAddrOnSocket(CArchSocket, bool reuse) = 0;
|
---|
235 |
|
---|
236 | //! Return local host's name
|
---|
237 | virtual std::string getHostName() = 0;
|
---|
238 |
|
---|
239 | //! Create an "any" network address
|
---|
240 | virtual CArchNetAddress newAnyAddr(EAddressFamily) = 0;
|
---|
241 |
|
---|
242 | //! Copy a network address
|
---|
243 | virtual CArchNetAddress copyAddr(CArchNetAddress) = 0;
|
---|
244 |
|
---|
245 | //! Convert a name to a network address
|
---|
246 | virtual CArchNetAddress nameToAddr(const std::string&) = 0;
|
---|
247 |
|
---|
248 | //! Destroy a network address
|
---|
249 | virtual void closeAddr(CArchNetAddress) = 0;
|
---|
250 |
|
---|
251 | //! Convert an address to a host name
|
---|
252 | virtual std::string addrToName(CArchNetAddress) = 0;
|
---|
253 |
|
---|
254 | //! Convert an address to a string
|
---|
255 | virtual std::string addrToString(CArchNetAddress) = 0;
|
---|
256 |
|
---|
257 | //! Get an address's family
|
---|
258 | virtual EAddressFamily getAddrFamily(CArchNetAddress) = 0;
|
---|
259 |
|
---|
260 | //! Set the port of an address
|
---|
261 | virtual void setAddrPort(CArchNetAddress, int port) = 0;
|
---|
262 |
|
---|
263 | //! Get the port of an address
|
---|
264 | virtual int getAddrPort(CArchNetAddress) = 0;
|
---|
265 |
|
---|
266 | //! Test addresses for equality
|
---|
267 | virtual bool isEqualAddr(CArchNetAddress, CArchNetAddress) = 0;
|
---|
268 |
|
---|
269 | //! Test for the "any" address
|
---|
270 | /*!
|
---|
271 | Returns true if \c addr is the "any" address. \c newAnyAddr()
|
---|
272 | returns an "any" address.
|
---|
273 | */
|
---|
274 | virtual bool isAnyAddr(CArchNetAddress addr) = 0;
|
---|
275 |
|
---|
276 | //@}
|
---|
277 | };
|
---|
278 |
|
---|
279 | #endif
|
---|