You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by tn...@apache.org on 2001/01/22 17:43:44 UTC

cvs commit: xml-xerces/c/src/util/NetAccessors/WinSock BinHTTPURLInputStream.cpp BinHTTPURLInputStream.hpp WinSockNetAccessor.cpp

tng         01/01/22 08:43:43

  Modified:    c/src/util/NetAccessors/WinSock BinHTTPURLInputStream.cpp
                        BinHTTPURLInputStream.hpp WinSockNetAccessor.cpp
  Log:
  Loads winsock dynamically.  Fixed by Curt Arnold.
  Winsock2 is not initialized unless an http URL is used.    If an http
  URL is used and the Winsock 2 DLL is not installed, then an NetAccessor
  initialization exception is thrown.
  
  Revision  Changes    Path
  1.6       +152 -0    xml-xerces/c/src/util/NetAccessors/WinSock/BinHTTPURLInputStream.cpp
  
  Index: BinHTTPURLInputStream.cpp
  ===================================================================
  RCS file: /home/cvs/xml-xerces/c/src/util/NetAccessors/WinSock/BinHTTPURLInputStream.cpp,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- BinHTTPURLInputStream.cpp	2000/07/21 03:22:44	1.5
  +++ BinHTTPURLInputStream.cpp	2001/01/22 16:43:38	1.6
  @@ -56,6 +56,12 @@
   
   /*
    * $Log: BinHTTPURLInputStream.cpp,v $
  + * Revision 1.6  2001/01/22 16:43:38  tng
  + * Loads winsock dynamically.  Fixed by Curt Arnold.
  + * Winsock2 is not initialized unless an http URL is used.    If an http
  + * URL is used and the Winsock 2 DLL is not installed, then an NetAccessor
  + * initialization exception is thrown.
  + *
    * Revision 1.5  2000/07/21 03:22:44  andyh
    * Improved (but still weak) http access by the parser.
    * Windows only.  UNIX will follow, probably tomorrow.
  @@ -85,8 +91,10 @@
   
   #define _WINSOCKAPI_
   
  +#define INCL_WINSOCK_API_TYPEDEFS 1 
   #include <winsock2.h>
   #include <windows.h>
  +#include <tchar.h>
   
   #include <stdio.h>
   #include <string.h>
  @@ -98,12 +106,158 @@
   #include <util/Janitor.hpp>
   
   
  +HMODULE gWinsockLib = NULL;
  +LPFN_GETHOSTBYNAME gWSgethostbyname = NULL;
  +LPFN_INET_ADDR gWSinet_addr = NULL;
  +LPFN_GETHOSTBYADDR gWSgethostbyaddr = NULL;
  +LPFN_HTONS gWShtons = NULL;
  +LPFN_SOCKET gWSsocket = NULL;
  +LPFN_CONNECT gWSconnect = NULL;
  +LPFN_SEND gWSsend = NULL;
  +LPFN_RECV gWSrecv = NULL;
  +LPFN_SHUTDOWN gWSshutdown = NULL;
  +LPFN_CLOSESOCKET gWSclosesocket = NULL;
  +LPFN_WSACLEANUP gWSACleanup = NULL;
  +
  +bool BinHTTPURLInputStream::fInitialized = false;
  +
  +void BinHTTPURLInputStream::Initialize() {
  +    //
  +    // Initialize the WinSock library here.
  +    //
  +	fInitialized = true;
  +    WORD        wVersionRequested;
  +    WSADATA     wsaData;
  +
  +	LPFN_WSASTARTUP startup = NULL;
  +	if(gWinsockLib == NULL) {
  +		gWinsockLib = LoadLibrary(_T("WSOCK32"));
  +		if(gWinsockLib == NULL) {
  +			ThrowXML(NetAccessorException, XMLExcepts::NetAcc_InitFailed);
  +		}
  +		else {
  +			startup = (LPFN_WSASTARTUP) GetProcAddress(gWinsockLib,_T("WSAStartup"));
  +			gWSACleanup = (LPFN_WSACLEANUP) GetProcAddress(gWinsockLib,_T("WSACleanup"));
  +			gWSgethostbyname = (LPFN_GETHOSTBYNAME) GetProcAddress(gWinsockLib,_T("gethostbyname"));
  +			gWSinet_addr = (LPFN_INET_ADDR) GetProcAddress(gWinsockLib,_T("inet_addr"));
  +			gWSgethostbyaddr = (LPFN_GETHOSTBYADDR) GetProcAddress(gWinsockLib,_T("gethostbyaddr"));
  +			gWShtons = (LPFN_HTONS) GetProcAddress(gWinsockLib,_T("htons"));
  +			gWSsocket = (LPFN_SOCKET) GetProcAddress(gWinsockLib,_T("socket"));
  +			gWSconnect = (LPFN_CONNECT) GetProcAddress(gWinsockLib,_T("connect"));
  +			gWSsend = (LPFN_SEND) GetProcAddress(gWinsockLib,_T("send"));
  +			gWSrecv = (LPFN_RECV) GetProcAddress(gWinsockLib,_T("recv"));
  +			gWSshutdown = (LPFN_SHUTDOWN) GetProcAddress(gWinsockLib,_T("shutdown"));
  +			gWSclosesocket = (LPFN_CLOSESOCKET) GetProcAddress(gWinsockLib,_T("closesocket"));
  +
  +			if(startup == NULL || 
  +				gWSACleanup == NULL || 
  +				gWSgethostbyname == NULL ||
  +				gWSinet_addr == NULL ||
  +				gWSgethostbyaddr == NULL ||
  +				gWShtons == NULL ||
  +				gWSsocket == NULL ||
  +				gWSconnect == NULL ||
  +				gWSsend == NULL ||
  +				gWSrecv == NULL ||
  +				gWSshutdown == NULL ||
  +				gWSclosesocket == NULL)
  +			{
  +				gWSACleanup = NULL;
  +				Cleanup();
  +				ThrowXML(NetAccessorException, XMLExcepts::NetAcc_InitFailed);
  +			}
  +		}
  +	}
  +    wVersionRequested = MAKEWORD( 2, 2 );
  +    int err = (*startup)(wVersionRequested, &wsaData);
  +    if (err != 0)
  +    {
  +        // Call WSAGetLastError() to get the last error.
  +        ThrowXML(NetAccessorException, XMLExcepts::NetAcc_InitFailed);
  +    }
  +}
  +
  +void BinHTTPURLInputStream::Cleanup() {
  +	if(fInitialized) 
  +	{
  +		if(gWSACleanup) (*gWSACleanup)();
  +		gWSACleanup = NULL;
  +		gWinsockLib = NULL;
  +		gWSgethostbyname = NULL;
  +		gWSinet_addr = NULL;
  +		gWSgethostbyaddr = NULL;
  +		gWShtons = NULL;
  +		gWSsocket = NULL;
  +		gWSconnect = NULL;
  +		gWSsend = NULL;
  +		gWSrecv = NULL;
  +		gWSshutdown = NULL;
  +		gWSclosesocket = NULL;
  +		FreeLibrary(gWinsockLib);
  +		fInitialized = false;
  +	}
  +}
  +
  +
  +hostent* BinHTTPURLInputStream::gethostbyname(const char* name)
  +{
  +	return (*gWSgethostbyname)(name);
  +}
  +
  +unsigned long BinHTTPURLInputStream::inet_addr(const char* cp)
  +{
  +	return (*gWSinet_addr)(cp);
  +}
  +
  +hostent* BinHTTPURLInputStream::gethostbyaddr(const char* addr,int len,int type)
  +{
  +	return (*gWSgethostbyaddr)(addr,len,type);
  +}
  +
  +unsigned short BinHTTPURLInputStream::htons(unsigned short hostshort)
  +{
  +	return (*gWShtons)(hostshort);
  +}
  +
  +unsigned short BinHTTPURLInputStream::socket(int af,int type,int protocol)
  +{
  +	return (*gWSsocket)(af,type,protocol);
  +}
  +
  +int BinHTTPURLInputStream::connect(unsigned short s,const sockaddr* name,int namelen)
  +{
  +	return (*gWSconnect)(s,name,namelen);
  +}
  +
  +int BinHTTPURLInputStream::send(unsigned short s,const char* buf,int len,int flags)
  +{
  +	return (*gWSsend)(s,buf,len,flags);
  +}
  +
  +int BinHTTPURLInputStream::recv(unsigned short s,char* buf,int len,int flags)
  +{
  +	return (*gWSrecv)(s,buf,len,flags);
  +}
  +
  +int BinHTTPURLInputStream::shutdown(unsigned int s,int how)
  +{
  +	return (*gWSshutdown)(s,how);
  +}
  +
  +int BinHTTPURLInputStream::closesocket(unsigned int socket)
  +{
  +	return (*gWSclosesocket)(socket);
  +}
   
   
   BinHTTPURLInputStream::BinHTTPURLInputStream(const XMLURL& urlSource)
         : fSocketHandle(0)
         , fBytesProcessed(0)
   {
  +	if(!fInitialized) 
  +	{
  +		Initialize();
  +	}
   
       //
       // Pull all of the parts of the URL out of th urlSource object, and transcode them
  
  
  
  1.4       +22 -0     xml-xerces/c/src/util/NetAccessors/WinSock/BinHTTPURLInputStream.hpp
  
  Index: BinHTTPURLInputStream.hpp
  ===================================================================
  RCS file: /home/cvs/xml-xerces/c/src/util/NetAccessors/WinSock/BinHTTPURLInputStream.hpp,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- BinHTTPURLInputStream.hpp	2000/07/21 03:22:45	1.3
  +++ BinHTTPURLInputStream.hpp	2001/01/22 16:43:39	1.4
  @@ -56,6 +56,12 @@
   
   /*
    * $Log: BinHTTPURLInputStream.hpp,v $
  + * Revision 1.4  2001/01/22 16:43:39  tng
  + * Loads winsock dynamically.  Fixed by Curt Arnold.
  + * Winsock2 is not initialized unless an http URL is used.    If an http
  + * URL is used and the Winsock 2 DLL is not installed, then an NetAccessor
  + * initialization exception is thrown.
  + *
    * Revision 1.3  2000/07/21 03:22:45  andyh
    * Improved (but still weak) http access by the parser.
    * Windows only.  UNIX will follow, probably tomorrow.
  @@ -90,6 +96,8 @@
   // This class implements the BinInputStream interface specified by the XML
   // parser.
   //
  +struct hostent;
  +struct sockaddr;
   
   class XMLUTIL_EXPORT BinHTTPURLInputStream : public BinInputStream
   {
  @@ -104,6 +112,8 @@
           , const unsigned int    maxToRead
       );
   
  +	static void Cleanup();
  +
   
   private :
       // -----------------------------------------------------------------------
  @@ -130,6 +140,20 @@
       char                fBuffer[4000];
       char *              fBufferEnd;
       char *              fBufferPos;
  +	static bool         fInitialized;
  +
  +	static void Initialize();
  +
  +	inline static hostent* gethostbyname(const char* name);
  +	inline static unsigned long inet_addr(const char* cp);
  +	inline static hostent* gethostbyaddr(const char* addr,int len,int type);
  +	inline static unsigned short htons(unsigned short hostshort);
  +	inline static unsigned short socket(int af,int type,int protocol);
  +	inline static int connect(unsigned short s,const sockaddr* name,int namelen);
  +	inline static int send(unsigned short s,const char* buf,int len,int flags);
  +	inline static int recv(unsigned short s,char* buf,int len,int flags);
  +	inline static int shutdown(unsigned int s,int how);
  +	inline static int closesocket(unsigned int socket);
   };
   
   
  
  
  
  1.4       +3 -19     xml-xerces/c/src/util/NetAccessors/WinSock/WinSockNetAccessor.cpp
  
  Index: WinSockNetAccessor.cpp
  ===================================================================
  RCS file: /home/cvs/xml-xerces/c/src/util/NetAccessors/WinSock/WinSockNetAccessor.cpp,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- WinSockNetAccessor.cpp	2000/07/25 22:29:16	1.3
  +++ WinSockNetAccessor.cpp	2001/01/22 16:43:41	1.4
  @@ -55,13 +55,12 @@
    */
   
   /*
  - * $Id: WinSockNetAccessor.cpp,v 1.3 2000/07/25 22:29:16 aruna1 Exp $
  + * $Id: WinSockNetAccessor.cpp,v 1.4 2001/01/22 16:43:41 tng Exp $
    */
   
   
   #define _WINSOCKAPI_
   
  -#include <winsock2.h>
   #include <windows.h>
   
   #include <util/XMLUniDefs.hpp>
  @@ -72,6 +71,7 @@
   #include <util/NetAccessors/WinSock/WinSockNetAccessor.hpp>
   
   
  +
   const XMLCh WinSockNetAccessor::fgMyName[] =
   {
       chLatin_W, chLatin_i, chLatin_n, chLatin_S, chLatin_o, chLatin_c,
  @@ -80,31 +80,15 @@
       chNull
   };
   
  -
   WinSockNetAccessor::WinSockNetAccessor()
   {
  -    //
  -    // Initialize the WinSock library here.
  -    //
  -
  -    WORD        wVersionRequested;
  -    WSADATA     wsaData;
  - 
  -    wVersionRequested = MAKEWORD( 2, 2 );
  -    int err = WSAStartup(wVersionRequested, &wsaData);
  -    if (err != 0)
  -    {
  -        // Call WSAGetLastError() to get the last error.
  -        ThrowXML(NetAccessorException, XMLExcepts::NetAcc_InitFailed);
  -    }
   }
   
   
   WinSockNetAccessor::~WinSockNetAccessor()
   {
       // Cleanup code for the WinSock library here.
  -
  -    WSACleanup();
  +	BinHTTPURLInputStream::Cleanup();
   }