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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//=============================================================================
#include "net_ws_headers.h"
#include "net_ws_queued_packet_sender.h"
#include "tier1/utlvector.h"
#include "tier1/utlpriorityqueue.h"
#include "tier0/etwprof.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
ConVar net_queued_packet_thread( "net_queued_packet_thread", "1", 0, "Use a high priority thread to send queued packets out instead of sending them each frame." );
ConVar net_queue_trace( "net_queue_trace", "0", 0 );
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 FAR *buf, int len, const struct sockaddr FAR * to, int tolen, uint32 msecDelay );
virtual bool HasQueuedPackets( const INetChannel *pChan ) const;
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_QueuedPacketSender;
IQueuedPacketSender *g_pQueuedPackedSender = &g_QueuedPacketSender;
CQueuedPacketSender::CQueuedPacketSender() :
m_QueuedPackets( 0, 0, CQueuedPacket::LessFunc )
{
SetName( "QueuedPacketSender" );
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 IS_WINDOWS_PC
SetPriority( THREAD_PRIORITY_HIGHEST );
#elif POSIX
//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 )
{
AUTO_LOCK( m_QueuedPacketsCS );
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;
}
}
}
bool CQueuedPacketSender::HasQueuedPackets( const INetChannel *pChan ) const
{
AUTO_LOCK( m_QueuedPacketsCS );
for ( int i = 0; i < m_QueuedPackets.Count(); ++i )
{
const CQueuedPacket *p = m_QueuedPackets.Element( i );
if ( p->m_pChannel == pChan )
{
return true;
}
}
return false;
}
void CQueuedPacketSender::QueuePacket( INetChannel *pChan, SOCKET s, const char FAR *buf, int len, const struct sockaddr FAR * to, int tolen, uint32 msecDelay )
{
AUTO_LOCK( m_QueuedPacketsCS );
// We'll pull all packets we should have sent by now and send them out right away
uint32 msNow = Plat_MSTime();
int nMaxQueuedPackets = 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();
}
extern int NET_SendToImpl( SOCKET s, const char FAR * buf, int len, const struct sockaddr FAR * 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.
{
AUTO_LOCK( m_QueuedPacketsCS );
// 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;
// Emit ETW events to help with diagnosing network throttling issues as
// these often have a severe effect on load times in Dota.
ETWMark1I( "CQueuedPacketSender::Run sleeping (ms)", waitInterval );
if ( bTrace )
{
Warning( "SQ: sleeping for %u msecs at %f\n", waitInterval, Plat_FloatTime() );
}
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 )
{
Warning( "SQ: sending %d bytes at %f\n", pPacket->buf.Count(), Plat_FloatTime() );
}
NET_SendToImpl
(
pPacket->m_Socket,
pPacket->buf.Base(),
pPacket->buf.Count(),
(sockaddr*)pPacket->to.Base(),
pPacket->to.Count(),
-1
);
}
delete pPacket;
m_QueuedPackets.RemoveAtHead();
}
}
}
}
|