-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrconthread.cc
297 lines (224 loc) · 6.93 KB
/
rconthread.cc
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
//***************************************************************************
// File rconthread.cc
// Date 23.07.17 - #1
// Copyright (c) 2017-2017 s710 (s710 (at) posteo (dot) de). All rights reserved.
// --------------------------------------------------------------------------
// Ark ClusterChat / RCON thread
//***************************************************************************
#include "rconthread.hpp"
#include "channel.hpp"
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
//***************************************************************************
// class RConThread
//***************************************************************************
//***************************************************************************
// constructors
//***************************************************************************
RConThread::RConThread(std::list<RConThread*>* aThreads)
{
threads= aThreads;
hostName= 0;
passwd= 0;
map= 0;
port= -1;
tellBuffer= (char*)calloc(1024*1024, sizeof(char));
channel= new RConChannel;
sendBuffer= (char*)calloc(1024*10, sizeof(char));
sendBufferSize= 1024*10;
}
RConThread::~RConThread()
{
::free((void*)hostName);
::free((void*)passwd);
::free((void*)map);
::free((void*)tellBuffer);
::free((void*)sendBuffer);
delete channel;
}
//***************************************************************************
// open
//***************************************************************************
int RConThread::start(int blockTimeout, const char* aHostName, int aPort, const char* aPasswd, const char* aMap)
{
::free((void*)hostName);
::free((void*)passwd);
::free((void*)map);
hostName= strdup(aHostName);
passwd= strdup(aPasswd);
map= strdup(aMap);
port= aPort;
return Thread::start(blockTimeout);
}
//***************************************************************************
// init
//***************************************************************************
int RConThread::init()
{
int res= channel->connect(hostName, port, passwd);
if (!res)
tell("Connected to host %s:%d", hostName, port);
else
error("Error: Failed to connect to host %s:%d (%d)", hostName, port, res);
return res;
}
//***************************************************************************
// exit
//***************************************************************************
int RConThread::exit()
{
channel->disconnect();
return done;
}
//***************************************************************************
// run
//***************************************************************************
int RConThread::run()
{
waitMutex.lock();
while (!isState(isExit))
{
if (threads->size() > 1)
{
control();
read();
}
if (!isState(isExit))
waitCond.timedWaitMs(waitMutex, 1000);
}
waitMutex.unlock();
tell("Shutting down");
return 0;
}
//***************************************************************************
// Wake Up
//***************************************************************************
void RConThread::wakeUp()
{
waitMutex.lock();
waitCond.broadcast();
waitMutex.unlock();
}
//***************************************************************************
// control
//***************************************************************************
int RConThread::control()
{
// process 'work' ...
Work* work= queue.dequeue();
while (work)
{
write(work);
delete work;
work= queue.dequeue();
}
return done;
}
//***************************************************************************
// command
//***************************************************************************
int RConThread::command(const char* cmd)
{
int res= channel->sendCommand(cmd);
if (res == wrnNoResponse)
;//tell("No chat messages available");
else if (res)
{
error("Error: Failed to send command (%d), reopening channel ...", res);
exit();
init();
}
return res;
}
//***************************************************************************
// read
//***************************************************************************
int RConThread::read()
{
int res= command("GetChat");
if (!res && strlen(channel->getBuffer()))
{
char *p1, *p2;
p2= channel->getBuffer();
while (true)
{
p1= strchr(p2, '\n');
if (p1)
*p1= 0;
if (strncmp(p2, "SERVER: ", 8))
{
int nQueued= 0;
if (Globals::cfgVerbose)
tell("-> [%s]", p2);
for (std::list<RConThread*>::iterator it= threads->begin(); it != threads->end(); ++it)
{
if (!Globals::cfgDebug && *it == this)
continue;
if (Globals::cfgDebug && *it != this)
continue;
if (!Globals::cfgShowAdmin && !strncmp(p2, "AdminCmd", 8))
continue;
Work* w= new Work;
w->server.assign(map);
w->message.assign(p2);
if (*it == this)
queue.list.push_back(w);
else
(*it)->enqueue(w);
nQueued++;
}
}
p2= p1+1;
if (!p1 || !*p2)
break;
}
}
return res;
}
//***************************************************************************
// write
//***************************************************************************
int RConThread::write(Work* work)
{
int res= success;
resizeBuffer(work->message.length() + strlen(work->server.c_str()) + 30);
snprintf(sendBuffer, sendBufferSize-1, "ServerChat [%s] %s", work->server.c_str(), work->message.c_str());
res= command(sendBuffer);
if (Globals::cfgVerbose)
tell("<- [%s]", sendBuffer);
return res;
}
//***************************************************************************
// tell
//***************************************************************************
void RConThread::tell(const char* format, ...)
{
va_list args;
va_start(args, format);
vsprintf (tellBuffer, format, args);
va_end (args);
printf("[%s] %s\n", map, tellBuffer);
}
//***************************************************************************
// error
//***************************************************************************
void RConThread::error(const char* format, ...)
{
va_list args;
va_start(args, format);
vsprintf (tellBuffer, format, args);
va_end (args);
fprintf(stderr, "[%s] %s\n", map, tellBuffer);
}
//***************************************************************************
// resizeBuffer
//***************************************************************************
void RConThread::resizeBuffer(int newSize)
{
if (sendBufferSize >= newSize)
return;
sendBuffer= (char*)realloc(sendBuffer, newSize * sizeof(char));
sendBufferSize= newSize;
}