版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
第oaptt搭建http服務(wù)的過(guò)程詳解Oat++介紹
Oat++主頁(yè):https://oatpp.io
Oat++文檔:https://oatpp.io/docs/start
GitHub地址:/oatpp/oatpp
編譯Oat++
環(huán)境要求
Oat++的編譯過(guò)程很簡(jiǎn)單,只需要有基本的開(kāi)發(fā)環(huán)境就行了:
Git編譯器支持的C++版本=11MakeCMake版本=3.1
如果沒(méi)有的話(huà),按照下述步驟安裝,以Ubuntu為例:
sudoaptinstallgit
sudoaptinstallcmake
sudoaptinstallbuild-essential
編譯安裝
下載Oat++源碼:
gitclone/oatpp/oatpp.git
隨后,執(zhí)行編譯安裝四部曲:
cdoatpp/
mkdirbuildcdbuild
cmake..
sudomakesudomakeinstall
hello示例程序
為了演示Oat++,我們從最簡(jiǎn)單的Hello,World!開(kāi)始!
創(chuàng)建一個(gè)CMake項(xiàng)目,CMakeLists.txt配置如下:
cmake_minimum_required(VERSION3.1)
project(helloworld)
set(CMAKE_CXX_STANDARD11)
set(SOURCE_FILESmain.cpphandler.h)
#查找oatpp依賴(lài)
find_package(oatppREQUIRED)
add_executable(${PROJECT_NAME}${SOURCE_FILES})
#將目標(biāo)文件與庫(kù)文件進(jìn)行鏈接
target_link_libraries(${PROJECT_NAME}oatpp::oatpp)
handler.h
//handler.h
#ifndefHANDLER_H
#defineHANDLER_H
#include"oatpp/web/server/HttpRequestHandler.hpp"
#defineO_UNUSED(x)(void)x;
//自定義請(qǐng)求處理程序
classHandler:publicoatpp::web::server::HttpRequestHandler
public:
//處理傳入的請(qǐng)求,并返回響應(yīng)
std::shared_ptrOutgoingResponsehandle(conststd::shared_ptrIncomingRequestrequest)override{
O_UNUSED(request);
returnResponseFactory::createResponse(Status::CODE_200,"Hello,World!");
#endif//HANDLER_H
main.cpp
//main.cpp
#include"oatpp/web/server/HttpConnectionHandler.hpp"
#include"oatpp/network/tcp/server/ConnectionProvider.hpp"
#include"oatpp/network/Server.hpp"
#include"handler.h"
voidrun()
//為HTTP請(qǐng)求創(chuàng)建路由器
autorouter=oatpp::web::server::HttpRouter::createShared();
//路由GET-"/hello"請(qǐng)求到處理程序
router-route("GET","/hello",std::make_sharedHandler
//創(chuàng)建HTTP連接處理程序
autoconnectionHandler=oatpp::web::server::HttpConnectionHandler::createShared(router);
//創(chuàng)建TCP連接提供者
autoconnectionProvider=oatpp::network::tcp::server::ConnectionProvider::createShared({"",8080,oatpp::network::Address::IP_4});
//創(chuàng)建服務(wù)器,它接受提供的TCP連接并將其傳遞給HTTP連接處理程序
oatpp::network::Serverserver(connectionProvider,connectionHandler);
//打印服務(wù)器端口
OATPP_LOGI("MyApp","Serverrunningonport%s",connectionProvider-getProperty("port").getData());
//運(yùn)行服務(wù)器
server.run();
intmain()
//初始化oatpp環(huán)境
oatpp::base::Environment::init();
//運(yùn)行應(yīng)用
run();
//銷(xiāo)毀oatpp環(huán)境
oatpp::base::Environment::destroy();
return0;
編譯helloworld程序
mkdirbuild
cdbuild
cmake..
./helloworld
運(yùn)行結(jié)果如下
HTTP模擬Onvif功能
啟動(dòng)HTTP服務(wù)器模擬Onvif服務(wù)端,接收Onvif客戶(hù)端發(fā)送過(guò)來(lái)的http請(qǐng)求并響應(yīng)xml數(shù)據(jù)
項(xiàng)目背景和解決方案點(diǎn)這里
CMakeList.txt
cmake_minimum_required(VERSION3.1)
project(helloworld)
set(CMAKE_CXX_STANDARD11)
set(SOURCE_FILESmain.cpphandler.h)
#查找oatpp依賴(lài)
find_package(oatppREQUIRED)
add_executable(${PROJECT_NAME}${SOURCE_FILES})
#將目標(biāo)文件與庫(kù)文件進(jìn)行鏈接
target_link_libraries(${PROJECT_NAME}oatpp::oatpp)
handler.h
//handler.h
#ifndefHANDLER_H
#defineHANDLER_H
#include"oatpp/web/server/HttpRequestHandler.hpp"
#include"oatpp/web/protocol/http/incoming/Response.hpp"
#includefcntl.h
#includeunistd.h
#includesys/stat.h
#includeunistd.h
#defineO_UNUSED(x)(void)x;
#defineXML_INFO"xmlversion=\"1.0\"encoding=\"utf-8\"standalone=\"yes\""
//自定義請(qǐng)求處理程序
classHandler:publicoatpp::web::server::HttpRequestHandler
public:
//處理傳入的請(qǐng)求,并返回響應(yīng)
std::shared_ptrOutgoingResponsehandle(conststd::shared_ptrIncomingRequestrequest)override{
//O_UNUSED(request);
OATPP_LOGI("MyApp","newconnect..");
//printf("method:%d");
intfileFd=open("../test.xml",O_RDONLY);
if(fileFd0)
return0;
//獲取文件大小
structstatfileStat;
fstat(fileFd,fileStat);
intfileSize=fileStat.st_size;
printf("fd:%d,filesize%d\n",fileFd,fileStat.st_size);
//申請(qǐng)空間存儲(chǔ)xml數(shù)據(jù)
charbuf[1024*5];
memset(buf,'\0',sizeof(buf));
read(fileFd,buf,sizeof(buf));
printf("newconnect..\n");
printf("buf:%s\n\n",buf);
//創(chuàng)建響應(yīng)并添加請(qǐng)求頭
std::shared_ptrOutgoingResponseresp=ResponseFactory::createResponse(Status::CODE_200,buf);
resp-putHeader("Content-Type","application/soap+xml;charset=utf-8;action=\"/ver20/media/wsdl/GetOSDs\"");
//resp-putHeader("charset","utf-8");
//resp-putHeader("action","\"/ver20/media/wsdl/GetOSDs\"");
resp-putHeader("X-Frame-Options","SAMEORIGIN");
close(fileFd);
returnresp;//響應(yīng)請(qǐng)求
#endif//HANDLER_H
main.cpp
//main.cpp
#include"oatpp/web/server/HttpConnectionHandler.hpp"
#include"oatpp/network/tcp/server/ConnectionProvider.hpp"
#include"oatpp/network/Server.hpp"
#include"handler.h"
voidrun()
//為HTTP請(qǐng)求創(chuàng)建路由器
autorouter=oatpp::web::server::HttpRouter::createShared();
//路由GET-"/hello"請(qǐng)求到處理程序
router-route("POST","/onvif/Media2",std::make_sharedHandler
//創(chuàng)建HTTP連接處理程序
autoconnectionHandler=oatpp::web::server::HttpConnectionHandler::createShared(router);
//創(chuàng)建TCP連接提供者
autoconnectionProvider=oatpp::network::tcp::server::ConnectionProvider::createShared({"",7681,oatpp::network::Address::IP_4});
//創(chuàng)建服務(wù)器,它接受提供的TCP連接并將其傳遞給HTTP連接處理程序
oatpp::network::Serverserver(connectionProvider,connectionHandler);
//打印服務(wù)器端口
OATPP_LOGI("MyApp","Serverrunningonport%s",connectionProvider-getProperty("port").getData());
//運(yùn)行服務(wù)器
server.run();
intmain()
//初始化oatpp環(huán)境
oatpp::base::Environment::init();
//運(yùn)行應(yīng)用
run();
//銷(xiāo)毀oatpp環(huán)境
oatpp::base:
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 衛(wèi)生用品更衣室管理制度
- 衛(wèi)生院行風(fēng)督查制度
- 衛(wèi)生院三病物資管理制度
- 生活區(qū)衛(wèi)生物品管理制度
- 衛(wèi)生院疾病預(yù)防管理制度
- 衛(wèi)生所規(guī)范管理制度
- 養(yǎng)殖場(chǎng)日常衛(wèi)生管理制度
- 幼兒園8項(xiàng)衛(wèi)生管理制度
- 衛(wèi)生所首診負(fù)責(zé)制度
- 衛(wèi)生院新冠病人轉(zhuǎn)診制度
- 箱涵預(yù)制、安裝、現(xiàn)澆施工方案
- 現(xiàn)金日記賬模板(出納版)
- DB34T 1948-2013 建設(shè)工程造價(jià)咨詢(xún)檔案立卷標(biāo)準(zhǔn)
- 2024中藥藥渣處理協(xié)議
- 心源性暈厥的查房
- 機(jī)械氣道廓清技術(shù)臨床應(yīng)用專(zhuān)家共識(shí)(2023版)解讀
- 壓力性損傷風(fēng)險(xiǎn)評(píng)估與管理護(hù)理課件
- 專(zhuān)家解析:渲染,烘托等的區(qū)別課件
- 廣州花城匯UUPARK招商手冊(cè)
- 20S517 排水管道出水口
- (完整word)長(zhǎng)沙胡博士工作室公益發(fā)布新加坡SM2考試物理全真模擬試卷(附答案解析)
評(píng)論
0/150
提交評(píng)論