-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.cpp
95 lines (77 loc) · 1.99 KB
/
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
//This is an open source non-commercial project. Dear PVS-Studio, please check it.
#include <ctime>
#include <fstream>
#include <algorithm>
#include "server.h"
int Server::countFiles = 0;
Server::Server(TypeParams _params, TypeInterface interface):
Worker(_params, interface)
{
isGetData = false;
if(_params->getName() == "UDP")
{
interface->bind();
}
}
string Server::getFileName()
{
char buff[128];
time_t t = time(nullptr); // get time now
buff[0] = 0x0;
countFiles++;
strftime(buff, 128, "%X", localtime( &t ));
return string(buff) + "_" + std::to_string(countFiles) + ".hex";
}
void Server::run_func()
{
char buff[BUFF_SIZE];
int res = getInterface()->read(buff, BUFF_SIZE, 2*DELAY_MSEC);
#ifdef VERBOSE_PRINT
cout << "server get res=" << res << endl;
#endif
if(res > 0)
{
if(!isGetData)
{
cout << "Start get data" << endl;
}
isGetData = true;
#ifdef ECHO_RESPONSE
getInterface()->write(buff, res);
#endif
for(int iii = 0; iii<res; iii++)
{
#ifdef VERBOSE_PRINT
std::cout << std::hex;
std::cout << (unsigned int)buff[iii] << " ";
#endif
ArrayData.push_back(buff[iii]);
}
#ifdef VERBOSE_PRINT
std::cout << std::endl;
#endif
}
else
{
if(isGetData)
{
//Write data to file
string fileName = getFileName();
std::replace(fileName.begin(), fileName.end(), ':', '_');
ofstream file(fileName);
if(file.is_open())
{
file.write(ArrayData.data(), static_cast<int>(ArrayData.size()));
cout << "Write file " << fileName << endl;
}
else
{
cout << "!!! Error open file " << fileName << endl;
}
file.close();
//Clear some data
isGetData = false;
ArrayData.clear();
}
}
}