提交 038bcff37f02021682fe48e33d882aec7fca74cc

作者 qingxiongf
1 个父辈 3787f4fa

f

@@ -47,11 +47,12 @@ bool DmpVectorLayer::writeXml(boost::property_tree::ptree &layerNode) @@ -47,11 +47,12 @@ bool DmpVectorLayer::writeXml(boost::property_tree::ptree &layerNode)
47 47
48 void DmpVectorLayer::setRenderer(DmpFeatureRenderer *renderer) 48 void DmpVectorLayer::setRenderer(DmpFeatureRenderer *renderer)
49 { 49 {
50 - 50 +
51 } 51 }
52 52
53 void DmpVectorLayer::setDataSource(const std::string &dataSource, const std::string &baseName, const std::string &provider, bool loadDefaultStyleFlag) 53 void DmpVectorLayer::setDataSource(const std::string &dataSource, const std::string &baseName, const std::string &provider, bool loadDefaultStyleFlag)
54 { 54 {
  55 +
55 DmpWkbTypes::GeometryType geomType = GeometryType(); 56 DmpWkbTypes::GeometryType geomType = GeometryType();
56 } 57 }
57 58
1 -[Core]  
2 -Filter="%Severity% >= debug"  
3 -  
4 -[Sinks.TextFileSettings]  
5 -Destination=TextFile  
6 -FileName="dmap_%Y%m%d.log"  
7 -AutoFlush=true  
8 -Format="[%TimeStamp%] [%Uptime%] [%Scope%] <%Severity%> %Message%"  
9 -Asynchronous=false  
10 -Append=true 1 +[Core]
  2 +Filter="%Severity% >= debug"
  3 +
  4 +[Sinks.TextFileSettings]
  5 +Destination=TextFile
  6 +FileName="dmap_%Y%m%d.log"
  7 +AutoFlush=true
  8 +Format="[%TimeStamp%] [%Uptime%] [%Scope%] <%Severity%> %Message%"
  9 +Asynchronous=false
  10 +Append=true
1 -/**************************************************************************  
2 -* file: dmphttpbase.cpp  
3 -  
4 -* Author: lijiahuan  
5 -* Date: 2021-12-09 17:41:00  
6 -* Email: jiahuanl@chinadci.com  
7 -* copyright: 广州城市信息研究所有限公司  
8 -***************************************************************************/  
9 -#include "dmphttpbase.h"  
10 -  
11 -#define HTTP_JSON_BEGIN ("[")  
12 -#define HTTP_JSON_END ("]")  
13 -  
14 -DmpHttp::DmpHttpBase::DmpHttpBase(){}  
15 -DmpHttp::DmpHttpBase::~DmpHttpBase(){}  
16 -int DmpHttp::DmpHttpBase::parseUrl(const std::string& url, std::string& out_server,  
17 - std::string& out_port, std::string& out_path){  
18 -const std::string& http_ = "http://";  
19 -const std::string& https_ = "https://";  
20 -std::string temp_data = url;  
21 -if (temp_data.find(http_) == 0) {  
22 - temp_data = temp_data.substr(http_.length());  
23 - }  
24 - else if (temp_data.find(https_) == 0) {  
25 - temp_data = temp_data.substr(https_.length());  
26 - }  
27 - else {  
28 - return HTTP_ERROR_HTTP_HEAD;  
29 - }  
30 - // 解析域名  
31 - std::size_t idx = temp_data.find('/');  
32 - // 解析 域名后的page  
33 - if (std::string::npos == idx) {  
34 - out_path = "/";  
35 - idx = temp_data.size();  
36 - }  
37 - else {  
38 - out_path = temp_data.substr(idx);  
39 - }  
40 - // 解析域名  
41 - out_server = temp_data.substr(0, idx);  
42 - // 解析端口  
43 - idx = out_server.find(':');  
44 - if (std::string::npos == idx) {  
45 - out_port = "80";  
46 - }  
47 - else {  
48 - out_port = out_server.substr(idx + 1);  
49 - out_server = out_server.substr(0, idx);  
50 - }  
51 - return HTTP_SUCCESS;  
52 -}  
53 -  
54 -int DmpHttp::DmpHttpBase::buildPostRequest(const std::string& server, const std::string& path,  
55 - std::ostream& out_request) {  
56 - // 分割path中的json数据  
57 - std::string temp_path(path), temp_json;  
58 - int json_pos_begin = temp_path.find(HTTP_JSON_BEGIN) + 1;  
59 - int json_pos_end = temp_path.find(HTTP_JSON_END);  
60 - if (json_pos_begin != std::string::npos) {  
61 - // 计算json的长度  
62 - int temp_json_lenth = std::string::npos;  
63 - if (json_pos_end != temp_json_lenth) {  
64 - temp_json_lenth = (json_pos_end - json_pos_begin);  
65 - }  
66 - temp_json = temp_path.substr(json_pos_begin, temp_json_lenth);  
67 - temp_path = temp_path.substr(0, (json_pos_begin - 1));  
68 - }  
69 - out_request << "POST " << temp_path.c_str() << " HTTP/1.0\r\n";  
70 - out_request << "Host: " << server.c_str() << "\r\n";  
71 - out_request << "Content-Length: " << temp_json.length() << "\r\n";  
72 - out_request << "Content-Type: application/x-www-form-urlencoded\r\n";  
73 - out_request << "Accept: */*\r\n";  
74 - out_request << "Connection: close\r\n\r\n";  
75 - out_request << temp_json.c_str();  
76 - return HTTP_SUCCESS;  
77 -}  
78 -  
79 -int DmpHttp::DmpHttpBase::buildGetRequest(const std::string& server, const std::string& path,  
80 - std::ostream& out_request) {  
81 - out_request << "GET " << path.c_str() << " HTTP/1.0\r\n";  
82 - out_request << "Host: " << server.c_str() << "\r\n";  
83 - out_request << "Accept: */*\r\n";  
84 - out_request << "Connection: close\r\n\r\n";  
85 - return HTTP_SUCCESS;  
86 -} 1 +/**************************************************************************
  2 +* file: dmphttpbase.cpp
  3 +
  4 +* Author: lijiahuan
  5 +* Date: 2021-12-09 17:41:00
  6 +* Email: jiahuanl@chinadci.com
  7 +* copyright: 广州城市信息研究所有限公司
  8 +***************************************************************************/
  9 +#include "dmphttpbase.h"
  10 +
  11 +#define HTTP_JSON_BEGIN ("[")
  12 +#define HTTP_JSON_END ("]")
  13 +
  14 +DmpHttp::DmpHttpBase::DmpHttpBase(){}
  15 +DmpHttp::DmpHttpBase::~DmpHttpBase(){}
  16 +int DmpHttp::DmpHttpBase::parseUrl(const std::string& url, std::string& out_server,
  17 + std::string& out_port, std::string& out_path){
  18 +const std::string& http_ = "http://";
  19 +const std::string& https_ = "https://";
  20 +std::string temp_data = url;
  21 +if (temp_data.find(http_) == 0) {
  22 + temp_data = temp_data.substr(http_.length());
  23 + }
  24 + else if (temp_data.find(https_) == 0) {
  25 + temp_data = temp_data.substr(https_.length());
  26 + }
  27 + else {
  28 + return HTTP_ERROR_HTTP_HEAD;
  29 + }
  30 + // 解析域名
  31 + std::size_t idx = temp_data.find('/');
  32 + // 解析 域名后的page
  33 + if (std::string::npos == idx) {
  34 + out_path = "/";
  35 + idx = temp_data.size();
  36 + }
  37 + else {
  38 + out_path = temp_data.substr(idx);
  39 + }
  40 + // 解析域名
  41 + out_server = temp_data.substr(0, idx);
  42 + // 解析端口
  43 + idx = out_server.find(':');
  44 + if (std::string::npos == idx) {
  45 + out_port = "80";
  46 + }
  47 + else {
  48 + out_port = out_server.substr(idx + 1);
  49 + out_server = out_server.substr(0, idx);
  50 + }
  51 + return HTTP_SUCCESS;
  52 +}
  53 +
  54 +int DmpHttp::DmpHttpBase::buildPostRequest(const std::string& server, const std::string& path,
  55 + std::ostream& out_request) {
  56 + // 分割path中的json数据
  57 + std::string temp_path(path), temp_json;
  58 + int json_pos_begin = temp_path.find(HTTP_JSON_BEGIN) + 1;
  59 + int json_pos_end = temp_path.find(HTTP_JSON_END);
  60 + if (json_pos_begin != std::string::npos) {
  61 + // 计算json的长度
  62 + int temp_json_lenth = std::string::npos;
  63 + if (json_pos_end != temp_json_lenth) {
  64 + temp_json_lenth = (json_pos_end - json_pos_begin);
  65 + }
  66 + temp_json = temp_path.substr(json_pos_begin, temp_json_lenth);
  67 + temp_path = temp_path.substr(0, (json_pos_begin - 1));
  68 + }
  69 + out_request << "POST " << temp_path.c_str() << " HTTP/1.0\r\n";
  70 + out_request << "Host: " << server.c_str() << "\r\n";
  71 + out_request << "Content-Length: " << temp_json.length() << "\r\n";
  72 + out_request << "Content-Type: application/x-www-form-urlencoded\r\n";
  73 + out_request << "Accept: */*\r\n";
  74 + out_request << "Connection: close\r\n\r\n";
  75 + out_request << temp_json.c_str();
  76 + return HTTP_SUCCESS;
  77 +}
  78 +
  79 +int DmpHttp::DmpHttpBase::buildGetRequest(const std::string& server, const std::string& path,
  80 + std::ostream& out_request) {
  81 + out_request << "GET " << path.c_str() << " HTTP/1.0\r\n";
  82 + out_request << "Host: " << server.c_str() << "\r\n";
  83 + out_request << "Accept: */*\r\n";
  84 + out_request << "Connection: close\r\n\r\n";
  85 + return HTTP_SUCCESS;
  86 +}
