您的位置:首页 > 其它

第二人生的源码分析(六十二)类Easy实现多协议文件传送

2008-05-14 20:42 791 查看
从上一节里可以看到,在类LLCurl声明里主要声明嵌套类Easy,最后通过嵌套类Easy来实现libcurl功能,下面就来仔细地分析它。它的类声明如下:
#001 class LLCurl::Easy
#002 {
#003 LOG_CLASS(Easy);
#004
#005 private:
#006 Easy();
#007
#008 public:

用工厂模式来创建类实例。
#009 static Easy* getEasy();
#010 ~Easy();
#011

返回当前libcurl库的句柄。
#012 CURL* getCurlHandle() const { return mCurlEasyHandle; }
#013

设置错误缓冲区。
#014 void setErrorBuffer();

设置授权认证。
#015 void setCA();
#016

调用库的函数curl_easy_setopt。
#017 void setopt(CURLoption option, S32 value);
#018 // These assume the setter does not free value!
#019 void setopt(CURLoption option, void* value);
#020 void setopt(CURLoption option, char* value);
#021 // Copies the string so that it is gauranteed to stick around
#022 void setoptString(CURLoption option, const std::string& value);
#023

调用库函数curl_slist_append来添加到列表头里面。
#024 void slist_append(const char* str);

设置HTTP协议头数据。
#025 void setHeaders();
#026

报告错误信息。
#027 U32 report(CURLcode);

获取传送信息。
#028 void getTransferInfo(LLCurl::TransferInfo* info);
#029

在使用库下载数据前的设置。
#030 void prepRequest(const std::string& url, ResponderPtr, bool post = false);
#031
#032 const char* getErrorBuffer();
#033

获取输入流对象。
#034 std::stringstream& getInput() { return mInput; }

获取输出协议头流对象。
#035 std::stringstream& getHeaderOutput() { return mHeaderOutput; }

获取输出流对象。
#036 LLIOPipe::buffer_ptr_t& getOutput() { return mOutput; }
#037 const LLChannelDescriptors& getChannels() { return mChannels; }
#038

清空所有流对象和一些状态。
#039 void resetState();
#040
#041 private:
#042 CURL* mCurlEasyHandle;
#043 struct curl_slist* mHeaders;
#044
#045 std::stringstream mRequest;
#046 LLChannelDescriptors mChannels;
#047 LLIOPipe::buffer_ptr_t mOutput;
#048 std::stringstream mInput;
#049 std::stringstream mHeaderOutput;
#050 char mErrorBuffer[CURL_ERROR_SIZE];
#051
#052 // Note: char*'s not strings since we pass pointers to curl
#053 std::vector<char*> mStrings;
#054
#055 ResponderPtr mResponder;
#056 };

上面的类Easy封装了函数curl_easy_init来初始化一个协议实例,函数curl_easy_cleanup来删除相应的实例,函数curl_easy_reset来复位相关状态,通过函数curl_easy_getinfo来获取信息。最后通过函数curl_easy_setopt来设置不同的参数,这样才实现libcurl库的功能调用。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: