-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmsgserver.cpp
More file actions
92 lines (78 loc) · 2.28 KB
/
msgserver.cpp
File metadata and controls
92 lines (78 loc) · 2.28 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
89
90
91
92
/**
* @class SCDMsgServer - https://github.com/SC-Develop/SCD_MC
*
* @author Ing. Salvatore Cerami - dev.salvatore.cerami@gmail.com - https://github.com/SC-Develop/
*
* @brief SCD Message Center Server: Interprocess messaging comunications server
*
* This is a part of SCD Message Center QT Class Library, a realtime messaging/commands system for exchange
* of inter-process messages/commands based on Tcp Socket
*
* This file must be distribuited with files:
*
* - msgcenter.cpp,
* - msgcenter.h,
* - msgserverthread.h,
* - msgserverthread.cpp,
* - msgthreadhandler.h
* - msgthreadhandler.cpp
*
*/
#include <QString>
#include "msgserver.h"
#include "msgserverthread.h"
/**
* @brief SCDMsgServer::SCDMsgServer
* @param parent
* @param mc
*/
SCDMsgServer::SCDMsgServer(int port, bool verbose, QString logFile, SCDMsgCenter *msgCnt, QObject *parent) : QTcpServer(parent), mc(msgCnt)
{
Verbose = verbose;
if (!msgCnt) // if not provided external message center create own message center
{
mc = new SCDMsgCenter();
}
Port = port;
if (logFile.isEmpty())
{
logFile = "msgserver.log";
}
LogErrorFile = logFile;
}
/**
* @brief SCDMsgServer::StartServer
*/
bool SCDMsgServer::start()
{
Status = listen(QHostAddress::Any,Port);
if (Status) // listen for incoming connections
{
QTextStream(stdout) << "\nMessage Center Server is listening on port: " << Port << "" << " for incoming connections..." << endl;
}
else
{
QTextStream(stdout) << "Could not start the Message Center Server on port: " << Port << " => " << this->errorString();
}
return Status;
}
/**
* @brief SCDMsgServer::StopServer
*/
void SCDMsgServer::stop()
{
if (isListening())
{
close();
}
}
/**
* @brief SCDMsgServer::incomingConnection
* @param SocketDescriptor
*/
void SCDMsgServer::incomingConnection(qintptr SocketDescriptor)
{
SCDMsgServerThread *SockThread = new SCDMsgServerThread(SocketDescriptor, mc, this); // Create a thread for handling client connections passing a socket descrirptor
connect(SockThread,SIGNAL(finished()),SockThread,SLOT(deleteLater())); // delete thread when finisced()
SockThread->start(); // start the thread;
}