lib/sockets.cpp
Go to the documentation of this file.
1 
9 /*****************************************************************************
10 ** Includes
11 *****************************************************************************/
12 
13 #include <cstring>
14 #include <ecl/config/ecl.hpp>
15 #ifndef ECL_IS_WIN32
16  #include <unistd.h>
17 #endif
18 #include <ecl/errors/handlers.hpp>
19 #include "../../include/ecl/io/sockets.hpp"
20 
21 /*****************************************************************************
22 ** Namespaces
23 *****************************************************************************/
24 
25 namespace ecl {
26 
28 #ifdef ECL_IS_WIN32
29  static bool already_initialised = false;
30  if ( !already_initialised ) {
31  struct WSAData wsaData;
32  int err;
33  /* Initialises use of the Winsock DLL subsystem. */
34  if ( (err = WSAStartup(MAKEWORD(2, 0), &wsaData)) != 0) {
35  switch( WSAGetLastError() ) {
36  case(WSASYSNOTREADY) : { return SocketError(NotInitialisedError); }
37  case(WSAVERNOTSUPPORTED) : { return SocketError(NotSupportedError); }
38  case(WSAEINPROGRESS) : { return SocketError(BusyError); }
39  case(WSAEPROCLIM) : { return SocketError(OutOfResourcesError); }
40  case(WSAEFAULT) : { return SocketError(InvalidArgError); }
41  default : { return SocketError(UnknownError); }
42  }
43  }
44  }
45 #endif
46  return SocketError(NoError);
47 }
48 
50 #if defined(ECL_IS_WIN32)
51  if ( ::closesocket(socket) == SOCKET_ERROR ) {
52  switch(WSAGetLastError()) {
53  case(WSANOTINITIALISED) : { return SocketError(NotInitialisedError); }
54  case(WSAENETDOWN) : { return SocketError(SystemFailureError); }
55  case(WSAENOTSOCK) : { return SocketError(InvalidArgError); }
56  case(WSAEINTR) : { return SocketError(InterruptedError); }
57  case(WSAEWOULDBLOCK) : { return SocketError(BlockingError); }
58  default : { return SocketError(UnknownError); }
59  }
60  }
61 #elif defined(ECL_IS_POSIX)
62  if ( ::close(socket) != 0 ) {
63  switch(errno) {
64  case(EBADF) : { return SocketError(InvalidArgError); }
65  case(EINTR) : { return SocketError(InterruptedError); }
66  case(EIO) : { return SocketError(InterruptedError); }
67  default : { return SocketError(UnknownError); }
68  }
69  }
70 #endif
71  return SocketError(NoError);
72 }
73 
75 #ifdef ECL_IS_WIN32
76  if ( WSACleanup() == SOCKET_ERROR ) {
77  switch(WSAGetLastError()) {
78  case(WSANOTINITIALISED) : { return SocketError(NotInitialisedError); }
79  case(WSAENETDOWN) : { return SocketError(SystemFailureError); }
80  case(WSAEINPROGRESS) : { return SocketError(BusyError); }
81  default : { return SocketError(UnknownError); }
82  }
83  }
84 #endif
85  return SocketError(NoError);
86 }
87 
88 
89 } // namespace ecl
InterruptedError
InvalidArgError
NotSupportedError
UnknownError
NotInitialisedError
int socket_descriptor
Cross-platform typedef for a socket file descriptor.
Definition: sockets.hpp:58
BlockingError
ecl_io_PUBLIC SocketError init_sockets()
Initialise the socket subsystem.
Definition: lib/sockets.cpp:27
OutOfResourcesError
ecl_io_PUBLIC SocketError close_socket(const socket_descriptor &sock)
Close a socket.
Definition: lib/sockets.cpp:49
SystemFailureError
ecl_io_PUBLIC SocketError shutdown_sockets()
Shutdown the socket subsystem.
Definition: lib/sockets.cpp:74
Extends the generic error handler with socket specific error strings.
Definition: sockets.hpp:68


ecl_io
Author(s): Daniel Stonier
autogenerated on Mon Jun 10 2019 13:09:05