Program Listing for File sockets.hpp

Return to documentation for file (include/ecl/io/sockets.hpp)

/*****************************************************************************
 ** Ifdefs
 *****************************************************************************/

#ifndef ECL_IO_SOCKETS_HPP_
#define ECL_IO_SOCKETS_HPP_

/*****************************************************************************
 ** Include
 *****************************************************************************/

#include <string>
#include <ecl/config/ecl.hpp>
#include <ecl/errors/handlers.hpp>

#ifdef ECL_IS_WIN32
  #include <ecl/config/windows.hpp>
  #include <winsock2.h>
  #include <ws2tcpip.h>
  //  #include <iphlpapi.h>
  //  #include <sys/types.h>
#elif defined(ECL_IS_POSIX)
  #include <netinet/in.h> // provides AF_LOCAL etc...
  #include <errno.h>
#else
  #error("There is not a supporting sockets implementation on this platform (possibly needs extended ecl support).")
#endif
#include "macros.hpp"

/*****************************************************************************
 ** Namespaces
 *****************************************************************************/

namespace ecl
{

/*****************************************************************************
 ** Types
 *****************************************************************************/

#ifdef ECL_IS_WIN32
  typedef SOCKET socket_descriptor;
#else
  typedef int socket_descriptor;
#endif

/*****************************************************************************
 ** Errors
 *****************************************************************************/

class ecl_io_PUBLIC SocketError : public Error
{
public:
  SocketError(const ErrorFlag& flag = UnknownError) : Error(flag)
  {}
protected:
  virtual const char* invalidArgErrorString() const
  { return "One of the arguments is invalid (usually a socket descriptor).";}
#ifdef ECL_IS_WIN32
  virtual const char* notSupportedError() const
  { return "This version of winsock is not supported on this platform.";}
  virtual const char* interruptedErrorString() const
  { return "Interrupted by WSACancelBlockingCall.";}
  virtual const char* notInitialisedErrorString() const
  { return "The underlying winsock subsystem is not initialised (needs WSAStartup).";}
  virtual const char* blockingErrorString() const
  { return "Marked as non-blocking, but current configuration would block.";}
  virtual const char* busyErrorString() const
  { return "A blocking winsock operation is in progress.";}
  virtual const char* systemFailureErrorString() const
  { return "The network subsystem has failed.";}
#elif defined(ECL_IS_POSIX)
  virtual const char* interruptedErrorString() const
  { return "Interrupted by a signal or an io error.";}
  virtual const char* argNotSupportedString() const
  { return "The specified address family or protocol is not supported on this machine.";}
#endif
};

/*****************************************************************************
 ** Functions
 *****************************************************************************/

ecl_io_PUBLIC SocketError init_sockets();
ecl_io_PUBLIC SocketError close_socket(const socket_descriptor& sock);
ecl_io_PUBLIC SocketError shutdown_sockets();

} // namespace ecl

#endif /* ECL_IO_SOCKETS_HPP_ */