Skip to content

Commit 0f1cb6b

Browse files
committed
Move generic thread logic into shared Thread class
1 parent 07e0696 commit 0f1cb6b

6 files changed

Lines changed: 129 additions & 72 deletions

File tree

‎Server/AIServer/SendThreadMain.cpp‎

Lines changed: 47 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -6,104 +6,91 @@
66
#include <spdlog/spdlog.h>
77

88
SendThreadMain::SendThreadMain(CIOCPort* iocPort)
9-
: _iocPort(iocPort), _running(false)
9+
: _iocPort(iocPort), _aiSocketCount(0)
1010
{
1111
}
1212

13-
void SendThreadMain::start()
13+
bool SendThreadMain::shutdown()
1414
{
15-
if (_running)
16-
return;
15+
if (!Thread::shutdown())
16+
return false;
1717

18-
_running = true;
19-
_workerThread = std::thread(&SendThreadMain::run, this);
20-
}
21-
22-
void SendThreadMain::shutdown()
23-
{
24-
if (!_running)
25-
return;
26-
27-
{
28-
std::lock_guard<std::mutex> lock(_queueMutex);
29-
_running = false;
30-
_cv.notify_one();
31-
}
32-
33-
_workerThread.join();
18+
clear();
19+
return true;
3420
}
3521

3622
void SendThreadMain::queue(_SEND_DATA* sendData)
3723
{
38-
std::lock_guard<std::mutex> lock(_queueMutex);
24+
std::lock_guard<std::mutex> lock(_mutex);
3925
_sendDataQueue.push(sendData);
4026
_cv.notify_one();
4127
}
4228

43-
SendThreadMain::~SendThreadMain()
44-
{
45-
shutdown();
46-
clear();
47-
}
48-
49-
void SendThreadMain::run()
29+
void SendThreadMain::thread_loop()
5030
{
5131
while (_running)
5232
{
53-
std::unique_lock<std::mutex> lock(_queueMutex);
33+
std::unique_lock<std::mutex> lock(_mutex);
5434
_cv.wait(lock);
5535

5636
if (!_running)
5737
break;
5838

59-
int nRet = 0;
60-
CGameSocket* pSocket = nullptr;
61-
int size = 0, index = 0;
39+
tick();
40+
}
41+
}
6242

63-
while (!_sendDataQueue.empty())
43+
void SendThreadMain::tick()
44+
{
45+
while (!_sendDataQueue.empty())
46+
{
47+
_SEND_DATA* pSendData = _sendDataQueue.front();
48+
49+
int count = -1;
50+
for (int i = 0; i < MAX_SOCKET; i++)
6451
{
65-
_SEND_DATA* pSendData = _sendDataQueue.front();
52+
CGameSocket* pSocket = (CGameSocket*) _iocPort->m_SockArray[i];
53+
if (pSocket == nullptr)
54+
continue;
6655

67-
int count = -1;
68-
for (int i = 0; i < MAX_SOCKET; i++)
56+
count++;
57+
58+
if (_aiSocketCount == count)
6959
{
70-
CGameSocket* pSocket = (CGameSocket*) _iocPort->m_SockArray[i];
71-
if (pSocket == nullptr)
60+
int size = pSocket->Send(pSendData->pBuf, pSendData->sLength);
61+
if (size <= 0)
62+
{
63+
spdlog::error("SendThreadMain::tick: send failed: size={} socket_num={}",
64+
size, count);
65+
count--;
7266
continue;
67+
}
7368

74-
count++;
69+
if (++_aiSocketCount >= MAX_AI_SOCKET)
70+
_aiSocketCount = 0;
7571

76-
if (_aiSocketCount == count)
77-
{
78-
size = pSocket->Send(pSendData->pBuf, pSendData->sLength);
79-
if (size <= 0)
80-
{
81-
spdlog::error("SendThreadMain::run: send failed: size={} socket_num={}",
82-
size, count);
83-
count--;
84-
continue;
85-
}
86-
87-
if (++_aiSocketCount >= MAX_AI_SOCKET)
88-
_aiSocketCount = 0;
89-
90-
//TRACE(_T("SendThreadMain - Send : size=%d, socket_num=%d\n"), size, count);
91-
break;
92-
}
72+
//TRACE(_T("SendThreadMain - Send : size=%d, socket_num=%d\n"), size, count);
73+
break;
9374
}
94-
95-
delete pSendData;
96-
_sendDataQueue.pop();
9775
}
76+
77+
delete pSendData;
78+
_sendDataQueue.pop();
9879
}
9980
}
10081