1 -/**************************************************************************  
2 -* file: dmphttpbase.h  
3 -  
4 -* Author: lijiahuan  
5 -* Date: 2021-12-09 17:41:00  
6 -* Email: jiahuanl@chinadci.com  
7 -* copyright: 广州城市信息研究所有限公司  
8 -***************************************************************************/  
9 -#ifndef __dmphttpbase_h__  
10 -#define __dmphttpbase_h__  
11 -#include <iostream>  
12 -#include <boost/asio.hpp>  
13 -namespace DmpHttp  
14 -{  
15 - #define HTTP_SUCCESS (0) // 操作成功  
16 - #define HTTP_ERROR_UNKNOWN (-1) // 未知的错误  
17 - #define HTTP_ERROR_NETWORK (-2) // 网络连接失败  
18 - #define HTTP_ERROR_HTTP_HEAD (-3) // 未找到协议头 http || https  
19 -  
20 - #define HTTP_ERROR_SERVICE (-1000) // 服务器请求失败  
21 - #define HTTP_ERROR_LOGIN (-1001) // 登录失败  
22 - #define HTTP_ERROR_ID (-1002) // 企业ID错误  
23 - #define HTTP_ERROR_USER (-1003) // 帐号不存在  
24 - #define HTTP_ERROR_PASSWORD (-1004) // 密码错误  
25 -  
26 - #define HTTP_ERROR_PARAMETER (1) // 参数错误  
27 - #define HTTP_ERROR_PHONE (2) // 电话号码错误  
28 - #define HTTP_ERROR_MESSAGE (3) // 短信有屏蔽字段  
29 - #define HTTP_ERROR_FUNCTION (4) // 当前平台不支持这个功能  
30 -  
31 - class DmpHttpBase  
32 - {  
33 - public:  
34 - DmpHttpBase();  
35 - virtual ~DmpHttpBase();  
36 - // Post请求  
37 - virtual int post(const std::string& url) = 0;  
38 - // get请求  
39 - virtual int get(const std::string& url) = 0;  
40 - virtual std::string getResponse(void) = 0;  
41 -  
42 - protected:  
43 - typedef int(*pBuildRequest)(const std::string&, const std::string&,std::ostream&);  
44 -  
45 - static int parseUrl(const std::string& url, std::string& out_server,  
46 - std::string& out_port, std::string& out_path);  
47 - static int buildPostRequest(const std::string& server, const std::string& path,  
48 - std::ostream& out_request);  
49 - static int buildGetRequest(const std::string& server, const std::string& path,  
50 - std::ostream& out_request);  
51 - };  
52 -}  
53 -  
54 -  
55 -  
56 - 1 +/**************************************************************************
  2 +* file: dmphttpbase.h
  3 +
  4 +* Author: lijiahuan
  5 +* Date: 2021-12-09 17:41:00
  6 +* Email: jiahuanl@chinadci.com
  7 +* copyright: 广州城市信息研究所有限公司
  8 +***************************************************************************/
  9 +#ifndef __dmphttpbase_h__
  10 +#define __dmphttpbase_h__
  11 +#include <iostream>
  12 +#include <boost/asio.hpp>
  13 +namespace DmpHttp
  14 +{
  15 + #define HTTP_SUCCESS (0) // 操作成功
  16 + #define HTTP_ERROR_UNKNOWN (-1) // 未知的错误
  17 + #define HTTP_ERROR_NETWORK (-2) // 网络连接失败
  18 + #define HTTP_ERROR_HTTP_HEAD (-3) // 未找到协议头 http || https
  19 +
  20 + #define HTTP_ERROR_SERVICE (-1000) // 服务器请求失败
  21 + #define HTTP_ERROR_LOGIN (-1001) // 登录失败
  22 + #define HTTP_ERROR_ID (-1002) // 企业ID错误
  23 + #define HTTP_ERROR_USER (-1003) // 帐号不存在
  24 + #define HTTP_ERROR_PASSWORD (-1004) // 密码错误
  25 +
  26 + #define HTTP_ERROR_PARAMETER (1) // 参数错误
  27 + #define HTTP_ERROR_PHONE (2) // 电话号码错误
  28 + #define HTTP_ERROR_MESSAGE (3) // 短信有屏蔽字段
  29 + #define HTTP_ERROR_FUNCTION (4) // 当前平台不支持这个功能
  30 +
  31 + class DmpHttpBase
  32 + {
  33 + public:
  34 + DmpHttpBase();
  35 + virtual ~DmpHttpBase();
  36 + // Post请求
  37 + virtual int post(const std::string& url) = 0;
  38 + // get请求
  39 + virtual int get(const std::string& url) = 0;
  40 + virtual std::string getResponse(void) = 0;
  41 +
  42 + protected:
  43 + typedef int(*pBuildRequest)(const std::string&, const std::string&,std::ostream&);
  44 +
  45 + static int parseUrl(const std::string& url, std::string& out_server,
  46 + std::string& out_port, std::string& out_path);
  47 + static int buildPostRequest(const std::string& server, const std::string& path,
  48 + std::ostream& out_request);
  49 + static int buildGetRequest(const std::string& server, const std::string& path,
  50 + std::ostream& out_request);
  51 + };
  52 +}
  53 +
  54 +
  55 +
  56 +
