Skip to content

Commit 08b2519

Browse files
committed
Catch some Qt socket errors.
1 parent 6779e2a commit 08b2519

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

Common/Cpp/Sockets/ClientSocket_Qt.h

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
#ifndef PokemonAutomation_ClientSocket_Qt_H
88
#define PokemonAutomation_ClientSocket_Qt_H
99

10+
#include <iostream>
1011
#include <mutex>
1112
#include <condition_variable>
1213
#include <QThread>
1314
#include <QTcpSocket>
1415
//#include "Common/Cpp/Concurrency/SpinPause.h"
1516
#include "AbstractClientSocket.h"
1617

17-
//#include <iostream>
1818
//using std::cout;
1919
//using std::endl;
2020

@@ -27,7 +27,8 @@ class ClientSocket_Qt final : public QThread, public AbstractClientSocket{
2727

2828
struct SendData{
2929
const void* data;
30-
size_t bytes;
30+
size_t total_bytes;
31+
size_t bytes_sent;
3132
std::mutex lock;
3233
std::condition_variable cv;
3334
};
@@ -66,7 +67,8 @@ class ClientSocket_Qt final : public QThread, public AbstractClientSocket{
6667

6768
SendData send_data;
6869
send_data.data = data;
69-
send_data.bytes = bytes;
70+
send_data.total_bytes = bytes;
71+
send_data.bytes_sent = 0;
7072

7173
emit send(&send_data);
7274

@@ -76,7 +78,7 @@ class ClientSocket_Qt final : public QThread, public AbstractClientSocket{
7678
});
7779

7880
// cout << "blocking_send() - end: " << std::string((const char*)data, bytes) << endl;
79-
return send_data.bytes;
81+
return send_data.bytes_sent;
8082
}
8183

8284

@@ -96,6 +98,22 @@ class ClientSocket_Qt final : public QThread, public AbstractClientSocket{
9698
m_listeners.run_method_unique(&Listener::on_connect_finished, "");
9799
}
98100
);
101+
QThread::connect(
102+
&socket, &QTcpSocket::disconnected,
103+
&socket, [this]{
104+
std::cout << "QTcpSocket::disconnected()" << std::endl;
105+
m_state.store(State::DESTRUCTING, std::memory_order_release);
106+
quit();
107+
}
108+
);
109+
QThread::connect(
110+
&socket, &QTcpSocket::errorOccurred,
111+
&socket, [this](QAbstractSocket::SocketError error){
112+
std::cout << "QTcpSocket::errorOccurred(): error = " << (int)error << std::endl;
113+
m_state.store(State::DESTRUCTING, std::memory_order_release);
114+
quit();
115+
}
116+
);
99117
QThread::connect(
100118
&socket, &QTcpSocket::readyRead,
101119
&socket, [this]{
@@ -124,7 +142,7 @@ class ClientSocket_Qt final : public QThread, public AbstractClientSocket{
124142
SendData& data = *(SendData*)params;
125143
size_t sent = 0;
126144

127-
size_t bytes = data.bytes;
145+
size_t bytes = data.total_bytes;
128146

129147
const char* ptr = (const char*)data.data;
130148
while (bytes > 0 && m_socket->state() == QAbstractSocket::ConnectedState){
@@ -135,14 +153,13 @@ class ClientSocket_Qt final : public QThread, public AbstractClientSocket{
135153
sent += current_sent;
136154
ptr += current_sent;
137155
bytes -= current_sent;
138-
data.bytes = sent;
139156
}
140157

141158
m_socket->flush();
142159

143160
std::lock_guard<std::mutex> lg(data.lock);
144161
data.data = nullptr;
145-
data.bytes = sent;
162+
data.bytes_sent = sent;
146163
data.cv.notify_all();
147164

148165
// cout << "internal_send() - exit " << endl;
@@ -152,6 +169,9 @@ class ClientSocket_Qt final : public QThread, public AbstractClientSocket{
152169

153170
{
154171
std::lock_guard<std::mutex> lg(m_lock);
172+
if (this->state() == State::DESTRUCTING){
173+
return;
174+
}
155175
m_socket = &socket;
156176
}
157177
m_cv.notify_all();

0 commit comments

Comments
 (0)