Commit 4f84899a authored by Andreas Kempe's avatar Andreas Kempe

The server will now wait until all data is sent on the socket.

parent a28bf5d2
......@@ -7,13 +7,14 @@
using namespace std;
mandel_socket::mandel_socket(QString host, unsigned int port, QObject* parent) :
QTcpSocket(parent)
QTcpSocket(parent), transmission_done(false), bytes_written(0), bytes_to_write(0)
{
setup_slot_connection();
connectToHost(host, port, QIODevice::ReadWrite, QAbstractSocket::AnyIPProtocol);
}
mandel_socket::mandel_socket(QObject* parent) : QTcpSocket(parent)
mandel_socket::mandel_socket(QObject* parent) :
QTcpSocket(parent), transmission_done(false), bytes_written(0), bytes_to_write(0)
{
setup_slot_connection();
}
......@@ -35,7 +36,10 @@ void mandel_socket::send_data_packet(const data_packet& packet)
stream << packet;
write(buf);
flush();
transmission_done = false;
bytes_written = 0;
bytes_to_write = buf.size();
}
data_packet* mandel_socket::get_next_packet()
......@@ -49,9 +53,15 @@ data_packet* mandel_socket::get_next_packet()
return tmp;
}
bool mandel_socket::is_transmission_done()
{
return transmission_done;
}
void mandel_socket::setup_slot_connection()
{
connect(this, &mandel_socket::readyRead, this, &mandel_socket::handle_recv_data);
connect(this, &mandel_socket::bytesWritten, this, &mandel_socket::handle_bytes_written);
}
void mandel_socket::decode_packet_data(QByteArray& incoming)
......@@ -102,3 +112,11 @@ void mandel_socket::handle_recv_data()
decode_packet_data(incoming);
}
void mandel_socket::handle_bytes_written(qint64 written_bytes)
{
bytes_written += written_bytes;
if (bytes_written >= bytes_to_write)
transmission_done = true;
}
......@@ -16,6 +16,8 @@ class mandel_socket : public QTcpSocket
void send_data_packet(const data_packet& packet);
data_packet* get_next_packet();
bool is_transmission_done();
private:
void setup_slot_connection();
......@@ -25,8 +27,13 @@ class mandel_socket : public QTcpSocket
void handle_op_code(const QByteArray& data);
void decode_packet_data(QByteArray& incoming);
bool transmission_done;
qint64 bytes_written;
qint64 bytes_to_write;
private slots:
void handle_recv_data();
void handle_bytes_written(qint64 written_bytes);
signals:
void packet_ready();
......
......@@ -50,7 +50,7 @@ void root_process_handling(unsigned long *receive_buffer)
cout << "Sending image!" << endl;
mandel_socket socket("127.0.0.1", PORT, nullptr);
mandel_socket socket("192.168.1.2", PORT, nullptr);
socket.waitForConnected(10000);
Blob image_data;
......@@ -64,6 +64,11 @@ void root_process_handling(unsigned long *receive_buffer)
data_packet packet(op_codes::IMG_DATA, enc_img.size() + 1, data);
socket.send_data_packet(packet);
// Since we don't have an event loop (yet?)
// we'll wait for the socket to finish sending its data.
while (!socket.is_transmission_done())
socket.waitForBytesWritten();
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment