fix send from client to server
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
|
|
||||||
#include "TcpClient.hpp"
|
#include "TcpClient.hpp"
|
||||||
|
|
||||||
|
#include <QThread>
|
||||||
|
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
|
|
||||||
TcpClient::TcpClient(ConfigService* config, LoggerService* logger, QObject* parent) : QObject(parent), logger_(logger) {
|
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);
|
socket_.connectToHost(QString::fromStdString(configuration_.hostname), configuration_.port);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TcpClient::send(const char* data, size_t numBytes) {
|
void TcpClient::tcpSend(const char* data, size_t numBytes) {
|
||||||
socket_.write(data, numBytes);
|
|
||||||
|
// encode header
|
||||||
|
uint32_t msgSize = static_cast<uint32_t>(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() {
|
void TcpClient::onConnected() {
|
||||||
@@ -73,12 +86,20 @@ void TcpClient::handleMessage() {
|
|||||||
QByteArray message = receiveBuffer_.mid(kHeaderSize, msgSize);
|
QByteArray message = receiveBuffer_.mid(kHeaderSize, msgSize);
|
||||||
receiveBuffer_.remove(0, msgSize + kHeaderSize);
|
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() {
|
// ErrorCode TcpClient::init() {
|
||||||
// logger_->log("TcpClient", LogFlag::Debug, "Initializing TcpClient...");
|
// logger_->log("TcpClient", LogFlag::Debug, "Initializing TcpClient...");
|
||||||
|
|
||||||
|
|||||||
@@ -29,14 +29,17 @@ public:
|
|||||||
~TcpClient();
|
~TcpClient();
|
||||||
|
|
||||||
void connectServer(); // asynchronous so no errorcode return
|
void connectServer(); // asynchronous so no errorcode return
|
||||||
void send(const char* data, size_t numBytes);
|
void tcpSend(const char* data, size_t numBytes);
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void connected();
|
void connected();
|
||||||
void disconnected();
|
void disconnected();
|
||||||
void messageReceived(QByteArray message);
|
void messageReceived(QString message);
|
||||||
void errorOccurred(QString error);
|
void errorOccurred(QString error);
|
||||||
|
|
||||||
|
public slots:
|
||||||
|
void send(QString message);
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void onConnected();
|
void onConnected();
|
||||||
void onDisconnected();
|
void onDisconnected();
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ int main(int argc, char* argv[]) {
|
|||||||
|
|
||||||
client.connectServer();
|
client.connectServer();
|
||||||
const char data[] = "hi hello I would like some beans on toast";
|
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
|
// attach backend gui components
|
||||||
qmlRegisterType<TimerComponent>("accordion", 1, 0, "TimerComponent");
|
qmlRegisterType<TimerComponent>("accordion", 1, 0, "TimerComponent");
|
||||||
|
|||||||
@@ -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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user