-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
88 lines (66 loc) · 2.62 KB
/
server.cpp
File metadata and controls
88 lines (66 loc) · 2.62 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "server.h"
Cuma::Server::Server(unsigned int Port,
QSharedPointer<Cuma::NetworkConfig::ServerList>& list,
QSharedPointer<Cuma::DbFileFrag::DbFileFragInfo>& info,
QSharedPointer<Cuma::DbAddress::DbAddressPathByFile>& DbAddressByFile,
QSharedPointer<Cuma::FileBlockStorage::FileFragDir>& FileStorage):
ServerList(list),
port(Port),
DbFileFragInfo(info),
DbAddressByFile(DbAddressByFile),
FileBlockStorage(FileStorage)
{
ConnectRequestServer = QSharedPointer<QTcpServer>::create();
}
Cuma::Server::~Server()
{
ConnectRequestServer->close();
for (QSharedPointer<Cuma::ClientHandler> iter : RunningClientHandler)
{
iter->Stop();
}
}
void Cuma::Server::OnConnect()
{
QTcpSocket* Socket = ConnectRequestServer->nextPendingConnection();
QSharedPointer<QtJsonSocketLib_v3> JsonSocket = QSharedPointer<QtJsonSocketLib_v3>::create(false, true);
JsonSocket->set_socket(Socket);
QSharedPointer<Cuma::ClientHandler> Handler = QSharedPointer<Cuma::ClientHandler>::create(JsonSocket,
ServerList,
DbAddressByFile,
DbFileFragInfo,
FileBlockStorage,
QCryptographicHash::Sha3_512);
QSharedPointer<QThread> Thread = QSharedPointer<QThread>::create();
BindThreadClientHandler(Handler, Thread);
Cuma::Address::IpAddress ConnectFromClient;
ConnectFromClient.IP = Socket->peerAddress().toString();
ConnectFromClient.Port = Socket->peerPort();
QString StringIpAddress = ConnectFromClient.IP + ":" + QString::number(ConnectFromClient.Port);
RunningClientHandler.insert(StringIpAddress, Handler);
ClientHandlerThreadList.append(Thread);
Thread->start();
}
void Cuma::Server::OnStop()
{
ConnectRequestServer->close();
for (QSharedPointer<QThread> HandlerThread : ClientHandlerThreadList)
{
HandlerThread->quit();
HandlerThread->wait();
return;
}
}
void Cuma::Server::OnStart()
{
DEBUGLOG("서버 스레드가 시작되었습니다.");
connect(ConnectRequestServer.data(), &QTcpServer::newConnection, this, &Server::OnConnect);
ConnectRequestServer->listen(QHostAddress::Any, port);
DEBUGLOG("포트 : " + QString::number(port));
}
void Cuma::Server::BindThreadClientHandler(const QSharedPointer<Cuma::ClientHandler>& Handler, const QSharedPointer<QThread>& Thread)
{
Handler->moveToThread(Thread.data());
connect(Thread.data(), &QThread::started, Handler.data(), &ClientHandler::Start);
connect(Thread.data(), &QThread::finished, Handler.data(), &ClientHandler::Stop);
}