-
Notifications
You must be signed in to change notification settings - Fork 0
/
KRBase.cpp
109 lines (89 loc) · 2.22 KB
/
KRBase.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
/*
* Copyright (C) 2020 by Thomas Early N7TAE
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <csignal>
#include <iostream>
#include "KRBase.h"
std::atomic<bool> CKRBase::keep_running(true);
CKRBase::CKRBase()
{
std::signal(SIGINT, CKRBase::SigHandler);
std::signal(SIGHUP, CKRBase::SigHandler);
std::signal(SIGTERM, CKRBase::SigHandler);
}
bool CKRBase::IsRunning()
{
return keep_running;
}
void CKRBase::SetState(bool state)
{
keep_running = state;
}
void CKRBase::SigHandler(int sig)
{
switch (sig)
{
case SIGINT:
case SIGHUP:
case SIGTERM:
keep_running = false;
break;
default:
std::cerr << "caught an unexpected signal=" << sig << std::endl;
break;
}
}
void CKRBase::AddFDSet(int &max, int newfd, fd_set *set)
{
if (newfd > max)
max = newfd;
FD_SET(newfd, set);
}
void CKRBase::Dump(const std::string &title, const unsigned char *data, unsigned int length)
{
printf("%s\n", title.c_str());
unsigned int offset = 0U;
while (length > 0U)
{
std::string output;
unsigned int bytes = (length > 16U) ? 16U : length;
for (unsigned i = 0U; i < bytes; i++)
{
char temp[10U];
::sprintf(temp, "%02X ", data[offset + i]);
output += temp;
}
for (unsigned int i = bytes; i < 16U; i++)
output += " ";
output += " *";
for (unsigned i = 0U; i < bytes; i++)
{
unsigned char c = data[offset + i];
if (::isprint(c))
output += c;
else
output += '.';
}
output += '*';
printf("%04X: %s\n", offset, output.c_str());
offset += 16U;
if (length >= 16U)
length -= 16U;
else
length = 0U;
}
}