57 #endif //__dmphttpbase_h__ 57 #endif //__dmphttpbase_h__
1 -/**************************************************************************  
2 -* file: dmphttputils.cpp  
3 -  
4 -* Author: lijiahuan  
5 -* Date: 2021-12-09 17:41:00  
6 -* Email: jiahuanl@chinadci.com  
7 -* copyright: 广州城市信息研究所有限公司  
8 -***************************************************************************/  
9 -#include "dmphttputils.h"  
10 -#include <boost/bind.hpp>  
11 -  
12 -DmpHttp::DmpHttpUtils::DmpHttpUtils(boost::asio::io_service& io_service)  
13 -: resolver_(io_service), socket_(io_service) {}  
14 -DmpHttp::DmpHttpUtils::~DmpHttpUtils() {}  
15 -  
16 -int DmpHttp::DmpHttpUtils::post(const std::string& url)  
17 -{  
18 - handle_request_resolve(url, DmpHttpBase::buildPostRequest);  
19 - return HTTP_SUCCESS;  
20 -}  
21 -int DmpHttp::DmpHttpUtils::get(const std::string& url)  
22 -{  
23 - handle_request_resolve(url, DmpHttpBase::buildGetRequest);  
24 - return HTTP_SUCCESS;  
25 -}  
26 -void DmpHttp::DmpHttpUtils::handle_request_resolve(const std::string& url, pBuildRequest func) {  
27 - try {  
28 - responseData_.clear();  
29 - // 解析URL  
30 - std::string server, port, path;  
31 - parseUrl(url, server, port, path);  
32 -  
33 - std::ostream request_stream(&request_);  
34 - // 合成请求  
35 - func(server, path, request_stream);  
36 -  
37 - // 解析服务地址\端口  
38 - boost::asio::ip::tcp::resolver::query query(server, port);  
39 - resolver_.async_resolve(query,  
40 - boost::bind(&DmpHttpUtils::handle_resolve, this,  
41 - boost::asio::placeholders::error,  
42 - boost::asio::placeholders::iterator));  
43 - }  
44 - catch (std::exception& e) {  
45 - socket_.close();  
46 - std::cout << "Exception: " << e.what() << "\n";  
47 - }  
48 - return;  
49 -}  
50 -void DmpHttp::DmpHttpUtils::handle_resolve(const boost::system::error_code& err,  
51 - boost::asio::ip::tcp::resolver::iterator endpoint_iterator) {  
52 - if (err) {  
53 - std::cout << "Error: " << err << "\n";  
54 - return;  
55 - }  
56 - // 尝试连接  
57 - boost::asio::async_connect(socket_, endpoint_iterator,  
58 - boost::bind(&DmpHttpUtils::handle_connect, this,  
59 - boost::asio::placeholders::error));  
60 -}  
61 -void DmpHttp::DmpHttpUtils::handle_connect(const boost::system::error_code& err) {  
62 - if (err) {  
63 - std::cout << "Error: " << err << "\n";  
64 - return;  
65 - }  
66 - // 发送request请求  
67 - boost::asio::async_write(socket_, request_,  
68 - boost::bind(&DmpHttpUtils::handle_write_request, this,  
69 - boost::asio::placeholders::error));  
70 -}  
71 -void DmpHttp::DmpHttpUtils::handle_write_request(const boost::system::error_code& err) {  
72 - if (err) {  
73 - std::cout << "Error: " << err << "\n";  
74 - return;  
75 - }  
76 - // 异步持续读数据到response_,直到接收协议符号 \r\n 为止  
77 - boost::asio::async_read_until(socket_, response_, "\r\n",  
78 - boost::bind(&DmpHttpUtils::handle_read_status_line, this,  
79 - boost::asio::placeholders::error));  
80 -}  
81 -void DmpHttp::DmpHttpUtils::handle_read_status_line(const boost::system::error_code& err) {  
82 - if (err) {  
83 - std::cout << "Error: " << err << "\n";  
84 - return;  
85 - }  
86 - // 解析buff  
87 - std::istream response_stream(&response_);  
88 - unsigned int status_code;  
89 - std::string http_version, status_message;  
90 - response_stream >> http_version;  
91 - response_stream >> status_code;  
92 - std::getline(response_stream, status_message);  
93 -  
94 - // 核对是否是正确返回  
95 - if (!response_stream || http_version.substr(0, 5) != "HTTP/") {  
96 - std::cout << "错误的响应数据\n";  
97 - return;  
98 - }  
99 - if (status_code != 200) {  
100 - std::cout << "服务器响应的状态码: " << status_code << "\n";  
101 - return;  
102 - }  
103 - // 读取响应头,直到接收协议符号 \r\n\r\n 为止  
104 - boost::asio::async_read_until(socket_, response_, "\r\n\r\n",  
105 - boost::bind(&DmpHttpUtils::handle_read_headers, this,  
106 - boost::asio::placeholders::error));  
107 -}  
108 -  
109 -void DmpHttp::DmpHttpUtils::handle_read_headers(const boost::system::error_code& err) {  
110 - if (err) {  
111 - std::cout << "Error: " << err << "\n";  
112 - return;  
113 - }  
114 - // 输出响应头  
115 - std::istream response_stream(&response_);  
116 - std::string header;  
117 - while (std::getline(response_stream, header) && header != "\r") {  
118 -#ifdef _DEBUG  
119 - std::cout << header << "\n";  
120 -#endif  
121 - }  
122 -#ifdef _DEBUG  
123 - std::cout << "\n";  
124 -#endif  
125 -  
126 - // 写完所有剩余的内容  
127 - if (response_.size() > 0) {  
128 - boost::asio::streambuf::const_buffers_type cbt = response_.data();  
129 - responseData_ += std::string(boost::asio::buffers_begin(cbt), boost::asio::buffers_end(cbt));  
130 -#ifdef _DEBUG  
131 - std::cout << &response_;  
132 -#endif  
133 - }  
134 -  
135 - // 开始读取剩余所有内容  
136 - boost::asio::async_read(socket_, response_,  
137 - boost::asio::transfer_at_least(1),  
138 - boost::bind(&DmpHttpUtils::handle_read_content, this,  
139 - boost::asio::placeholders::error));  
140 -}  
141 -void DmpHttp::DmpHttpUtils::handle_read_content(const boost::system::error_code& err) {  
142 - if (!err) {  
143 -  
144 - // 输出读到的数据  
145 - boost::asio::streambuf::const_buffers_type cbt = response_.data();  
146 -  
147 - responseData_ = std::string(boost::asio::buffers_begin(cbt), boost::asio::buffers_end(cbt));  
148 -#ifdef _DEBUG  
149 - std::cout << &response_;  
150 -#endif  
151 -  
152 - // 继续读取剩余内容,直到读到EOF  
153 - boost::asio::async_read(socket_, response_,  
154 - boost::asio::transfer_at_least(1),  
155 - boost::bind(&DmpHttpUtils::handle_read_content, this,  
156 - boost::asio::placeholders::error));  
157 -  
158 - }  
159 - else if (err != boost::asio::error::eof) {  
160 - std::cout << "Error: " << err << "\n";  
161 - }  
162 - else {  
163 - socket_.close();  
164 - resolver_.cancel();  
165 - std::cout << "读取响应数据完毕." << std::endl;  
166 - //std::cout << responseData_;  
167 - }  
168 -}  
169 -  
170 -  
171 -std::string DmpHttp::post(std::string url) {  
172 - boost::asio::io_service io;  
173 - DmpHttp::DmpHttpUtils c(io);  
174 - c.post(url);  
175 - io.run();  
176 - return c.getResponse();  
177 -}  
178 -  
179 -std::string DmpHttp::get(std::string url) {  
180 - boost::asio::io_service io;  
181 - DmpHttp::DmpHttpUtils c(io);  
182 - c.get(url);  
183 - io.run();  
184 - return c.getResponse(); 1 +/**************************************************************************
  2 +* file: dmphttputils.cpp
  3 +
  4 +* Author: lijiahuan
  5 +* Date: 2021-12-09 17:41:00
  6 +* Email: jiahuanl@chinadci.com
  7 +* copyright: 广州城市信息研究所有限公司
  8 +***************************************************************************/
  9 +#include "dmphttputils.h"
  10 +#include <boost/bind.hpp>
  11 +
  12 +DmpHttp::DmpHttpUtils::DmpHttpUtils(boost::asio::io_service& io_service)
  13 +: resolver_(io_service), socket_(io_service) {}
  14 +DmpHttp::DmpHttpUtils::~DmpHttpUtils() {}
  15 +
  16 +int DmpHttp::DmpHttpUtils::post(const std::string& url)
  17 +{
  18 + handle_request_resolve(url, DmpHttpBase::buildPostRequest);
  19 + return HTTP_SUCCESS;
  20 +}
  21 +int DmpHttp::DmpHttpUtils::get(const std::string& url)
  22 +{
  23 + handle_request_resolve(url, DmpHttpBase::buildGetRequest);
  24 + return HTTP_SUCCESS;
  25 +}
  26 +void DmpHttp::DmpHttpUtils::handle_request_resolve(const std::string& url, pBuildRequest func) {
  27 + try {
  28 + responseData_.clear();
  29 + // 解析URL
  30 + std::string server, port, path;
  31 + parseUrl(url, server, port, path);
  32 +
  33 + std::ostream request_stream(&request_);
  34 + // 合成请求
  35 + func(server, path, request_stream);
  36 +
  37 + // 解析服务地址\端口
  38 + boost::asio::ip::tcp::resolver::query query(server, port);
  39 + resolver_.async_resolve(query,
  40 + boost::bind(&DmpHttpUtils::handle_resolve, this,
  41 + boost::asio::placeholders::error,
  42 + boost::asio::placeholders::iterator));
  43 + }
  44 + catch (std::exception& e) {
  45 + socket_.close();
  46 + std::cout << "Exception: " << e.what() << "\n";
  47 + }
  48 + return;
  49 +}
  50 +void DmpHttp::DmpHttpUtils::handle_resolve(const boost::system::error_code& err,
  51 + boost::asio::ip::tcp::resolver::iterator endpoint_iterator) {
  52 + if (err) {
  53 + std::cout << "Error: " << err << "\n";
  54 + return;
  55 + }
  56 + // 尝试连接
  57 + boost::asio::async_connect(socket_, endpoint_iterator,
  58 + boost::bind(&DmpHttpUtils::handle_connect, this,
  59 + boost::asio::placeholders::error));
  60 +}
  61 +void DmpHttp::DmpHttpUtils::handle_connect(const boost::system::error_code& err) {
  62 + if (err) {
  63 + std::cout << "Error: " << err << "\n";
  64 + return;
  65 + }
  66 + // 发送request请求
  67 + boost::asio::async_write(socket_, request_,
  68 + boost::bind(&DmpHttpUtils::handle_write_request, this,
  69 + boost::asio::placeholders::error));
  70 +}
  71 +void DmpHttp::DmpHttpUtils::handle_write_request(const boost::system::error_code& err) {
  72 + if (err) {
  73 + std::cout << "Error: " << err << "\n";
  74 + return;
  75 + }
  76 + // 异步持续读数据到response_,直到接收协议符号 \r\n 为止
  77 + boost::asio::async_read_until(socket_, response_, "\r\n",
  78 + boost::bind(&DmpHttpUtils::handle_read_status_line, this,
  79 + boost::asio::placeholders::error));
  80 +}
  81 +void DmpHttp::DmpHttpUtils::handle_read_status_line(const boost::system::error_code& err) {
  82 + if (err) {
  83 + std::cout << "Error: " << err << "\n";
  84 + return;
  85 + }
  86 + // 解析buff
  87 + std::istream response_stream(&response_);
  88 + unsigned int status_code;
  89 + std::string http_version, status_message;
  90 + response_stream >> http_version;
  91 + response_stream >> status_code;
  92 + std::getline(response_stream, status_message);
  93 +
  94 + // 核对是否是正确返回
  95 + if (!response_stream || http_version.substr(0, 5) != "HTTP/") {
  96 + std::cout << "错误的响应数据\n";
  97 + return;
  98 + }
  99 + if (status_code != 200) {
  100 + std::cout << "服务器响应的状态码: " << status_code << "\n";
  101 + return;
  102 + }
  103 + // 读取响应头,直到接收协议符号 \r\n\r\n 为止
  104 + boost::asio::async_read_until(socket_, response_, "\r\n\r\n",
  105 + boost::bind(&DmpHttpUtils::handle_read_headers, this,
  106 + boost::asio::placeholders::error));
  107 +}
  108 +
  109 +void DmpHttp::DmpHttpUtils::handle_read_headers(const boost::system::error_code& err) {
  110 + if (err) {
  111 + std::cout << "Error: " << err << "\n";
  112 + return;
  113 + }
  114 + // 输出响应头
  115 + std::istream response_stream(&response_);
  116 + std::string header;
  117 + while (std::getline(response_stream, header) && header != "\r") {
  118 +#ifdef _DEBUG
  119 + std::cout << header << "\n";
  120 +#endif
  121 + }
  122 +#ifdef _DEBUG
  123 + std::cout << "\n";
  124 +#endif
  125 +
  126 + // 写完所有剩余的内容
  127 + if (response_.size() > 0) {
  128 + boost::asio::streambuf::const_buffers_type cbt = response_.data();
  129 + responseData_ += std::string(boost::asio::buffers_begin(cbt), boost::asio::buffers_end(cbt));
  130 +#ifdef _DEBUG
  131 + std::cout << &response_;
  132 +#endif
  133 + }
  134 +
  135 + // 开始读取剩余所有内容
  136 + boost::asio::async_read(socket_, response_,
  137 + boost::asio::transfer_at_least(1),
  138 + boost::bind(&DmpHttpUtils::handle_read_content, this,
  139 + boost::asio::placeholders::error));
  140 +}
  141 +void DmpHttp::DmpHttpUtils::handle_read_content(const boost::system::error_code& err) {
  142 + if (!err) {
  143 +
  144 + // 输出读到的数据
  145 + boost::asio::streambuf::const_buffers_type cbt = response_.data();
  146 +
  147 + responseData_ = std::string(boost::asio::buffers_begin(cbt), boost::asio::buffers_end(cbt));
  148 +#ifdef _DEBUG
  149 + std::cout << &response_;
  150 +#endif
  151 +
  152 + // 继续读取剩余内容,直到读到EOF
  153 + boost::asio::async_read(socket_, response_,
  154 + boost::asio::transfer_at_least(1),
  155 + boost::bind(&DmpHttpUtils::handle_read_content, this,
  156 + boost::asio::placeholders::error));
  157 +
  158 + }
  159 + else if (err != boost::asio::error::eof) {
  160 + std::cout << "Error: " << err << "\n";
  161 + }
  162 + else {
  163 + socket_.close();
  164 + resolver_.cancel();
  165 + std::cout << "读取响应数据完毕." << std::endl;
  166 + //std::cout << responseData_;
  167 + }
  168 +}
  169 +
  170 +
  171 +std::string DmpHttp::post(std::string url) {
  172 + boost::asio::io_service io;
  173 + DmpHttp::DmpHttpUtils c(io);
  174 + c.post(url);
  175 + io.run();
  176 + return c.getResponse();
  177 +}
  178 +
  179 +std::string DmpHttp::get(std::string url) {
  180 + boost::asio::io_service io;
  181 + DmpHttp::DmpHttpUtils c(io);
  182 + c.get(url);
  183 + io.run();
  184 + return c.getResponse();
185 } 185 }
1 -/**************************************************************************  
2 -* file: dmphttputils.h  
3 -  
4 -* Author: lijiahuan  
5 -* Date: 2021-12-09 17:41:00  
6 -* Email: jiahuanl@chinadci.com  
7 -* copyright: 广州城市信息研究所有限公司  
8 -***************************************************************************/  
9 -#ifndef __dmppghttputils_h__  
10 -#define __dmppghttputils_h__  
11 -#include <iostream>  
12 -#include <boost/asio.hpp>  
13 -#include "dmphttpbase.h"  
14 -  
15 -namespace DmpHttp  
16 -{  
17 - class DmpHttpUtils : public DmpHttpBase  
18 - {  
19 - public:  
20 - DmpHttpUtils(boost::asio::io_service& io_service);  
21 - virtual ~DmpHttpUtils();  
22 - virtual int post(const std::string& url);  
23 - virtual int get(const std::string& url);  
24 -  
25 - virtual std::string getResponse(void) {return responseData_;}  
26 - private:  
27 - // 建立请求  
28 - void handle_request_resolve(const std::string& url, pBuildRequest func);  
29 - // 解析后  
30 - void handle_resolve(const boost::system::error_code& err,  
31 - boost::asio::ip::tcp::resolver::iterator endpoint_iterator);  
32 - // 连接后  
33 - void handle_connect(const boost::system::error_code& err);  
34 - // 发送请求后  
35 - void handle_write_request(const boost::system::error_code& err);  
36 - // 读取响应后  
37 - void handle_read_status_line(const boost::system::error_code& err);  
38 - // 读取响应头后  
39 - void handle_read_headers(const boost::system::error_code& err);  
40 - // 读取正文数据后  
41 - void handle_read_content(const boost::system::error_code& err);  
42 - private:  
43 - // 解析器  
44 - boost::asio::ip::tcp::resolver resolver_;  
45 - // 套接字  
46 - boost::asio::ip::tcp::socket socket_;  
47 - // 请求缓冲区  
48 - boost::asio::streambuf request_;  
49 - // 响应缓冲区  
50 - boost::asio::streambuf response_;  
51 - // 响应数据  
52 - std::string responseData_;  
53 - };  
54 - std::string post(std::string url);  
55 - std::string get(std::string url);  
56 -}  
57 - 1 +/**************************************************************************
  2 +* file: dmphttputils.h
  3 +
  4 +* Author: lijiahuan
  5 +* Date: 2021-12-09 17:41:00
  6 +* Email: jiahuanl@chinadci.com
  7 +* copyright: 广州城市信息研究所有限公司
  8 +***************************************************************************/
  9 +#ifndef __dmppghttputils_h__
  10 +#define __dmppghttputils_h__
  11 +#include <iostream>
  12 +#include <boost/asio.hpp>
  13 +#include "dmphttpbase.h"
  14 +
  15 +namespace DmpHttp
  16 +{
  17 + class DmpHttpUtils : public DmpHttpBase
  18 + {
  19 + public:
  20 + DmpHttpUtils(boost::asio::io_service& io_service);
  21 + virtual ~DmpHttpUtils();
  22 + virtual int post(const std::string& url);
  23 + virtual int get(const std::string& url);
  24 +
  25 + virtual std::string getResponse(void) {return responseData_;}
  26 + private:
  27 + // 建立请求
  28 + void handle_request_resolve(const std::string& url, pBuildRequest func);
  29 + // 解析后
  30 + void handle_resolve(const boost::system::error_code& err,
  31 + boost::asio::ip::tcp::resolver::iterator endpoint_iterator);
  32 + // 连接后
  33 + void handle_connect(const boost::system::error_code& err);
  34 + // 发送请求后
  35 + void handle_write_request(const boost::system::error_code& err);
  36 + // 读取响应后
  37 + void handle_read_status_line(const boost::system::error_code& err);
  38 + // 读取响应头后
  39 + void handle_read_headers(const boost::system::error_code& err);
  40 + // 读取正文数据后
  41 + void handle_read_content(const boost::system::error_code& err);
  42 + private:
  43 + // 解析器
  44 + boost::asio::ip::tcp::resolver resolver_;
  45 + // 套接字
  46 + boost::asio::ip::tcp::socket socket_;
  47 + // 请求缓冲区
  48 + boost::asio::streambuf request_;
  49 + // 响应缓冲区
  50 + boost::asio::streambuf response_;
  51 + // 响应数据
  52 + std::string responseData_;
  53 + };
  54 + std::string post(std::string url);
  55 + std::string get(std::string url);
  56 +}
  57 +
