/* $Revision: 14 $ $Date: 19/08/04 12:48 $ Copyright © 1999-2007, FSL Technologies Limited. Contact "http://fost.3.felspar.com". */ #include "stdafx.h" #include #include #include #include #include #include #include using namespace std; using namespace FSLib; using namespace FSLib::Exceptions; namespace { Revision c_revision( L"$Archive: /FOST.3/F3Util/internet.cpp $", __DATE__, L"$Revision: 14 $", L"$Date: 19/08/04 12:48 $" ); Revision c_header( FOST_INTERNET_HPP_ARCHIVE, __DATE__, FOST_INTERNET_HPP_REVISION, FOST_INTERNET_HPP_DATE ); const wchar_t SocketErrorMsg[]=L"A Winsock socket error occured"; const wchar_t ProtocolMsg[]=L"Network protocol error"; const wchar_t ParseErrorMsg[]=L"Parsing error"; Mutex g_initWSA_mutex; } /* Exceptions */ inline SocketError::SocketError( const FSLib::wstring &message ) : Exception( message ) { } inline SocketError::SocketError( const FSLib::wstring &message, int errorNumber ) : Exception( message ) { errorText( errorNumber ); } inline SocketError::SocketError( int errorNumber ) : Exception( L"WSA error reported" ) { errorText( errorNumber ); } inline const wchar_t *const SocketError::message() const { return SocketErrorMsg; } inline void SocketError::errorText( int errorNumber ) { switch( errorNumber ) { // case statements are in alphabetic order case WSAECONNREFUSED: m_info << L"WSA Connection Refused.\n"; break; case WSAEADDRINUSE: m_info << L"The socket's local address is already in use.\n"; break; case WSAEADDRNOTAVAIL: m_info << L"Host address not valid.\n"; break; case WSAEAFNOSUPPORT: m_info << L"Address family invalid for this socket.\n"; break; case WSAECONNRESET: m_info << L"Connection reset by peer.\n"; break; case WSAEFAULT: m_info << L"Bad (memory) address.\n"; break; case WSAEINPROGRESS: m_info << L"A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function.\n"; break; case WSAEINVAL: m_info << L"The socket has not been bound with bind.\n"; break; case WSAEISCONN: m_info << L"The socket is already connected.\n"; break; case WSAEMFILE: m_info << L"No more socket descriptors are available.\n"; break; case WSAENETDOWN: m_info << L"The network subsystem has failed.\n"; break; case WSAENOBUFS: m_info << L"No buffer space is available.\n"; break; case WSAENOTSOCK: m_info << L"Not A Socket\n"; break; case WSAEOPNOTSUPP: m_info << L"The referenced socket is not of a type that supports the listen operation.\n"; break; case WSAEPROTONOSUPPORT: m_info << L"WSA Protocol Not Supported\n"; break; case WSAETIMEDOUT: m_info << L"Time out expired\n"; break; case WSAEWOULDBLOCK: m_info << L"The call would block on a non-blocking socket.\n"; break; case WSAHOST_NOT_FOUND: m_info << L"Host not found\n"; break; case WSANO_DATA: m_info << L"Valid name, no data record of requested type"; break; case WSANOTINITIALISED: m_info << L"WSA Not Initialised.\n"; break; default: m_info << L"Unknown Error\nError number: " << errorNumber << endl; break; } } Protocol::Protocol( const FSLib::wstring &message ) : Exception( message ) { } Protocol::Protocol( const FSLib::wstring &message, const FSLib::wstring &inf ) : Exception( message ) { m_info << inf << std::endl; } const wchar_t *const Protocol::message() const { return ProtocolMsg; } ParseError::ParseError( const FSLib::wstring &message ) : Exception( message ) { } ParseError::ParseError( const FSLib::wstring &message, const FSLib::wstring &value ) : Exception( message ) { m_info << value << std::endl; } const wchar_t * const ParseError::message() const { return ParseErrorMsg; } /* WSA initialisation Note that this code may not be thread safe (don't know if each thread needs an initialisation)! */ inline void FSLib::initWSA() { Mutex::Lock lock( g_initWSA_mutex ); static bool executed( false ); if ( !executed ) { static WSADATA wsa_data; WSAStartup(0x0002, &wsa_data); executed = true; } }