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


ecl_io
Author(s): Daniel Stonier (d.stonier@gmail.com)
autogenerated on Thu Jan 2 2014 11:11:37