forked from madMAx43v3r/xpmclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
baseclient.h
159 lines (98 loc) · 2.53 KB
/
baseclient.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
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
/*
* baseclient.h
*
* Created on: 30.04.2014
* Author: mad
*/
#ifndef BASECLIENT_H_
#define BASECLIENT_H_
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include "protocol.pb.h"
using namespace pool;
#ifndef _WIN32
typedef int SOCKET;
#endif
#include <zmq.h>
#include <czmq.h>
#include <chrono>
#include <locale.h>
#include <config4cpp/Configuration.h>
using namespace config4cpp;
extern std::string gClientName;
extern unsigned gClientID;
extern const unsigned gClientVersion;
extern std::string gAddr;
extern proto::Block gBlock;
class Timer {
public:
Timer() {
reset();
}
void reset() {
m_timestamp = std::chrono::high_resolution_clock::now();
}
float diff() {
std::chrono::duration<float> fs = std::chrono::high_resolution_clock::now() - m_timestamp;
return fs.count();
}
private:
std::chrono::high_resolution_clock::time_point m_timestamp;
};
struct share_t {
share_t(){
accepted = 0;
invalid = 0;
stale = 0;
duplicate = 0;
}
unsigned accepted;
unsigned invalid;
unsigned stale;
unsigned duplicate;
};
template<class C>
static bool Receive(C& rep, void* socket) {
zmsg_t* msg = zmsg_recv(socket);
if(!msg) return false;
zframe_t* frame = zmsg_last(msg);
size_t fsize = zframe_size(frame);
const byte* fbytes = zframe_data(frame);
rep.ParseFromArray(fbytes, fsize);
zmsg_destroy(&msg);
return true;
}
template<class C>
static bool ReceivePub(C& sig, void* socket) {
zmsg_t* msg = zmsg_recv(socket);
if(!msg) return false;
zframe_t* frame = zmsg_next(msg);
size_t fsize = zframe_size(frame);
const byte* fbytes = zframe_data(frame);
sig.ParseFromArray(fbytes+1, fsize-1);
zmsg_destroy(&msg);
return true;
}
template<class C>
static void Send(const C& req, void* socket) {
zmsg_t* msg = zmsg_new();
size_t fsize = req.ByteSize();
zframe_t* frame = zframe_new(0, fsize);
byte* data = zframe_data(frame);
req.SerializeToArray(data, fsize);
zmsg_append(msg, &frame);
zmsg_send(&msg, socket);
}
template<class C>
static void SendPub(const C& sig, void* socket) {
size_t fsize = sig.ByteSize()+1;
zframe_t* frame = zframe_new(0, fsize);
byte* data = zframe_data(frame);
data[0] = 1;
sig.SerializeToArray(data+1, fsize-1);
zmsg_t* msg = zmsg_new();
zmsg_append(msg, &frame);
zmsg_send(&msg, socket);
}
#endif /* BASECLIENT_H_ */