58 #endif //__dmphttpsutils_h__ 58 #endif //__dmphttpsutils_h__
1 -/**************************************************************************  
2 -* file: dmptmstileprovider.cpp  
3 -  
4 -* Author: lijiahuan  
5 -* Date: 2021-12-09 17:41:00  
6 -* Email: jiahuanl@chinadci.com  
7 -* copyright: 广州城市信息研究所有限公司  
8 -***************************************************************************/  
9 -#include "dmptmstileprovider.h"  
10 -#include "dmpserverresponse.h"  
11 -namespace DmpTms  
12 -{  
13 - DmpTmsTileProvider::DmpTmsTileProvider(const std::string& root_path)  
14 - {  
15 - file_rootPath=root_path;  
16 - }  
17 - void DmpTmsTileProvider::WriteTile(const int row, const int col, const int level, const std::string& format, DmpServerResponse& response)  
18 - {  
19 - std::string tile_path = file_rootPath+"/"+std::to_string(level)+"/"+std::to_string(row)+"/"+std::to_string(col)+"."+format;  
20 - std::ifstream fread(tile_path, std::ifstream::binary);  
21 - if(!fread)  
22 - {  
23 - response.sendError(500, "找不到瓦片:(");  
24 - return;  
25 - }  
26 - fread.seekg(0,fread.end);  
27 - int length = fread.tellg();  
28 - if(length > 0)  
29 - {  
30 - fread.seekg(0,fread.beg);  
31 - char* buff = new char[length];  
32 - fread.read(buff,length);  
33 -  
34 - std::string f = (format == "jpg") ? "image/jpg" : "image/png";  
35 - response.setHeader("Content-Type", f);  
36 - response.writeContent(buff, length);  
37 -  
38 - delete buff;  
39 - }  
40 - else  
41 - {  
42 - response.sendError(500, "找不到瓦片:(");  
43 - }  
44 - fread.close();  
45 - } 1 +/**************************************************************************
  2 +* file: dmptmstileprovider.cpp
  3 +
  4 +* Author: lijiahuan
  5 +* Date: 2021-12-09 17:41:00
  6 +* Email: jiahuanl@chinadci.com
  7 +* copyright: 广州城市信息研究所有限公司
  8 +***************************************************************************/
  9 +#include "dmptmstileprovider.h"
  10 +#include "dmpserverresponse.h"
  11 +namespace DmpTms
  12 +{
  13 + DmpTmsTileProvider::DmpTmsTileProvider(const std::string& root_path)
  14 + {
  15 + file_rootPath=root_path;
  16 + }
  17 + void DmpTmsTileProvider::WriteTile(const int row, const int col, const int level, const std::string& format, DmpServerResponse& response)
  18 + {
  19 + std::string tile_path = file_rootPath+"/"+std::to_string(level)+"/"+std::to_string(row)+"/"+std::to_string(col)+"."+format;
  20 + std::ifstream fread(tile_path, std::ifstream::binary);
  21 + if(!fread)
  22 + {
  23 + response.sendError(500, "找不到瓦片:(");
  24 + return;
  25 + }
  26 + fread.seekg(0,fread.end);
  27 + int length = fread.tellg();
  28 + if(length > 0)
  29 + {
  30 + fread.seekg(0,fread.beg);
  31 + char* buff = new char[length];
  32 + fread.read(buff,length);
  33 +
  34 + std::string f = (format == "jpg") ? "image/jpg" : "image/png";
  35 + response.setHeader("Content-Type", f);
  36 + response.writeContent(buff, length);
  37 +
  38 + delete buff;
  39 + }
  40 + else
  41 + {
  42 + response.sendError(500, "找不到瓦片:(");
  43 + }
  44 + fread.close();
  45 + }
46 } 46 }
1 -/**************************************************************************  
2 -* file: dmptmstileprovider.h  
3 -  
4 -* Author: lijiahuan  
5 -* Date: 2021-12-09 17:41:00  
6 -* Email: jiahuanl@chinadci.com  
7 -* copyright: 广州城市信息研究所有限公司  
8 -***************************************************************************/  
9 -#ifndef __dmptmstileprovider_h__  
10 -#define __dmptmstileprovider_h__  
11 -  
12 -#include <string>  
13 -#include "dmpserverresponse.h"  
14 -  
15 -namespace DmpTms  
16 -{  
17 - class DmpTmsTileProvider  
18 - {  
19 - public:  
20 - std::string ImageFormat() const;  
21 - DmpTmsTileProvider(const std::string& root_path);  
22 - void WriteTile(const int row, const int col, const int level, const std::string& format, DmpServerResponse& response) ;  
23 - protected:  
24 - std::string file_rootPath;  
25 - };  
26 -}  
27 - 1 +/**************************************************************************
  2 +* file: dmptmstileprovider.h
  3 +
  4 +* Author: lijiahuan
  5 +* Date: 2021-12-09 17:41:00
  6 +* Email: jiahuanl@chinadci.com
  7 +* copyright: 广州城市信息研究所有限公司
  8 +***************************************************************************/
  9 +#ifndef __dmptmstileprovider_h__
  10 +#define __dmptmstileprovider_h__
  11 +
  12 +#include <string>
  13 +#include "dmpserverresponse.h"
  14 +
  15 +namespace DmpTms
  16 +{
  17 + class DmpTmsTileProvider
  18 + {
  19 + public:
  20 + std::string ImageFormat() const;
  21 + DmpTmsTileProvider(const std::string& root_path);
  22 + void WriteTile(const int row, const int col, const int level, const std::string& format, DmpServerResponse& response) ;
  23 + protected:
  24 + std::string file_rootPath;
  25 + };
  26 +}
  27 +
28 #endif 28 #endif
注册登录 后发表评论