EOS代碼分析4 命令註冊和通信機制分析

客戶端是cleos,服務器端是nodeos,通過cleos命令行控制和管理整個EOS鏈,非常重要的機制。
客戶端和服務器端的通信採用RESTful軟件架構風格,服務器端的每個資源對應一個唯一的URL地址,客戶端將URL地址封裝成http請求發送到服務器端,請求對應的資源或者執行相應操作。
EOS代碼分析4 命令註冊和通信機制分析
客戶端發送消息流程
以轉賬爲例,說明EOS消息處理流程。通過cleos客戶端發起轉賬命令,在main函數中,解析transfer命令,通過create_transfer函數將交易發送者、交易接收者、token數量等信息封裝成mutable_variant_object對象,然後調用send_action函數,將交易信息發送到服務器端打包進區塊鏈。

./cleos transfer sender recipient amount memo

programs/cleos/main.cpp
main()
{

send_actions({create_transfer(sender, recipient, amount, memo)});

}
void send_actions {
        auto result = push_actions( move(actions), extra_kcpu, compression);
        …
}
fc::variant push_actions {
       signed_transaction trx;
trx.actions = std::forward<decltype(actions)>(actions);
return push_transaction(trx, extra_kcpu, compression);
}
fc::variant push_transaction{
    trx.set_reference_block(ref_block_id);
// 發送 ”/V1/chain/push_transaction” URL地址到服務器端
    if (!tx_dont_broadcast) {
         return call(push_txn_func, packed_transaction(trx, compression));
     }
}
fc::variant call{
       try {
              return eosio::client::http::do_http_call( url, path, fc::variant(v) );
        }
}
fc::variant do_http_call {
       // 將請求的URL封裝成http包
       request_stream << “POST ” << path_prefix + path << ” HTTP/1.0\r\n”;
request_stream << “Host: ” << server << “\r\n”;
request_stream << “content-length: ” << postjson.size() << “\r\n”;
request_stream << “Accept: /\r\n”;
request_stream << “Connection: close\r\n\r\n”;
request_stream << postjson;
       // 和服務器建立連接
       do_connect(socket, server, port);
       // 發送http報文,並獲取返回結果
       re = do_txrx(socket, request, status_code);
}

服務器接收消息流程
nodeos服務器先通過http_plugin插件接收客戶端發過來的http請求報文,然後解析出請求的URL地址和數據信息,然後調用對應的回調函數處理,並將結果返回給cleos客戶端。
HTTP消息處理流程
在nodeos的main函數中啓動http_plugin插件,註冊處理http請求的回調函數(handle_http_request),然後監聽socket通信端口,等待建立客戶端遠程連接。
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();
transport_type::async_accept(&type::handle_accept
// This method will have no effect until the underlying io_service * starts running. It may be called after the io_service is already running.
}
void create_server_for_endpoint{
       ws.set_http_handler([&](connection_hdl hdl) {
handle_http_request<T>(ws.get_con_from_hdl(hdl));
       });
}
         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));
});
       }
       …
}
註冊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);
}
例如,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)
});
}

點擊關注
EOS代碼分析4 命令註冊和通信機制分析

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章