This repository was archived by the owner on Mar 15, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIRCppServer.cpp
More file actions
72 lines (63 loc) · 2.54 KB
/
IRCppServer.cpp
File metadata and controls
72 lines (63 loc) · 2.54 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
#include <SocketConnection.h>
#include <conio.h>
#include <ServerSocket.h>
#include <thread>
#include <Logger.h>
#include <CmdProcessor.h>
void WriteToConsole(std::string &msg) {
printf("%s %s\n", Logger::getFormattedTime().c_str(), msg.c_str());
}
void dataListenThread(ServerSocket *server, int clientId) {
printf("%s Started listening for data of client #%d\n", Logger::getFormattedTime().c_str(), clientId);
Server::CmdProcessor cmdProccessor(server);
while (server->IsClientAlive(clientId)) {
cmdProccessor.acceptMessage(clientId, server->ListenForData(clientId), (HandlePrint) WriteToConsole);
Sleep(150);
}
printf("%s Client #%d dead, stopping listening for data\n", Logger::getFormattedTime().c_str(), clientId);
}
void connectionsListenThread(ServerSocket *server, std::vector<std::thread *> *dataListenThreads) {
printf("%s Started connections listening\n", Logger::getFormattedTime().c_str());
while (server->IsSocketAlive()) {
int clientId = server->WaitForConnection();
if (clientId < 0)
return;
server->SendData(clientId, "Successfully connected!");
std::thread dlt(dataListenThread, server, clientId);
dlt.detach();
dataListenThreads->push_back(&dlt);
Sleep(150);
}
printf("%s Connections listening stopped\n", Logger::getFormattedTime().c_str());
}
void pingThread(ServerSocket *server) {
while (server->IsSocketAlive()) {
// server->PingAllClients();
Sleep(25000);
}
}
int main() {
setlocale(0, "ru_RU.UTF-8");
system("chcp 65001");
system("cls");
const char PORT[] = "1376";
auto *socketConnection = new SocketConnection();
socketConnection->CreateServer(PORT, "0.0.0.0");
socketConnection->OpenServerConnection();
auto *server = new ServerSocket(socketConnection);
printf("%s Creating threads for listening...\n", Logger::getFormattedTime().c_str());
std::vector<std::thread *> dataListenThreads;
std::thread connectionsThread(connectionsListenThread, server, &dataListenThreads);
std::thread pinger(pingThread, server);
printf("===== TO STOP SERVER PRESS SHIFT+Q =====\n");
while (_getch() != 'Q');
printf("%s Starting connection close...\n", Logger::getFormattedTime().c_str());
socketConnection->CloseConnection();
// Terminating threads
// TerminateThread((HANDLE) pinger.native_handle(), 0);
// TerminateThread((HANDLE) connectionsThread.native_handle(), 0);
pinger.detach();
connectionsThread.join();
system("pause");
return 0;
}