Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions Common/Cpp/Sockets/ClientSocket_Qt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

#include <chrono>
#include <QSharedPointer>
#include <QTimer>
#include <QTcpSocket>
#include "Common/Qt/QtThreadPool.h"
Expand Down Expand Up @@ -93,25 +94,27 @@ size_t ClientSocket_Qt::send(const void* data, size_t bytes){
return 0;
}

SendData send_data;
send_data.packet.assign((const char*)data, bytes);
QSharedPointer<SendData> send_data = QSharedPointer<SendData>::create();
send_data->packet.assign((const char*)data, bytes);

emit internal_send(&send_data);
emit internal_send(send_data);

// Wait for the socket's event thread to finish writing the packet.
// The timeout covers the case where "close()" destroys the socket
// before the queued call ever runs, in which case it will never
// complete on its own.
// complete on its own. The shared ownership makes the timeout
// safe: if it expires, the queued call's own copy keeps the
// payload alive until it runs.
{
std::unique_lock<Mutex> lg(send_data.lock);
send_data.cv.wait_for(
std::unique_lock<Mutex> lg(send_data->lock);
send_data->cv.wait_for(
lg,
std::chrono::seconds(5),
[&send_data]{ return send_data.done; }
[&send_data]{ return send_data->done; }
);
}

return send_data.bytes_sent;
return send_data->bytes_sent;
}

void ClientSocket_Qt::connection_failed(const std::string& error_message){
Expand Down Expand Up @@ -238,7 +241,7 @@ std::unique_ptr<QTcpSocket> ClientSocket_Qt::make_socket(){
);
QObject::connect(
this, &ClientSocket_Qt::internal_send,
socket.get(), [this, sock = socket.get()](SendData* send_data){
socket.get(), [this, sock = socket.get()](QSharedPointer<SendData> send_data){
{
std::lock_guard<Mutex> lg(m_lock);
m_sends_in_flight.fetch_add(1, std::memory_order_acq_rel);
Expand Down
3 changes: 2 additions & 1 deletion Common/Cpp/Sockets/ClientSocket_Qt.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <string>
#include <atomic>
#include <QObject>
#include <QSharedPointer>
#include "Common/Cpp/Concurrency/Mutex.h"
#include "Common/Cpp/Concurrency/ConditionVariable.h"
#include "AbstractClientSocket.h"
Expand Down Expand Up @@ -60,7 +61,7 @@ class ClientSocket_Qt final : public QObject, public AbstractClientSocket{

signals:
void internal_connect(const std::string& address, uint16_t port);
void internal_send(SendData* send_data);
void internal_send(QSharedPointer<SendData> send_data);


private:
Expand Down
Loading