sockets.cpp
Go to the documentation of this file.
00001 
00009 /*****************************************************************************
00010 ** Includes
00011 *****************************************************************************/
00012 
00013 #include <cstring>
00014 #include <ecl/config/ecl.hpp>
00015 #ifndef ECL_IS_WIN32
00016   #include <unistd.h>
00017 #endif
00018 #include <ecl/errors/handlers.hpp>
00019 #include "../../include/ecl/io/sockets.hpp"
00020 
00021 /*****************************************************************************
00022 ** Namespaces
00023 *****************************************************************************/
00024 
00025 namespace ecl {
00026 
00027 SocketError init_sockets(void) {
00028 #ifdef ECL_IS_WIN32
00029         static bool already_initialised = false;
00030         if ( !already_initialised ) {
00031                 struct WSAData wsaData;
00032                 int err;
00033                 /* Initialises use of the Winsock DLL subsystem. */
00034                 if ( (err = WSAStartup(MAKEWORD(2, 0), &wsaData)) != 0) {
00035                         switch( WSAGetLastError() ) {
00036                                 case(WSASYSNOTREADY) : { return SocketError(NotInitialisedError); }
00037                                 case(WSAVERNOTSUPPORTED) : { return SocketError(NotSupportedError); }
00038                                 case(WSAEINPROGRESS) : { return SocketError(BusyError); }
00039                                 case(WSAEPROCLIM) : { return SocketError(OutOfResourcesError); }
00040                                 case(WSAEFAULT) : { return SocketError(InvalidArgError); }
00041                                 default : { return SocketError(UnknownError); }
00042                         }
00043                 }
00044         }
00045 #endif
00046         return SocketError(NoError);
00047 }
00048 
00049 SocketError close_socket(const socket_descriptor& socket) {
00050 #if defined(ECL_IS_WIN32)
00051         if ( ::closesocket(socket) == SOCKET_ERROR ) {
00052                 switch(WSAGetLastError()) {
00053                         case(WSANOTINITIALISED) : { return SocketError(NotInitialisedError); }
00054                         case(WSAENETDOWN) : { return SocketError(SystemFailureError); }
00055                         case(WSAENOTSOCK) : { return SocketError(InvalidArgError); }
00056                         case(WSAEINTR) : { return SocketError(InterruptedError); }
00057                         case(WSAEWOULDBLOCK) : { return SocketError(BlockingError); }
00058                         default : { return SocketError(UnknownError); }
00059                 }
00060         }
00061 #elif defined(ECL_IS_POSIX)
00062         if ( ::close(socket) != 0 ) {
00063                 switch(errno) {
00064                         case(EBADF) : { return SocketError(InvalidArgError); }
00065                         case(EINTR) : { return SocketError(InterruptedError); }
00066                         case(EIO) :   { return SocketError(InterruptedError); }
00067                         default : { return SocketError(UnknownError); }
00068                 }
00069         }
00070 #endif
00071         return SocketError(NoError);
00072 }
00073 
00074 SocketError shutdown_sockets() {
00075 #ifdef ECL_IS_WIN32
00076         if ( WSACleanup() == SOCKET_ERROR ) {
00077                 switch(WSAGetLastError()) {
00078                         case(WSANOTINITIALISED) : { return SocketError(NotInitialisedError); }
00079                         case(WSAENETDOWN) : { return SocketError(SystemFailureError); }
00080                         case(WSAEINPROGRESS) : { return SocketError(BusyError); }
00081                         default : { return SocketError(UnknownError); }
00082                 }
00083         }
00084 #endif
00085         return SocketError(NoError);
00086 }
00087 
00088 
00089 } // namespace ecl


ecl_io
Author(s): Daniel Stonier
autogenerated on Sun Oct 5 2014 23:35:30