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
144 lines (119 loc) · 3.13 KB
/
dthread.cpp
File metadata and controls
144 lines (119 loc) · 3.13 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
/**
* @file dthread.cpp
*
* Implementation of functions for updating game state from network commands.
*/
#include "all.h"
#include "../3rdParty/Storm/Source/storm.h"
static CCritSect sgMemCrit;
unsigned int glpDThreadId;
TMegaPkt *sgpInfoHead; /* may not be right struct */
BOOLEAN dthread_running;
HANDLE sghWorkToDoEvent;
/* rdata */
static HANDLE sghThread = INVALID_HANDLE_VALUE;
static unsigned int __stdcall dthread_handler(void *data)
{
const char *error_buf;
TMegaPkt *pkt;
DWORD dwMilliseconds;
while (dthread_running) {
if (!sgpInfoHead && WaitForSingleObject(sghWorkToDoEvent, INFINITE) == WAIT_FAILED) {
error_buf = TraceLastError();
app_fatal("dthread4:\n%s", error_buf);
}
sgMemCrit.Enter();
pkt = sgpInfoHead;
if (sgpInfoHead)
sgpInfoHead = sgpInfoHead->pNext;
else
ResetEvent(sghWorkToDoEvent);
sgMemCrit.Leave();
if (pkt) {
if (pkt->dwSpaceLeft != MAX_PLRS)
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;
}
void dthread_remove_player(int pnum)
{
TMegaPkt *pkt;
sgMemCrit.Enter();
for (pkt = sgpInfoHead; pkt; pkt = pkt->pNext) {
if (pkt->dwSpaceLeft == pnum)
pkt->dwSpaceLeft = MAX_PLRS;
}
sgMemCrit.Leave();
}
void 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);
sgMemCrit.Enter();
p = (TMegaPkt *)&sgpInfoHead;
while (p->pNext) {
p = p->pNext;
}
p->pNext = pkt;
SetEvent(sghWorkToDoEvent);
sgMemCrit.Leave();
}
void dthread_start()
{
const char *error_buf;
if (gbMaxPlayers == 1) {
return;
}
sghWorkToDoEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (sghWorkToDoEvent == NULL) {
error_buf = TraceLastError();
app_fatal("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();
app_fatal("dthread2:\n%s", error_buf);
}
}
void dthread_cleanup()
{
const char *error_buf;
TMegaPkt *tmp;
if (sghWorkToDoEvent == NULL) {
return;
}
dthread_running = FALSE;
SetEvent(sghWorkToDoEvent);
if (sghThread != INVALID_HANDLE_VALUE && glpDThreadId != GetCurrentThreadId()) {
if (WaitForSingleObject(sghThread, INFINITE) == WAIT_FAILED) {
error_buf = TraceLastError();
app_fatal("dthread3:\n(%s)", error_buf);
}
CloseHandle(sghThread);
sghThread = INVALID_HANDLE_VALUE;
}
CloseHandle(sghWorkToDoEvent);
sghWorkToDoEvent = NULL;
while (sgpInfoHead) {
tmp = sgpInfoHead->pNext;
MemFreeDbg(sgpInfoHead);
sgpInfoHead = tmp;
}
}