2003-01-04 22:01:32 +00:00
|
|
|
/*
|
|
|
|
* synergy -- mouse and keyboard sharing utility
|
|
|
|
* Copyright (C) 2002 Chris Schoeneman
|
|
|
|
*
|
|
|
|
* This package is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* found in the file COPYING that should have accompanied this file.
|
|
|
|
*
|
|
|
|
* This package is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef IARCHNETWORK_H
|
|
|
|
#define IARCHNETWORK_H
|
|
|
|
|
|
|
|
#include "IInterface.h"
|
|
|
|
#include "stdstring.h"
|
|
|
|
|
2004-02-28 12:19:49 +00:00
|
|
|
class CArchThreadImpl;
|
|
|
|
typedef CArchThreadImpl* CArchThread;
|
|
|
|
|
2003-01-05 21:48:54 +00:00
|
|
|
/*!
|
|
|
|
\class CArchSocketImpl
|
|
|
|
\brief Internal socket data.
|
|
|
|
An architecture dependent type holding the necessary data for a socket.
|
|
|
|
*/
|
2003-01-04 22:01:32 +00:00
|
|
|
class CArchSocketImpl;
|
2003-01-05 21:48:54 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
\var CArchSocket
|
|
|
|
\brief Opaque socket type.
|
|
|
|
An opaque type representing a socket.
|
|
|
|
*/
|
2003-01-04 22:01:32 +00:00
|
|
|
typedef CArchSocketImpl* CArchSocket;
|
2003-01-05 21:48:54 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
\class CArchNetAddressImpl
|
|
|
|
\brief Internal network address data.
|
|
|
|
An architecture dependent type holding the necessary data for a network
|
|
|
|
address.
|
|
|
|
*/
|
|
|
|
class CArchNetAddressImpl;
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\var CArchNetAddress
|
|
|
|
\brief Opaque network address type.
|
|
|
|
An opaque type representing a network address.
|
|
|
|
*/
|
2003-01-04 22:01:32 +00:00
|
|
|
typedef CArchNetAddressImpl* CArchNetAddress;
|
|
|
|
|
|
|
|
//! Interface for architecture dependent networking
|
|
|
|
/*!
|
|
|
|
This interface defines the networking operations required by
|
|
|
|
synergy. Each architecture must implement this interface.
|
|
|
|
*/
|
|
|
|
class IArchNetwork : public IInterface {
|
|
|
|
public:
|
2003-01-05 21:48:54 +00:00
|
|
|
//! Supported address families
|
2003-01-04 22:01:32 +00:00
|
|
|
enum EAddressFamily {
|
|
|
|
kUNKNOWN,
|
|
|
|
kINET,
|
|
|
|
};
|
|
|
|
|
2003-01-05 21:48:54 +00:00
|
|
|
//! Supported socket types
|
2003-01-04 22:01:32 +00:00
|
|
|
enum ESocketType {
|
|
|
|
kDGRAM,
|
|
|
|
kSTREAM
|
|
|
|
};
|
|
|
|
|
2003-01-05 21:48:54 +00:00
|
|
|
//! Events for \c poll()
|
|
|
|
/*!
|
|
|
|
Events for \c poll() are bitmasks and can be combined using the
|
|
|
|
bitwise operators.
|
|
|
|
*/
|
2003-01-04 22:01:32 +00:00
|
|
|
enum {
|
2003-01-05 21:48:54 +00:00
|
|
|
kPOLLIN = 1, //!< Socket is readable
|
|
|
|
kPOLLOUT = 2, //!< Socket is writable
|
|
|
|
kPOLLERR = 4, //!< The socket is in an error state
|
|
|
|
kPOLLNVAL = 8 //!< The socket is invalid
|
2003-01-04 22:01:32 +00:00
|
|
|
};
|
|
|
|
|
2003-01-05 21:48:54 +00:00
|
|
|
//! A socket query for \c poll()
|
2003-01-04 22:01:32 +00:00
|
|
|
class CPollEntry {
|
|
|
|
public:
|
2003-01-05 21:48:54 +00:00
|
|
|
//! The socket to query
|
2003-01-04 22:01:32 +00:00
|
|
|
CArchSocket m_socket;
|
2003-01-05 21:48:54 +00:00
|
|
|
|
|
|
|
//! The events to query for
|
|
|
|
/*!
|
|
|
|
The events to query for can be any combination of kPOLLIN and
|
|
|
|
kPOLLOUT.
|
|
|
|
*/
|
2003-01-04 22:01:32 +00:00
|
|
|
unsigned short m_events;
|
2003-01-05 21:48:54 +00:00
|
|
|
|
|
|
|
//! The result events
|
2003-01-04 22:01:32 +00:00
|
|
|
unsigned short m_revents;
|
|
|
|
};
|
|
|
|
|
|
|
|
//! @name manipulators
|
|
|
|
//@{
|
|
|
|
|
2003-01-05 21:48:54 +00:00
|
|
|
//! Create a new socket
|
|
|
|
/*!
|
|
|
|
The socket is an opaque data type.
|
|
|
|
*/
|
2003-01-04 22:01:32 +00:00
|
|
|
virtual CArchSocket newSocket(EAddressFamily, ESocketType) = 0;
|
2003-01-05 21:48:54 +00:00
|
|
|
|
|
|
|
//! Copy a socket object
|
|
|
|
/*!
|
|
|
|
Returns a reference to to socket referred to by \c s.
|
|
|
|
*/
|
2003-01-04 22:01:32 +00:00
|
|
|
virtual CArchSocket copySocket(CArchSocket s) = 0;
|
2003-01-05 21:48:54 +00:00
|
|
|
|
|
|
|
//! Release a socket reference
|
|
|
|
/*!
|
|
|
|
Deletes the given socket object. This does not destroy the socket
|
|
|
|
the object referred to until there are no remaining references for
|
|
|
|
the socket.
|
|
|
|
*/
|
2003-01-04 22:01:32 +00:00
|
|
|
virtual void closeSocket(CArchSocket s) = 0;
|
2003-01-05 21:48:54 +00:00
|
|
|
|
|
|
|
//! Close socket for further reads
|
|
|
|
/*!
|
|
|
|
Calling this disallows future reads on socket \c s.
|
|
|
|
*/
|
2003-01-04 22:01:32 +00:00
|
|
|
virtual void closeSocketForRead(CArchSocket s) = 0;
|
2003-01-05 21:48:54 +00:00
|
|
|
|
|
|
|
//! Close socket for further writes
|
|
|
|
/*!
|
|
|
|
Calling this disallows future writes on socket \c s.
|
|
|
|
*/
|
2003-01-04 22:01:32 +00:00
|
|
|
virtual void closeSocketForWrite(CArchSocket s) = 0;
|
2003-01-05 21:48:54 +00:00
|
|
|
|
|
|
|
//! Bind socket to address
|
|
|
|
/*!
|
|
|
|
Binds socket \c s to the address \c addr.
|
|
|
|
*/
|
2003-01-04 22:01:32 +00:00
|
|
|
virtual void bindSocket(CArchSocket s, CArchNetAddress addr) = 0;
|
2003-01-05 21:48:54 +00:00
|
|
|
|
|
|
|
//! Listen for connections on socket
|
|
|
|
/*!
|
|
|
|
Causes the socket \c s to begin listening for incoming connections.
|
|
|
|
*/
|
2003-01-04 22:01:32 +00:00
|
|
|
virtual void listenOnSocket(CArchSocket s) = 0;
|
2003-01-05 21:48:54 +00:00
|
|
|
|
|
|
|
//! Accept connection on socket
|
|
|
|
/*!
|
|
|
|
Accepts a connection on socket \c s, returning a new socket for the
|
|
|
|
connection and filling in \c addr with the address of the remote
|
|
|
|
end. \c addr may be NULL if the remote address isn't required.
|
|
|
|
The original socket \c s is unaffected and remains in the listening
|
|
|
|
state. The new socket shares most of the properties of \c s except
|
2004-02-29 16:11:17 +00:00
|
|
|
it's not in the listening state and it's connected. Returns NULL
|
|
|
|
if there are no pending connection requests.
|
2003-01-05 21:48:54 +00:00
|
|
|
*/
|
2003-01-04 22:01:32 +00:00
|
|
|
virtual CArchSocket acceptSocket(CArchSocket s, CArchNetAddress* addr) = 0;
|
2003-01-05 21:48:54 +00:00
|
|
|
|
|
|
|
//! Connect socket
|
|
|
|
/*!
|
2004-02-29 16:11:17 +00:00
|
|
|
Connects the socket \c s to the remote address \c addr. Returns
|
|
|
|
true if the connection succeed immediately, false if the connection
|
|
|
|
is in progress, and throws if the connection failed immediately.
|
|
|
|
If it returns false, \c pollSocket() can be used to wait on the
|
|
|
|
socket for writing to detect when the connection finally succeeds
|
|
|
|
or fails.
|
2003-01-05 21:48:54 +00:00
|
|
|
*/
|
2004-02-29 16:11:17 +00:00
|
|
|
virtual bool connectSocket(CArchSocket s, CArchNetAddress addr) = 0;
|
2003-01-05 21:48:54 +00:00
|
|
|
|
|
|
|
//! Check socket state
|
|
|
|
/*!
|
|
|
|
Tests the state of \c num sockets for readability and/or writability.
|
|
|
|
Waits up to \c timeout seconds for some socket to become readable
|
|
|
|
and/or writable (or indefinitely if \c timeout < 0). Returns the
|
|
|
|
number of sockets that were readable (if readability was being
|
|
|
|
queried) or writable (if writablility was being queried) and sets
|
|
|
|
the \c m_revents members of the entries. \c kPOLLERR and \c kPOLLNVAL
|
|
|
|
are set in \c m_revents as appropriate. If a socket indicates
|
|
|
|
\c kPOLLERR then \c throwErrorOnSocket() can be used to determine
|
2004-02-28 12:19:49 +00:00
|
|
|
the type of error. Returns 0 immediately regardless of the \c timeout
|
|
|
|
if no valid sockets are selected for testing.
|
2003-01-05 21:48:54 +00:00
|
|
|
|
|
|
|
(Cancellation point)
|
|
|
|
*/
|
2003-01-04 22:01:32 +00:00
|
|
|
virtual int pollSocket(CPollEntry[], int num, double timeout) = 0;
|
2003-01-05 21:48:54 +00:00
|
|
|
|
2004-02-28 12:19:49 +00:00
|
|
|
//! Unblock thread in pollSocket()
|
|
|
|
/*!
|
|
|
|
Cause a thread that's in a pollSocket() call to return. This
|
|
|
|
call may return before the thread is unblocked. If the thread is
|
|
|
|
not in a pollSocket() call this call has no effect.
|
|
|
|
*/
|
|
|
|
virtual void unblockPollSocket(CArchThread thread) = 0;
|
|
|
|
|
2003-01-05 21:48:54 +00:00
|
|
|
//! Read data from socket
|
|
|
|
/*!
|
|
|
|
Read up to \c len bytes from socket \c s in \c buf and return the
|
|
|
|
number of bytes read. The number of bytes can be less than \c len
|
|
|
|
if not enough data is available. Returns 0 if the remote end has
|
2004-02-29 16:11:17 +00:00
|
|
|
disconnected and/or there is no more queued received data.
|
2003-01-05 21:48:54 +00:00
|
|
|
*/
|
2003-01-04 22:01:32 +00:00
|
|
|
virtual size_t readSocket(CArchSocket s, void* buf, size_t len) = 0;
|
2003-01-05 21:48:54 +00:00
|
|
|
|
|
|
|
//! Write data from socket
|
|
|
|
/*!
|
|
|
|
Write up to \c len bytes to socket \c s from \c buf and return the
|
|
|
|
number of bytes written. The number of bytes can be less than
|
2004-02-29 16:11:17 +00:00
|
|
|
\c len if the remote end disconnected or the internal buffers fill
|
|
|
|
up.
|
2003-01-05 21:48:54 +00:00
|
|
|
*/
|
2003-01-04 22:01:32 +00:00
|
|
|
virtual size_t writeSocket(CArchSocket s,
|
|
|
|
const void* buf, size_t len) = 0;
|
2003-01-05 21:48:54 +00:00
|
|
|
|
|
|
|
//! Check error on socket
|
|
|
|
/*!
|
|
|
|
If the socket \c s is in an error state then throws an appropriate
|
|
|
|
XArchNetwork exception.
|
|
|
|
*/
|
|
|
|
virtual void throwErrorOnSocket(CArchSocket s) = 0;
|
2003-01-04 22:01:32 +00:00
|
|
|
|
|
|
|
//! Turn Nagle algorithm on or off on socket
|
|
|
|
/*!
|
|
|
|
Set socket to send messages immediately (true) or to collect small
|
|
|
|
messages into one packet (false). Returns the previous state.
|
|
|
|
*/
|
|
|
|
virtual bool setNoDelayOnSocket(CArchSocket, bool noDelay) = 0;
|
|
|
|
|
2007-06-17 11:19:18 +00:00
|
|
|
//! Turn address reuse on or off on socket
|
|
|
|
/*!
|
|
|
|
Allows the address this socket is bound to to be reused while in the
|
|
|
|
TIME_WAIT state. Returns the previous state.
|
|
|
|
*/
|
|
|
|
virtual bool setReuseAddrOnSocket(CArchSocket, bool reuse) = 0;
|
|
|
|
|
2003-01-05 21:48:54 +00:00
|
|
|
//! Return local host's name
|
2003-01-04 22:01:32 +00:00
|
|
|
virtual std::string getHostName() = 0;
|
2003-01-05 21:48:54 +00:00
|
|
|
|
|
|
|
//! Create an "any" network address
|
2003-01-04 22:01:32 +00:00
|
|
|
virtual CArchNetAddress newAnyAddr(EAddressFamily) = 0;
|
2003-01-05 21:48:54 +00:00
|
|
|
|
|
|
|
//! Copy a network address
|
2003-01-04 22:01:32 +00:00
|
|
|
virtual CArchNetAddress copyAddr(CArchNetAddress) = 0;
|
2003-01-05 21:48:54 +00:00
|
|
|
|
|
|
|
//! Convert a name to a network address
|
2003-01-04 22:01:32 +00:00
|
|
|
virtual CArchNetAddress nameToAddr(const std::string&) = 0;
|
2003-01-05 21:48:54 +00:00
|
|
|
|
|
|
|
//! Destroy a network address
|
2003-01-04 22:01:32 +00:00
|
|
|
virtual void closeAddr(CArchNetAddress) = 0;
|
2003-01-05 21:48:54 +00:00
|
|
|
|
|
|
|
//! Convert an address to a host name
|
2003-01-04 22:01:32 +00:00
|
|
|
virtual std::string addrToName(CArchNetAddress) = 0;
|
2003-01-05 21:48:54 +00:00
|
|
|
|
|
|
|
//! Convert an address to a string
|
2003-01-04 22:01:32 +00:00
|
|
|
virtual std::string addrToString(CArchNetAddress) = 0;
|
2003-01-05 21:48:54 +00:00
|
|
|
|
|
|
|
//! Get an address's family
|
2003-01-04 22:01:32 +00:00
|
|
|
virtual EAddressFamily getAddrFamily(CArchNetAddress) = 0;
|
2003-01-05 21:48:54 +00:00
|
|
|
|
|
|
|
//! Set the port of an address
|
2003-01-04 22:01:32 +00:00
|
|
|
virtual void setAddrPort(CArchNetAddress, int port) = 0;
|
2003-01-05 21:48:54 +00:00
|
|
|
|
|
|
|
//! Get the port of an address
|
2003-01-04 22:01:32 +00:00
|
|
|
virtual int getAddrPort(CArchNetAddress) = 0;
|
2003-01-05 21:48:54 +00:00
|
|
|
|
2004-02-14 14:04:36 +00:00
|
|
|
//! Test addresses for equality
|
|
|
|
virtual bool isEqualAddr(CArchNetAddress, CArchNetAddress) = 0;
|
|
|
|
|
2003-01-05 21:48:54 +00:00
|
|
|
//! Test for the "any" address
|
|
|
|
/*!
|
|
|
|
Returns true if \c addr is the "any" address. \c newAnyAddr()
|
|
|
|
returns an "any" address.
|
|
|
|
*/
|
|
|
|
virtual bool isAnyAddr(CArchNetAddress addr) = 0;
|
2003-01-04 22:01:32 +00:00
|
|
|
|
|
|
|
//@}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|