mirror of
https://github.com/debauchee/barrier.git
synced 2026-12-01 08:15:25 +08:00
Refactored some platform dependent code into a new library,
lib/arch. This should make porting easier. Will probably continue to refactor a little more, moving platform dependent event handling stuff into lib/platform.
This commit is contained in:
1026
lib/net/CNetwork.cpp
1026
lib/net/CNetwork.cpp
File diff suppressed because it is too large
Load Diff
@@ -1,272 +0,0 @@
|
||||
/*
|
||||
* 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 CNETWORK_H
|
||||
#define CNETWORK_H
|
||||
|
||||
#include "BasicTypes.h"
|
||||
#include "CString.h"
|
||||
#include "stdvector.h"
|
||||
|
||||
#if !defined(HAVE_SOCKLEN_T)
|
||||
// Darwin is so unsure what to use for socklen_t it makes us choose
|
||||
# if defined(__APPLE__)
|
||||
# if !defined(_BSD_SOCKLEN_T_)
|
||||
# define _BSD_SOCKLEN_T_ int
|
||||
# endif
|
||||
# else
|
||||
typedef int socklen_t;
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#if WINDOWS_LIKE
|
||||
// declare no functions in winsock2
|
||||
# define INCL_WINSOCK_API_PROTOTYPES 0
|
||||
# define INCL_WINSOCK_API_TYPEDEFS 0
|
||||
# include <winsock2.h>
|
||||
typedef int ssize_t;
|
||||
# define SELECT_TYPE_ARG1 int
|
||||
# define SELECT_TYPE_ARG234 (fd_set *)
|
||||
# define SELECT_TYPE_ARG5 (struct timeval *)
|
||||
#endif
|
||||
|
||||
#if UNIX_LIKE
|
||||
# if HAVE_SYS_TYPES_H
|
||||
# include <sys/types.h>
|
||||
# endif
|
||||
# if HAVE_SYS_SOCKET_H
|
||||
# include <sys/socket.h>
|
||||
# endif
|
||||
# if HAVE_POLL
|
||||
# include <sys/poll.h>
|
||||
# endif
|
||||
# include <netinet/in.h>
|
||||
# include <netdb.h>
|
||||
# include <errno.h>
|
||||
#endif
|
||||
|
||||
//! Networking functions
|
||||
class CNetwork {
|
||||
public:
|
||||
// platform dependent types
|
||||
#if WINDOWS_LIKE
|
||||
typedef SOCKET Socket;
|
||||
typedef struct sockaddr Address;
|
||||
typedef int AddressLength;
|
||||
typedef short AddressType;
|
||||
typedef struct in_addr InternetAddress;
|
||||
#elif UNIX_LIKE
|
||||
typedef int Socket;
|
||||
typedef struct sockaddr Address;
|
||||
typedef socklen_t AddressLength;
|
||||
typedef int AddressType;
|
||||
typedef struct in_addr InternetAddress;
|
||||
#endif
|
||||
|
||||
#if !HAVE_POLL
|
||||
class PollEntry {
|
||||
public:
|
||||
Socket fd;
|
||||
short events;
|
||||
short revents;
|
||||
};
|
||||
enum {
|
||||
kPOLLIN = 1,
|
||||
kPOLLOUT = 2,
|
||||
kPOLLERR = 4,
|
||||
kPOLLNVAL = 8
|
||||
};
|
||||
#else
|
||||
typedef struct pollfd PollEntry;
|
||||
enum {
|
||||
kPOLLIN = POLLIN,
|
||||
kPOLLOUT = POLLOUT,
|
||||
kPOLLERR = POLLERR,
|
||||
kPOLLNVAL = POLLNVAL
|
||||
};
|
||||
#endif
|
||||
|
||||
//! Host name information
|
||||
class CHostInfo {
|
||||
public:
|
||||
CHostInfo() { }
|
||||
CHostInfo(const struct hostent*);
|
||||
|
||||
void swap(CHostInfo&);
|
||||
|
||||
public:
|
||||
typedef std::vector<CString> AliasList;
|
||||
typedef std::vector<const char*> AddressList;
|
||||
|
||||
CString m_name;
|
||||
AliasList m_aliases;
|
||||
AddressType m_addressType;
|
||||
int m_addressLength;
|
||||
AddressList m_addresses;
|
||||
|
||||
private:
|
||||
std::string m_addressData;
|
||||
};
|
||||
|
||||
//! Network service information
|
||||
class CServiceInfo {
|
||||
public:
|
||||
CServiceInfo() { }
|
||||
CServiceInfo(const struct servent*);
|
||||
|
||||
void swap(CServiceInfo&);
|
||||
|
||||
public:
|
||||
typedef std::vector<CString> AliasList;
|
||||
|
||||
CString m_name;
|
||||
AliasList m_aliases;
|
||||
int m_port;
|
||||
CString m_protocol;
|
||||
};
|
||||
|
||||
//! Network protocol information
|
||||
class CProtocolInfo {
|
||||
public:
|
||||
CProtocolInfo() { }
|
||||
CProtocolInfo(const struct protoent*);
|
||||
|
||||
void swap(CProtocolInfo&);
|
||||
|
||||
public:
|
||||
typedef std::vector<CString> AliasList;
|
||||
|
||||
CString m_name;
|
||||
AliasList m_aliases;
|
||||
int m_protocol;
|
||||
};
|
||||
|
||||
//! @name manipulators
|
||||
//@{
|
||||
|
||||
//! Initialize network subsystem
|
||||
/*!
|
||||
This \b must be called before any other calls to the network subsystem.
|
||||
*/
|
||||
static void init();
|
||||
|
||||
//! Clean up network subsystem
|
||||
/*!
|
||||
This should be called when the network subsystem is no longer needed
|
||||
and no longer in use.
|
||||
*/
|
||||
static void cleanup();
|
||||
|
||||
// byte swapping functions
|
||||
//! Swap bytes to network order
|
||||
static UInt32 swaphtonl(UInt32 hostlong);
|
||||
//! Swap bytes to network order
|
||||
static UInt16 swaphtons(UInt16 hostshort);
|
||||
//! Swap bytes to host order
|
||||
static UInt32 swapntohl(UInt32 netlong);
|
||||
//! Swap bytes to host order
|
||||
static UInt16 swapntohs(UInt16 netshort);
|
||||
|
||||
//@}
|
||||
//! @name constants
|
||||
//@{
|
||||
|
||||
//! The error type
|
||||
static const int Error;
|
||||
//! The non-socket
|
||||
static const Socket Null;
|
||||
|
||||
// getsockerror() constants
|
||||
enum {
|
||||
#if WINDOWS_LIKE
|
||||
kEADDRINUSE = WSAEADDRINUSE,
|
||||
kECONNECTING = WSAEWOULDBLOCK,
|
||||
kEINTR = WSAEINTR,
|
||||
#elif UNIX_LIKE
|
||||
kEADDRINUSE = EADDRINUSE,
|
||||
kECONNECTING = EINPROGRESS,
|
||||
kEINTR = EINTR,
|
||||
#endif
|
||||
kNone = 0
|
||||
};
|
||||
|
||||
// gethosterror() constants
|
||||
enum {
|
||||
#if WINDOWS_LIKE
|
||||
kHOST_NOT_FOUND = WSAHOST_NOT_FOUND,
|
||||
kNO_DATA = WSANO_DATA,
|
||||
kNO_RECOVERY = WSANO_RECOVERY,
|
||||
kTRY_AGAIN = WSATRY_AGAIN,
|
||||
#elif UNIX_LIKE
|
||||
kHOST_NOT_FOUND = HOST_NOT_FOUND,
|
||||
kNO_DATA = NO_DATA,
|
||||
kNO_RECOVERY = NO_RECOVERY,
|
||||
kTRY_AGAIN = TRY_AGAIN,
|
||||
#endif
|
||||
kHOST_OK = 0
|
||||
};
|
||||
|
||||
//@}
|
||||
|
||||
// socket interface (only available after init())
|
||||
|
||||
static Socket accept(Socket s, Address* addr, AddressLength* addrlen);
|
||||
static int bind(Socket s, const Address* addr, AddressLength namelen);
|
||||
static int close(Socket s);
|
||||
static int connect(Socket s, const Address* name, AddressLength namelen);
|
||||
static int gethostname(char* name, int namelen);
|
||||
static int getpeername(Socket s, Address* name, AddressLength* namelen);
|
||||
static int getsockerror(void);
|
||||
static int getsockname(Socket s, Address* name, AddressLength* namelen);
|
||||
static int getsockopt(Socket s, int level, int optname, void* optval, AddressLength* optlen);
|
||||
static int inet_aton(const char* cp, InternetAddress* addr);
|
||||
static CString inet_ntoa(struct in_addr in);
|
||||
static int ioctl(Socket s, int cmd, void* );
|
||||
static int listen(Socket s, int backlog);
|
||||
static int poll(PollEntry[], int nfds, int timeout);
|
||||
static ssize_t read(Socket s, void* buf, size_t len);
|
||||
static ssize_t recv(Socket s, void* buf, size_t len, int flags);
|
||||
static ssize_t recvfrom(Socket s, void* buf, size_t len, int flags, Address* from, AddressLength* fromlen);
|
||||
static ssize_t send(Socket s, const void* buf, size_t len, int flags);
|
||||
static ssize_t sendto(Socket s, const void* buf, size_t len, int flags, const Address* to, AddressLength tolen);
|
||||
static int setsockopt(Socket s, int level, int optname, const void* optval, AddressLength optlen);
|
||||
static int shutdown(Socket s, int how);
|
||||
static Socket socket(int af, int type, int protocol);
|
||||
static ssize_t write(Socket s, const void* buf, size_t len);
|
||||
static int gethostbyaddr(CHostInfo* hostinfo, const char* addr, int len, int type);
|
||||
static int gethostbyname(CHostInfo* hostinfo, const char* name);
|
||||
static int getservbyport(CServiceInfo* servinfo, int port, const char* proto);
|
||||
static int getservbyname(CServiceInfo* servinfo, const char* name, const char* proto);
|
||||
static int getprotobynumber(CProtocolInfo* protoinfo, int proto);
|
||||
static int getprotobyname(CProtocolInfo* protoinfo, const char* name);
|
||||
|
||||
// convenience functions (only available after init())
|
||||
|
||||
//! Set socket to (non-)blocking operation
|
||||
static int setblocking(Socket s, bool blocking);
|
||||
|
||||
//! Turn Nagle algorithm on or off on socket
|
||||
/*!
|
||||
Set socket to send messages immediately (true) or to collect small
|
||||
messages into one packet (false).
|
||||
*/
|
||||
static int setnodelay(Socket s, bool nodelay);
|
||||
|
||||
private:
|
||||
#if WINDOWS_LIKE
|
||||
static void init2(HMODULE);
|
||||
#endif
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -14,53 +14,47 @@
|
||||
|
||||
#include "CNetworkAddress.h"
|
||||
#include "XSocket.h"
|
||||
#include "CArch.h"
|
||||
#include "XArch.h"
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
|
||||
//
|
||||
// CNetworkAddress
|
||||
//
|
||||
|
||||
CNetworkAddress::CNetworkAddress() :
|
||||
m_port(0)
|
||||
m_address(NULL)
|
||||
{
|
||||
// note -- make no calls to CNetwork socket interface here;
|
||||
// we're often called prior to CNetwork::init().
|
||||
|
||||
struct sockaddr_in* inetAddress = reinterpret_cast<
|
||||
struct sockaddr_in*>(&m_address);
|
||||
inetAddress->sin_family = AF_INET;
|
||||
inetAddress->sin_port = CNetwork::swaphtons(m_port);
|
||||
inetAddress->sin_addr.s_addr = INADDR_ANY;
|
||||
memset(inetAddress->sin_zero, 0, sizeof(inetAddress->sin_zero));
|
||||
}
|
||||
|
||||
CNetworkAddress::CNetworkAddress(UInt16 port) :
|
||||
m_port(port)
|
||||
CNetworkAddress::CNetworkAddress(int port)
|
||||
{
|
||||
if (port == 0) {
|
||||
throw XSocketAddress(XSocketAddress::kBadPort, m_hostname, m_port);
|
||||
throw XSocketAddress(XSocketAddress::kBadPort, "", port);
|
||||
}
|
||||
|
||||
struct sockaddr_in* inetAddress = reinterpret_cast<
|
||||
struct sockaddr_in*>(&m_address);
|
||||
inetAddress->sin_family = AF_INET;
|
||||
inetAddress->sin_port = CNetwork::swaphtons(m_port);
|
||||
inetAddress->sin_addr.s_addr = INADDR_ANY;
|
||||
memset(inetAddress->sin_zero, 0, sizeof(inetAddress->sin_zero));
|
||||
m_address = ARCH->newAnyAddr(IArchNetwork::kINET);
|
||||
ARCH->setAddrPort(m_address, port);
|
||||
}
|
||||
|
||||
CNetworkAddress::CNetworkAddress(const CString& hostname_, UInt16 port) :
|
||||
m_hostname(hostname_),
|
||||
m_port(port)
|
||||
CNetworkAddress::CNetworkAddress(const CNetworkAddress& addr) :
|
||||
m_address(ARCH->copyAddr(addr.m_address)),
|
||||
m_hostname(addr.m_hostname)
|
||||
{
|
||||
CString hostname(m_hostname);
|
||||
// do nothing
|
||||
}
|
||||
|
||||
CNetworkAddress::CNetworkAddress(const CString& hostname_, int port) :
|
||||
m_hostname(hostname_)
|
||||
{
|
||||
if (port == 0) {
|
||||
throw XSocketAddress(XSocketAddress::kBadPort, m_hostname, m_port);
|
||||
throw XSocketAddress(XSocketAddress::kBadPort, m_hostname, port);
|
||||
}
|
||||
|
||||
// check for port suffix
|
||||
CString hostname(m_hostname);
|
||||
CString::size_type i = hostname.rfind(':');
|
||||
if (i != CString::npos && i + 1 < hostname.size()) {
|
||||
// found a colon. see if it looks like an IPv6 address.
|
||||
@@ -92,81 +86,77 @@ CNetworkAddress::CNetworkAddress(const CString& hostname_, UInt16 port) :
|
||||
suffixPort <= 0 || suffixPort > 65535) {
|
||||
// bogus port
|
||||
throw XSocketAddress(XSocketAddress::kBadPort,
|
||||
m_hostname, m_port);
|
||||
m_hostname, port);
|
||||
}
|
||||
else {
|
||||
// good port
|
||||
port = static_cast<UInt16>(suffixPort);
|
||||
port = static_cast<int>(suffixPort);
|
||||
hostname.erase(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// if hostname is empty then use wildcard address
|
||||
struct sockaddr_in* inetAddress = reinterpret_cast<
|
||||
struct sockaddr_in*>(&m_address);
|
||||
if (hostname.empty()) {
|
||||
inetAddress->sin_family = AF_INET;
|
||||
inetAddress->sin_port = CNetwork::swaphtons(port);
|
||||
inetAddress->sin_addr.s_addr = INADDR_ANY;
|
||||
memset(inetAddress->sin_zero, 0, sizeof(inetAddress->sin_zero));
|
||||
return;
|
||||
m_address = ARCH->newAnyAddr(IArchNetwork::kINET);
|
||||
ARCH->setAddrPort(m_address, port);
|
||||
}
|
||||
|
||||
// convert dot notation to address
|
||||
if (CNetwork::inet_aton(hostname.c_str(), &inetAddress->sin_addr) != 0) {
|
||||
inetAddress->sin_family = AF_INET;
|
||||
inetAddress->sin_port = CNetwork::swaphtons(port);
|
||||
memset(inetAddress->sin_zero, 0, sizeof(inetAddress->sin_zero));
|
||||
return;
|
||||
else {
|
||||
// look up name
|
||||
try {
|
||||
m_address = ARCH->nameToAddr(hostname);
|
||||
ARCH->setAddrPort(m_address, port);
|
||||
}
|
||||
catch (XArchNetworkNameUnknown&) {
|
||||
throw XSocketAddress(XSocketAddress::kNotFound, hostname, port);
|
||||
}
|
||||
catch (XArchNetworkNameNoAddress&) {
|
||||
throw XSocketAddress(XSocketAddress::kNoAddress, hostname, port);
|
||||
}
|
||||
catch (XArchNetworkName&) {
|
||||
throw XSocketAddress(XSocketAddress::kUnknown, hostname, port);
|
||||
}
|
||||
}
|
||||
|
||||
// look up name
|
||||
CNetwork::CHostInfo hostInfo;
|
||||
switch (CNetwork::gethostbyname(&hostInfo, hostname.c_str())) {
|
||||
case CNetwork::kHOST_OK:
|
||||
break;
|
||||
|
||||
case CNetwork::kHOST_NOT_FOUND:
|
||||
throw XSocketAddress(XSocketAddress::kNotFound, hostname, port);
|
||||
|
||||
case CNetwork::kNO_DATA:
|
||||
throw XSocketAddress(XSocketAddress::kNoAddress, hostname, port);
|
||||
|
||||
case CNetwork::kNO_RECOVERY:
|
||||
case CNetwork::kTRY_AGAIN:
|
||||
default:
|
||||
throw XSocketAddress(XSocketAddress::kUnknown, hostname, port);
|
||||
}
|
||||
|
||||
inetAddress->sin_family = hostInfo.m_addressType;
|
||||
inetAddress->sin_port = CNetwork::swaphtons(port);
|
||||
memcpy(&inetAddress->sin_addr, hostInfo.m_addresses[0],
|
||||
hostInfo.m_addressLength);
|
||||
memset(inetAddress->sin_zero, 0, sizeof(inetAddress->sin_zero));
|
||||
}
|
||||
|
||||
CNetworkAddress::~CNetworkAddress()
|
||||
{
|
||||
// do nothing
|
||||
if (m_address != NULL) {
|
||||
ARCH->closeAddr(m_address);
|
||||
}
|
||||
}
|
||||
|
||||
CNetworkAddress&
|
||||
CNetworkAddress::operator=(const CNetworkAddress& addr)
|
||||
{
|
||||
CArchNetAddress newAddr = NULL;
|
||||
if (addr.m_address != NULL) {
|
||||
newAddr = ARCH->copyAddr(addr.m_address);
|
||||
}
|
||||
if (m_address != NULL) {
|
||||
ARCH->closeAddr(m_address);
|
||||
}
|
||||
m_hostname = addr.m_hostname;
|
||||
m_address = newAddr;
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool
|
||||
CNetworkAddress::isValid() const
|
||||
{
|
||||
return (m_port != 0);
|
||||
return (m_address != NULL);
|
||||
}
|
||||
|
||||
const CNetwork::Address*
|
||||
const CArchNetAddress&
|
||||
CNetworkAddress::getAddress() const
|
||||
{
|
||||
return &m_address;
|
||||
return m_address;
|
||||
}
|
||||
|
||||
CNetwork::AddressLength
|
||||
CNetworkAddress::getAddressLength() const
|
||||
|
||||
int
|
||||
CNetworkAddress::getPort() const
|
||||
{
|
||||
return sizeof(m_address);
|
||||
return (m_address == NULL) ? 0 : ARCH->getAddrPort(m_address);
|
||||
}
|
||||
|
||||
CString
|
||||
@@ -174,9 +164,3 @@ CNetworkAddress::getHostname() const
|
||||
{
|
||||
return m_hostname;
|
||||
}
|
||||
|
||||
UInt16
|
||||
CNetworkAddress::getPort() const
|
||||
{
|
||||
return m_port;
|
||||
}
|
||||
|
||||
@@ -15,9 +15,9 @@
|
||||
#ifndef CNETWORKADDRESS_H
|
||||
#define CNETWORKADDRESS_H
|
||||
|
||||
#include "CNetwork.h"
|
||||
#include "CString.h"
|
||||
#include "BasicTypes.h"
|
||||
#include "IArchNetwork.h"
|
||||
|
||||
//! Network address type
|
||||
/*!
|
||||
@@ -34,7 +34,7 @@ public:
|
||||
Construct the wildcard address with the given port. \c port must
|
||||
not be zero.
|
||||
*/
|
||||
CNetworkAddress(UInt16 port);
|
||||
CNetworkAddress(int port);
|
||||
|
||||
/*!
|
||||
Construct the network address for the given \c hostname and \c port.
|
||||
@@ -44,10 +44,14 @@ public:
|
||||
that suffix is extracted and used as the port, overridding the port
|
||||
parameter. Neither the extracted port or \c port may be zero.
|
||||
*/
|
||||
CNetworkAddress(const CString& hostname, UInt16 port);
|
||||
CNetworkAddress(const CString& hostname, int port);
|
||||
|
||||
CNetworkAddress(const CNetworkAddress&);
|
||||
|
||||
~CNetworkAddress();
|
||||
|
||||
CNetworkAddress& operator=(const CNetworkAddress&);
|
||||
|
||||
//! @name accessors
|
||||
//@{
|
||||
|
||||
@@ -62,14 +66,14 @@ public:
|
||||
Returns the address in the platform's native network address
|
||||
structure.
|
||||
*/
|
||||
const CNetwork::Address* getAddress() const;
|
||||
const CArchNetAddress& getAddress() const;
|
||||
|
||||
//! Get address length
|
||||
//! Get port
|
||||
/*!
|
||||
Returns the length of the address in the platform's native network
|
||||
address structure.
|
||||
Returns the port passed to the c'tor as a suffix to the hostname,
|
||||
if that existed, otherwise as passed directly to the c'tor.
|
||||
*/
|
||||
CNetwork::AddressLength getAddressLength() const;
|
||||
int getPort() const;
|
||||
|
||||
//! Get hostname
|
||||
/*!
|
||||
@@ -77,19 +81,11 @@ public:
|
||||
*/
|
||||
CString getHostname() const;
|
||||
|
||||
//! Get port
|
||||
/*!
|
||||
Returns the port passed to the c'tor as a suffix to the hostname,
|
||||
if that existed, otherwise as passed directly to the c'tor.
|
||||
*/
|
||||
UInt16 getPort() const;
|
||||
|
||||
//@}
|
||||
|
||||
private:
|
||||
CNetwork::Address m_address;
|
||||
CArchNetAddress m_address;
|
||||
CString m_hostname;
|
||||
UInt16 m_port;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -18,6 +18,8 @@
|
||||
#include "XIO.h"
|
||||
#include "XSocket.h"
|
||||
#include "CThread.h"
|
||||
#include "CArch.h"
|
||||
#include "XArch.h"
|
||||
|
||||
//
|
||||
// CTCPListenSocket
|
||||
@@ -25,16 +27,18 @@
|
||||
|
||||
CTCPListenSocket::CTCPListenSocket()
|
||||
{
|
||||
m_fd = CNetwork::socket(PF_INET, SOCK_STREAM, 0);
|
||||
if (m_fd == CNetwork::Null) {
|
||||
throw XSocketCreate();
|
||||
try {
|
||||
m_socket = ARCH->newSocket(IArchNetwork::kINET, IArchNetwork::kSTREAM);
|
||||
}
|
||||
catch (XArchNetwork& e) {
|
||||
throw XSocketCreate(e.what());
|
||||
}
|
||||
}
|
||||
|
||||
CTCPListenSocket::~CTCPListenSocket()
|
||||
{
|
||||
try {
|
||||
close();
|
||||
ARCH->closeSocket(m_socket);
|
||||
}
|
||||
catch (...) {
|
||||
// ignore
|
||||
@@ -44,15 +48,15 @@ CTCPListenSocket::~CTCPListenSocket()
|
||||
void
|
||||
CTCPListenSocket::bind(const CNetworkAddress& addr)
|
||||
{
|
||||
if (CNetwork::bind(m_fd, addr.getAddress(),
|
||||
addr.getAddressLength()) == CNetwork::Error) {
|
||||
if (CNetwork::getsockerror() == CNetwork::kEADDRINUSE) {
|
||||
throw XSocketAddressInUse();
|
||||
}
|
||||
throw XSocketBind();
|
||||
try {
|
||||
ARCH->bindSocket(m_socket, addr.getAddress());
|
||||
ARCH->listenOnSocket(m_socket);
|
||||
}
|
||||
if (CNetwork::listen(m_fd, 3) == CNetwork::Error) {
|
||||
throw XSocketBind();
|
||||
catch (XArchNetworkAddressInUse& e) {
|
||||
throw XSocketAddressInUse(e.what());
|
||||
}
|
||||
catch (XArchNetwork& e) {
|
||||
throw XSocketBind(e.what());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,31 +64,35 @@ IDataSocket*
|
||||
CTCPListenSocket::accept()
|
||||
{
|
||||
// accept asynchronously so we can check for cancellation
|
||||
CNetwork::PollEntry pfds[1];
|
||||
pfds[0].fd = m_fd;
|
||||
pfds[0].events = CNetwork::kPOLLIN;
|
||||
IArchNetwork::CPollEntry pfds[1];
|
||||
pfds[0].m_socket = m_socket;
|
||||
pfds[0].m_events = IArchNetwork::kPOLLIN;
|
||||
for (;;) {
|
||||
CThread::testCancel();
|
||||
const int status = CNetwork::poll(pfds, 1, 10);
|
||||
if (status > 0 && (pfds[0].revents & CNetwork::kPOLLIN) != 0) {
|
||||
CNetwork::Address addr;
|
||||
CNetwork::AddressLength addrlen = sizeof(addr);
|
||||
CNetwork::Socket fd = CNetwork::accept(m_fd, &addr, &addrlen);
|
||||
if (fd != CNetwork::Null) {
|
||||
return new CTCPSocket(fd);
|
||||
ARCH->testCancelThread();
|
||||
try {
|
||||
const int status = ARCH->pollSocket(pfds, 1, 0.01);
|
||||
if (status > 0 &&
|
||||
(pfds[0].m_revents & IArchNetwork::kPOLLIN) != 0) {
|
||||
return new CTCPSocket(ARCH->acceptSocket(m_socket, NULL));
|
||||
}
|
||||
}
|
||||
catch (XArchNetwork&) {
|
||||
// ignore and retry
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
CTCPListenSocket::close()
|
||||
{
|
||||
if (m_fd == CNetwork::Null) {
|
||||
if (m_socket == NULL) {
|
||||
throw XIOClosed();
|
||||
}
|
||||
if (CNetwork::close(m_fd) == CNetwork::Error) {
|
||||
throw XSocketIOClose();
|
||||
try {
|
||||
ARCH->closeSocket(m_socket);
|
||||
m_socket = NULL;
|
||||
}
|
||||
catch (XArchNetwork& e) {
|
||||
throw XSocketIOClose(e.what());
|
||||
}
|
||||
m_fd = CNetwork::Null;
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
#define CTCPLISTENSOCKET_H
|
||||
|
||||
#include "IListenSocket.h"
|
||||
#include "CNetwork.h"
|
||||
#include "IArchNetwork.h"
|
||||
|
||||
//! TCP listen socket
|
||||
/*!
|
||||
@@ -35,7 +35,7 @@ public:
|
||||
virtual IDataSocket* accept();
|
||||
|
||||
private:
|
||||
CNetwork::Socket m_fd;
|
||||
CArchSocket m_socket;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -18,12 +18,12 @@
|
||||
#include "CNetworkAddress.h"
|
||||
#include "XIO.h"
|
||||
#include "XSocket.h"
|
||||
#include "CCondVar.h"
|
||||
#include "CLock.h"
|
||||
#include "CMutex.h"
|
||||
#include "CThread.h"
|
||||
#include "CStopwatch.h"
|
||||
#include "TMethodJob.h"
|
||||
#include "CArch.h"
|
||||
#include "XArch.h"
|
||||
|
||||
//
|
||||
// CTCPSocket
|
||||
@@ -31,17 +31,19 @@
|
||||
|
||||
CTCPSocket::CTCPSocket()
|
||||
{
|
||||
m_fd = CNetwork::socket(PF_INET, SOCK_STREAM, 0);
|
||||
if (m_fd == CNetwork::Null) {
|
||||
throw XSocketCreate();
|
||||
try {
|
||||
m_socket = ARCH->newSocket(IArchNetwork::kINET, IArchNetwork::kSTREAM);
|
||||
}
|
||||
catch (XArchNetwork& e) {
|
||||
throw XSocketCreate(e.what());
|
||||
}
|
||||
init();
|
||||
}
|
||||
|
||||
CTCPSocket::CTCPSocket(CNetwork::Socket fd) :
|
||||
m_fd(fd)
|
||||
CTCPSocket::CTCPSocket(CArchSocket socket) :
|
||||
m_socket(socket)
|
||||
{
|
||||
assert(m_fd != CNetwork::Null);
|
||||
assert(m_socket != NULL);
|
||||
|
||||
init();
|
||||
|
||||
@@ -59,7 +61,7 @@ CTCPSocket::~CTCPSocket()
|
||||
close();
|
||||
}
|
||||
catch (...) {
|
||||
// ignore failures
|
||||
// ignore
|
||||
}
|
||||
|
||||
// clean up
|
||||
@@ -71,12 +73,14 @@ CTCPSocket::~CTCPSocket()
|
||||
void
|
||||
CTCPSocket::bind(const CNetworkAddress& addr)
|
||||
{
|
||||
if (CNetwork::bind(m_fd, addr.getAddress(),
|
||||
addr.getAddressLength()) == CNetwork::Error) {
|
||||
if (errno == CNetwork::kEADDRINUSE) {
|
||||
throw XSocketAddressInUse();
|
||||
}
|
||||
throw XSocketBind();
|
||||
try {
|
||||
ARCH->bindSocket(m_socket, addr.getAddress());
|
||||
}
|
||||
catch (XArchNetworkAddressInUse& e) {
|
||||
throw XSocketAddressInUse(e.what());
|
||||
}
|
||||
catch (XArchNetwork& e) {
|
||||
throw XSocketBind(e.what());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,12 +100,21 @@ CTCPSocket::close()
|
||||
}
|
||||
|
||||
// cause ioThread to exit
|
||||
{
|
||||
if (m_socket != NULL) {
|
||||
CLock lock(m_mutex);
|
||||
if (m_fd != CNetwork::Null) {
|
||||
CNetwork::shutdown(m_fd, 2);
|
||||
m_connected = kClosed;
|
||||
try {
|
||||
ARCH->closeSocketForRead(m_socket);
|
||||
}
|
||||
catch (XArchNetwork&) {
|
||||
// ignore
|
||||
}
|
||||
try {
|
||||
ARCH->closeSocketForWrite(m_socket);
|
||||
}
|
||||
catch (XArchNetwork&) {
|
||||
// ignore
|
||||
}
|
||||
m_connected = kClosed;
|
||||
}
|
||||
|
||||
// wait for thread
|
||||
@@ -112,67 +125,71 @@ CTCPSocket::close()
|
||||
}
|
||||
|
||||
// close socket
|
||||
if (m_fd != CNetwork::Null) {
|
||||
if (CNetwork::close(m_fd) == CNetwork::Error) {
|
||||
throw XSocketIOClose();
|
||||
if (m_socket != NULL) {
|
||||
try {
|
||||
ARCH->closeSocket(m_socket);
|
||||
m_socket = NULL;
|
||||
}
|
||||
catch (XArchNetwork& e) {
|
||||
throw XSocketIOClose(e.what());
|
||||
}
|
||||
m_fd = CNetwork::Null;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
CTCPSocket::connect(const CNetworkAddress& addr)
|
||||
{
|
||||
// connect asynchronously so we can check for cancellation
|
||||
CNetwork::setblocking(m_fd, false);
|
||||
if (CNetwork::connect(m_fd, addr.getAddress(),
|
||||
addr.getAddressLength()) == CNetwork::Error) {
|
||||
// check for failure
|
||||
if (CNetwork::getsockerror() != CNetwork::kECONNECTING) {
|
||||
XSocketConnect e;
|
||||
CNetwork::setblocking(m_fd, true);
|
||||
throw e;
|
||||
do {
|
||||
// connect asynchronously so we can check for cancellation.
|
||||
// we can't wrap setting and resetting the blocking flag in
|
||||
// the c'tor/d'tor of a class (to make resetting automatic)
|
||||
// because setBlockingOnSocket() can throw and it might be
|
||||
// called while unwinding the stack due to a throw.
|
||||
try {
|
||||
ARCH->setBlockingOnSocket(m_socket, false);
|
||||
ARCH->connectSocket(m_socket, addr.getAddress());
|
||||
ARCH->setBlockingOnSocket(m_socket, true);
|
||||
|
||||
// connected
|
||||
break;
|
||||
}
|
||||
catch (XArchNetworkConnecting&) {
|
||||
// connection is in progress
|
||||
ARCH->setBlockingOnSocket(m_socket, true);
|
||||
}
|
||||
catch (XArchNetwork& e) {
|
||||
ARCH->setBlockingOnSocket(m_socket, true);
|
||||
throw XSocketConnect(e.what());
|
||||
}
|
||||
|
||||
// wait for connection or failure
|
||||
CNetwork::PollEntry pfds[1];
|
||||
pfds[0].fd = m_fd;
|
||||
pfds[0].events = CNetwork::kPOLLOUT;
|
||||
IArchNetwork::CPollEntry pfds[1];
|
||||
pfds[0].m_socket = m_socket;
|
||||
pfds[0].m_events = IArchNetwork::kPOLLOUT;
|
||||
for (;;) {
|
||||
CThread::testCancel();
|
||||
const int status = CNetwork::poll(pfds, 1, 10);
|
||||
if (status > 0) {
|
||||
if ((pfds[0].revents & (CNetwork::kPOLLERR |
|
||||
CNetwork::kPOLLNVAL)) != 0) {
|
||||
// connection failed
|
||||
int error = 0;
|
||||
CNetwork::AddressLength size = sizeof(error);
|
||||
CNetwork::setblocking(m_fd, true);
|
||||
CNetwork::getsockopt(m_fd, SOL_SOCKET, SO_ERROR,
|
||||
reinterpret_cast<char*>(&error), &size);
|
||||
throw XSocketConnect(error);
|
||||
}
|
||||
if ((pfds[0].revents & CNetwork::kPOLLOUT) != 0) {
|
||||
int error;
|
||||
CNetwork::AddressLength size = sizeof(error);
|
||||
if (CNetwork::getsockopt(m_fd, SOL_SOCKET, SO_ERROR,
|
||||
reinterpret_cast<char*>(&error),
|
||||
&size) == CNetwork::Error ||
|
||||
error != 0) {
|
||||
ARCH->testCancelThread();
|
||||
try {
|
||||
const int status = ARCH->pollSocket(pfds, 1, 0.01);
|
||||
if (status > 0) {
|
||||
if ((pfds[0].m_revents & (IArchNetwork::kPOLLERR |
|
||||
IArchNetwork::kPOLLNVAL)) != 0) {
|
||||
// connection failed
|
||||
CNetwork::setblocking(m_fd, true);
|
||||
throw XSocketConnect(error);
|
||||
ARCH->throwErrorOnSocket(m_socket);
|
||||
}
|
||||
if ((pfds[0].m_revents & IArchNetwork::kPOLLOUT) != 0) {
|
||||
// connection may have failed or succeeded
|
||||
ARCH->throwErrorOnSocket(m_socket);
|
||||
|
||||
// connected!
|
||||
break;
|
||||
// connected!
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (XArchNetwork& e) {
|
||||
throw XSocketConnect(e.what());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// back to blocking
|
||||
CNetwork::setblocking(m_fd, true);
|
||||
} while (false);
|
||||
|
||||
// start servicing the socket
|
||||
m_connected = kReadWrite;
|
||||
@@ -208,7 +225,20 @@ CTCPSocket::init()
|
||||
// turn off Nagle algorithm. we send lots of very short messages
|
||||
// that should be sent without (much) delay. for example, the
|
||||
// mouse motion messages are much less useful if they're delayed.
|
||||
CNetwork::setnodelay(m_fd, true);
|
||||
// FIXME -- the client should do this
|
||||
try {
|
||||
ARCH->setNoDelayOnSocket(m_socket, true);
|
||||
}
|
||||
catch (XArchNetwork& e) {
|
||||
try {
|
||||
ARCH->closeSocket(m_socket);
|
||||
m_socket = NULL;
|
||||
}
|
||||
catch (XArchNetwork&) {
|
||||
// ignore
|
||||
}
|
||||
throw XSocketCreate(e.what());
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
@@ -244,85 +274,81 @@ CTCPSocket::ioCleanup()
|
||||
void
|
||||
CTCPSocket::ioService()
|
||||
{
|
||||
assert(m_fd != CNetwork::Null);
|
||||
assert(m_socket != NULL);
|
||||
|
||||
// now service the connection
|
||||
CNetwork::PollEntry pfds[1];
|
||||
pfds[0].fd = m_fd;
|
||||
IArchNetwork::CPollEntry pfds[1];
|
||||
pfds[0].m_socket = m_socket;
|
||||
for (;;) {
|
||||
{
|
||||
// choose events to poll for
|
||||
CLock lock(m_mutex);
|
||||
pfds[0].events = 0;
|
||||
pfds[0].m_events = 0;
|
||||
if (m_connected == 0) {
|
||||
return;
|
||||
}
|
||||
if ((m_connected & kRead) != 0) {
|
||||
// still open for reading
|
||||
pfds[0].events |= CNetwork::kPOLLIN;
|
||||
pfds[0].m_events |= IArchNetwork::kPOLLIN;
|
||||
}
|
||||
if ((m_connected & kWrite) != 0 && m_output->getSize() > 0) {
|
||||
// data queued for writing
|
||||
pfds[0].events |= CNetwork::kPOLLOUT;
|
||||
pfds[0].m_events |= IArchNetwork::kPOLLOUT;
|
||||
}
|
||||
}
|
||||
|
||||
// check for status
|
||||
const int status = CNetwork::poll(pfds, 1, 10);
|
||||
try {
|
||||
// check for status
|
||||
const int status = ARCH->pollSocket(pfds, 1, 0.01);
|
||||
|
||||
// transfer data and handle errors
|
||||
if (status == 1) {
|
||||
if ((pfds[0].revents & (CNetwork::kPOLLERR |
|
||||
CNetwork::kPOLLNVAL)) != 0) {
|
||||
// stream is no good anymore so bail
|
||||
m_input->hangup();
|
||||
return;
|
||||
}
|
||||
|
||||
// read some data
|
||||
if (pfds[0].revents & CNetwork::kPOLLIN) {
|
||||
UInt8 buffer[4096];
|
||||
ssize_t n = CNetwork::read(m_fd, buffer, sizeof(buffer));
|
||||
if (n > 0) {
|
||||
// transfer data and handle errors
|
||||
if (status == 1) {
|
||||
if ((pfds[0].m_revents & (IArchNetwork::kPOLLERR |
|
||||
IArchNetwork::kPOLLNVAL)) != 0) {
|
||||
// stream is no good anymore so bail
|
||||
CLock lock(m_mutex);
|
||||
m_input->write(buffer, n);
|
||||
}
|
||||
else if (n == 0) {
|
||||
// stream hungup
|
||||
m_input->hangup();
|
||||
m_connected &= ~kRead;
|
||||
return;
|
||||
}
|
||||
else {
|
||||
// socket failed
|
||||
if (CNetwork::getsockerror() != CNetwork::kEINTR) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// write some data
|
||||
if (pfds[0].revents & CNetwork::kPOLLOUT) {
|
||||
CLock lock(m_mutex);
|
||||
|
||||
// get amount of data to write
|
||||
UInt32 n = m_output->getSize();
|
||||
|
||||
// write data
|
||||
const void* buffer = m_output->peek(n);
|
||||
ssize_t n2 = (UInt32)CNetwork::write(m_fd, buffer, n);
|
||||
|
||||
// discard written data
|
||||
if (n2 > 0) {
|
||||
m_output->pop(n);
|
||||
}
|
||||
else if (n2 < 0) {
|
||||
// socket failed
|
||||
if (CNetwork::getsockerror() != CNetwork::kEINTR) {
|
||||
return;
|
||||
|
||||
// read some data
|
||||
if (pfds[0].m_revents & IArchNetwork::kPOLLIN) {
|
||||
UInt8 buffer[4096];
|
||||
size_t n = ARCH->readSocket(m_socket,
|
||||
buffer, sizeof(buffer));
|
||||
CLock lock(m_mutex);
|
||||
if (n > 0) {
|
||||
m_input->write(buffer, n);
|
||||
}
|
||||
else {
|
||||
// stream hungup
|
||||
m_input->hangup();
|
||||
m_connected &= ~kRead;
|
||||
}
|
||||
}
|
||||
|
||||
// write some data
|
||||
if (pfds[0].m_revents & IArchNetwork::kPOLLOUT) {
|
||||
CLock lock(m_mutex);
|
||||
|
||||
// get amount of data to write
|
||||
UInt32 n = m_output->getSize();
|
||||
|
||||
// write data
|
||||
const void* buffer = m_output->peek(n);
|
||||
size_t n2 = ARCH->writeSocket(m_socket, buffer, n);
|
||||
|
||||
// discard written data
|
||||
if (n2 > 0) {
|
||||
m_output->pop(n2);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (XArchNetwork&) {
|
||||
// socket has failed
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -330,14 +356,24 @@ void
|
||||
CTCPSocket::closeInput(void*)
|
||||
{
|
||||
// note -- m_mutex should already be locked
|
||||
CNetwork::shutdown(m_fd, 0);
|
||||
m_connected &= ~kRead;
|
||||
try {
|
||||
ARCH->closeSocketForRead(m_socket);
|
||||
m_connected &= ~kRead;
|
||||
}
|
||||
catch (XArchNetwork&) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
CTCPSocket::closeOutput(void*)
|
||||
{
|
||||
// note -- m_mutex should already be locked
|
||||
CNetwork::shutdown(m_fd, 1);
|
||||
m_connected &= ~kWrite;
|
||||
try {
|
||||
ARCH->closeSocketForWrite(m_socket);
|
||||
m_connected &= ~kWrite;
|
||||
}
|
||||
catch (XArchNetwork&) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,11 +16,10 @@
|
||||
#define CTCPSOCKET_H
|
||||
|
||||
#include "IDataSocket.h"
|
||||
#include "CNetwork.h"
|
||||
#include "BasicTypes.h"
|
||||
#include "IArchNetwork.h"
|
||||
|
||||
class CMutex;
|
||||
template <class T>
|
||||
class CCondVar;
|
||||
class CThread;
|
||||
class CBufferedInputStream;
|
||||
class CBufferedOutputStream;
|
||||
@@ -32,7 +31,7 @@ A data socket using TCP.
|
||||
class CTCPSocket : public IDataSocket {
|
||||
public:
|
||||
CTCPSocket();
|
||||
CTCPSocket(CNetwork::Socket);
|
||||
CTCPSocket(CArchSocket);
|
||||
~CTCPSocket();
|
||||
|
||||
// ISocket overrides
|
||||
@@ -55,7 +54,7 @@ private:
|
||||
private:
|
||||
enum { kClosed = 0, kRead = 1, kWrite = 2, kReadWrite = 3 };
|
||||
|
||||
CNetwork::Socket m_fd;
|
||||
CArchSocket m_socket;
|
||||
CBufferedInputStream* m_input;
|
||||
CBufferedOutputStream* m_output;
|
||||
|
||||
|
||||
@@ -25,14 +25,11 @@ MAINTAINERCLEANFILES = \
|
||||
|
||||
noinst_LIBRARIES = libnet.a
|
||||
libnet_a_SOURCES = \
|
||||
CNetwork.cpp \
|
||||
CNetworkAddress.cpp \
|
||||
CTCPListenSocket.cpp \
|
||||
CTCPSocket.cpp \
|
||||
CTCPSocketFactory.cpp \
|
||||
XNetwork.cpp \
|
||||
XSocket.cpp \
|
||||
CNetwork.h \
|
||||
CNetworkAddress.h \
|
||||
CTCPListenSocket.h \
|
||||
CTCPSocket.h \
|
||||
@@ -41,10 +38,11 @@ libnet_a_SOURCES = \
|
||||
IListenSocket.h \
|
||||
ISocket.h \
|
||||
ISocketFactory.h \
|
||||
XNetwork.h \
|
||||
XSocket.h \
|
||||
$(NULL)
|
||||
INCLUDES = \
|
||||
-I$(VDEPTH)/lib/common \
|
||||
-I$(VDEPTH)/lib/arch \
|
||||
-I$(VDEPTH)/lib/base \
|
||||
-I$(VDEPTH)/lib/mt \
|
||||
-I$(VDEPTH)/lib/io \
|
||||
|
||||
@@ -1,93 +0,0 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include "XNetwork.h"
|
||||
|
||||
//
|
||||
// XNetworkUnavailable
|
||||
//
|
||||
|
||||
CString
|
||||
XNetworkUnavailable::getWhat() const throw()
|
||||
{
|
||||
return format("XNetworkUnavailable", "network library is not available");
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// XNetworkFailed
|
||||
//
|
||||
|
||||
CString
|
||||
XNetworkFailed::getWhat() const throw()
|
||||
{
|
||||
return format("XNetworkFailed", "cannot initialize network library");
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// XNetworkVersion
|
||||
//
|
||||
|
||||
XNetworkVersion::XNetworkVersion(int major, int minor) throw() :
|
||||
m_major(major),
|
||||
m_minor(minor)
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
|
||||
int
|
||||
XNetworkVersion::getMajor() const throw()
|
||||
{
|
||||
return m_major;
|
||||
}
|
||||
|
||||
int
|
||||
XNetworkVersion::getMinor() const throw()
|
||||
{
|
||||
return m_minor;
|
||||
}
|
||||
|
||||
CString
|
||||
XNetworkVersion::getWhat() const throw()
|
||||
{
|
||||
return format("XNetworkVersion",
|
||||
"unsupported network version %{1}.%{2}",
|
||||
CStringUtil::print("%d", m_major).c_str(),
|
||||
CStringUtil::print("%d", m_minor).c_str());
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// XNetworkFunctionUnavailable
|
||||
//
|
||||
|
||||
XNetworkFunctionUnavailable::XNetworkFunctionUnavailable(
|
||||
const char* name) throw()
|
||||
{
|
||||
try {
|
||||
m_name = name;
|
||||
}
|
||||
catch (...) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
||||
CString
|
||||
XNetworkFunctionUnavailable::getWhat() const throw()
|
||||
{
|
||||
return format("XNetworkFunctionUnavailable",
|
||||
"missing network function %{1}",
|
||||
m_name.c_str());
|
||||
}
|
||||
@@ -1,94 +0,0 @@
|
||||
/*
|
||||
* 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 XNETWORK_H
|
||||
#define XNETWORK_H
|
||||
|
||||
#include "XBase.h"
|
||||
#include "CString.h"
|
||||
|
||||
//! Generic network exception
|
||||
/*!
|
||||
Network exceptions are thrown when initializing the network subsystem
|
||||
and not during normal network use.
|
||||
*/
|
||||
class XNetwork : public XBase { };
|
||||
|
||||
//! Network subsystem not available exception
|
||||
/*!
|
||||
Thrown when the network subsystem is unavailable, typically because
|
||||
the necessary shared library is unavailable.
|
||||
*/
|
||||
class XNetworkUnavailable : public XNetwork {
|
||||
protected:
|
||||
// XBase overrides
|
||||
virtual CString getWhat() const throw();
|
||||
};
|
||||
|
||||
//! Network subsystem failed exception
|
||||
/*!
|
||||
Thrown when the network subsystem cannot be initialized.
|
||||
*/
|
||||
class XNetworkFailed : public XNetwork {
|
||||
protected:
|
||||
// XBase overrides
|
||||
virtual CString getWhat() const throw();
|
||||
};
|
||||
|
||||
//! Network subsystem vesion unsupported exception
|
||||
/*!
|
||||
Thrown when the network subsystem has a version incompatible with
|
||||
what's expected.
|
||||
*/
|
||||
class XNetworkVersion : public XNetwork {
|
||||
public:
|
||||
XNetworkVersion(int major, int minor) throw();
|
||||
|
||||
//! @name accessors
|
||||
//@{
|
||||
|
||||
//! Get the network subsystem's major version
|
||||
int getMajor() const throw();
|
||||
//! Get the network subsystem's minor version
|
||||
int getMinor() const throw();
|
||||
|
||||
//@}
|
||||
|
||||
protected:
|
||||
// XBase overrides
|
||||
virtual CString getWhat() const throw();
|
||||
|
||||
private:
|
||||
int m_major;
|
||||
int m_minor;
|
||||
};
|
||||
|
||||
//! Network subsystem incomplete exception
|
||||
/*!
|
||||
Thrown when the network subsystem is missing an expected and required
|
||||
function.
|
||||
*/
|
||||
class XNetworkFunctionUnavailable : public XNetwork {
|
||||
public:
|
||||
XNetworkFunctionUnavailable(const char* name) throw();
|
||||
|
||||
protected:
|
||||
// XBase overrides
|
||||
virtual CString getWhat() const throw();
|
||||
|
||||
private:
|
||||
CString m_name;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -13,14 +13,14 @@
|
||||
*/
|
||||
|
||||
#include "XSocket.h"
|
||||
#include "CNetwork.h"
|
||||
#include "CStringUtil.h"
|
||||
|
||||
//
|
||||
// XSocketAddress
|
||||
//
|
||||
|
||||
XSocketAddress::XSocketAddress(EError error,
|
||||
const CString& hostname, UInt16 port) throw() :
|
||||
const CString& hostname, int port) throw() :
|
||||
m_error(error),
|
||||
m_hostname(hostname),
|
||||
m_port(port)
|
||||
@@ -40,7 +40,7 @@ XSocketAddress::getHostname() const throw()
|
||||
return m_hostname;
|
||||
}
|
||||
|
||||
UInt16
|
||||
int
|
||||
XSocketAddress::getPort() const throw()
|
||||
{
|
||||
return m_port;
|
||||
@@ -67,97 +67,6 @@ XSocketAddress::getWhat() const throw()
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// XSocketErrno
|
||||
//
|
||||
|
||||
XSocketErrno::XSocketErrno() :
|
||||
MXErrno(CNetwork::getsockerror())
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
|
||||
XSocketErrno::XSocketErrno(int err) :
|
||||
MXErrno(err)
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
|
||||
const char*
|
||||
XSocketErrno::getErrstr() const
|
||||
{
|
||||
#if WINDOWS_LIKE
|
||||
// built-in windows function for looking up error message strings
|
||||
// may not look up network error messages correctly. we'll have
|
||||
// to do it ourself.
|
||||
static const struct { int m_code; const char* m_msg; } s_netErrorCodes[] = {
|
||||
/* 10004 */{WSAEINTR, "The (blocking) call was canceled via WSACancelBlockingCall"},
|
||||
/* 10009 */{WSAEBADF, "Bad file handle"},
|
||||
/* 10013 */{WSAEACCES, "The requested address is a broadcast address, but the appropriate flag was not set"},
|
||||
/* 10014 */{WSAEFAULT, "WSAEFAULT"},
|
||||
/* 10022 */{WSAEINVAL, "WSAEINVAL"},
|
||||
/* 10024 */{WSAEMFILE, "No more file descriptors available"},
|
||||
/* 10035 */{WSAEWOULDBLOCK, "Socket is marked as non-blocking and no connections are present or the receive operation would block"},
|
||||
/* 10036 */{WSAEINPROGRESS, "A blocking Windows Sockets operation is in progress"},
|
||||
/* 10037 */{WSAEALREADY, "The asynchronous routine being canceled has already completed"},
|
||||
/* 10038 */{WSAENOTSOCK, "At least on descriptor is not a socket"},
|
||||
/* 10039 */{WSAEDESTADDRREQ, "A destination address is required"},
|
||||
/* 10040 */{WSAEMSGSIZE, "The datagram was too large to fit into the specified buffer and was truncated"},
|
||||
/* 10041 */{WSAEPROTOTYPE, "The specified protocol is the wrong type for this socket"},
|
||||
/* 10042 */{WSAENOPROTOOPT, "The option is unknown or unsupported"},
|
||||
/* 10043 */{WSAEPROTONOSUPPORT,"The specified protocol is not supported"},
|
||||
/* 10044 */{WSAESOCKTNOSUPPORT,"The specified socket type is not supported by this address family"},
|
||||
/* 10045 */{WSAEOPNOTSUPP, "The referenced socket is not a type that supports that operation"},
|
||||
/* 10046 */{WSAEPFNOSUPPORT, "BSD: Protocol family not supported"},
|
||||
/* 10047 */{WSAEAFNOSUPPORT, "The specified address family is not supported"},
|
||||
/* 10048 */{WSAEADDRINUSE, "The specified address is already in use"},
|
||||
/* 10049 */{WSAEADDRNOTAVAIL, "The specified address is not available from the local machine"},
|
||||
/* 10050 */{WSAENETDOWN, "The Windows Sockets implementation has detected that the network subsystem has failed"},
|
||||
/* 10051 */{WSAENETUNREACH, "The network can't be reached from this hos at this time"},
|
||||
/* 10052 */{WSAENETRESET, "The connection must be reset because the Windows Sockets implementation dropped it"},
|
||||
/* 10053 */{WSAECONNABORTED, "The virtual circuit was aborted due to timeout or other failure"},
|
||||
/* 10054 */{WSAECONNRESET, "The virtual circuit was reset by the remote side"},
|
||||
/* 10055 */{WSAENOBUFS, "No buffer space is available or a buffer deadlock has occured. The socket cannot be created"},
|
||||
/* 10056 */{WSAEISCONN, "The socket is already connected"},
|
||||
/* 10057 */{WSAENOTCONN, "The socket is not connected"},
|
||||
/* 10058 */{WSAESHUTDOWN, "The socket has been shutdown"},
|
||||
/* 10059 */{WSAETOOMANYREFS, "BSD: Too many references"},
|
||||
/* 10060 */{WSAETIMEDOUT, "Attempt to connect timed out without establishing a connection"},
|
||||
/* 10061 */{WSAECONNREFUSED, "The attempt to connect was forcefully rejected"},
|
||||
/* 10062 */{WSAELOOP, "Undocumented WinSock error code used in BSD"},
|
||||
/* 10063 */{WSAENAMETOOLONG, "Undocumented WinSock error code used in BSD"},
|
||||
/* 10064 */{WSAEHOSTDOWN, "Undocumented WinSock error code used in BSD"},
|
||||
/* 10065 */{WSAEHOSTUNREACH, "No route to host"},
|
||||
/* 10066 */{WSAENOTEMPTY, "Undocumented WinSock error code"},
|
||||
/* 10067 */{WSAEPROCLIM, "Undocumented WinSock error code"},
|
||||
/* 10068 */{WSAEUSERS, "Undocumented WinSock error code"},
|
||||
/* 10069 */{WSAEDQUOT, "Undocumented WinSock error code"},
|
||||
/* 10070 */{WSAESTALE, "Undocumented WinSock error code"},
|
||||
/* 10071 */{WSAEREMOTE, "Undocumented WinSock error code"},
|
||||
/* 10091 */{WSASYSNOTREADY, "Underlying network subsytem is not ready for network communication"},
|
||||
/* 10092 */{WSAVERNOTSUPPORTED, "The version of WinSock API support requested is not provided in this implementation"},
|
||||
/* 10093 */{WSANOTINITIALISED, "WinSock subsystem not properly initialized"},
|
||||
/* 10101 */{WSAEDISCON, "Virtual circuit has gracefully terminated connection"},
|
||||
/* 11001 */{WSAHOST_NOT_FOUND, "Host not found"},
|
||||
/* 11002 */{WSATRY_AGAIN, "Host not found"},
|
||||
/* 11003 */{WSANO_RECOVERY, "Host name lookup error"},
|
||||
/* 11004 */{WSANO_DATA, "No data for host"},
|
||||
/* end */{0, NULL}
|
||||
};
|
||||
|
||||
const int err = getErrno();
|
||||
for (unsigned int i = 0; s_netErrorCodes[i].m_code != 0; ++i) {
|
||||
if (s_netErrorCodes[i].m_code == err) {
|
||||
return s_netErrorCodes[i].m_msg;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
// not a network error code. fallback to system error message.
|
||||
return MXErrno::getErrstr();
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// XSocketIOClose
|
||||
//
|
||||
@@ -165,8 +74,7 @@ XSocketErrno::getErrstr() const
|
||||
CString
|
||||
XSocketIOClose::getWhat() const throw()
|
||||
{
|
||||
return format("XSocketIOClose", "close: %{1}",
|
||||
getErrstr());
|
||||
return format("XSocketIOClose", "close: %{1}", what());
|
||||
}
|
||||
|
||||
|
||||
@@ -177,8 +85,7 @@ XSocketIOClose::getWhat() const throw()
|
||||
CString
|
||||
XSocketBind::getWhat() const throw()
|
||||
{
|
||||
return format("XSocketBind", "cannot bind address: %{1}",
|
||||
getErrstr());
|
||||
return format("XSocketBind", "cannot bind address: %{1}", what());
|
||||
}
|
||||
|
||||
|
||||
@@ -189,8 +96,7 @@ XSocketBind::getWhat() const throw()
|
||||
CString
|
||||
XSocketConnect::getWhat() const throw()
|
||||
{
|
||||
return format("XSocketConnect", "cannot connect socket: %{1}",
|
||||
getErrstr());
|
||||
return format("XSocketConnect", "cannot connect socket: %{1}", what());
|
||||
}
|
||||
|
||||
|
||||
@@ -201,6 +107,5 @@ XSocketConnect::getWhat() const throw()
|
||||
CString
|
||||
XSocketCreate::getWhat() const throw()
|
||||
{
|
||||
return format("XSocketCreate", "cannot create socket: %{1}",
|
||||
getErrstr());
|
||||
return format("XSocketCreate", "cannot create socket: %{1}", what());
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
#include "BasicTypes.h"
|
||||
|
||||
//! Generic socket exception
|
||||
class XSocket : public XBase { };
|
||||
XBASE_SUBCLASS(XSocket, XBase);
|
||||
|
||||
//! Socket bad address exception
|
||||
/*!
|
||||
@@ -37,7 +37,7 @@ public:
|
||||
kBadPort //!< The port is invalid
|
||||
};
|
||||
|
||||
XSocketAddress(EError, const CString& hostname, UInt16 port) throw();
|
||||
XSocketAddress(EError, const CString& hostname, int port) throw();
|
||||
|
||||
//! @name accessors
|
||||
//@{
|
||||
@@ -47,7 +47,7 @@ public:
|
||||
//! Get the hostname
|
||||
CString getHostname() const throw();
|
||||
//! Get the port
|
||||
UInt16 getPort() const throw();
|
||||
int getPort() const throw();
|
||||
|
||||
//@}
|
||||
|
||||
@@ -58,80 +58,38 @@ protected:
|
||||
private:
|
||||
EError m_error;
|
||||
CString m_hostname;
|
||||
UInt16 m_port;
|
||||
};
|
||||
|
||||
//! Generic socket exception using \c errno
|
||||
class XSocketErrno : public MXErrno {
|
||||
public:
|
||||
XSocketErrno();
|
||||
XSocketErrno(int);
|
||||
|
||||
// MXErrno overrides
|
||||
virtual const char* getErrstr() const;
|
||||
int m_port;
|
||||
};
|
||||
|
||||
//! I/O closing exception
|
||||
/*!
|
||||
Thrown if a stream cannot be closed.
|
||||
*/
|
||||
class XSocketIOClose : public XIOClose, public XSocketErrno {
|
||||
protected:
|
||||
// XBase overrides
|
||||
virtual CString getWhat() const throw();
|
||||
};
|
||||
XBASE_SUBCLASS_FORMAT(XSocketIOClose, XIOClose);
|
||||
|
||||
//! Socket cannot bind address exception
|
||||
/*!
|
||||
Thrown when a socket cannot be bound to an address.
|
||||
*/
|
||||
class XSocketBind : public XSocket, public XSocketErrno {
|
||||
public:
|
||||
XSocketBind() { }
|
||||
XSocketBind(int e) : XSocketErrno(e) { }
|
||||
|
||||
protected:
|
||||
// XBase overrides
|
||||
virtual CString getWhat() const throw();
|
||||
};
|
||||
XBASE_SUBCLASS_FORMAT(XSocketBind, XSocket);
|
||||
|
||||
//! Socket address in use exception
|
||||
/*!
|
||||
Thrown when a socket cannot be bound to an address because the address
|
||||
is already in use.
|
||||
*/
|
||||
class XSocketAddressInUse : public XSocketBind {
|
||||
public:
|
||||
XSocketAddressInUse() { }
|
||||
XSocketAddressInUse(int e) : XSocketBind(e) { }
|
||||
};
|
||||
XBASE_SUBCLASS(XSocketAddressInUse, XSocketBind);
|
||||
|
||||
//! Cannot connect socket exception
|
||||
/*!
|
||||
Thrown when a socket cannot connect to a remote endpoint.
|
||||
*/
|
||||
class XSocketConnect : public XSocket, public XSocketErrno {
|
||||
public:
|
||||
XSocketConnect() { }
|
||||
XSocketConnect(int e) : XSocketErrno(e) { }
|
||||
|
||||
protected:
|
||||
// XBase overrides
|
||||
virtual CString getWhat() const throw();
|
||||
};
|
||||
XBASE_SUBCLASS_FORMAT(XSocketConnect, XSocket);
|
||||
|
||||
//! Cannot create socket exception
|
||||
/*!
|
||||
Thrown when a socket cannot be created (by the operating system).
|
||||
*/
|
||||
class XSocketCreate : public XSocket, public XSocketErrno {
|
||||
public:
|
||||
XSocketCreate() { }
|
||||
XSocketCreate(int e) : XSocketErrno(e) { }
|
||||
|
||||
protected:
|
||||
// XBase overrides
|
||||
virtual CString getWhat() const throw();
|
||||
};
|
||||
XBASE_SUBCLASS_FORMAT(XSocketCreate, XSocket);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -41,7 +41,7 @@ RSC=rc.exe
|
||||
# PROP Intermediate_Dir "Release"
|
||||
# PROP Target_Dir ""
|
||||
# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_MBCS" /D "_LIB" /YX /FD /c
|
||||
# ADD CPP /nologo /MT /W4 /GX /O2 /I "..\base" /I "..\io" /I "..\mt" /D "WIN32" /D "NDEBUG" /D "_MBCS" /D "_LIB" /FD /c
|
||||
# ADD CPP /nologo /MT /W4 /GX /O2 /I "..\common" /I "..\arch" /I "..\base" /I "..\io" /I "..\mt" /D "WIN32" /D "NDEBUG" /D "_MBCS" /D "_LIB" /FD /c
|
||||
# SUBTRACT CPP /YX
|
||||
# ADD BASE RSC /l 0x409 /d "NDEBUG"
|
||||
# ADD RSC /l 0x409 /d "NDEBUG"
|
||||
@@ -65,7 +65,7 @@ LIB32=link.exe -lib
|
||||
# PROP Intermediate_Dir "Debug"
|
||||
# PROP Target_Dir ""
|
||||
# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_MBCS" /D "_LIB" /YX /FD /GZ /c
|
||||
# ADD CPP /nologo /MTd /W4 /Gm /GX /ZI /Od /I "..\base" /I "..\io" /I "..\mt" /D "WIN32" /D "_DEBUG" /D "_MBCS" /D "_LIB" /FD /GZ /c
|
||||
# ADD CPP /nologo /MTd /W4 /Gm /GX /ZI /Od /I "..\common" /I "..\arch" /I "..\base" /I "..\io" /I "..\mt" /D "WIN32" /D "_DEBUG" /D "_MBCS" /D "_LIB" /FD /GZ /c
|
||||
# SUBTRACT CPP /YX
|
||||
# ADD BASE RSC /l 0x409 /d "_DEBUG"
|
||||
# ADD RSC /l 0x409 /d "_DEBUG"
|
||||
@@ -87,10 +87,6 @@ LIB32=link.exe -lib
|
||||
# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\CNetwork.cpp
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\CNetworkAddress.cpp
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
@@ -107,10 +103,6 @@ SOURCE=.\CTCPSocketFactory.cpp
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\XNetwork.cpp
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\XSocket.cpp
|
||||
# End Source File
|
||||
# End Group
|
||||
@@ -119,10 +111,6 @@ SOURCE=.\XSocket.cpp
|
||||
# PROP Default_Filter "h;hpp;hxx;hm;inl"
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\CNetwork.h
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\CNetworkAddress.h
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
@@ -151,10 +139,6 @@ SOURCE=.\ISocketFactory.h
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\XNetwork.h
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\XSocket.h
|
||||
# End Source File
|
||||
# End Group
|
||||
|
||||
Reference in New Issue
Block a user