http客户端-基于boost开发
基于BOOST编写的http客户端,作为BOOST开发学习之用。目前支持功能:
- http协议,单向链接返回http response code 200
- 可content type 为text或image下载到本地
- 仅支持http返回code为200,不支持3XX、4XX等
- 暂不支持chunk传输,chunk代码待调试
- 日志文件,提供类ACE的输出方式,如LOG((LOG_DEBUG,"[%T(%t)] test begin %d %s\n"), int_num_2, string_test.c_str());
- 数据缓冲区,当前为new方式,后续可更改为从boost pool获取
- 。。。
2 核心代码
2.1 IOServer,提供asio线程环境
1 #pragma once 2 #include "boost/serialization/singleton.hpp" 3 #include "boost/asio.hpp" 4 #include "boost/thread.hpp" 5 #include "boost/bind.hpp" 6 #include "Event.h" 7 #include "../concurrent/IRunable.h" 8 #include "SimpleLog.h" 9 class IO_Server : public IRunable 10 { 11 public: 12 IO_Server(): bExit(false) 13 { 14 } 15 boost::asio::io_service* GetIOS() 16 { 17 return &ios; 18 } 19 void ActiveIOS() 20 { 21 boost::unique_lock<boost::mutex> lock(mu); 22 if (ios.stopped()) 23 { 24 cond.notify_one(); 25 } 26 } 27 void Exit() 28 { 29 ios.stop(); 30 bExit = true; 31 } 32 private: 33 virtual int Run() 34 { 35 LOG((LOG_DEBUG,"[%T(%t)] ios server run ,id = %d\n", boost::this_thread::get_id())); 36 ios.stop(); 37 while (1) 38 { 39 // 设置退出线程 40 if (bExit) 41 { 42 break; 43 } 44 // 45 { 46 boost::unique_lock<boost::mutex> lock(mu); 47 if (ios.stopped()) 48 { 49 cond.wait(mu); 50 } 51 } 52 if (ios.stopped()) 53 { 54 ios.reset(); 55 ios.run(); 56 } 57 } 58 return 0; 59 } 60 private: 61 boost::asio::io_service ios; //所有的asio都要有个ios 62 boost::mutex mu; 63 boost::condition_variable_any cond; 64 bool bExit; 65 }; 66 typedef boost::serialization::singleton<IO_Server> IOS;