Skip to content

Commit f03e719

Browse files
committed
Throttle the serial error messages.
1 parent fc2f127 commit f03e719

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

ClientSource/Connection/PABotBaseConnection.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,11 @@ void PABotBaseConnection::on_recv(const void* data, size_t bytes){
106106

107107
// Message is too long.
108108
if (length > MAX_MESSAGE_SIZE){
109-
m_sniffer->log("Message is too long: bytes = " + std::to_string(length));
109+
char ascii = ~length;
110+
std::string text = ascii < 32
111+
? ", ascii = " + std::to_string(ascii)
112+
: std::string(", char = ") + ascii;
113+
m_sniffer->log("Message is too long: bytes = " + std::to_string(length) + text);
110114
m_recv_buffer.pop_front();
111115
continue;
112116
}

ClientSource/Connection/SerialConnectionWinAPI.h

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,24 @@ class SerialConnection : public StreamConnection{
118118
}
119119

120120
private:
121-
void clear_error(){
121+
void process_error(const std::string& message){
122+
m_errors++;
123+
if (m_errors < 100 || m_errors % 1000 == 0){
124+
log(message);
125+
}
126+
127+
std::string clear_error;
122128
DWORD comm_error;
123129
if (ClearCommError(m_handle, &comm_error, nullptr) == 0){
124130
DWORD error = GetLastError();
125-
log("ClearCommError() failed. Error = " + std::to_string(error));
131+
clear_error = "ClearCommError() failed. Error = " + std::to_string(error);
132+
}else{
133+
clear_error = "ClearCommError error flag = " + std::to_string(comm_error);
134+
}
135+
136+
if (m_errors < 100 || m_errors % 1000 == 0){
137+
log(clear_error);
126138
}
127-
log("ClearCommError error flag = " + std::to_string(comm_error));
128139
}
129140

130141

@@ -141,12 +152,11 @@ class SerialConnection : public StreamConnection{
141152
DWORD written;
142153
if (WriteFile(m_handle, data, (DWORD)bytes, &written, nullptr) == 0 || bytes != written){
143154
DWORD error = GetLastError();
144-
log(
155+
process_error(
145156
"Failed to write: " + std::to_string(written) +
146157
" / " + std::to_string(bytes) +
147158
", error = " + std::to_string(error)
148159
);
149-
clear_error();
150160
}
151161
// auto stop = current_time();
152162
// cout << "WriteFile() : " << std::chrono::duration_cast<std::chrono::milliseconds>(stop - start).count() << endl;
@@ -164,8 +174,7 @@ class SerialConnection : public StreamConnection{
164174
DWORD read;
165175
if (ReadFile(m_handle, buffer, 32, &read, nullptr) == 0){
166176
DWORD error = GetLastError();
167-
log("ReadFile() failed. Error = " + std::to_string(error));
168-
clear_error();
177+
process_error("ReadFile() failed. Error = " + std::to_string(error));
169178
}
170179
// auto stop = current_time();
171180
// cout << "ReadFile() : " << std::chrono::duration_cast<std::chrono::milliseconds>(stop - start).count() << endl;
@@ -202,6 +211,7 @@ class SerialConnection : public StreamConnection{
202211
private:
203212
HANDLE m_handle;
204213
std::atomic<bool> m_exit;
214+
uint64_t m_errors = 0;
205215
SpinLock m_send_lock;
206216
std::thread m_listener;
207217
};

0 commit comments

Comments
 (0)