10182
void SendThreadMain::clear()
10283
{
103-
std::lock_guard<std::mutex> lock(_queueMutex);
84+
std::lock_guard<std::mutex> lock(_mutex);
10485
while (!_sendDataQueue.empty());
10586
{
10687
delete _sendDataQueue.front();
10788
_sendDataQueue.pop();
10889
}
10990
}
91+
92+
SendThreadMain::~SendThreadMain()
93+
{
94+
shutdown();
95+
clear();
96+
}

‎Server/AIServer/SendThreadMain.h‎

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,26 @@
11
#pragma once
22

3-
#include <condition_variable>
4-
#include <mutex>
3+
#include <shared/Thread.h>
4+
55
#include <queue>
6-
#include <thread>
76

87
class CIOCPort;
98
struct _SEND_DATA;
10-
class SendThreadMain
9+
class SendThreadMain : public Thread
1110
{
1211
public:
1312
SendThreadMain(CIOCPort* iocPort);
14-
void start();
15-
void shutdown();
13+
bool shutdown() override;
1614
void queue(_SEND_DATA* sendData);
17-
~SendThreadMain();
15+
~SendThreadMain() override;
1816

1917
protected:
20-
void run();
18+
void thread_loop() override;
19+
void tick() override;
2120
void clear();
2221

2322
protected:
2423
CIOCPort* _iocPort;
2524
std::queue<_SEND_DATA*> _sendDataQueue;
26-
std::mutex _queueMutex;
27-
std::condition_variable _cv;
28-
std::thread _workerThread;
29-
bool _running;
3025
int _aiSocketCount;
3126
};

‎Server/shared/Thread.cpp‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include "stdafx.h"
2+
#include "Thread.h"
3+
4+
Thread::Thread()
5+
{
6+
_running = false;
7+
}
8+
9+
void Thread::start()
10+
{
11+
if (_running)
12+
return;
13+
14+
_running = true;
15+
_thread = std::thread(&Thread::thread_loop, this);
16+
}
17+
18+
bool Thread::shutdown()
19+
{
20+
if (!_running)
21+
return false;
22+
23+
{
24+
std::lock_guard<std::mutex> lock(_mutex);
25+
_running = false;
26+
_cv.notify_one();
27+
}
28+
29+
if (_thread.joinable())
30+
_thread.join();
31+
32+
return true;
33+
}
34+
35+
Thread::~Thread()
36+
{
37+
shutdown();
38+
}

‎Server/shared/Thread.h‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#pragma once
2+
3+
#include <condition_variable>
4+
#include <mutex>
5+
#include <thread>
6+
7+
class Thread
8+
{
9+
public:
10+
bool IsRunning() const
11+
{
12+
return _running;
13+
}
14+
15+
Thread();
16+
virtual void start();
17+
virtual bool shutdown();
18+
virtual ~Thread();
19+
20+
protected:
21+
virtual void thread_loop() = 0;
22+
virtual void tick() = 0;
23+
24+
protected:
25+
std::mutex _mutex;
26+
std::condition_variable _cv;
27+
std::thread _thread;
28+
bool _running;
29+
};

‎Server/shared/shared.vcxproj‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@
153153
</ClCompile>
154154
<ClCompile Include="StringConversion.cpp" />
155155
<ClCompile Include="StringUtils.cpp" />
156+
<ClCompile Include="Thread.cpp" />
156157
<ClCompile Include="TimeThread.cpp" />
157158
<ClCompile Include="logger.cpp" />
158159
</ItemGroup>
@@ -172,6 +173,7 @@
172173
<ClInclude Include="STLMap.h" />
173174
<ClInclude Include="StringConversion.h" />
174175
<ClInclude Include="StringUtils.h" />
176+
<ClInclude Include="Thread.h" />
175177
<ClInclude Include="TimeThread.h" />
176178
<ClInclude Include="types.h" />
177179
<ClInclude Include="version.h" />

‎Server/shared/shared.vcxproj.filters‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@
5151
<ClCompile Include="logger.cpp">
5252
<Filter>Source Files</Filter>
5353
</ClCompile>
54+
<ClCompile Include="Thread.cpp">
55+
<Filter>Source Files</Filter>
56+
</ClCompile>
5457
</ItemGroup>
5558
<ItemGroup>
5659
<ClInclude Include="ByteBuffer.h">
@@ -110,5 +113,8 @@
110113
<ClInclude Include="server_config.h">
111114
<Filter>Header Files</Filter>
112115
</ClInclude>
116+
<ClInclude Include="Thread.h">
117+
<Filter>Header Files</Filter>
118+
</ClInclude>
113119
</ItemGroup>
114120
</Project>

0 commit comments

Comments
 (0)