-
Notifications
You must be signed in to change notification settings - Fork 0
/
rconthread.hpp
131 lines (94 loc) · 2.87 KB
/
rconthread.hpp
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
//***************************************************************************
// File rconthread.hpp
// Date 23.07.17 - #1
// Copyright (c) 2017-2017 s710 (s710 (at) posteo (dot) de). All rights reserved.
// --------------------------------------------------------------------------
// Ark ClusterChat / RCON thread
//***************************************************************************
#ifndef __RCONTHREAD_HPP__
#define __RCONTHREAD_HPP__
#include <list> // std::list
#include <string> // std::string
#include "thread.hpp"
class RConChannel;
//***************************************************************************
// struct Work, class WorkList
//***************************************************************************
struct Work
{
std::string message;
std::string server;
};
class WorkList
{
friend class RConThread;
public:
int enqueue(Work* work)
{
int res= 0;
mutex.lock();
list.push_back(work);
res= (int)list.size();
mutex.unlock();
return res;
}
Work* dequeue()
{
Work* res= 0;
mutex.lock();
std::list<Work*>::iterator it= list.begin();
if (it != list.end())
{
res= *it;
list.pop_front();
}
mutex.unlock();
return res;
}
size_t getCount() { return list.size(); }
private:
Mutex mutex;
std::list<Work*> list;
};
//***************************************************************************
// class RConThread
//***************************************************************************
class RConThread : public Thread
{
public:
RConThread(std::list<RConThread*>* threads);
virtual ~RConThread();
// functions
void wakeUp();
void enqueue(Work* work) { queue.enqueue(work); wakeUp(); }
int start(int blockTimeout, const char* aHostName, int aPport, const char* aPasswd, const char* aMap);
int stop() { return Thread::stop(&waitCond); }
protected:
// frame
int run();
int init();
int exit();
int read();
int write(Work* work);
int command(const char* command);
// functions
int control();
void tell(const char* format, ...);
void error(const char* format, ...);
void resizeBuffer(int newSize);
// data
Mutex waitMutex;
CondVar waitCond;
WorkList queue; // write: other thread read: rconthread
char* hostName;
char* passwd;
char* tellBuffer;
char* map;
char* sendBuffer;
int sendBufferSize;
int port;
RConChannel* channel;
std::list<RConThread*>* threads;
};
//-----------------------------------------------------------------
#endif // __RCONTHREAD_HPP__