forked from jensewe/custom_fakelag
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnet_ws_queued_packet_sender.cpp
More file actions
250 lines (207 loc) · 6.15 KB
/
net_ws_queued_packet_sender.cpp
File metadata and controls
250 lines (207 loc) · 6.15 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
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
//====== Copyright � 1996-2005, Valve Corporation, All rights reserved. =======
//
// Purpose:
//
//=============================================================================
#include "net_structures.h"
#include "tier0/threadtools.h"
#include "net_ws_queued_packet_sender.h"
#include "wrappers.h"
#include "tier1/utlvector.h"
#include "tier1/utlpriorityqueue.h"
class CQueuedPacketSender : public CThread, public IQueuedPacketSender
{
public:
CQueuedPacketSender();
~CQueuedPacketSender();
// IQueuedPacketSender
virtual bool Setup();
virtual void Shutdown();
virtual bool IsRunning() { return CThread::IsAlive(); }
virtual void ClearQueuedPacketsForChannel( INetChannel *pChan );
virtual void QueuePacket( INetChannel *pChan, SOCKET s, const char *buf, int len, const struct sockaddr * to, int tolen, uint32 msecDelay );
private:
// CThread Overrides
virtual bool Start( unsigned int nBytesStack = 0 );
virtual int Run();
private:
class CQueuedPacket
{
public:
uint32 m_unSendTime;
const void *m_pChannel; // We don't actually use the channel
SOCKET m_Socket;
CUtlVector<char> to; // sockaddr
CUtlVector<char> buf;
// We want the list sorted in ascending order, so note that we return > rather than <
static bool LessFunc( CQueuedPacket * const &lhs, CQueuedPacket * const &rhs )
{
return lhs->m_unSendTime > rhs->m_unSendTime;
}
};
CUtlPriorityQueue< CQueuedPacket * > m_QueuedPackets;
CThreadMutex m_QueuedPacketsCS;
CThreadEvent m_hThreadEvent;
volatile bool m_bThreadShouldExit;
};
static CQueuedPacketSender g_LagPacketSender;
IQueuedPacketSender *g_pLagPackedSender = &g_LagPacketSender;
CQueuedPacketSender::CQueuedPacketSender() :
m_QueuedPackets( 0, 0, CQueuedPacket::LessFunc )
{
m_bThreadShouldExit = false;
}
CQueuedPacketSender::~CQueuedPacketSender()
{
Shutdown();
}
bool CQueuedPacketSender::Setup()
{
return Start();
}
bool CQueuedPacketSender::Start( unsigned nBytesStack )
{
Shutdown();
if ( CThread::Start( nBytesStack ) )
{
// Ahhh the perfect cross-platformness of the threads library.
#ifdef _WIN32
SetPriority( THREAD_PRIORITY_HIGHEST );
#elif _LINUX
//SetPriority( PRIORITY_MAX );
#endif
m_bThreadShouldExit = false;
return true;
}
else
{
return false;
}
}
void CQueuedPacketSender::Shutdown()
{
if ( !IsAlive() )
return;
m_bThreadShouldExit = true;
m_hThreadEvent.Set();
Join(); // Wait for the thread to exit.
while ( m_QueuedPackets.Count() > 0 )
{
delete m_QueuedPackets.ElementAtHead();
m_QueuedPackets.RemoveAtHead();
}
m_QueuedPackets.Purge();
}
void CQueuedPacketSender::ClearQueuedPacketsForChannel( INetChannel *pChan )
{
m_QueuedPacketsCS.Lock();
for ( int i = m_QueuedPackets.Count()-1; i >= 0; i-- )
{
CQueuedPacket *p = m_QueuedPackets.Element( i );
if ( p->m_pChannel == pChan )
{
m_QueuedPackets.RemoveAt( i );
delete p;
}
}
m_QueuedPacketsCS.Unlock();
}
void CQueuedPacketSender::QueuePacket( INetChannel *pChan, SOCKET s, const char *buf, int len, const struct sockaddr * to, int tolen, uint32 msecDelay )
{
m_QueuedPacketsCS.Lock();
// We'll pull all packets we should have sent by now and send them out right away
uint32 msNow = Plat_MSTime();
int nMaxQueuedPackets = 2048; // @Forgetest: was 1024
if ( m_QueuedPackets.Count() < nMaxQueuedPackets )
{
// Add this packet to the queue.
CQueuedPacket *pPacket = new CQueuedPacket;
pPacket->m_unSendTime = msNow + msecDelay;
pPacket->m_Socket = s;
pPacket->m_pChannel = pChan;
pPacket->buf.CopyArray( (char*)buf, len );
pPacket->to.CopyArray( (char*)to, tolen );
m_QueuedPackets.Insert( pPacket );
}
else
{
// static int nWarnings = 5;
// if ( --nWarnings > 0 )
{
Warning( "CQueuedPacketSender: num queued packets >= nMaxQueuedPackets. Not queueing anymore.\n" );
}
}
// Tell the thread that we have a queued packet.
m_hThreadEvent.Set();
m_QueuedPacketsCS.Unlock();
}
extern int NET_SendToImpl( SOCKET s, const char * buf, int len, const struct sockaddr * to, int tolen, int iGameDataLength );
int CQueuedPacketSender::Run()
{
// Normally TT_INFINITE but we wakeup every 50ms just in case.
uint32 waitIntervalNoPackets = 50;
uint32 waitInterval = waitIntervalNoPackets;
while ( 1 )
{
if ( m_hThreadEvent.Wait( waitInterval ) )
{
// Someone signaled the thread. Either we're being told to exit or
// we're being told that a packet was just queued.
if ( m_bThreadShouldExit )
return 0;
}
// Assume nothing to do and that we'll sleep again
waitInterval = waitIntervalNoPackets;
// OK, now send a packet.
m_QueuedPacketsCS.Lock();
// We'll pull all packets we should have sent by now and send them out right away
uint32 msNow = Plat_MSTime();
// bool bTrace = net_queue_trace.GetInt() == NET_QUEUED_PACKET_THREAD_DEBUG_VALUE;
while ( m_QueuedPackets.Count() > 0 )
{
CQueuedPacket *pPacket = m_QueuedPackets.ElementAtHead();
if ( pPacket->m_unSendTime > msNow )
{
// Sleep until next we need this packet
waitInterval = pPacket->m_unSendTime - msNow;
// if ( bTrace )
// {
// char sz[ 256 ];
// Q_snprintf( sz, sizeof( sz ), "SQ: sleeping for %u msecs at %f\n", waitInterval, Plat_FloatTime() );
// Warning( sz );
// }
break;
}
// If it's a bot, don't do anything. Note: we DO want this code deep here because bots only
// try to send packets when sv_stressbots is set, in which case we want it to act as closely
// as a real player as possible.
sockaddr_in *pInternetAddr = (sockaddr_in*)pPacket->to.Base();
#ifdef _WIN32
if ( pInternetAddr->sin_addr.S_un.S_addr != 0
#else
if ( pInternetAddr->sin_addr.s_addr != 0
#endif
&& pInternetAddr->sin_port != 0 )
{
// if ( bTrace )
// {
// char sz[ 256 ];
// Q_snprintf( sz, sizeof( sz ), "SQ: sending %d bytes at %f\n", pPacket->buf.Count(), Plat_FloatTime() );
// Warning( sz );
// }
NET_SendToImpl
(
pPacket->m_Socket,
pPacket->buf.Base(),
pPacket->buf.Count(),
(sockaddr*)pPacket->to.Base(),
pPacket->to.Count(),
NET_QUEUED_PACKET_THREAD_SEND_PACKET
);
}
delete pPacket;
m_QueuedPackets.RemoveAtHead();
}
m_QueuedPacketsCS.Unlock();
}
}