-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainDialog.cpp
More file actions
122 lines (99 loc) · 3.57 KB
/
MainDialog.cpp
File metadata and controls
122 lines (99 loc) · 3.57 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
113
114
115
116
117
118
119
120
121
122
/**
* \file MainDialog.cpp
* \brief
*/
#include "MainDialog.h"
#include <QtCore\qtextstream.h>
#include "MainDialog.moc"
//-------------------------------------------------------------------------------------------------
MainDialog::MainDialog() :
_sharedMemory ("SharedMemory"),
_sysSemaphoreWriteDone("SysSemaphoreWriteDone", 0)
{
setupUi(this);
bool result = _sharedMemory.create(1024);
if (result) {
// Client: We've created memory, so we are first. This one will send messages.
char *to = (char *)_sharedMemory.data();
memset(to, 0, _sharedMemory.size());
connect(m_sendButton, SIGNAL(clicked()), this, SLOT(onSend()));
} else {
// Server: Shared memory already exists, so this is second process. This one will listen.
result = _sharedMemory.attach();
Q_ASSERT(result);
m_sendButton->setEnabled(false);
ThreadClient *client = new ThreadClient(_sharedMemory);
connect(client, SIGNAL( sig_textReady(QString) ), this, SLOT( onTextReady(QString) ));
client->start();
}
}
//-------------------------------------------------------------------------------------------------
MainDialog::~MainDialog()
{
// need to add waiting for thread termination
}
//-------------------------------------------------------------------------------------------------
void
MainDialog::onTextReady(
QString a_text
)
{
m_receivedTextLabel->setText(a_text);
}
//-------------------------------------------------------------------------------------------------
void
MainDialog::onSend()
{
//-- _sharedMemory.lock();
char *to = (char *)_sharedMemory.data();
QString text = m_whatToSend->text();
QChar *data = text.data();
while ( !data->isNull() ) {
memset(to, data->toLatin1(), 1);
++ data;
++ to;
}
memset(to, 0, 1); // null terminator
//-- _sharedMemory.unlock();
//-- _sharedMemory.lock();
// resume other process
_sysSemaphoreWriteDone.release();
// this one will block client again, however, it could take time for client to read data,
_sysSemaphoreWriteDone.acquire();
}
//-------------------------------------------------------------------------------------------------
/**************************************************************************************************
* ThreadClient
*
**************************************************************************************************/
//-------------------------------------------------------------------------------------------------
// you can comment semaphores and uncomment lock/unlock, result will be the same
ThreadClient::ThreadClient(
QSharedMemory &a_sharedMemory
) :
// I assume that counter is already initialized by the server
_sysSemaphoreWriteDone("SysSemaphoreWriteDone"),
_sharedMemory (a_sharedMemory)
{
}
//-------------------------------------------------------------------------------------------------
void
ThreadClient::run()
{
while (true) {
// there is no active pooling, we just wait for semaphore
//-- _sharedMemory.lock();
_sysSemaphoreWriteDone.acquire();
char const *from = (char const *)_sharedMemory.data();
QString text;
QTextStream stream(&text);
while (from && *from) {
stream << *from;
++ from;
}
//-- _sharedMemory.unlock();
_sysSemaphoreWriteDone.release();
Q_EMIT sig_textReady(text);
}
}
//-------------------------------------------------------------------------------------------------