-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathxrpc_server.cpp
More file actions
304 lines (267 loc) · 8.53 KB
/
xrpc_server.cpp
File metadata and controls
304 lines (267 loc) · 8.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include <unistd.h>
#include <string>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <iostream>
#include <msgpack.hpp>
#include "xserialize.h"
#include "xnet.h"
#include "xmessage_dispatcher.hpp"
#include "xstore_manager.h"
#include "xckey.h"
#include "xhash.h"
#include "xchaincore.h"
#include <sys/time.h>
#include "stdlib.h"
#include "xconfig.hpp"
#include "xlog.h"
#include "xstring.h"
#include "xtimer.h"
#include "xrpc_server.hpp"
#include "xrpc_handler.hpp"
using namespace std;
using namespace top;
using namespace top::rpc;
using asio::ip::tcp;
void xrpc_connection::do_read_http_body()
{
auto self(shared_from_this());
// TODO need read the complete body
async_read_until(m_socket, m_request, "}", [this, self](std::error_code ec, std::size_t bytes_transferred){
if(!ec)
{
asio::streambuf::const_buffers_type cbt = m_request.data();
string request_data(asio::buffers_begin(cbt), asio::buffers_end(cbt));
xinfo("rpcserver: http request %d", bytes_transferred);
//m_request.consume(bytes_transferred);
auto handler (std::make_shared<xrpc_handler> (request_data));
handler->handle_request();
string rsp = handler->get_response();
do_write(rsp);
}
else
{
xwarn("rpcserver: socket async_read_until error");
}
});
}
bool xrpc_connection::parse_method()
{
auto self(shared_from_this());
async_read_until(m_socket, m_request, " ", [this, self](std::error_code ec, std::size_t bytes_transferred){
if(!ec)
{
asio::streambuf::const_buffers_type cbt = m_request.data();
string method(asio::buffers_begin(cbt), asio::buffers_begin(cbt) + bytes_transferred - 1);
if (!method.empty())
{
if (method.compare("OPTIONS") == 0)
{
on_options();
return false;
}
else if (method.compare("POST") == 0)
{
on_post();
return false;
}
}
return true;
}
else
{
xwarn("xrpc_connection: socket async_read_until parse_method error");
}
});
}
void xrpc_connection::on_post()
{
auto self(shared_from_this());
async_read_until(m_socket, m_request, "\r\n\r\n", [this, self](std::error_code ec, std::size_t bytes_transferred) {
if (!ec)
{
#if 0
asio::streambuf::const_buffers_type cbt = m_request.data();
string request_data(asio::buffers_begin(cbt), asio::buffers_begin(cbt) + bytes_transferred);
cout << "http header: " << bytes_transferred << endl;
cout << request_data << endl;
#endif
m_request.consume(bytes_transferred);
do_read_http_body();
}
else
{
xwarn("xrpc_connection: socket async_read_until on_post error");
}
});
}
void xrpc_connection::on_options()
{
std::ostream rsp_stream(&m_response);
rsp_stream << "HTTP/1.1 200 OK\r\n";
rsp_stream << "Access-Control-Allow-Origin:*\r\n";
rsp_stream << "Access-Control-Request-Method:POST\r\n";
rsp_stream << "Access-Control-Allow-Headers:Origin,X-Requested-With,Content-Type,Accept\r\n";
rsp_stream << "Access-Control-Request-Headers:Content-type\r\n";
// rsp_stream << "Content-Type:application/json;charset=utf-8\r\n";
auto self(shared_from_this());
asio::async_write(m_socket, m_response, [this, self](std::error_code ec, std::size_t bytes_transferred) {
});
}
void xrpc_connection::on_error()
{
std::ostream rsp_stream(&m_response);
rsp_stream << "HTTP/1.0 403 Forbidden\r\n";
auto self(shared_from_this());
asio::async_write(m_socket, m_response, [this, self](std::error_code ec, std::size_t bytes_transferred) {
});
}
void xrpc_connection::do_read()
{
if (!parse_method())
{
return;
}
xwarn("rpcserver: socket async_read_until error");
on_error();
}
void xrpc_connection::do_write(string &rsp)
{
std::ostream rsp_stream(&m_response);
if(rsp.empty())
{
rsp_stream << "HTTP/1.1 201 Error\r\n";
}
else
{
rsp_stream << "HTTP/1.1 200 OK\r\n";
rsp_stream << "Access-Control-Allow-Origin: *\r\n";
rsp_stream << "Content-Type: " << "application/json" << "\r\n";
rsp_stream << "Content-Length: " << rsp.length() << "\r\n";
rsp_stream << "\r\n";
rsp_stream << rsp << "\r\n";
}
auto self(shared_from_this());
asio::async_write(m_socket, m_response, [this, self](std::error_code ec, std::size_t bytes_transferred)
{
//self->handle_accept();
//return;
});
}
void xrpc_connection::handle_accept()
{
do_read();
}
xrpc_server::xrpc_server(const std::string &host, const uint16_t port)
: m_acceptor(m_io_service, tcp::endpoint(asio::ip::address_v4::from_string(host), port))
{
std::cout << "xrpc http server listening on " << host+":"+to_string(port) << std::endl;
}
xrpc_server::~xrpc_server()
{
}
void xrpc_server::do_accept()
{
auto connection (std::make_shared<xrpc_connection> (m_io_service));
m_acceptor.async_accept(connection->m_socket, [this, connection](const asio::error_code &ec) {
if (!ec)
{
connection->handle_accept ();
do_accept ();
}
else
{
xwarn("rpcserver: async_accept error");
}
});
}
void test()
{
uint8_t* pData;
//创建几个测试账户
top::utl::xecprikey_t pri_key_obj;
std::cout << "private key: ";
pData = pri_key_obj.data();
for(int i=0;i<pri_key_obj.size();i++)
{
printf("%02x",pData[i]);
}
std::cout << std::endl;
top::utl::xecpubkey_t pub_key_obj = pri_key_obj.get_public_key();
std::cout << "public key: ";
pData = pub_key_obj.data();
for(int i=0;i<pub_key_obj.size();i++)
{
printf("%02x",pData[i]);
}
std::cout << std::endl;
std::string address = pub_key_obj.to_address();
std::cout << "address: " << address << std::endl;
}
bool genesis_account_create()
{
uint8_t test_genesis_account_pri_key[32] = {0x83,0xab,0x81,0xe3,0xcf,0xe1,0x44,0x25,0xe5,0x30,0xea,0xb1,0xd3,0x87,0xc6,0xff,0x80,0x50,0x63,0x73,0xc0,0xa1,0xcf,0x5c,0x63,0xc7,0x55,0x1c,0x68,0x40,0x06,0xed};
top::utl::xecprikey_t pri_key_obj(test_genesis_account_pri_key);
std::string test_genesis_account_address = "T-Um45LZ8HZYUpm5jdWi6bh99mdNjEHkz6w";
shared_ptr<top::chain::xtransaction_t> tx = make_shared<top::chain::xtransaction_t>();
tx->m_transaction_type = top::chain::xtransbase_t::enum_xtransaction_type_create_account;
tx->m_sender_addr = test_genesis_account_address;
tx->m_receiver_addr = test_genesis_account_address;
tx->m_property_key = "$$";
tx->m_transaction_params = "200000000000";
tx->m_timestamp = 0;
//tx->m_last_msg_digest;
tx->m_random_nounce = 0;
tx->m_work_proof = 0;
tx->tx_hash_and_sign_calc(pri_key_obj);
tx->m_edge_timestamp = top::base::xtimer_t::timestamp_now_ms();
//对hash和sign进行校验
if(false == tx->tx_hash_and_sign_verify())
{
xwarn("rpcserver: genesis_account_create tx_hash_and_sign_verify error");
return false;
}
shared_ptr<top::chain::xfullunit_t> fullunit = make_shared<top::chain::xfullunit_t>();
fullunit->m_asset_properties.insert(std::make_pair(tx->m_property_key, std::stoll(tx->m_transaction_params)));
fullunit->m_unit_type = chain::x_unit_type::full_unit_type;
fullunit->m_seq_id = 1;
fullunit->m_associated_event_hash = tx->m_tx_digest;
fullunit->create_unit_digest_and_signature(pri_key_obj);
top::store::xstore_base* m_store_mgr = xsingleton<top::store::xstore_manager>::Instance();
chain::pbft_store_params_t event;
event.m_transbase = tx;
event.m_unit = fullunit;
auto ret = m_store_mgr->update_event(event);
if(ret != top::store::enum_store_return_type::store_ok && ret != top::store::enum_store_return_type::create_account_is_exist)
{
xwarn("rpcserver: genesis_account_create update_event error");
return false;
}
return true;
}
uint32_t xrpc_server::run()
{
if(false == genesis_account_create())
{
return 0;
}
while(1)
{
try
{
do_accept();
m_io_service.run();
}
catch (std::exception& e)
{
std::cerr << "xrpc http server exception " << e.what() << std::endl;
}
}
std::cout << "rpc server exit" << std::endl;
return 0;
}