📄 sbinet.cpp
字号:
/****************License************************************************ * * Copyright 2001. SpeechWorks International, Inc. * * Use of this software is subject to notices and obligations set forth * in the SpeechWorks Public License - Software Version 1.1 which is * included with this software. * * SpeechWorks is a registered trademark, and SpeechWorks Here, * DialogModules and the SpeechWorks logo are trademarks of SpeechWorks * International, Inc. in the United States and other countries. * *********************************************************************** * * SBinet Implementation * * $Id: SBinet.cpp,v 1.96.2.2.2.9 2001/10/09 16:37:52 dmeyer Exp $ * ***********************************************************************/#ifndef _SB_USE_STD_NAMESPACE#define _SB_USE_STD_NAMESPACE#endif#ifdef WIN32#ifndef UNICODE#define UNICODE#endif#ifndef _UNICODE#define _UNICODE#endif#endif /* WIN32 */#include <stdio.h>#include <stdlib.h>#include <stdarg.h>#include <wctype.h>#include <WWWLib.h>#include <WWWHTTP.h>#include <WWWInit.h>#include <HTUtils.h>#ifdef EINVAL// Conflicts with OS definition#undef EINVAL#endif#define OSBINET_EXPORTS#include "VXItypes.h"#include "VXIvalue.h"#include "VXIinet.h"#ifdef OPENVXI#define SBINET_API static#else#define SBINET_EXPORTS#include "SBinet.h"#endif#include "OSBinet.h"#include "VXItrd.h"#include "VXIlog.h"#ifdef WIN32#undef HTTP_VERSION#define WIN32_LEAN_AND_MEAN#include <windows.h>#include <wininet.h>#endif /* WIN32 */#include "SBHTEvtLst.h"#include "SBinetLog.h"#include "SBinetURL.h"#include "SBinetStream.h"#include "SBinetChannel.h"#include "SBinetCookie.h"#include "SBinetCacheLock.h"class SBinet : public SBinetLogger {public: SBinet( VXIlogInterface *log, VXIunsigned diagLogBase ); virtual ~SBinet(); VXIinetResult Init(const VXIchar* pszCacheFolder, const VXIint nCacheTotalSizeMB, const VXIint nCacheEntryMaxSizeMB, const VXIint nCacheEntryExpTimeSec, const VXIchar* pszProxyServer, const VXIulong nProxyPort, const VXIchar* userAgentName, const VXIMap* extensionRules); VXIinetResult Terminate(VXIbool clearLogResource); VXItrdResult LockLibwww() { return VXItrdMutexLock(m_pLibwwwMutex); } VXItrdResult UnlockLibwww() { return VXItrdMutexUnlock(m_pLibwwwMutex); } const VXIString* mapExtension(const VXIchar* ext) { if (m_extensionRules == NULL) return (NULL); if (ext == NULL) return (NULL); return ((const VXIString*) VXIMapGetProperty(m_extensionRules,ext)); }private: VXIinetResult CheckEnvironment(); VXIinetResult InitializeCache(); VXIinetResult InitializeProxy(); VXIinetResult TerminateCache(); static VXITRD_DEFINE_THREAD_FUNC( EventThreadMain, userData ); static BOOL CacheLockRemovalConfirmCallback(HTRequest *request, HTAlertOpcode op, int msgnum, const char *deflt, void *input, HTAlertPar *reply); static int PrintCallback(const char * fmt, va_list pArgs); static int TraceCallback(const char * fmt, va_list pArgs); static int TraceDataCallback(char * data, size_t len, char * fmt, va_list pArgs); static BOOL AlertCallback( HTRequest* pHtRequest, HTAlertOpcode eOpCode, int nMessageNum, const char* pszDefault, void* pvInput, HTAlertPar* pReply ); static void OutOfMemoryExitCallback(char *name, char *file, unsigned long line);private: VXItrdThread* m_pEventThread; VXItrdMutex* m_pLibwwwMutex; VXItrdTimer* m_pEventTimer; VXIulong m_nSleepInterval; // 5 second sleep interval VXIbool m_fDone; VXIbool m_fShutdown; VXItrdTimer * m_nInitializeStatus; SBinetString m_strCacheFolder; VXIint m_nCacheTotalSizeMB; VXIint m_nCacheEntryMaxSizeMB; VXIint m_nCacheEntryExpTimeSec; VXIbool m_fInitializedCache; SBinetString m_strProxyServer; VXIulong m_nProxyPort; SBinetNString m_strUserAgentApp; SBinetNString m_strUserAgentVersion; VXIMap* m_extensionRules; int runEventLoop;};// Static class to ensure the global inet is destroyed even if// SBinetShutDown() is not called, otherwise the libwww based cache// is not properly shutdownclass SBinetTerminate {public: SBinetTerminate() { } virtual ~SBinetTerminate();};/***************************************************************************** ***************************************************************************** * Globals ***************************************************************************** ***************************************************************************** */// The Global inet object. Manages Event Thread, etc.static SBinet *g_SBinet = NULL;// Allows proper shutdown even when SBinetShutdown() is not calledstatic SBinetTerminate g_SBinetTerminate;// Global diagnostic logging baseVXIunsigned g_SBinetDiagLogBase = 0;/***************************************************************************** ***************************************************************************** * The Event Thread entry. Inits libwww event stuff ***************************************************************************** ***************************************************************************** */VXITRD_DEFINE_THREAD_FUNC( SBinet::EventThreadMain, userData ){ SBinet *pThis = (SBinet *) userData; HTList * converters = NULL; HTList * transfer_encodings = NULL; HTList * content_encodings = NULL; // HTProfile_newPreemptiveClient("TestApp", "1.0"); // HTProfile_newNoCacheClient("TestApp", "1.0"); if (!HTLib_isInitialized()) HTLibInit(pThis->m_strUserAgentApp.c_str(), pThis->m_strUserAgentVersion.c_str()); /* Register the default set of messages and dialog functions, for now everything goes to the diagnostic log */ HTAlertInit(); HTAlert_setInteractive(YES); HTAlert_add( AlertCallback, HT_A_PROGRESS ); HTAlert_add( AlertCallback, HT_A_MESSAGE ); HTAlert_add( AlertCallback, HT_A_CONFIRM ); HTAlert_add( AlertCallback, HT_A_PROMPT ); HTAlert_add( AlertCallback, HT_A_SECRET ); HTAlert_add( AlertCallback, HT_A_USER_PW ); /* Register the default out of memory handler */ HTMemory_setExit(OutOfMemoryExitCallback); if (!converters) converters = HTList_new(); if (!transfer_encodings) transfer_encodings = HTList_new(); if (!content_encodings) content_encodings = HTList_new(); /* Register the default set of transport protocols */ HTTransportInit(); /* Register the default set of application protocol modules */ //HTProtocolPreemptiveInit(); HTProtocolInit(); /* Initialize suffix bindings for local files */ HTBind_init(); /* Set max number of sockets we want open simultanously */ HTNet_setMaxSocket(32); /* The persistent cache does not work in preemptive mode */ /* We init cach later */ /* Register the default set of BEFORE and AFTER filters */ HTNetInit(); /* Set up the default set of Authentication schemes */ HTAAInit(); /* Get any proxy or gateway environment variables */ HTProxy_getEnvVar(); /* Register the default set of converters */ HTConverterInit(converters); /* Set the convertes as global converters for all requests */ HTFormat_setConversion(converters); /* Register the default set of transfer encoders and decoders */ HTTransferEncoderInit(transfer_encodings); HTFormat_setTransferCoding(transfer_encodings); /* Register the default set of content encoders and decoders */ HTContentEncoderInit(content_encodings); if (HTList_count(content_encodings) > 0) HTFormat_setContentCoding(content_encodings); else { HTList_delete(content_encodings); content_encodings = NULL; } /* Register the default set of MIME header parsers */ HTMIMEInit(); /* Register the default set of file suffix bindings */ HTFileInit(); /* Register the default set of Icons for directory listings */ HTIconInit(NULL); /* Initialize event loop */ SBinetHTEventInit (); /* Turn on TRACE so we can see what is going on by turning on diagnostic logging tags */ HTPrint_setCallback(PrintCallback); HTTrace_setCallback(TraceCallback); HTSetTraceMessageMask("ospt"); HTTraceData_setCallback(TraceDataCallback); /* * Need to fold startup errors back to main thread */ pThis->InitializeCache(); pThis->InitializeProxy(); // Initiate cookies module HTCookie_init(); HTCookie_setCookieMode((HTCookieMode)(HT_COOKIE_ACCEPT | HT_COOKIE_SEND)); HTCookie_setCallbacks( SBinetInterface::setCookie, NULL, SBinetInterface::findCookie, NULL ); VXItrdTimerWake(pThis->m_nInitializeStatus); SBinetHTEventList_newLoop(); // Start the event loop /* Terminate the Library */ SBinetHTEventTerminate(); HTProfile_delete(); return (0);}// -----1=0-------2=0-------3=0-------4=0-------5=0-------6=0-------7=0-------8/***************************************************************************** ***************************************************************************** * SBinet C routines: Init(), ShutDown(), CreateResource(), DestroyResource() * * NOTE: We provide both OSB and SB prefixed entry points, OSB for the * OpenVXI open source release, SB for the OpenSpeech Browser PIK product. ***************************************************************************** ***************************************************************************** */SBINET_APIVXIinetResult SBinetInit( VXIlogInterface* piVXILog, const VXIunsigned diagLogBase, const VXIchar* pszCacheFolder, const VXIint nCacheTotalSizeMB, const VXIint nCacheEntryMaxSizeMB, const VXIint nCacheEntryExpTimeSec, const VXIchar* pszProxyServer, const VXIulong nProxyPort, const VXIchar* userAgentName, const VXIMap* extensionRules, const VXIVector* reserved ){ VXIinetResult rc = VXIinet_RESULT_SUCCESS; g_SBinetDiagLogBase = diagLogBase; SBinetLogFunc apiTrace (piVXILog, diagLogBase+ MODULE_SBINET_TAGID, L"SBinetInit", (int *) &rc, L"entering: 0x%p, %s, %d, %d, %d, %s, %lu, %s, " L"0x%p, 0x%p", piVXILog, pszCacheFolder, nCacheTotalSizeMB, nCacheEntryMaxSizeMB, nCacheEntryExpTimeSec, pszProxyServer, nProxyPort, userAgentName, extensionRules, reserved); /* check the remaining arguments */ if ((nCacheTotalSizeMB < 0) || (nCacheEntryMaxSizeMB < 0) || (nCacheEntryExpTimeSec < 0) || (nCacheEntryMaxSizeMB > nCacheTotalSizeMB)) { SBinetLogger::Error(piVXILog,MODULE_SBINET,200,NULL); SBinetLogger::Error(piVXILog,MODULE_SBINET,100,NULL); return VXIinet_RESULT_INVALID_ARGUMENT; } // Global SBinet initialization if (! g_SBinet) { g_SBinet = new SBinet(piVXILog, diagLogBase); if (! g_SBinet) return VXIinet_RESULT_OUT_OF_MEMORY; rc = g_SBinet->Init(pszCacheFolder, nCacheTotalSizeMB, nCacheEntryMaxSizeMB, nCacheEntryExpTimeSec, pszProxyServer, nProxyPort, userAgentName, extensionRules); if (rc != VXIinet_RESULT_SUCCESS) { // To avoid use of global during shutdown SBinet *temp = g_SBinet; g_SBinet = NULL; delete temp; } } return ( rc );}OSBINET_APIVXIinetResult OSBinetInit( VXIlogInterface* piVXILog, const VXIunsigned diagLogBase, const VXIchar* pszCacheFolder, const VXIint nCacheTotalSizeMB, const VXIint nCacheEntryMaxSizeMB, const VXIint nCacheEntryExpTimeSec, const VXIchar* pszProxyServer, const VXIulong nProxyPort, const VXIchar* userAgentName, const VXIMap* extensionRules, const VXIVector* reserved ){ return SBinetInit(piVXILog, diagLogBase, pszCacheFolder, nCacheTotalSizeMB, nCacheEntryMaxSizeMB, nCacheEntryExpTimeSec, pszProxyServer, nProxyPort, userAgentName, extensionRules, reserved);}SBINET_APIVXIinetResult SBinetShutDown( VXIlogInterface* piVXILog ){ VXIinetResult rc = VXIinet_RESULT_SUCCESS; SBinetLogFunc apiTrace (piVXILog, g_SBinetDiagLogBase + MODULE_SBINET_TAGID, L"SBinetShutdown", (int *) &rc, L"entering: 0x%p", piVXILog); if (! g_SBinet) return VXIinet_RESULT_NON_FATAL_ERROR; // Use a temporary to avoid references to the global during destruction SBinet *temp = g_SBinet; g_SBinet = NULL; temp->Terminate(FALSE); delete temp; return (rc);}OSBINET_APIVXIinetResult OSBinetShutDown( VXIlogInterface* piVXILog ){ return SBinetShutDown(piVXILog);}SBINET_APIVXIinetResult SBinetCreateResource( VXIlogInterface* piVXILog, VXIinetInterface** ppiVXIInet ){ VXIinetResult rc = VXIinet_RESULT_SUCCESS; SBinetLogFunc apiTrace (piVXILog, g_SBinetDiagLogBase + MODULE_SBINET_TAGID, L"SBinetCreateResource", (int *) &rc, L"entering: 0x%p, 0x%p", piVXILog, ppiVXIInet); if (! g_SBinet) return VXIinet_RESULT_FATAL_ERROR; if (!piVXILog || !ppiVXIInet) { SBinetLogger::Error(piVXILog,MODULE_SBINET,102,NULL); return (rc = VXIinet_RESULT_INVALID_ARGUMENT); } SBinetInterface* pInter = new SBinetInterface(piVXILog, g_SBinetDiagLogBase); if (! pInter) { SBinetLogger::Error(piVXILog,MODULE_SBINET,103,NULL); return (rc = VXIinet_RESULT_OUT_OF_MEMORY); } // Could add to Channel/Interface table for validation but... *ppiVXIInet = (VXIinetInterface*)pInter; // Downcast... return (rc);}OSBINET_APIVXIinetResult OSBinetCreateResource( VXIlogInterface* piVXILog, VXIinetInterface** ppiVXIInet ){ return SBinetCreateResource(piVXILog, ppiVXIInet);}SBINET_APIVXIinetResult SBinetDestroyResource( VXIinetInterface** ppiVXIInet ){ if (! g_SBinet) return VXIinet_RESULT_FATAL_ERROR; VXIlogInterface* piVXILog = g_SBinet->GetLog(); VXIinetResult rc = VXIinet_RESULT_SUCCESS; SBinetLogFunc apiTrace (piVXILog, g_SBinetDiagLogBase + MODULE_SBINET_TAGID, L"SBinetDestroyResource", (int *) &rc, L"entering: 0x%p (0x%p)", ppiVXIInet, (ppiVXIInet ? *ppiVXIInet : NULL)); if (! ppiVXIInet) { g_SBinet->Error(102,NULL); return (rc = VXIinet_RESULT_INVALID_ARGUMENT); } if (! *ppiVXIInet) { g_SBinet->Error(102,NULL); return (rc = VXIinet_RESULT_INVALID_ARGUMENT); } SBinetInterface* pSBinet = (SBinetInterface*)(*ppiVXIInet); // Could delete to Channel/Interface table for validation but... // Delete it, destructor cleans up delete pSBinet; *ppiVXIInet = static_cast<VXIinetInterface*>(NULL); return (rc);}OSBINET_APIVXIinetResult OSBinetDestroyResource( VXIinetInterface** ppiVXIInet ){ return SBinetDestroyResource(ppiVXIInet);}/**************************************************************************** **************************************************************************** * SBinetTerminate methods * **************************************************************************** **************************************************************************** */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -