-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnkn_local.h
107 lines (78 loc) · 2.77 KB
/
nkn_local.h
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
//
// Created by yc chow on 2021/2/7.
//
#ifndef NKN_LOCAL_H
#define NKN_LOCAL_H
#include <sodium.h>
#include <boost/atomic.hpp>
#include "smux-cpp/local.h"
#include "smux-cpp/encrypt.h"
#include "smux-cpp/smux.h"
#include "smux-cpp/client_session.h"
#include "include/crypto/ed25519.h"
#include "include/rpc.h"
#include "include/wallet.h"
#include "nkn.h"
#include "nkn_client_session.h"
#include "server_session.h"
#include "pb/tuna.pb.h"
#include "config.h"
#include "encrypt.h"
#define MiB 1024*1024
#define MAX_OVERDUE_BYTES 32*MiB
#define MAX_PAYMENT_DURATION 60
using boost::asio::ip::tcp;
using namespace NKN;
struct node_info;
class nkn_Local : public Local {
public:
nkn_Local(boost::asio::io_context &io_context, shared_ptr<Wallet::Wallet> w, shared_ptr<node_info> ni, bool *stop);
~nkn_Local();
void run() override;
void async_connect(std::function<void(std::shared_ptr<smux_sess>)> handler) override;
void negotiate_conn_metadata(std::shared_ptr<tcp::socket> sock, pb::ConnectionMetadata md);
void send_payment();
void payment_checker(uint32_t sid);
void send_service_metadata();
void receive_service_metadata();
void connect_local_service(string ip, int port);
uint get_service_id();
// uint32_t read_var_bytes(std::shared_ptr<tcp::socket> s, char *buf);
//
// static void write_var_bytes(std::shared_ptr<tcp::socket> s, char *buf, std::size_t len);
bool connected;
private:
void do_sess_receive() override;
private:
atomic_int64_t total_in_bytes_;
atomic_int64_t total_out_bytes_;
atomic_int64_t paid_bytes_;
boost::posix_time::ptime last_payment_;
char recv_msg_[38324]{};
char send_msg_[38324]{};
char plain_[38324]{};
char nanopay_buf_[256]{};
char stream_metadata_buf_[128];
char service_metadata_buf_[4096];
shared_ptr<Wallet::Wallet> wallet_;
shared_ptr<Wallet::NanoPay> nanopay_;
shared_ptr<boost::asio::high_resolution_timer> nanopay_sender_timer_;
shared_ptr<node_info> ni_;
string remote_beneficiary_;
bool *stop_;
uint16_t payment_stream_id_;
uint16_t service_stream_id_;
private:
unsigned char nonce_[32]{};
unsigned char remote_pk_[crypto_sign_ed25519_PUBLICKEYBYTES]{};
unsigned char msg_enc_pk_[crypto_sign_ed25519_PUBLICKEYBYTES]{};
unsigned char msg_enc_sk_[crypto_sign_ed25519_SECRETKEYBYTES]{};
unsigned char curve_pk_[crypto_box_curve25519xchacha20poly1305_PUBLICKEYBYTES]{};
unsigned char curve_sk_[crypto_box_curve25519xchacha20poly1305_SECRETKEYBYTES]{};
unsigned char shared_[crypto_box_BEFORENMBYTES]{};
unsigned char enc_key_[crypto_box_curve25519xchacha20poly1305_SECRETKEYBYTES]{};
private:
char conn_metadata_[128];
shared_ptr<XSalsa20poly1305Encrypter> enc_dec_;
};
#endif //ENTRY_NKN_LOCAL_H