欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

出块过程(2)nodeos 服务器接收消息

发布时间:2025/3/21 编程问答 28 豆豆
生活随笔 收集整理的这篇文章主要介绍了 出块过程(2)nodeos 服务器接收消息 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

1. 步骤

在nodeos的main函数中启动http_plugin插件,注册处理http请求的回调函数(handle_http_request),然后监听socket通信端口,等待建立客户端远程连接。

step1. 通过http_plugin插件接收客户端发过来的http请求报文
step2. 解析请求的URL地址和数据信息
step3. 调用对应的回调函数处理,并将结果返回给cleos客户端。

void http_plugin::plugin_startup() {// 注册http请求处理函数my->create_server_for_endpoint(*my->https_listen_endpoint, my->https_server);// 监听socket通信端口my->https_server.listen(*my->https_listen_endpoint);// 等待建立客户端远程连接my->https_server.start_accept(); }void create_server_for_endpoint{ws.set_http_handler([&](connection_hdl hdl) {handle_http_request<T>(ws.get_con_from_hdl(hdl));}); }

1.1 http请求处理函数

从http报文中解析出URL地址(resource)、消息内容(body),然后在url_handlers集合中查找URL对应的回调函数,最后通过handler_itr->second调用处理函数。

void handle_http_request {auto body = con->get_request_body();auto resource = con->get_uri()->get_resource();auto handler_itr = url_handlers.find(resource);if(handler_itr != url_handlers.end()) {handler_itr->second(resource, body, [con](int code, string body) {con->set_body(body);con->set_status(websocketpp::http::status_code::value(code));});}}

1.2. 注册URL处理函数

url_handlers是一个URL和处理函数的键值对map集合,由class http_plugin_impl管理,其它插件模块通过add_api函数注册URL回调函数。

plugins/http_plugin/http_plugin.cpp

class http_plugin_impl {map<string,url_handler> url_handlers; ... }void add_api(const api_description& api) {for (const auto& call : api)add_handler(call.first, call.second); }void http_plugin::add_handler {…my->url_handlers.insert(std::make_pair(url,handler); }

eg,chain_api_plugin插件在启动函数中注册了以下URL回调函数,包括查询区块信息、处理交易数据:

void chain_api_plugin::plugin_startup() {app().get_plugin<http_plugin>().add_api({CHAIN_RO_CALL(get_info, 200),CHAIN_RO_CALL(get_block, 200),CHAIN_RW_CALL(push_transaction, 202),CHAIN_RW_CALL(push_transactions, 202)}); }

总结

以上是生活随笔为你收集整理的出块过程(2)nodeos 服务器接收消息的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。