-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.cpp
96 lines (75 loc) · 2.09 KB
/
client.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
//This is an open source non-commercial project. Dear PVS-Studio, please check it.
#include "client.h"
Client::Client(TypeParams _params, TypeInterface interface, string fileName):
Worker(_params, interface)
{
this->fileName = fileName;
openFile();
}
Client::~Client()
{
//std::cout << "Close client " << std::endl;
fileStream.close();
//std::cout << "~Client " << std::endl;
}
void Client::run_func()
{
if(!isOpened)
{
return;
}
char buff[BUFF_SIZE];
fileStream.read(buff, BUFF_SIZE);
int res = fileStream.gcount();
//std::cout << "slice bytes res=" <<res << std::endl;
if(res > 0)
{
int res_send = getInterface()->write(buff, res);
count_send += res_send;
std::cout << "Send " << count_send << " bytes" << std::endl;
#ifdef ECHO_RESPONSE
{ // read
int count1 = 0;
std::cout << std::hex;
do {
res = getInterface()->read(buff, BUFF_SIZE, DELAY_MSEC*1000);
if(res > 0)
{
for(int iii = 0; iii < res; ++iii)
{
std::cout << " " << buff[iii];
}
}
count1 += res;
if(count1 == res_send)
{
break;
}
} while ( res != 0 );
std::cout << std::endl;
std::cout << std::dec;
}
#endif
}
else
{
std::cout << "All data send "<< count_send <<", file size " << file_size << std::endl;
std::cout << "press Enter..." << std::endl;
isRun = false;
}
}
void Client::openFile()
{
fileStream.open(fileName, std::ios::binary);
if(!fileStream.is_open())
{
isRun = false;
throw WorkerEx("File do not opened " + fileName);
}
// get length of file:
fileStream.seekg (0, fileStream.end);
file_size = fileStream.tellg();
fileStream.seekg (0, fileStream.beg);
//std::cout << "file_size=" << file_size << std::endl;
isOpened = true;
}