📄 readerwriternet.cpp
字号:
#include <iostream>#include <fstream>#include <sstream>#include <osg/Notify>#include <osgDB/Input>#include <osgDB/Registry>#include <osgDB/FileUtils>#include <osgDB/FileNameUtils>#include <osgDB/ReadFile>#include <osgDB/WriteFile>#include <osgDB/FileUtils>#include <osgDB/Archive>#include <osg/MatrixTransform>#include <osg/Group>#include <osg/Timer>#include "sockinet.h" /* * Semantics: * Two methods for using the .net loader. * 1) Add a hostname prefix and a '.net' suffix on a model when passing * to osgDB::readNodeFile() * e.g: osgDB::readNodeFile( "openscenegraph.org:cow.osg.net" ); * * 2) Explicitely load the .net plugin and pass the plugin options including * hostname=<hostname> * * Method #1 takes precedence. SO, if the hostname option is passed the * plugin, but the name also contains a hostname prefix, the hostname * prefix on the file name will override the option * * Plugin options: * hostname=<hostname> - Specify the host where the data file is to * be fetched from. * * prefix=<prefix> - Specify a server directory to prefix the * file name with. * * local_cache_dir=<dir> - Specify a directory in which to cache files * on the local machine once they've been fetched. * This directory is also searched before fetching * the file from the server when Read mode is * enabled on the cache. * * cache_mode=<mode> - Set the mode for the local cache if local_cache * was specified. If local_cache was not specified * this directive is ignored. <mode> may * be specified with ReadOnly, WriteOnly, or * ReadWrite. Behavior for the different modes is * defined as: * * ReadOnly - When retrieving files, cache is * searched first, and if the file is * not present, it is fetched from the * server. If it is fetched from the * server it is not stored in local cache * * WriteOnly - When retrieving files, cache is not * searched, file is always retrieved * from the server and always written to * cache. * * ReadWrite - (the default). When retrieving files * cache is searched first, if file is * not present in cache, it is fetched from * the server. If fetched, it is written * to cache. * * proxy_host=<hostname> - Specify the name of a Proxy host. Proxies are not * used by default * * proxy_port=<portnumber> - When using a Proxy host, specify the port. The * internal variable defaults to 8080. * * Environmental Variables: * * OSG_PROXY_HOST Specifies the name of a Proxy host. Overrides options. * * OSG_PROXY_PORT When using a Proxy, sets the proxy port deliberately. * The internal variable defaults to 8080. */class NetReader : public osgDB::ReaderWriter{ public: NetReader() { supportsProtocol("http","HTTP Protocol"); supportsExtension("net","Psuedo loader extension for selecting NET plugin"); supportsExtension("*","Passes all file loading onto other plugins"); } virtual const char* className() const { return "HTTP Protocol Model Reader"; } virtual bool acceptsExtension(const std::string& extension) const { return osgDB::equalCaseInsensitive(extension,"net"); } enum ObjectType { OBJECT, ARCHIVE, IMAGE, HEIGHTFIELD, NODE }; virtual ReadResult openArchive(const std::string& fileName,ArchiveStatus status, unsigned int , const Options* options) const { if (status!=READ) return ReadResult(ReadResult::FILE_NOT_HANDLED); else return readFile(ARCHIVE,fileName,options); } virtual ReadResult readObject(const std::string& fileName, const Options* options) const { return readFile(OBJECT,fileName,options); } virtual ReadResult readImage(const std::string& fileName, const Options *options) const { return readFile(IMAGE,fileName,options); } virtual ReadResult readHeightField(const std::string& fileName, const Options *options) const { return readFile(HEIGHTFIELD,fileName,options); } virtual ReadResult readNode(const std::string& fileName, const Options *options) const { return readFile(NODE,fileName,options); } ReadResult readFile(ObjectType objectType, osgDB::ReaderWriter* rw, std::istream& fin, const Options *options) const { switch(objectType) { case(OBJECT): return rw->readObject(fin,options); case(ARCHIVE): return rw->openArchive(fin,options); case(IMAGE): return rw->readImage(fin,options); case(HEIGHTFIELD): return rw->readHeightField(fin,options); case(NODE): return rw->readNode(fin,options); default: break; } return ReadResult::FILE_NOT_HANDLED; } virtual ReadResult readFile(ObjectType objectType, const std::string& inFileName, const Options *options) const { osg::Timer_t start = osg::Timer::instance()->tick(); osg::notify(osg::NOTICE) << "osgPlugin .net: start load" << inFileName << std::endl; std::string hostname; std::string serverPrefix; std::string localCacheDir; int port = 80; //CARLO: proxy modf std::string proxyHost; int proxyPort = 8080; enum CacheMode { Read = 1, Write = 2, ReadWrite = 3 }; std::string cacheFile; CacheMode cacheMode = ReadWrite; if (options) { std::istringstream iss(options->getOptionString()); std::string opt; while (iss >> opt) { int index = opt.find( "=" ); if( opt.substr( 0, index ) == "hostname" || opt.substr( 0, index ) == "HOSTNAME" ) { hostname = opt.substr( index+1 ); } else if( opt.substr( 0, index ) == "port" || opt.substr( 0, index ) == "PORT" ) { port = atoi( opt.substr(index+1).c_str() ); } else if( opt.substr( 0, index ) == "proxy_hostname" || opt.substr( 0, index ) == "PROXY_HOSTNAME" ) //CARLO: proxy modf { proxyHost = opt.substr(index+1); osg::notify(osg::WARN) << "proxy host " << proxyHost << std::endl; } else if( opt.substr( 0, index ) == "proxy_port" || opt.substr( 0, index ) == "PROXY_PORT" ) { proxyPort = atoi( opt.substr(index+1).c_str() ); } else if( opt.substr( 0, index ) == "server_prefix" || opt.substr( 0, index ) == "SERVER_PREFIX" || opt.substr( 0, index ) == "prefix" || opt.substr( 0, index ) == "PREFIX" ) { serverPrefix = opt.substr(index+1); } else if( opt.substr( 0, index ) == "local_cache_dir" || opt.substr( 0, index ) == "LOCAL_CACHE_DIR" ) { localCacheDir = opt.substr(index+1); osg::notify(osg::INFO)<<"LOCAL_CACHE_DIR="<<localCacheDir<<std::endl; } else if( opt.substr( 0, index ) == "cache_mode" || opt.substr( 0, index ) == "CACHE_MODE" ) { if( opt.substr(index+1) == "ReadOnly" ) cacheMode = Read; else if( opt.substr(index+1) == "WriteOnly" ) cacheMode = Write; else if( opt.substr(index+1) == "ReadWrite" ) cacheMode = ReadWrite; else osg::notify(osg::WARN) << "NET plug-in warning: cache_mode " << opt.substr(index+1) << " not understood. Defaulting to ReadWrite." << std::endl; } } } // Env variables should override plug-in options. { char * env_proxyHost = getenv("OSG_PROXY_HOST"); //Checking proxy environment variables char * env_proxyPort = getenv("OSG_PROXY_PORT"); if( env_proxyHost ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -