SBinetTimedStream.cpp
上传用户:xqtpzdz
上传日期:2022-05-21
资源大小:1764k
文件大小:5k
源码类别:

xml/soap/webservice

开发平台:

Visual C++

  1. /****************License************************************************
  2.  * Vocalocity OpenVXI
  3.  * Copyright (C) 2004-2005 by Vocalocity, Inc. All Rights Reserved.
  4.  * This program is free software; you can redistribute it and/or
  5.  * modify it under the terms of the GNU General Public License
  6.  * as published by the Free Software Foundation; either version 2
  7.  * of the License, or (at your option) any later version.
  8.  *  
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, write to the Free Software
  16.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  17.  * Vocalocity, the Vocalocity logo, and VocalOS are trademarks or 
  18.  * registered trademarks of Vocalocity, Inc. 
  19.  * OpenVXI is a trademark of Scansoft, Inc. and used under license 
  20.  * by Vocalocity.
  21.  ***********************************************************************/
  22. #if _MSC_VER >= 1100    // Visual C++ 5.x
  23. #pragma warning( disable : 4786 4503 )
  24. #endif
  25. #include "SBinetTimedStream.hpp"
  26. #include "SBinetStoppableStream.hpp"
  27. #include "SBinetUtils.hpp"
  28. #include "SBinetChannel.h"
  29. // SBinetTimedStream::SBinetTimedStream
  30. // Refer to SBinetTimedStream.hpp for doc.
  31. SBinetTimedStream::SBinetTimedStream(SBinetStoppableStream *aStream,
  32.                                      VXIlogInterface *log,
  33.                                      VXIunsigned diagLogBase):
  34.   SWIutilLogger(MODULE_SBINET, log, diagLogBase),
  35.   _stream(aStream),
  36.   _timeoutOpen(-1),
  37.   _timeoutIO(-1),
  38.   _timeoutDownload(-1),
  39.   _finalTime(NULL)
  40. {}
  41. // SBinetTimedStream::~SBinetTimedStream
  42. // Refer to SBinetTimedStream.hpp for doc.
  43. SBinetTimedStream::~SBinetTimedStream()
  44. {
  45.   delete _finalTime;
  46.   delete _stream;
  47. }
  48. VXIinetResult SBinetTimedStream::Open(VXIint flags,
  49.                                       const VXIMap* properties,
  50.                                       VXIMap* streamInfo)
  51. {
  52.   // For Cache Stream, allow only one channel to access the cache & validator at 
  53.   // a time to avoid race-condition.  Must lock here to avoid time-out in case other
  54.   // channel holds the lock just a bit longer. 
  55.   const VXIchar* fnname = L"SBinetTimedStream::Open";
  56.   VXItrdMutexRef* elock = NULL;
  57.   if( _stream->getType() == SBinetStream_CACHE ) {
  58.     elock = SBinetChannel::acquireEntryLock(_stream->getURL()->getAbsolute(), this);
  59.   }
  60.   
  61.   if(!SBinetUtils::getInteger(properties,
  62.                               INET_TIMEOUT_DOWNLOAD,
  63.                               _timeoutDownload))
  64.   {
  65.     //_timeoutDownload = INET_TIMEOUT_DOWNLOAD_DEFAULT;
  66.     _timeoutDownload = SBinetChannel::getPageLoadTimeout();
  67.   }
  68.   delete _finalTime;
  69.   if (_timeoutDownload < 0)
  70.   {
  71.     _finalTime = NULL;
  72.   }
  73.   else
  74.   {
  75.     _finalTime = new SWITimeStamp;
  76.     _finalTime->setTimeStamp();
  77.     _finalTime->addDelay(_timeoutDownload);
  78.   }
  79.   setDelay(_timeoutOpen);
  80.   VXIinetResult rc = _stream->Open(flags, properties, streamInfo);
  81.   if (rc == VXIinet_RESULT_FETCH_TIMEOUT)
  82.     Error(236, L"%s%i", L"Timeout", _timeoutDownload);
  83.   _stream->setTimeOut(NULL);
  84.   
  85.   // Release the entry lock to allow other channel to gain access
  86.   if( _stream->getType() == SBinetStream_CACHE ) {
  87.     SBinetChannel::releaseEntryLock(elock, this);
  88.     // Sometimes it may take a bit longer to open the cache sucessfully
  89.     // that may result in timeout for read, but it is not correct, so reset it
  90.     if( hasTimedOut() ) {
  91.       _finalTime->setTimeStamp();
  92.       _finalTime->addDelay(_timeoutDownload);        
  93.     }    
  94.   } 
  95.   return rc;
  96. }
  97. VXIinetResult SBinetTimedStream::Read(/* [OUT] */ VXIbyte*         pBuffer,
  98.                                       /* [IN]  */ VXIulong         nBuflen,
  99.                                       /* [OUT] */ VXIulong*        pnRead )
  100. {
  101.   setDelay(_timeoutIO);
  102.   VXIinetResult rc =  _stream->Read(pBuffer, nBuflen, pnRead);
  103.   if (rc == VXIinet_RESULT_FETCH_TIMEOUT)
  104.     Error(237, L"%s%i", L"Timeout", _timeoutDownload);
  105.   _stream->setTimeOut(NULL);
  106.   return rc;
  107. }
  108. VXIinetResult SBinetTimedStream::Write(/* [IN]  */ const VXIbyte*   pBuffer,
  109.                                        /* [IN]  */ VXIulong         nBuflen,
  110.                                        /* [OUT] */ VXIulong*        pnWritten)
  111. {
  112.   setDelay(_timeoutIO);
  113.   VXIinetResult rc =  _stream->Write(pBuffer, nBuflen, pnWritten);
  114.   if (rc == VXIinet_RESULT_FETCH_TIMEOUT)
  115.     Error(238, L"%s%i", L"Timeout", _timeoutDownload);
  116.   _stream->setTimeOut(NULL);
  117.   return rc;
  118. }
  119. VXIinetResult SBinetTimedStream::Close()
  120. {
  121.   return _stream->Close();
  122. }
  123. void SBinetTimedStream::setDelay(int timeoutFromNow)
  124. {
  125.   if (timeoutFromNow < 0)
  126.   {
  127.     _stream->setTimeOut(_finalTime);
  128.   }
  129.   else
  130.   {
  131.     SWITimeStamp expirationTime;
  132.     expirationTime.setTimeStamp();
  133.     expirationTime.addDelay(timeoutFromNow);
  134.     if (_finalTime == NULL ||
  135.         _finalTime->compare(expirationTime) > 0)
  136.       _stream->setTimeOut(&expirationTime);
  137.     else
  138.       _stream->setTimeOut(_finalTime);
  139.   }
  140. }
  141. bool SBinetTimedStream::hasTimedOut() const
  142. {
  143.   if( !_finalTime ) return false;
  144.   if (_finalTime->getSecs() == (time_t) 0)
  145.     return false;
  146.   SWITimeStamp now;
  147.   now.setTimeStamp();
  148.   return _finalTime->compare(now) <= 0;
  149. }