diff --git a/client/src/TcpClient.cpp b/client/src/TcpClient.cpp index 759c04d..bc9bb9a 100644 --- a/client/src/TcpClient.cpp +++ b/client/src/TcpClient.cpp @@ -1,6 +1,8 @@ #include "TcpClient.hpp" +#include + #include TcpClient::TcpClient(ConfigService* config, LoggerService* logger, QObject* parent) : QObject(parent), logger_(logger) { @@ -29,8 +31,19 @@ void TcpClient::connectServer() { socket_.connectToHost(QString::fromStdString(configuration_.hostname), configuration_.port); } -void TcpClient::send(const char* data, size_t numBytes) { - socket_.write(data, numBytes); +void TcpClient::tcpSend(const char* data, size_t numBytes) { + + // encode header + uint32_t msgSize = static_cast(numBytes); + uint32_t msgType = 0; + + QByteArray buffer(kHeaderSize + numBytes, 0); + memcpy(buffer.data(), &msgSize, sizeof(msgSize)); + memcpy(buffer.data() + sizeof(msgSize), &msgType, sizeof(msgType)); + + logger_->log("TcpClient", LogFlag::Info, "tcpSend()"); + socket_.write(buffer.data(), numBytes); + } void TcpClient::onConnected() { @@ -73,12 +86,20 @@ void TcpClient::handleMessage() { QByteArray message = receiveBuffer_.mid(kHeaderSize, msgSize); receiveBuffer_.remove(0, msgSize + kHeaderSize); - emit messageReceived(message); + logger_->log("TcpClient", LogFlag::Error, "Received message from server: {}", receiveBuffer_.constData()); + + emit messageReceived(QString::fromUtf8(receiveBuffer_, msgSize)); } } +void TcpClient::send(QString message) { + + logger_->log("TcpClient", LogFlag::Debug, "Sending {} bytes: {}", message.length(), message.toUtf8().constData()); + tcpSend(message.toUtf8().constData(), message.length()); +} + // ErrorCode TcpClient::init() { // logger_->log("TcpClient", LogFlag::Debug, "Initializing TcpClient..."); diff --git a/client/src/TcpClient.hpp b/client/src/TcpClient.hpp index 4cfa55b..68f1fd4 100644 --- a/client/src/TcpClient.hpp +++ b/client/src/TcpClient.hpp @@ -29,14 +29,17 @@ public: ~TcpClient(); void connectServer(); // asynchronous so no errorcode return - void send(const char* data, size_t numBytes); + void tcpSend(const char* data, size_t numBytes); signals: void connected(); void disconnected(); - void messageReceived(QByteArray message); + void messageReceived(QString message); void errorOccurred(QString error); +public slots: + void send(QString message); + private slots: void onConnected(); void onDisconnected(); diff --git a/client/src/main.cpp b/client/src/main.cpp index 96a8f9b..139c966 100644 --- a/client/src/main.cpp +++ b/client/src/main.cpp @@ -29,7 +29,7 @@ int main(int argc, char* argv[]) { client.connectServer(); const char data[] = "hi hello I would like some beans on toast"; - client.send(data, sizeof(data)); + client.tcpSend(data, sizeof(data)); // attach backend gui components qmlRegisterType("accordion", 1, 0, "TimerComponent"); diff --git a/client/ui/Main.qml b/client/ui/Main.qml index 79d6698..d0bd2ed 100644 --- a/client/ui/Main.qml +++ b/client/ui/Main.qml @@ -31,4 +31,23 @@ ApplicationWindow { } } + ScrollView { + anchors.centerIn: parent + width: 300 + height: 150 + + TextArea { + id: messageArea + placeholderText: "Message..." + wrapMode: TextEdit.WordWrap + } + Button { + text: "Submit" + onClicked: { + tcpClient.send(messageArea.text) + messageArea.clear() + } + } + } + } \ No newline at end of file