蔡军生先生第二人生的源码分析(六十一)LLCurl类实现libcurl库封装

本文详细解析了LLCurl库的类声明与部分关键方法,包括数据响应处理、HTTPS认证配置及初始化等,并预告了后续将深入分析Easy类。
通过上面介绍了libcurl库的功能,那么在第二人生里到底是怎么样使用它的呢?又是怎么样封装它,才会更好用呢?下面就来分析一下它的类声明:
#001 // For whatever reason, this is not typedef'd in curl.h
#002 typedef size_t (*curl_header_callback)(void *ptr, size_t size, size_t nmemb, void *stream);
#003 
#004 class LLCurl
#005 {
 
定义LOG类型信息。
#006    LOG_CLASS(LLCurl);
#007   
#008 public:
 
声明两个嵌套类。
#009    class Easy;
#010    class Multi;
#011 
 
声明传送数据信息的结构。
#012    struct TransferInfo
#013    {
#014        TransferInfo() : mSizeDownload(0.0), mTotalTime(0.0), mSpeedDownload(0.0) {}
#015         F64 mSizeDownload;
#016         F64 mTotalTime;
#017         F64 mSpeedDownload;
#018    };
#019   
 
下面是定义数据响应类。
#020    class Responder
#021    {
#022    //LOG_CLASS(Responder);
#023    public:
#024 
#025        Responder();
#026        virtual ~Responder();
#027 
#028        /**
#029         * @brief return true if the status code indicates success.
#030         */
#031        static bool isGoodStatus(U32 status)
#032        {
#033            return((200 <= status) && (status < 300));
#034        }
#035       
#036        virtual void error(U32 status, const std::string& reason);
#037            // called with non-200 status codes
#038       
#039        virtual void result(const LLSD& content);
#040       
#041        // Override point for clients that may want to use this class when the response is some other format besides LLSD
#042        virtual void completedRaw(U32 status, const std::string& reason,
#043                                  const LLChannelDescriptors& channels,
#044                                  const LLIOPipe::buffer_ptr_t& buffer);
#045 
#046        virtual void completed(U32 status, const std::string& reason, const LLSD& content);
#047            /**< The default implemetnation calls
#048                either:
#049                * result(), or
#050                * error()
#051            */
#052           
#053            // Override to handle parsing of the header only. Note: this is the only place where the contents
#054            // of the header can be parsed. In the ::completed call above only the body is contained in the LLSD.
#055            virtual void completedHeader(U32 status, const std::string& reason, const LLSD& content);
#056 
#057    public: /* but not really -- don't touch this */
#058        U32 mReferenceCount;
#059    };
#060    typedef boost::intrusive_ptr<Responder> ResponderPtr;
#061 
#062 
 
下面函数设置HTTPS授权认证文件。
#063    /**
#064     * @ brief Set certificate authority file used to verify HTTPS certs.
#065     */
#066    static void setCAFile(const std::string& file);
#067 
 
下面函数设置HTTPS授权认证的路径。
#068    /**
#069     * @ brief Set certificate authority path used to verify HTTPS certs.
#070     */
#071    static void setCAPath(const std::string& path);
#072   
 
下面函数获取HTTPS授权认证文件。
#073    /**
#074     * @ brief Get certificate authority file used to verify HTTPS certs.
#075     */
#076    static const std::string& getCAFile() { return sCAFile; }
#077 
 
下面函数获取HTTPS授权文件路径。
#078    /**
#079     * @ brief Get certificate authority path used to verify HTTPS certs.
#080     */
#081    static const std::string& getCAPath() { return sCAPath; }
#082 
 
下面函数初始化LLCurl类。
#083    /**
#084     * @ brief Initialize LLCurl class
#085     */
#086    static void initClass();
#087 
 
下面函数清除LLCurl类。
#088    /**
#089     * @ brief Cleanup LLCurl class
#090     */
#091    static void cleanupClass();
#092 
 
获取类LLCurl的出错码字符串。
#093    /**
#094     * @ brief curl error code -> string
#095     */
#096    static std::string strerror(CURLcode errorcode);
#097   
 
设置SSL回调函数。
#098    // For OpenSSL callbacks
#099    static std::vector<LLMutex*> sSSLMutex;
#100 
#101    // OpenSSL callbacks
#102    static void LLCurl::ssl_locking_callback(int mode, int type, const char *file, int line);
#103    static unsigned long LLCurl::ssl_thread_id(void);
#104   
#105   
#106   
#107 private:
#108 
#109    static std::string sCAPath;
#110    static std::string sCAFile;
#111 };
 
上面就定义LLCurl类,声明了两个重要的嵌套类Easy和Multi,在类Easy里会封装libcurl基本的功能,下一次再仔细地分析它。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值