-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmsgthreadhandler.cpp
More file actions
113 lines (97 loc) · 2.83 KB
/
msgthreadhandler.cpp
File metadata and controls
113 lines (97 loc) · 2.83 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
* @class SCDMsgThreadHandler - https://github.com/SC-Develop/SCD_MC
*
* @author Ing. Salvatore Cerami - dev.salvatore.cerami@gmail.com - https://github.com/SC-Develop/
*
* @brief Message Center Server: Thread Signal Processing
*
* 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,
* - msgserver.h,
* - msgserver.cpp,
* - msgserverthread.h
* - msgserverthread.cpp
*
*/
#include "QObject"
#include "msgserverthread.h"
#include "msgthreadhandler.h"
#define echo QTextStream(stdout) << "\n" <<
/**
* @brief SCDMsgThreadHandler::SCDMsgThreadHandler
* @param Id
* @param parent
*/
SCDMsgThreadHandler::SCDMsgThreadHandler(int socketDescriptor, SCDMsgCenter *mc) : SocketDescriptor(socketDescriptor), mc(mc)
{
}
/**
* @brief SCDMsgThreadHandler::~SCDMsgThreadHandler
*/
SCDMsgThreadHandler::~SCDMsgThreadHandler()
{
if (Socket)
{
delete Socket;
}
}
/**
* @brief SCDMsgThreadHandler::start
* @return
*/
int SCDMsgThreadHandler::start()
{
Socket = new QTcpSocket(); // create the connection socket object
connect(Socket,SIGNAL(readyRead()),this,SLOT(readyRead())); // set event for reading data from socket when avalaible
connect(Socket,SIGNAL(disconnected()),this,SLOT(disconnected())); // set event for closing connection;
if (!Socket->setSocketDescriptor(SocketDescriptor)) // Check for socket error
{
echo "Socket connection error: " << Socket->error();;
return 0;
}
connect(mc,SIGNAL(messageToClient_signal(QString,int)),this,SLOT(receiveFromMsgCenter(QString,int))); // set the slot to receive message from messge center
return 1;
}
/**
* @brief SCDMsgThreadHandler::readyRead
*/
void SCDMsgThreadHandler::readyRead()
{
QByteArray Buffer;
Buffer = Socket->readAll(); // Reading incoming data
mc->sendCommand(Buffer,SocketDescriptor); // send a client command to message center
}
/**
* @brief SCDMsgThreadHandler::disconnected
*/
void SCDMsgThreadHandler::disconnected()
{
thread()->quit();
}
/**
* @brief SCDMsgThreadHandler::receiveFromMsgCenter receive a message form message center and writes the message into destination socket connection
*
* @param msg
* @param toSocketDescriptor
*/
void SCDMsgThreadHandler::receiveFromMsgCenter(QString msg, int toSocketDescriptor)
{
if (toSocketDescriptor==SocketDescriptor)
{
if (msg.trimmed()=="exit")
{
Socket->close();
}
else
{
Socket->write(msg.toLatin1());
Socket->flush();
Socket->waitForBytesWritten();
}
}
}