forked from yeyouqun/xdeltalib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxdelta_server.cpp
259 lines (227 loc) · 7.09 KB
/
xdelta_server.cpp
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
// author:[email protected]
#include <string>
#include <set>
#include <algorithm>
#include <assert.h>
#ifdef _WIN32
#include <functional>
#include <hash_map>
#else
#include <unistd.h>
#include <memory>
#include <ext/functional>
#if !defined (__CXX_11__)
#include <ext/hash_map>
#else
#include <unordered_map>
#endif
#include <memory.h>
#endif
#include "mytypes.h"
#include "Host.h"
#include "buffer.h"
#include "SimpleSocket.h"
#include "PassiveSocket.h"
#include "platform.h"
#include "rw.h"
#include "rollsum.h"
#include "digest.h"
#include "xdeltalib.h"
#include "tinythread.h"
#include "reconstruct.h"
#include "multiround.h"
#include "stream.h"
#include "xdelta_server.h"
namespace xdelta {
template <typename fobj_type>
struct file_releaser
{
file_operator * fop_;
fobj_type * fobj_;
file_releaser (file_operator * fop, fobj_type * fobj) : fop_ (fop), fobj_ (fobj) {}
~file_releaser () { fop_->release (fobj_); }
};
struct hasher_strcut
{
std::string addr_;
uint16_t port_;
file_operator * fop_;
xdelta_observer * observer_;
uint64_t auto_multiround_filsize_;
bool inplace_;
};
static void xdelta_server_thread (void * data)
{
//
// receive hasher request and send data back.
//
CActiveSocket client;
hasher_strcut * phs = (hasher_strcut*)data;
try {
init_active_socket (client, (uchar_t*)phs->addr_.c_str (), phs->port_);
DEFINE_STACK_BUFFER (buff);
while (true) {
block_header header = read_block_header (client, *phs->observer_);
if (header.blk_type != BT_CLIENT_FILE_BLOCK) {
std::string errmsg = fmt_string ("Error data format(not BT_CLIENT_FILE_BLOCK).");
THROW_XDELTA_EXCEPTION_NO_ERRNO (errmsg);
}
buff.reset ();
assert (header.blk_len != 0);
read_block (buff, client, header.blk_len, *phs->observer_);
std::string filename;
buff >> filename;
//how to determine multround stream?
file_releaser<file_reader> reader (phs->fop_, phs->fop_->create_reader (filename));
if (phs->inplace_) {
tcp_hasher_stream hasher (client, *phs->fop_, *phs->observer_, phs->inplace_);
hash_table ().hash_it (*reader.fobj_, hasher);
}
else {
uint64_t filsize = 0;
if (reader.fobj_->exist_file ()) {
reader.fobj_->open_file ();
filsize = reader.fobj_->get_file_size ();
}
if (phs->auto_multiround_filsize_ == NO_MULTI_ROUND_FILSIZE ||
filsize <= phs->auto_multiround_filsize_) {
tcp_hasher_stream hasher (client, *phs->fop_, *phs->observer_);
hash_table ().hash_it (*reader.fobj_, hasher);
}
else {
multiround_hash_table mht;
hash_table & table = mht;
multiround_tcp_stream hasher (client, *phs->fop_, *phs->observer_);
table.hash_it (*reader.fobj_, hasher);
}
}
}
}
catch (xdelta_exception &e) {
phs->observer_->on_error (e.what (), e.get_errno ());
}
catch (...) {
phs->observer_->on_error ("Unknow exception.", 0);
}
client.Close ();
}
// client ---> server (one link, used to transmit files to be hash and other parameters)
// client <---> server (one to multiple, used to transmit hash value and xdelta value)
//
void xdelta_server::_start_task (file_operator & foperator
, xdelta_observer & observer
, uint16_t port)
{
init_passive_socket (server_, port);
std::auto_ptr<CActiveSocket> client_auto;
CActiveSocket * client = 0;
DEFINE_STACK_BUFFER (buff);
if ((client = server_.Accept()) != NULL) {
client_auto.reset (client);
std::vector<thread*> hasher_threads;
block_header header = read_block_header (*client, observer);
if (header.blk_type != BT_CLIENT_BLOCK) {
std::string errmsg = fmt_string ("Error data format(not BT_CLIENT_BLOCK).");
THROW_XDELTA_EXCEPTION_NO_ERRNO (errmsg);
}
int nthread = header.blk_len / sizeof (uint16_t);
// get client ports that receive hasher data.
for (int i = 0; i < nthread; ++i) {
buff.reset ();
read_block (buff, (*client), sizeof (uint16_t), observer);
// create hasher and sender.
std::auto_ptr<hasher_strcut> data (new hasher_strcut);
data->addr_ = (char_t*)client->GetClientAddr ();
if (data->addr_.empty ())
continue;
buff >> data->port_;
data->fop_ = &foperator;
data->observer_ = &observer;
data->auto_multiround_filsize_ = auto_multiround_filsize_;
data->inplace_ = inplace_;
thread * thrd = new thread (xdelta_server_thread, (void *)data.release ());
hasher_threads.push_back (thrd);
}
wait_to_exit (hasher_threads);
}
server_.Close ();
}
void xdelta_server::run (file_operator & foperator
, xdelta_observer & observer
, uint16_t port)
{
try {
_start_task (foperator, observer, port);
}
catch (...) {
server_.Close ();
throw;
}
}
void xdelta_server::set_inplace ()
{
if (auto_multiround_filsize_ != NO_MULTI_ROUND_FILSIZE) {
std::string errmsg = fmt_string ("Multi-round already set.");
THROW_XDELTA_EXCEPTION_NO_ERRNO (errmsg);
}
inplace_ = true;
}
void xdelta_server::set_multiround_size (uint64_t multi_round_size)
{
if (inplace_) {
std::string errmsg = fmt_string ("In-place already set.");
THROW_XDELTA_EXCEPTION_NO_ERRNO (errmsg);
}
// less than this size will cause no difference with single round.
if (MULTIROUND_MAX_BLOCK_SIZE > auto_multiround_filsize_)
auto_multiround_filsize_ = NO_MULTI_ROUND_FILSIZE;
else
auto_multiround_filsize_ = multi_round_size;
}
void init_passive_socket (CPassiveSocket & server, uint16_t port)
{
if (!server.Initialize()) {
std::string errmsg = fmt_string ("Initializing server socket failed.");
THROW_XDELTA_EXCEPTION (errmsg);
}
if (!server.Listen((const uchar_t *)XDELTA_ADDR, port)) {
std::string errmsg = fmt_string ("Listen on port %d failed.", (uint32_t)port);
THROW_XDELTA_EXCEPTION (errmsg);
}
}
void init_active_socket (CActiveSocket & active, const uchar_t * addr, uint16_t port)
{
if (!active.Initialize ()) {
std::string errmsg = fmt_string ("Socket initialized failed on port %d."
, (uint32_t)port);
THROW_XDELTA_EXCEPTION (errmsg);
}
if (!active.Open (addr, port)) {
std::string errmsg = fmt_string ("Socket is broken or closed on port %d."
, (uint32_t)port);
THROW_XDELTA_EXCEPTION (errmsg);
}
}
void buffer_or_send (CSimpleSocket & socket
, char_buffer<uchar_t> & stream_buff
, char_buffer<uchar_t> & header_buff
, char_buffer<uchar_t> & data_buff
, xdelta_observer & observer)
{
uint32_t hbytes = header_buff.data_bytes ();
uint32_t dbytes = data_buff.data_bytes ();
if ((hbytes + dbytes) > stream_buff.available ())
send_block (socket, stream_buff, observer);
// size of header_buff_ is far more less than stream_buff.
stream_buff.copy (header_buff.rd_ptr (), hbytes);
header_buff.reset ();
if (data_buff.data_bytes () < stream_buff.available ()) {
stream_buff.copy (data_buff.rd_ptr (), dbytes);
data_buff.reset ();
}
else {
send_block (socket, stream_buff, observer);
send_block (socket, data_buff, observer);
}
}
} //namespace xdelta