| 1 |
// ---------------------------------------------------------------------------- |
| 2 |
// |
| 3 |
// flxmlrpc Copyright (c) 2015 by W1HKJ, Dave Freese <[email protected]> |
| 4 |
// |
| 5 |
// XmlRpc++ Copyright (c) 2002-2008 by Chris Morley |
| 6 |
// |
| 7 |
// This file is part of fldigi |
| 8 |
// |
| 9 |
// flxmlrpc is free software; you can redistribute it and/or modify |
| 10 |
// it under the terms of the GNU Lesser General Public License as published by |
| 11 |
// the Free Software Foundation; either version 3 of the License, or |
| 12 |
// (at your option) any later version. |
| 13 |
// |
| 14 |
// You should have received a copy of the GNU General Public License |
| 15 |
// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
// ---------------------------------------------------------------------------- |
| 17 |
|
| 18 |
#ifndef _XMLRPCSOCKET_H_ |
| 19 |
#define _XMLRPCSOCKET_H_ |
| 20 |
|
| 21 |
#if defined(_MSC_VER) |
| 22 |
# pragma warning(disable:4786) // identifier was truncated in debug info |
| 23 |
#endif |
| 24 |
|
| 25 |
#include <string> |
| 26 |
|
| 27 |
|
| 28 |
namespace XmlRpc { |
| 29 |
|
| 30 |
//! A platform-independent socket API. |
| 31 |
class XmlRpcSocket { |
| 32 |
public: |
| 33 |
|
| 34 |
// On windows, a socket is an unsigned int large enough to hold a ptr |
| 35 |
// This should match the definition of SOCKET in winsock2.h |
| 36 |
#if defined(_WINDOWS) |
| 37 |
# if defined(_WIN64) |
| 38 |
typedef unsigned __int64 Socket; |
| 39 |
# else |
| 40 |
typedef unsigned int Socket; |
| 41 |
# endif |
| 42 |
#else |
| 43 |
typedef int Socket; |
| 44 |
#endif |
| 45 |
|
| 46 |
//! An invalid socket constant. |
| 47 |
static const Socket Invalid = (Socket) -1; |
| 48 |
|
| 49 |
//! Creates a stream (TCP) socket. Returns XmlRpcSocket::Invalid on failure. |
| 50 |
static Socket socket(); |
| 51 |
|
| 52 |
//! Closes a socket. |
| 53 |
static void close(Socket socket); |
| 54 |
|
| 55 |
|
| 56 |
//! Sets a stream (TCP) socket to perform non-blocking IO. Returns false on failure. |
| 57 |
static bool setNonBlocking(Socket socket); |
| 58 |
|
| 59 |
|
| 60 |
// The next four methods are appropriate for servers. |
| 61 |
|
| 62 |
//! Allow the port the specified socket is bound to to be re-bound immediately so |
| 63 |
//! server re-starts are not delayed. Returns false on failure. |
| 64 |
static bool setReuseAddr(Socket socket); |
| 65 |
|
| 66 |
//! Bind to a specified port |
| 67 |
static bool bind(Socket socket, int port); |
| 68 |
|
| 69 |
//! Set socket in listen mode |
| 70 |
static bool listen(Socket socket, int backlog); |
| 71 |
|
| 72 |
//! Accept a client connection request |
| 73 |
static Socket accept(Socket socket); |
| 74 |
|
| 75 |
//! Connect a socket to a server (from a client) |
| 76 |
static bool connect(Socket socket, std::string& host, int port); |
| 77 |
|
| 78 |
//! Get the port of a bound socket |
| 79 |
static int getPort(Socket socket); |
| 80 |
|
| 81 |
//! Returns true if the last error was not a fatal one (eg, EWOULDBLOCK) |
| 82 |
static bool nonFatalError(); |
| 83 |
|
| 84 |
//! Returns last errno |
| 85 |
static int getError(); |
| 86 |
|
| 87 |
//! Returns message corresponding to last error |
| 88 |
static std::string getErrorMsg(); |
| 89 |
|
| 90 |
//! Returns message corresponding to error |
| 91 |
static std::string getErrorMsg(int error); |
| 92 |
}; |
| 93 |
|
| 94 |
} // namespace XmlRpc |
| 95 |
|
| 96 |
#endif |