forked from diasurgical/devilution
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdthread.cpp
More file actions
171 lines (140 loc) · 3.69 KB
/
dthread.cpp
File metadata and controls
171 lines (140 loc) · 3.69 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
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
//HEADER_GOES_HERE
#include "../types.h"
static CRITICAL_SECTION sgMemCrit; // idb
unsigned int glpDThreadId; // idb
TMegaPkt *sgpInfoHead; /* may not be right struct */
BOOLEAN dthread_running;
HANDLE sghWorkToDoEvent;
/* rdata */
static HANDLE sghThread = INVALID_HANDLE_VALUE;
#ifndef _MSC_VER
__attribute__((constructor))
#endif
static void
dthread_c_init(void)
{
dthread_init_mutex();
dthread_cleanup_mutex_atexit();
}
SEG_ALLOCATE(SEGMENT_C_INIT)
_PVFV dthread_c_init_funcs[] = { &dthread_c_init };
void __cdecl dthread_init_mutex()
{
InitializeCriticalSection(&sgMemCrit);
}
void __cdecl dthread_cleanup_mutex_atexit()
{
atexit(dthread_cleanup_mutex);
}
void __cdecl dthread_cleanup_mutex()
{
DeleteCriticalSection(&sgMemCrit);
}
void __fastcall dthread_remove_player(int pnum)
{
TMegaPkt *pkt;
EnterCriticalSection(&sgMemCrit);
for (pkt = sgpInfoHead; pkt; pkt = pkt->pNext) {
if (pkt->dwSpaceLeft == pnum)
pkt->dwSpaceLeft = 4;
}
LeaveCriticalSection(&sgMemCrit);
}
void __fastcall dthread_send_delta(int pnum, char cmd, void *pbSrc, int dwLen)
{
TMegaPkt *pkt;
TMegaPkt *p;
if (gbMaxPlayers == 1) {
return;
}
pkt = (TMegaPkt *)DiabloAllocPtr(dwLen + 20);
pkt->pNext = NULL;
pkt->dwSpaceLeft = pnum;
pkt->data[0] = cmd;
*(_DWORD *)&pkt->data[4] = dwLen;
memcpy(&pkt->data[8], pbSrc, dwLen);
EnterCriticalSection(&sgMemCrit);
p = (TMegaPkt *)&sgpInfoHead;
while (p->pNext) {
p = p->pNext;
}
p->pNext = pkt;
SetEvent(sghWorkToDoEvent);
LeaveCriticalSection(&sgMemCrit);
}
void __cdecl dthread_start()
{
char *error_buf;
if (gbMaxPlayers == 1) {
return;
}
sghWorkToDoEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (!sghWorkToDoEvent) {
error_buf = TraceLastError();
TermMsg("dthread:1\n%s", error_buf);
}
dthread_running = TRUE;
sghThread = (HANDLE)_beginthreadex(NULL, 0, dthread_handler, NULL, 0, &glpDThreadId);
if (sghThread == INVALID_HANDLE_VALUE) {
error_buf = TraceLastError();
TermMsg("dthread2:\n%s", error_buf);
}
}
unsigned int __stdcall dthread_handler(void *unused)
{
char *error_buf;
TMegaPkt *pkt;
DWORD dwMilliseconds;
while (dthread_running) {
if (!sgpInfoHead && WaitForSingleObject(sghWorkToDoEvent, 0xFFFFFFFF) == -1) {
error_buf = TraceLastError();
TermMsg("dthread4:\n%s", error_buf);
}
EnterCriticalSection(&sgMemCrit);
pkt = sgpInfoHead;
if (sgpInfoHead)
sgpInfoHead = sgpInfoHead->pNext;
else
ResetEvent(sghWorkToDoEvent);
LeaveCriticalSection(&sgMemCrit);
if (pkt) {
if (pkt->dwSpaceLeft != 4)
multi_send_zero_packet(pkt->dwSpaceLeft, pkt->data[0], &pkt->data[8], *(_DWORD *)&pkt->data[4]);
dwMilliseconds = 1000 * *(_DWORD *)&pkt->data[4] / gdwDeltaBytesSec;
if (dwMilliseconds >= 1)
dwMilliseconds = 1;
mem_free_dbg(pkt);
if (dwMilliseconds)
Sleep(dwMilliseconds);
}
}
return 0;
}
// 679730: using guessed type int gdwDeltaBytesSec;
void __cdecl dthread_cleanup()
{
char *error_buf;
TMegaPkt *tmp1, *tmp2;
if (sghWorkToDoEvent == NULL) {
return;
}
dthread_running = FALSE;
SetEvent(sghWorkToDoEvent);
if (sghThread != INVALID_HANDLE_VALUE && glpDThreadId != GetCurrentThreadId()) {
if (WaitForSingleObject(sghThread, 0xFFFFFFFF) == -1) {
error_buf = TraceLastError();
TermMsg("dthread3:\n(%s)", error_buf);
}
CloseHandle(sghThread);
sghThread = INVALID_HANDLE_VALUE;
}
CloseHandle(sghWorkToDoEvent);
sghWorkToDoEvent = NULL;
while (sgpInfoHead) {
tmp1 = sgpInfoHead->pNext;
tmp2 = sgpInfoHead;
sgpInfoHead = NULL;
mem_free_dbg(tmp2);
sgpInfoHead = tmp1;
}
}