-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasyncfilesystemClient_2.cpp
339 lines (288 loc) Β· 9.04 KB
/
asyncfilesystemClient_2.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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include <iostream>
#include <fstream>
#include <sstream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/array.hpp>
//λΆμ€νΈμ tcp μλΉλ₯΄λ¬λ¦¬ μ¬μ©
using boost::asio::ip::tcp;
bool is_send = true;
/************************νμΌμ μ‘μ νλ λΆλΆ*************************************/
class fileTcpClient {
private:
tcp::resolver _resolver;
tcp::socket _socket;
boost::array<char, 8192> _buf;
boost::asio::streambuf _request;
std::ifstream _sourceFile;
boost::asio::io_service & _io_service;
//μ€μ λ‘ μλ²μ μ μμ μ€μ νλ Resolveing μ²λ¦¬ ν¨μ
void handleResolve(const boost::system::error_code & err, tcp::resolver::iterator myiterator) {
if (!err) {
//μ€λ₯κ° λ°μνμ§ μμλ€λ©΄
tcp::endpoint endpoint = *myiterator;
_socket.async_connect(endpoint,
boost::bind(&fileTcpClient::handleConnect,
this, boost::asio::placeholders::error,
++myiterator));
}
else {
std::cout << "β μΈν
μ€λ₯ β" << std::endl;
std::cout << "Error : " << err.message() << "\n";
}
}
void handleConnect(const boost::system::error_code & err, tcp::resolver::iterator myiterator) {
//μ€λ₯κ° λ°μ νμ§ μμ κ²½μ°
if (!err)
{
std::cout << "β νμΌ μ μ‘ β" << std::endl;
boost::asio::async_write(_socket,
_request,
boost::bind(&fileTcpClient::handleWriteFile,
this,
boost::asio::placeholders::error));
}
else if (myiterator != tcp::resolver::iterator())
{
//리μ€νΈμ 첫 λ²μ§Έ endpoint λ‘ μ°κ²° μλ
_socket.close();
tcp::endpoint endpoint = *myiterator;
_socket.async_connect(endpoint,
boost::bind(&fileTcpClient::handleConnect,
this,
boost::asio::placeholders::error,
++myiterator));
}
else
{
std::cout << "handleConnect" << std::endl;
std::cout << "β μ μ‘ μ€λ₯ β" << std::endl;
std::cout << "Error : " << err.message() << "\n";
}
}
//μ€μ λ‘ νμΌμ μ°κΈ° μν ν¨μ
void handleWriteFile(const boost::system::error_code & err)
{
// μ€λ₯κ° λ°μ νμ§ μμ κ²½μ°
if (!err)
{
//보λ΄λ νμΌμ΄ λλ λκΉμ§
if (_sourceFile.eof() == false)
{
//λ²νΌμ λ΄μ
_sourceFile.read(_buf.c_array(), (std::streamsize) _buf.size());
if (_sourceFile.gcount() <= 0)
{
std::cout << "β νμΌ μ€λ₯ β" << std::endl;
std::cout << "read file error" << std::endl;
return;
}
else
{
//보λ΄λ νμΌ μ 보.
std::cout << _sourceFile.gcount() << " bytes μ μ‘ μλ£, μ 체 :"
<< _sourceFile.tellg() << " bytes. \n";
//μ€μ λ‘ νμΌμ μ μ‘ νλ async_write ν¨μ μ€ν
boost::asio::async_write(_socket,
boost::asio::buffer(_buf.c_array(), _sourceFile.gcount()),
boost::bind(&fileTcpClient::handleWriteFile,
this,
boost::asio::placeholders::error));
}
//μ€λ₯κ° λ°μ νλκ²½μ°.
if (err)
{
std::cout << "β μ μ‘ μ€λ₯ β" << std::endl;
std::cout << "send error : " << err << std::endl;
return;
}
}
else
return;
}
else
{
std::cout << "β μ μ‘ μ€λ₯ β" << std::endl;
std::cout << "Error : " << err.message() << "\n";
}
}
public:
fileTcpClient(boost::asio::io_service & io_service, const std::string & server, const std::string &path)
:_resolver(io_service), _socket(io_service), _io_service(io_service)
{
//μλ²μ IPμ ν¬νΈ μ£Όμλ₯Ό λλμ΄ μ μ₯.
size_t pos = server.find(":");
if (pos == std::string::npos)
return;
std::string portString = server.substr(pos + 1);
std::string serverIP = server.substr(0, pos);
//μμ μ 보λ΄κ³ μΆμ μμ€ νμΌμ μ΄μ.
_sourceFile.open(path.c_str(), std::ios_base::binary | std::ios_base::ate);
//μμ€νμΌμ΄ μ‘΄μ¬ νμ§ μλκ²½μ°.
if (!_sourceFile) {
std::cout << "β νμΌ μ€λ₯ β" << std::endl;
std::cout << "νμΌ λΆλ¬μ€κΈ° μ€ν¨ : " << path << std::endl;
return;
}
//νμΌμ λν μ 보λ₯Ό λ΄λ λ³μ.
size_t fileSize = _sourceFile.tellg();
_sourceFile.seekg(0);
//λ¨Όμ μλ²μ νμΌμ κ²½λ‘μ νμΌμ ν¬κΈ°λ₯Ό μ μ‘.
std::cout << "β νμΌ μ 보 μ μ‘ β" << std::endl;
std::ostream requestStream(&_request);
requestStream << "fileSend" << "\n" << path << "\n" << fileSize << "\n\n";
std::cout << "μμ² ν¬κΈ° : " << _request.size() << std::endl;
//μλ²μ μλΉμ€μ μ΄λ¦μ λ²μνκΈ°μν΄ λΉλκΈ°μμΌλ‘ resolving μ²λ¦¬
//endpointsμ 리μ€νΈ μ²λ¦¬.
tcp::resolver::query query(serverIP, portString);
_resolver.async_resolve(query, boost::bind(&fileTcpClient::handleResolve, this, boost::asio::placeholders::error, boost::asio::placeholders::iterator));
}
};
class userTcpClient
{
private:
tcp::resolver resolver;
tcp::socket socket;
boost::array<char, 8192> buf;
boost::asio::streambuf request;
boost::asio::io_service & _io_service;
void handleResolve(const boost::system::error_code & err, tcp::resolver::iterator myiterator) {
if (!err) {
//μ€λ₯κ° λ°μνμ§ μμλ€λ©΄
tcp::endpoint endpoint = *myiterator;
socket.async_connect(endpoint, boost::bind(&userTcpClient::handleConnect,
this, boost::asio::placeholders::error,
++myiterator));
}
else {
std::cout << "β μΈν
μ€λ₯ β" << std::endl;
std::cout << "Error : " << err.message() << "\n";
}
}
void handleConnect(const boost::system::error_code & err, tcp::resolver::iterator myiterator) {
if (!err)
{
std::cout << "β μ 보 μ μ‘ β" << std::endl;
boost::asio::async_write(socket, request, boost::bind(&userTcpClient::handleWriteUser,
this,
boost::asio::placeholders::error));
}
else if (myiterator != tcp::resolver::iterator())
{
socket.close();
tcp::endpoint endpoint = *myiterator;
socket.async_connect(endpoint,
boost::bind(&userTcpClient::handleConnect,
this, boost::asio::placeholders::error,
++myiterator));
}
else
{
std::cout << "β μ μ‘ μ€λ₯ β" << std::endl;
std::cout << "Error : " << err.message() << "\n";
is_send = false;
}
}
void handleWriteUser(const boost::system::error_code & err)
{
if (err)
{
std::cout << "β μ μ‘ μ€λ₯ β" << std::endl;
std::cout << "Error : " << err.message() << "\n";
is_send = false;
}
}
public:
userTcpClient(boost::asio::io_service & io_service, const std::string & server, std::string userName, std::string userInfo)
:resolver(io_service), socket(io_service), _io_service(io_service)
{
size_t pos = server.find(":");
if (pos == std::string::npos)
return;
std::string portString = server.substr(pos + 1);
std::string serverIP = server.substr(0, pos);
std::cout << "β μ¬μ©μ μ 보 μ μ‘ β" << std::endl;
std::ostream requestStream(&request);
requestStream << "enter" << "\n" << userName << "\n"
<< userInfo << "\n\n";
std::cout << "μ¬μ©μ μ΄λ¦ : " << userName << ", μ¬μ©μ μ 보 : " << userInfo << std::endl;
tcp::resolver::query query(serverIP, portString);
resolver.async_resolve(query,
boost::bind(&userTcpClient::handleResolve,
this, boost::asio::placeholders::error,
boost::asio::placeholders::iterator));
}
/*userTcpClient& operator=(const userTcpClient& utc)
{
return *this;
}*/
void close()
{
_io_service.post([this]() { socket.close(); });
}
};
void SendFile() {
try {
std::cout << "-----------------------" << std::endl;
std::cout << " νμΌ μ μ‘ " << std::endl;
std::cout << "-----------------------" << std::endl;
std::string * IP = new std::string("127.0.0.1:39800");
std::string FILE;
std::cout << "μ μ‘ ν νμΌ κ²½λ‘ : ";
std::getline(std::cin, FILE, '\n');
boost::asio::io_service io_service;
fileTcpClient client(io_service, *IP, FILE);
io_service.run();
std::cout << "β μ μ‘ κ²°κ³Ό β" << std::endl;
std::cout << "νμΌ" << FILE << "μ μ‘μ μ±κ³΅ νμμ΅λλ€. \n";
}
catch (std::exception & e) {
std::cout << "β μ μ‘ μ€λ₯ β" << std::endl;
std::cerr << e.what() << std::endl;
}
}
void SendUser(std::string userName, std::string userInfo) {
try {
std::cout << "-----------------------" << std::endl;
std::cout << " μ¬μ©μ μ μ‘ " << std::endl;
std::cout << "-----------------------" << std::endl;
std::string * IP = new std::string("127.0.0.1:39800");
boost::asio::io_service io_service;
userTcpClient client(io_service, *IP, userName, userInfo);
io_service.run();
std::cout << "β μ μ‘ κ²°κ³Ό β" << std::endl;
if (is_send == true) {
std::cout << " μ¬μ©μ μ 보 μ μ‘μ μ±κ³΅ νμ΅λλ€. " << std::endl;
}
else
{
std::cout << " μ¬μ©μ μ 보 μ μ‘μ μ€ν¨ νμ΅λλ€. " << std::endl;
}
}
catch (std::exception & e) {
std::cout << "β μ μ‘ μ€λ₯ β" << std::endl;
std::cerr << e.what() << std::endl;
}
}
int main() {
std::string input;
std::string userName;
std::string userInfo;
std::cout << "μ¬μ©μ μ΄λ¦μ μ
λ ₯ νμΈμ" << std::endl;
std::getline(std::cin, userName, '\n');
std::cout << "μ¬μ©μ μ 보λ₯Ό μ
λ ₯ νμΈμ" << std::endl;
std::getline(std::cin, userInfo, '\n');
std::cout << "μ¬μ©μ λ±λ‘μ μλ ν©λλ€" << std::endl;
SendUser(userName, userInfo);
std::cout << "β 1.νμΌ μ‘μ β" << std::endl;
std::cout << "β 2.νλ‘κ·Έλ¨ μ’
λ£ β" << std::endl;
while (std::getline(std::cin, input, '\n')) {
if (input == "1") {
SendFile();
}
else if (input == "2") {
system("pause");
return 0;
}
}
}