You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by ra...@locus.apache.org on 2000/03/22 01:21:12 UTC

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

rahulj      00/03/21 16:21:11

  Modified:    c/src/util/NetAccessors/WinSock BinHTTPURLInputStream.cpp
                        BinHTTPURLInputStream.hpp WinSockNetAccessor.cpp
  Log:
  Now we throw exceptions when errors occur.
  Simplified the code, based on the assumption that
  the calling function will make sure that the buffer into
  which the data has to be read is large enough.
  
  Revision  Changes    Path
  1.2       +36 -105   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.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- BinHTTPURLInputStream.cpp	2000/03/17 02:37:54	1.1
  +++ BinHTTPURLInputStream.cpp	2000/03/22 00:21:10	1.2
  @@ -56,6 +56,12 @@
   
   /*
    * $Log: BinHTTPURLInputStream.cpp,v $
  + * Revision 1.2  2000/03/22 00:21:10  rahulj
  + * Now we throw exceptions when errors occur.
  + * Simplified the code, based on the assumption that
  + * the calling function will make sure that the buffer into
  + * which the data has to be read is large enough.
  + *
    * Revision 1.1  2000/03/17 02:37:54  rahulj
    * First cut at adding HTTP capability via native sockets.
    * Still need to add:
  @@ -117,10 +123,7 @@
   
   
   BinHTTPURLInputStream::BinHTTPURLInputStream(const XMLURL& urlSource)
  -      : fBuffer(0)
  -      , fBufferSize(0)
  -      , fBufferIndex(0)
  -      , fSocketHandle(0)
  +      : fSocketHandle(0)
         , fBytesProcessed(0)
   {
       const XMLCh*        uri = urlSource.getURLText();
  @@ -139,15 +142,17 @@
           unsigned long  numAddress = inet_addr(hostNameAsCharStar);
           if (numAddress == INADDR_NONE)
           {
  -            // Throw cannot connect exception here.
  -            // call WSAGetLastError() to get the error number.
  +            // Call WSAGetLastError() to get the error number.
  +            ThrowXML(NetAccessorException,
  +                     XMLExcepts::NetAcc_TargetResolution);
           }
           if ((hostEntPtr = 
                   gethostbyaddr((const char *) &numAddress, 
                                 sizeof(unsigned long), AF_INET)) == NULL)
           {
  -            // Throw cannot connect exception here.
  -            // call WSAGetLastError() to get the error number.
  +            // Call WSAGetLastError() to get the error number.
  +            ThrowXML(NetAccessorException,
  +                     XMLExcepts::NetAcc_TargetResolution);
           }
       }
   
  @@ -159,14 +164,16 @@
       SOCKET s = socket(hostEntPtr->h_addrtype, SOCK_STREAM, 0);
       if (s == INVALID_SOCKET)
       {
  -        // Throw could not create socket exception.
  -        // call WSAGetLastError() to get the error number.
  +        // Call WSAGetLastError() to get the error number.
  +        ThrowXML(NetAccessorException,
  +                 XMLExcepts::NetAcc_CreateSocket);
       }
   
       if (connect(s, (struct sockaddr *) &sa, sizeof(sa)) == SOCKET_ERROR)
       {
  -        // Throw could not connect the socket exception.
  -        // call WSAGetLastError() to get the error number.
  +        // Call WSAGetLastError() to get the error number.
  +        ThrowXML(NetAccessorException,
  +                 XMLExcepts::NetAcc_ConnSocket);
       }
       
       // Now you can simply read and write from/to the socket.
  @@ -176,15 +183,13 @@
       int  aLent = 0;
       if ((aLent = send(s, (const char *) obuf, lent, 0)) != lent)
       {
  -        // Throw could not write URL to the socket exception.
  -        // call WSAGetLastError() to get the error number.
  -        // wrote only 'aLent' bytes.
  +        // Call WSAGetLastError() to get the error number.
  +        ThrowXML(NetAccessorException,
  +                 XMLExcepts::NetAcc_WriteSocket);
       }
   
       fSocketHandle = (unsigned int) s;
   
  -    fBuffer = new XMLByte[URLISBUFMAXSIZE];
  -
   }
   
   
  @@ -193,107 +198,31 @@
   {
       shutdown(fSocketHandle, SD_BOTH);
       closesocket(fSocketHandle);
  -    delete [] fBuffer;
  -    fBuffer = 0;
   }
   
  -
  -unsigned int BinHTTPURLInputStream::curPos() const
  -{
  -    return fBytesProcessed;
  -}
   
  +//
  +// Here we will not maintain any local buffer. We will read directly
  +// into the buffer that is passed as the parameter. The scanner always
  +// requests for more input in chunks of size 32Kbytes.
  +//
   
   unsigned int BinHTTPURLInputStream::readBytes(XMLByte* const    toFill
                                       , const unsigned int    maxToRead)
   {
       unsigned int  retval = 0;
  -    unsigned int  bytesAsked = maxToRead;
  -    unsigned int  bytesForCopy = 0;
   
  -    // Wipe out the old stuff from the destination buffer to fill.
  +    int lent = recv((SOCKET) fSocketHandle, (char *) toFill, maxToRead, 0);
   
  -    memset((void*)toFill, 0x00, sizeof(XMLByte) * maxToRead);
  -    
  -    if (fBufferSize > 0)
  -        bytesForCopy = fBufferSize - fBufferIndex;
  -
  -    if (bytesAsked <= bytesForCopy)
  +    if (lent == SOCKET_ERROR)
       {
  -        // ...then you can satisfy this request completely from fBuffer.
  -        // Simply copy over the bytes to the destination array.
  -        memcpy((void*) toFill, (void*) (fBuffer + fBufferIndex), bytesAsked);
  -        fBufferIndex += bytesAsked;
  -        if (fBufferIndex >= fBufferSize)
  -        {
  -            fBufferSize = 0;
  -            fBufferIndex = 0;
  -        }
  -        fBytesProcessed += bytesAsked;
  -        retval = bytesAsked;
  +        // Call WSAGetLastError() to get the error number.
  +        ThrowXML(NetAccessorException, XMLExcepts::NetAcc_ReadSocket);
       }
  -
       else
       {
  -        // ...will need to read some more bytes out of the stream.
  -        unsigned int    bufToFillIndex = 0;
  -
  -        // First copy over what is left in fBuffer, before reading another
  -        // chunk out of the stream.
  -
  -        if (bytesForCopy != 0)
  -        {
  -            memcpy((void*) toFill, (void*) (fBuffer + fBufferSize), bytesForCopy);
  -            fBufferSize = 0;
  -            fBufferIndex = 0;
  -            fBytesProcessed += bytesForCopy;
  -            bufToFillIndex = bytesForCopy;
  -            retval = bytesForCopy;
  -        }
  -
  -        unsigned int    bytesRemainingToFill = bytesAsked - bytesForCopy;
  -
  -        // Now blow away the internal buffer and read next 'chunk' into it
  -        // from the stream.
  -
  -        memset((void*) fBuffer, 0x00, URLISBUFMAXSIZE);
  -        int lent = recv((SOCKET) fSocketHandle,
  -                        (char *) fBuffer,
  -                        URLISBUFMAXSIZE - 1, 0);
  -
  -        if (lent == SOCKET_ERROR)
  -        {
  -            // Throw socket error exception.
  -            // call WSAGetLastError() to get the error number.
  -        }
  -        else if (lent == 0)
  -        {
  -            // No more data is available.
  -            fBufferSize = 0;
  -            fBufferIndex = 0;
  -        }
  -        else
  -        {
  -            fBufferIndex = 0;
  -            fBufferSize = lent;
  -
  -            // Now fill the destination buffer with the new data just read.
  -
  -            if (bytesRemainingToFill > fBufferSize)
  -            {
  -                bytesRemainingToFill = fBufferSize;
  -            }
  -            memcpy((void*) (toFill + bufToFillIndex),
  -                   (void*) fBuffer,
  -                   bytesRemainingToFill);
  -
  -            // Update counters.
  -            retval += bytesRemainingToFill;
  -            fBufferIndex += bytesRemainingToFill;
  -            fBytesProcessed += bytesRemainingToFill;
  -
  -        }
  -
  +        retval = lent;
  +        fBytesProcessed += retval;
       }
   
       return retval;
  
  
  
  1.2       +17 -18    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.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- BinHTTPURLInputStream.hpp	2000/03/17 02:37:54	1.1
  +++ BinHTTPURLInputStream.hpp	2000/03/22 00:21:10	1.2
  @@ -56,6 +56,12 @@
   
   /*
    * $Log: BinHTTPURLInputStream.hpp,v $
  + * Revision 1.2  2000/03/22 00:21:10  rahulj
  + * Now we throw exceptions when errors occur.
  + * Simplified the code, based on the assumption that
  + * the calling function will make sure that the buffer into
  + * which the data has to be read is large enough.
  + *
    * Revision 1.1  2000/03/17 02:37:54  rahulj
    * First cut at adding HTTP capability via native sockets.
    * Still need to add:
  @@ -103,30 +109,21 @@
       //      The socket representing the connection to the remote file.
       //      We deliberately did not define the type to be SOCKET, so as to
       //      avoid bringing in any Windows header into this file.
  -    //  fBuffer
  -    //      This is the array in which the data is stored after reading it
  -    //      of the network. The maximum size of this array is decided in the
  -    //      constructor via a file specific #define.
  -    //  fBufferIndex
  -    //      Its the index into fBuffer and points to the next unprocessed
  -    //      character. When the parser asks for more data to be read of the
  -    //      stream, then fBuffer[fBufferIndex] is the first byte returned,
  -    //      unless fBufferIndex equals fBufferSize indicating that all
  -    //      data in the fBuffer has been processed.
  -    //  fBufferSize
  -    //      This represents the extent of valid data in the fBuffer array.
       //  fBytesProcessed
       //      Its a rolling count of the number of bytes processed off this
  -    //      input stream. Its only reset back to zero, if the stream is
  -    //      reset. The maximum value this can have is fRemoteFileSize.
  +    //      input stream.
       // -----------------------------------------------------------------------
   
       unsigned int        fSocketHandle;
  -    XMLByte*            fBuffer;
  -    unsigned int        fBufferIndex;
  -    unsigned int        fBufferSize;
       unsigned int        fBytesProcessed;
   };
  +
  +
  +inline unsigned int BinHTTPURLInputStream::curPos() const
  +{
  +    return fBytesProcessed;
  +}
  +
   
   
   #endif // BINHTTPURLINPUTSTREAM_HPP
  
  
  
  1.2       +11 -3     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.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- WinSockNetAccessor.cpp	2000/03/17 02:37:54	1.1
  +++ WinSockNetAccessor.cpp	2000/03/22 00:21:10	1.2
  @@ -56,6 +56,12 @@
   
   /*
    * $Log: WinSockNetAccessor.cpp,v $
  + * Revision 1.2  2000/03/22 00:21:10  rahulj
  + * Now we throw exceptions when errors occur.
  + * Simplified the code, based on the assumption that
  + * the calling function will make sure that the buffer into
  + * which the data has to be read is large enough.
  + *
    * Revision 1.1  2000/03/17 02:37:54  rahulj
    * First cut at adding HTTP capability via native sockets.
    * Still need to add:
  @@ -101,8 +107,8 @@
       int err = WSAStartup(wVersionRequested, &wsaData);
       if (err != 0)
       {
  -        // Throw could not initialize WinSocket library exception.
  -        // call WSAGetLastError() to get the last error.
  +        // Call WSAGetLastError() to get the last error.
  +        ThrowXML(NetAccessorException, XMLExcepts::NetAcc_InitFailed);
       }
   }