-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimpletcpserver.cpp
72 lines (66 loc) · 2.62 KB
/
simpletcpserver.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <QCoreApplication>
#include <QDebug>
#include <QTcpServer>
#include <QTcpSocket>
#include "bcf.h"
using namespace std;
/**
* @file : simpletcpserver.cpp
* @brief : 使用 @class ByHeadProtocolModel 协议即指定头部长度协议演示一个简单的bcf tcp服务端使用方法
*/
class MyTcpServer : public QTcpServer
{
Q_OBJECT
public:
MyTcpServer(QObject* parent = nullptr) : QTcpServer(parent) {}
protected:
void incomingConnection(qintptr socketDescriptor) override
{
QTcpSocket* clientSocket = new QTcpSocket;
if (clientSocket->setSocketDescriptor(socketDescriptor)) {
connect(clientSocket, &QTcpSocket::readyRead, this, &MyTcpServer::readClient);
connect(clientSocket, &QTcpSocket::disconnected, clientSocket, &QTcpSocket::deleteLater);
} else {
delete clientSocket;
}
}
private slots:
void readClient()
{
QTcpSocket* clientSocket = qobject_cast<QTcpSocket*>(sender());
if (clientSocket) {
QByteArray res = clientSocket->readAll();
bcf::ByteBufferPtr ptr = std::make_shared<bb::ByteBuffer>((uint8_t*)res.data(), res.length());
bcf::ByHeadProtocolParser parser;
if (parser.sniff(ptr)) {
parser.parse([&clientSocket](bcf::ParserState state,
std::shared_ptr<bcf::AbstractProtocolModel> model) {
auto bmodel = std::dynamic_pointer_cast<bcf::ByHeadProtocolModel>(model);
if (bmodel) {
qDebug() << "recv:" << QString::fromStdString(bmodel->body());
bcf::ByHeadProtocolBuilder builder;
auto amodel = std::make_shared<bcf::ByHeadProtocolModel>();
amodel->seq = bmodel->seq;
amodel->cmd = bmodel->cmd;//作为服务端,使用和客户端一样的cmd进行回应
amodel->setBody("my name is bcf!");
qDebug() << "reply:" << QString::fromStdString(amodel->body());
auto ptr = builder.build(amodel);
clientSocket->write((const char* )ptr->data(), ptr->size());
}
});
}
}
}
};
int main(int argc, char* argv[])
{
QCoreApplication app(argc, argv);
MyTcpServer server;
if (!server.listen(QHostAddress::Any, 1234)) {
qCritical() << "Unable to start the server:" << server.errorString();
return app.exec();
}
qDebug() << "Server started on port" << server.serverPort();
return app.exec();
}
#include "simpletcpserver.moc"