server: base server code.
This commit is contained in:
4
.vscode/settings.json
vendored
4
.vscode/settings.json
vendored
@@ -155,6 +155,8 @@
|
||||
"qtablewidget": "cpp",
|
||||
"qapplication": "cpp",
|
||||
"text_encoding": "cpp",
|
||||
"hash_map": "cpp"
|
||||
"hash_map": "cpp",
|
||||
"qreadwritelock": "cpp",
|
||||
"qdatastream": "cpp"
|
||||
}
|
||||
}
|
||||
@@ -26,4 +26,5 @@ set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib/${CMAKE_BUILD_TYPE})
|
||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin/${CMAKE_BUILD_TYPE}/)
|
||||
|
||||
add_subdirectory(Protocol)
|
||||
add_subdirectory(Server)
|
||||
add_subdirectory(Test)
|
||||
|
||||
@@ -15,6 +15,9 @@ find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core)
|
||||
set(SOURCES
|
||||
Protocol.cxx
|
||||
Protocol.h
|
||||
InfoClient.h
|
||||
InfoClient.cpp
|
||||
InfoPack.hpp
|
||||
)
|
||||
|
||||
add_library(Protocol STATIC ${SOURCES})
|
||||
|
||||
13
Protocol/InfoClient.cpp
Normal file
13
Protocol/InfoClient.cpp
Normal file
@@ -0,0 +1,13 @@
|
||||
#include "InfoClient.h"
|
||||
|
||||
QDataStream& operator<<(QDataStream& data, const InfoClient& info)
|
||||
{
|
||||
info.serialize(data);
|
||||
return data;
|
||||
}
|
||||
|
||||
QDataStream& operator>>(QDataStream& data, InfoClient& info)
|
||||
{
|
||||
info.deserialize(data);
|
||||
return data;
|
||||
}
|
||||
49
Protocol/InfoClient.h
Normal file
49
Protocol/InfoClient.h
Normal file
@@ -0,0 +1,49 @@
|
||||
#ifndef COMSTRUCT_H
|
||||
#define COMSTRUCT_H
|
||||
|
||||
#include <QBuffer>
|
||||
#include <QDataStream>
|
||||
#include <QIODevice>
|
||||
#include <QString>
|
||||
#include <QVector>
|
||||
|
||||
struct InfoClient {
|
||||
QString id;
|
||||
QString name;
|
||||
|
||||
void serialize(QDataStream& data) const
|
||||
{
|
||||
data << id << name;
|
||||
}
|
||||
|
||||
void deserialize(QDataStream& data)
|
||||
{
|
||||
data >> id >> name;
|
||||
}
|
||||
};
|
||||
|
||||
QDataStream& operator<<(QDataStream& data, const InfoClient& info);
|
||||
QDataStream& operator>>(QDataStream& data, InfoClient& info);
|
||||
|
||||
struct InfoClientVec {
|
||||
QVector<InfoClient> vec;
|
||||
|
||||
void serialize(QDataStream& data) const
|
||||
{
|
||||
data << vec.size();
|
||||
for (const auto& info : vec) {
|
||||
data << info;
|
||||
}
|
||||
}
|
||||
void deserialize(QDataStream& data)
|
||||
{
|
||||
qsizetype size;
|
||||
data >> size;
|
||||
vec.resize(size);
|
||||
for (quint32 i = 0; i < size; ++i) {
|
||||
data >> vec[i];
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#endif // COMSTRUCT_H
|
||||
25
Protocol/InfoPack.hpp
Normal file
25
Protocol/InfoPack.hpp
Normal file
@@ -0,0 +1,25 @@
|
||||
#ifndef INFO_PACK_HPP
|
||||
#define INFO_PACK_HPP
|
||||
|
||||
#include <QByteArray>
|
||||
#include <QDataStream>
|
||||
#include <QDebug>
|
||||
|
||||
template <typename T> QByteArray infoPack(const T& obj)
|
||||
{
|
||||
QByteArray byteArray;
|
||||
QDataStream stream(&byteArray, QIODevice::ReadWrite);
|
||||
obj.serialize(stream);
|
||||
stream.device()->seek(0);
|
||||
return byteArray;
|
||||
}
|
||||
|
||||
template <typename T> T infoUnpack(const QByteArray& byteArray)
|
||||
{
|
||||
T obj;
|
||||
QDataStream stream(byteArray);
|
||||
obj.deserialize(stream);
|
||||
return obj;
|
||||
}
|
||||
|
||||
#endif // INFO_PACK_HPP
|
||||
17
Server/CMakeLists.txt
Normal file
17
Server/CMakeLists.txt
Normal file
@@ -0,0 +1,17 @@
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
|
||||
project(frelayServer LANGUAGES CXX)
|
||||
|
||||
set(CMAKE_AUTOUIC ON)
|
||||
set(CMAKE_AUTOMOC ON)
|
||||
set(CMAKE_AUTORCC ON)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 11)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core Network)
|
||||
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core Network)
|
||||
|
||||
add_executable(frelayServer Server.h Server.cpp main.cpp)
|
||||
target_link_libraries(frelayServer PRIVATE Protocol)
|
||||
target_link_libraries(frelayServer PRIVATE Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Network)
|
||||
221
Server/Server.cpp
Normal file
221
Server/Server.cpp
Normal file
@@ -0,0 +1,221 @@
|
||||
#include "Server.h"
|
||||
|
||||
#include <QDateTime>
|
||||
#include <QDebug>
|
||||
|
||||
#include "InfoClient.h"
|
||||
#include "InfoPack.hpp"
|
||||
|
||||
Server::Server(QObject* parent) : QTcpServer(parent)
|
||||
{
|
||||
monitorTimer_ = new QTimer(this);
|
||||
connect(monitorTimer_, &QTimer::timeout, this, &Server::monitorClients);
|
||||
}
|
||||
|
||||
Server::~Server()
|
||||
{
|
||||
stopServer();
|
||||
}
|
||||
|
||||
bool Server::startServer(quint16 port)
|
||||
{
|
||||
if (!listen(QHostAddress::Any, port)) {
|
||||
qWarning() << "Server start failed:" << errorString();
|
||||
return false;
|
||||
}
|
||||
|
||||
qDebug() << "Server started on port" << serverPort();
|
||||
monitorTimer_->start(30000);
|
||||
return true;
|
||||
}
|
||||
|
||||
void Server::stopServer()
|
||||
{
|
||||
monitorTimer_->stop();
|
||||
close();
|
||||
|
||||
QWriteLocker locker(&rwLock_);
|
||||
for (auto& client : clients_) {
|
||||
client->socket->disconnectFromHost();
|
||||
client->socket->deleteLater();
|
||||
}
|
||||
clients_.clear();
|
||||
}
|
||||
|
||||
void Server::onNewConnection()
|
||||
{
|
||||
QTcpSocket* clientSocket = nextPendingConnection();
|
||||
QString clientId = QString("%1:%2").arg(clientSocket->peerAddress().toString()).arg(clientSocket->peerPort());
|
||||
|
||||
if (clients_.size() >= 100) {
|
||||
qWarning() << "Client connection refused (max limit reached):" << clientId;
|
||||
clientSocket->disconnectFromHost();
|
||||
return;
|
||||
}
|
||||
|
||||
auto client = QSharedPointer<ClientInfo>::create();
|
||||
client->socket = clientSocket;
|
||||
client->id = clientId;
|
||||
client->connectTime = QDateTime::currentSecsSinceEpoch();
|
||||
|
||||
connect(clientSocket, &QTcpSocket::readyRead, this, &Server::onReadyRead);
|
||||
connect(clientSocket, &QTcpSocket::disconnected, this, &Server::onClientDisconnected);
|
||||
|
||||
{
|
||||
QWriteLocker locker(&rwLock_);
|
||||
clients_.insert(clientId, client);
|
||||
}
|
||||
|
||||
qDebug() << "Client connected:" << clientId;
|
||||
auto frame = QSharedPointer<FrameBuffer>::create();
|
||||
frame->type = FBT_SER_MSG_YOURID;
|
||||
frame->fid = "server";
|
||||
frame->tid = clientId;
|
||||
frame->data = QString("Welcome client %1").arg(clientId).toUtf8();
|
||||
sendData(clientSocket, frame);
|
||||
}
|
||||
|
||||
void Server::onClientDisconnected()
|
||||
{
|
||||
QTcpSocket* socket = qobject_cast<QTcpSocket*>(sender());
|
||||
if (!socket) {
|
||||
return;
|
||||
}
|
||||
|
||||
QString clientId = QString("%1:%2").arg(socket->peerAddress().toString()).arg(socket->peerPort());
|
||||
|
||||
{
|
||||
QWriteLocker locker(&rwLock_);
|
||||
clients_.remove(clientId);
|
||||
}
|
||||
|
||||
qDebug() << "Client disconnected:" << clientId;
|
||||
socket->deleteLater();
|
||||
}
|
||||
|
||||
void Server::onReadyRead()
|
||||
{
|
||||
QTcpSocket* socket = qobject_cast<QTcpSocket*>(sender());
|
||||
if (!socket) {
|
||||
return;
|
||||
}
|
||||
|
||||
QString clientId = QString("%1:%2").arg(socket->peerAddress().toString()).arg(socket->peerPort());
|
||||
|
||||
QSharedPointer<ClientInfo> client;
|
||||
{
|
||||
QReadLocker locker(&rwLock_);
|
||||
client = clients_.value(clientId);
|
||||
}
|
||||
|
||||
if (client) {
|
||||
client->buffer.append(socket->readAll());
|
||||
processClientData(client);
|
||||
}
|
||||
}
|
||||
|
||||
void Server::processClientData(QSharedPointer<ClientInfo> client)
|
||||
{
|
||||
while (true) {
|
||||
auto frame = Protocol::ParseBuffer(client->buffer);
|
||||
if (frame.isNull()) {
|
||||
break;
|
||||
}
|
||||
frame->fid = client->id;
|
||||
|
||||
if (frame->type <= 30) {
|
||||
frame->tid = "server";
|
||||
replyRequest(client, frame);
|
||||
} else {
|
||||
if (!forwardData(client, frame)) {
|
||||
qWarning() << "Failed to forward data from" << client->id << "to" << frame->tid;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool Server::forwardData(QSharedPointer<ClientInfo> client, QSharedPointer<FrameBuffer> frame)
|
||||
{
|
||||
QSharedPointer<ClientInfo> targetClient;
|
||||
{
|
||||
QReadLocker locker(&rwLock_);
|
||||
targetClient = clients_.value(frame->tid);
|
||||
}
|
||||
|
||||
if (targetClient) {
|
||||
return sendData(targetClient->socket, frame);
|
||||
} else {
|
||||
auto errorFrame = QSharedPointer<FrameBuffer>::create();
|
||||
errorFrame->type = FBT_SER_MSG_FORWARD_FAILED;
|
||||
errorFrame->fid = "server";
|
||||
errorFrame->tid = client->id;
|
||||
errorFrame->data = QString("Target client %1 not found").arg(frame->tid).toUtf8();
|
||||
return sendData(client->socket, errorFrame);
|
||||
}
|
||||
}
|
||||
|
||||
void Server::replyRequest(QSharedPointer<ClientInfo> client, QSharedPointer<FrameBuffer> frame)
|
||||
{
|
||||
switch (frame->type) {
|
||||
case FBT_SER_MSG_ASKCLIENTS: {
|
||||
QByteArray clientList = getClients();
|
||||
auto replyFrame = QSharedPointer<FrameBuffer>::create();
|
||||
replyFrame->type = FBT_SER_MSG_RESPONSE;
|
||||
replyFrame->fid = "server";
|
||||
replyFrame->tid = client->id;
|
||||
replyFrame->data = clientList;
|
||||
sendData(client->socket, replyFrame);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
qWarning() << "Unknown request type:" << frame->type;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
bool Server::sendData(QTcpSocket* socket, QSharedPointer<FrameBuffer> frame)
|
||||
{
|
||||
if (!socket || !socket->isOpen() || frame.isNull()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
QByteArray data = Protocol::PackBuffer(frame);
|
||||
if (data.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return socket->write(data) == data.size();
|
||||
}
|
||||
|
||||
void Server::monitorClients()
|
||||
{
|
||||
qint64 now = QDateTime::currentSecsSinceEpoch();
|
||||
QWriteLocker locker(&rwLock_);
|
||||
|
||||
for (auto it = clients_.begin(); it != clients_.end();) {
|
||||
if (now - it.value()->connectTime > 300) {
|
||||
qDebug() << "Disconnecting inactive client:" << it.value()->id;
|
||||
it.value()->socket->disconnectFromHost();
|
||||
it = clients_.erase(it);
|
||||
} else {
|
||||
++it;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
QByteArray Server::getClients()
|
||||
{
|
||||
InfoClientVec infoClients;
|
||||
|
||||
{
|
||||
QReadLocker locker(&rwLock_);
|
||||
for (auto& c : clients_) {
|
||||
InfoClient infoClient;
|
||||
infoClient.id = c->id;
|
||||
infoClients.vec.append(infoClient);
|
||||
}
|
||||
}
|
||||
|
||||
auto ret = infoPack<InfoClientVec>(infoClients);
|
||||
return ret;
|
||||
}
|
||||
50
Server/Server.h
Normal file
50
Server/Server.h
Normal file
@@ -0,0 +1,50 @@
|
||||
// Server.h
|
||||
#ifndef SERVER_H
|
||||
#define SERVER_H
|
||||
|
||||
#include <QMap>
|
||||
#include <QReadWriteLock>
|
||||
#include <QTcpServer>
|
||||
#include <QTcpSocket>
|
||||
#include <QTimer>
|
||||
|
||||
#include "Protocol.h"
|
||||
|
||||
class Server : public QTcpServer
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit Server(QObject* parent = nullptr);
|
||||
~Server();
|
||||
|
||||
bool startServer(quint16 port);
|
||||
void stopServer();
|
||||
|
||||
private slots:
|
||||
void onNewConnection();
|
||||
void onClientDisconnected();
|
||||
void onReadyRead();
|
||||
void monitorClients();
|
||||
|
||||
private:
|
||||
QByteArray getClients();
|
||||
|
||||
private:
|
||||
struct ClientInfo {
|
||||
QTcpSocket* socket;
|
||||
QString id;
|
||||
qint64 connectTime;
|
||||
QByteArray buffer;
|
||||
};
|
||||
|
||||
void processClientData(QSharedPointer<ClientInfo> client);
|
||||
bool forwardData(QSharedPointer<ClientInfo> client, QSharedPointer<FrameBuffer> frame);
|
||||
void replyRequest(QSharedPointer<ClientInfo> client, QSharedPointer<FrameBuffer> frame);
|
||||
bool sendData(QTcpSocket* socket, QSharedPointer<FrameBuffer> frame);
|
||||
|
||||
QMap<QString, QSharedPointer<ClientInfo>> clients_;
|
||||
QReadWriteLock rwLock_;
|
||||
QTimer* monitorTimer_;
|
||||
};
|
||||
|
||||
#endif // SERVER_H
|
||||
17
Server/main.cpp
Normal file
17
Server/main.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#include <QCoreApplication>
|
||||
|
||||
#include "Server.h"
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
QCoreApplication app(argc, argv);
|
||||
|
||||
Server server;
|
||||
if (!server.startServer(9009)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
qDebug() << "TCP server is running. Press Ctrl+C to exit...";
|
||||
|
||||
return app.exec();
|
||||
}
|
||||
@@ -4,5 +4,5 @@ project(frelayTest LANGUAGES CXX)
|
||||
set(CMAKE_CXX_STANDARD 11)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
add_executable(frelayTest protocolTest.cpp)
|
||||
add_executable(frelayTest protocolTest.cpp infoTest.h infoTest.cpp)
|
||||
target_link_libraries(frelayTest PRIVATE Protocol)
|
||||
83
Test/infoTest.cpp
Normal file
83
Test/infoTest.cpp
Normal file
@@ -0,0 +1,83 @@
|
||||
#include "infoTest.h"
|
||||
|
||||
#include <QDebug>
|
||||
#include <QElapsedTimer>
|
||||
|
||||
int infoTest1()
|
||||
{
|
||||
InfoClient ic;
|
||||
ic.id = "1234567890";
|
||||
ic.name = "Test";
|
||||
|
||||
InfoClientVec vec;
|
||||
vec.vec.append(ic);
|
||||
|
||||
QByteArray qa;
|
||||
QBuffer buf(&qa);
|
||||
buf.open(QIODevice::ReadWrite);
|
||||
|
||||
QDataStream qs(&buf);
|
||||
vec.serialize(qs);
|
||||
buf.close();
|
||||
|
||||
qDebug() << "Serialized data size:" << qa.size();
|
||||
qDebug() << "Serialized data:" << qa.toHex();
|
||||
|
||||
QBuffer buf2(&qa);
|
||||
buf2.open(QIODevice::ReadWrite);
|
||||
QDataStream qs2(&buf2);
|
||||
InfoClientVec vec2;
|
||||
vec2.deserialize(qs2);
|
||||
buf2.close();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int infoTest2()
|
||||
{
|
||||
InfoClient ic;
|
||||
ic.id = "1234567890";
|
||||
ic.name = "Test";
|
||||
|
||||
InfoClientVec vec;
|
||||
vec.vec.append(ic);
|
||||
|
||||
QByteArray qa;
|
||||
QDataStream qs(&qa, QIODevice::ReadWrite);
|
||||
vec.serialize(qs);
|
||||
|
||||
qDebug() << "Serialized data size:" << qa.size();
|
||||
qDebug() << "Serialized data:" << qa.toHex();
|
||||
|
||||
qs.device()->seek(0);
|
||||
InfoClientVec vec2;
|
||||
vec2.deserialize(qs);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void performanceTest()
|
||||
{
|
||||
QByteArray data;
|
||||
QElapsedTimer timer;
|
||||
|
||||
timer.start();
|
||||
QDataStream stream(&data, QIODevice::ReadWrite);
|
||||
for (int i = 0; i < 10000; ++i) {
|
||||
stream << i;
|
||||
stream.device()->seek(0);
|
||||
int val;
|
||||
stream >> val;
|
||||
}
|
||||
qDebug() << "Reuse stream:" << timer.elapsed() << "ms";
|
||||
|
||||
timer.start();
|
||||
for (int i = 0; i < 10000; ++i) {
|
||||
QDataStream out(&data, QIODevice::WriteOnly);
|
||||
out << i;
|
||||
QDataStream in(&data, QIODevice::ReadOnly);
|
||||
int val;
|
||||
in >> val;
|
||||
}
|
||||
qDebug() << "New streams:" << timer.elapsed() << "ms";
|
||||
}
|
||||
10
Test/infoTest.h
Normal file
10
Test/infoTest.h
Normal file
@@ -0,0 +1,10 @@
|
||||
#ifndef INFO_TEST_H_
|
||||
#define INFO_TEST_H_
|
||||
|
||||
#include <InfoClient.h>
|
||||
|
||||
int infoTest1();
|
||||
int infoTest2();
|
||||
void performanceTest();
|
||||
|
||||
#endif
|
||||
@@ -1,7 +1,8 @@
|
||||
#include <Protocol.h>
|
||||
#include <QDebug>
|
||||
#include "infoTest.h"
|
||||
|
||||
int main()
|
||||
int test1()
|
||||
{
|
||||
auto frame = QSharedPointer<FrameBuffer>::create();
|
||||
frame->type = FBT_CLI_BIN_FILEDATA;
|
||||
@@ -14,6 +15,11 @@ int main()
|
||||
qDebug() << "Packed data hex:" << packet.toHex();
|
||||
|
||||
auto ret = Protocol::ParseBuffer(packet);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
performanceTest();
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user