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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//=============================================================================//
#include "quakedef.h"
#include "sv_client.h"
#include "logofile_shared.h"
#include "server.h"
#include "filetransfermgr.h"
#include "filesystem_engine.h"
ConVar sv_logo_rate( "sv_logo_rate", "1024", 0, "How fast (bytes per second) the server sends logo files to clients." );
class CPendingFile
{
public:
CRC32_t m_nLogoFileCRC;
bool m_bWaitingForServerToGetFile;
};
class CPerClientLogoInfo
{
public:
CPerClientLogoInfo()
{
m_bLogoFileCRCValid = false;
m_bSendFileInProgress = false;
}
// This client's logo info.
bool m_bLogoFileCRCValid;
int m_nLogoFileCRC;
// Are we sending this client a file right now?
bool m_bSendFileInProgress;
// Files that this client has requested but we aren't able to send yet.
CUtlVector<CPendingFile> m_PendingFiles;
};
class CServerFileTransferMgr : public CFileTransferMgr
{
public:
virtual bool SendChunk( INetChannel *pDest, const void *pData, int len )
{
SVC_LogoFileData fileData;
fileData.m_Data.CopyArray( (const char*)pData, len );
return pDest->SendNetMsg( fileData, true );
}
virtual void OnSendCancelled( FileTransferID_t id )
{
}
CGameClient* GetClientByNetChannel( INetChannel *pChan )
{
for ( int i=0; i < sv.clients.Count(); i++ )
{
CGameClient *pClient = sv.Client( i );
if ( pClient && pClient->GetNetChannel() == pChan )
return pClient;
}
return NULL;
}
virtual void OnFinishedSending(
INetChannel *pDest,
const void *pUserData,
int userDataLen,
FileTransferID_t id )
{
// Start sending the next file to this guy.
CGameClient *pClient = GetClientByNetChannel( pDest );
if ( pClient )
{
pClient->m_pLogoInfo->m_bSendFileInProgress = false;
UpdatePendingFiles();
}
else
{
Warning( "OnFinishedSending: can't get CGameClient from INetChannel.\n" );
}
}
virtual void OnFileReceived(
INetChannel *pChan,
const void *pUserData,
int userDataLength,
const char *pFileData,
int fileLength )
{
// Ok, now the server has received a file the client sent. First, validate the VTF.
if ( !LogoFile_IsValidVTFFile( pFileData, fileLength ) )
{
Warning( "CServerFileTransferMgr::OnFileReceived: received an invalid logo file from a client.\n" );
return;
}
if ( userDataLength < sizeof( CRC32_t ) )
{
Warning( "CServerFileTransferMgr::OnFileReceived: invalid userDataLength (%d).\n", userDataLength );
}
CRC32_t crcValue = *((CRC32_t*)pUserData);
// Save this file in our cache.
if ( SaveCRCFileToCache( crcValue, pFileData, fileLength ) )
{
// Start transfers to any clients that we can now.
MarkPendingFilesWithCRC( crcValue );
UpdatePendingFiles();
}
}
// If any clients are waiting on this file, mark them so they know they can be sent the file now.
void MarkPendingFilesWithCRC( CRC32_t crcValue )
{
for ( int i=0; i < sv.clients.Count(); i++ )
{
CGameClient *pClient = sv.Client( i );
if ( !pClient || !pClient->m_pLogoInfo )
continue;
for ( int i=0; i < pClient->m_pLogoInfo->m_PendingFiles.Count(); i++ )
{
CPendingFile *pFile = &pClient->m_pLogoInfo->m_PendingFiles[i];
if ( pFile->m_nLogoFileCRC == crcValue )
pFile->m_bWaitingForServerToGetFile = false;
}
}
}
bool SaveCRCFileToCache( CRC32_t crcValue, const void *pFileData, int fileLength )
{
CLogoFilename logohex( crcValue, true );
FileHandle_t hFile = g_pFileSystem->Open( logohex.m_Filename, "wb" );
if ( hFile == FILESYSTEM_INVALID_HANDLE )
{
Warning( "SaveCRCFileToCache: couldn't open '%s' for writing.\n", logohex.m_Filename );
return false;
}
else
{
int writeRet = g_pFileSystem->Write( pFileData, fileLength, hFile );
g_pFileSystem->Close( hFile );
// If we couldn't write it, then delete it.
if ( writeRet == fileLength )
{
return true;
}
else
{
Warning( "SaveCRCFileToCache: couldn't write data (%d should be %d).\n", writeRet, fileLength );
return false;
}
}
}
void UpdatePendingFiles()
{
CUtlVector<char> fileData;
CRC32_t lastCRC = 0;
// Find clients who want to receive this file.
for ( int i=0; i < sv.clients.Count(); i++ )
{
CGameClient *pClient = sv.Client( i );
if ( !pClient || !pClient->m_pLogoInfo )
continue;
// Are we already sending the client a file?
if ( pClient->m_pLogoInfo->m_bSendFileInProgress )
continue;
for ( int iFile=0; iFile < pClient->m_pLogoInfo->m_PendingFiles.Count(); iFile++ )
{
CPendingFile *pFile = &pClient->m_pLogoInfo->m_PendingFiles[iFile];
// If we still have to wait for the server to get this file, then stop.
if ( pFile->m_bWaitingForServerToGetFile )
continue;
pClient->m_pLogoInfo->m_PendingFiles.Remove( iFile );
// Load the file, if we haven't already.
if ( fileData.Count() == 0 || lastCRC != pFile->m_nLogoFileCRC )
{
// Remember the last CRC so we don't have to reopen the file if
// this one is going to a bunch of clients in a row.
lastCRC = pFile->m_nLogoFileCRC;
if ( !LogoFile_ReadFile( pFile->m_nLogoFileCRC, fileData ) )
break;
}
StartSending(
pClient->GetNetChannel(),
&lastCRC,
sizeof( lastCRC ),
fileData.Base(),
fileData.Count(),
sv_logo_rate.GetInt()
);
pClient->m_pLogoInfo->m_bSendFileInProgress = true;
break;
}
}
}
};
CServerFileTransferMgr g_ServerFileTransferMgr;
bool SV_LogoFile_HasLogoFile( CRC32_t crcValue )
{
CLogoFilename logohex( crcValue, true );
return g_pFileSystem->FileExists( logohex.m_Filename );
}
PROCESS_MSG_SERVER( CLC_LogoFileData )
{
g_ServerFileTransferMgr.HandleReceivedData( m_Client->GetNetChannel(), m_Data.Base(), m_Data.Count() );
return true;
} };
PROCESS_MSG_SERVER( CLC_LogoFileRequest )
{
// The client is requesting that we send it a specific logo file.
int index = m_Client->m_pLogoInfo->m_PendingFiles.AddToTail();
CPendingFile &file = m_Client->m_pLogoInfo->m_PendingFiles[index];
file.m_nLogoFileCRC = m_nLogoFileCRC;
file.m_bWaitingForServerToGetFile = SV_LogoFile_HasLogoFile( file.m_nLogoFileCRC );
// Start sending it if it's time..
g_ServerFileTransferMgr.UpdatePendingFiles();
return true;
} };
CPerClientLogoInfo* SV_LogoFile_CreatePerClientLogoInfo()
{
CPerClientLogoInfo *pInfo = new CPerClientLogoInfo;
pInfo->m_bLogoFileCRCValid = false;
return pInfo;
}
void SV_LogoFile_DeletePerClientLogoInfo( CPerClientLogoInfo *pInfo )
{
delete pInfo;
}
void SV_LogoFile_HandleClientDisconnect( CGameClient *pClient )
{
g_ServerFileTransferMgr.HandleClientDisconnect( pClient->GetNetChannel() );
}
void SV_LogoFile_NewConnection( INetChannel *chan, CGameClient *pGameClient )
{
REGISTER_MSG_SERVER( CLC_LogoFileRequest );
}
bool SV_LogoFile_IsDownloadingLogoFile( CRC32_t crcValue )
{
for ( int i=g_ServerFileTransferMgr.FirstIncoming(); i != g_ServerFileTransferMgr.InvalidIncoming(); i=g_ServerFileTransferMgr.NextIncoming( i ) )
{
const void *pData;
int dataLen;
g_ServerFileTransferMgr.GetIncomingUserData( i, pData, dataLen );
CRC32_t *pTestValue = (CRC32_t*)pData;
if ( *pTestValue == crcValue )
return true;
}
return false;
}
void SV_LogoFile_OnConnect( CGameClient *pSenderClient, bool bValid, CRC32_t crcValue )
{
pSenderClient->m_pLogoInfo->m_bLogoFileCRCValid = bValid;
pSenderClient->m_pLogoInfo->m_nLogoFileCRC = crcValue;
if ( bValid )
{
// Does the server need this file? If so, request it.
if ( !SV_LogoFile_HasLogoFile( crcValue ) && !SV_LogoFile_IsDownloadingLogoFile( crcValue ) )
{
SVC_LogoFileRequest fileRequest;
fileRequest.m_nLogoFileCRC = crcValue;
if ( !pSenderClient->SendNetMsg( fileRequest, true ) )
{
Host_Error( "SV_LogoFile_OnConnect: Reliable broadcast message would overflow client" );
return;
}
}
// Tell all clients (except the sending client) about this logo.
SVC_LogoFileCRC logoNotify;
logoNotify.m_nLogoFileCRC = crcValue;
for ( int i=0; i < sv.clients.Count(); i++ )
{
CGameClient *pClient = sv.Client( i );
if ( !pClient || pClient == pSenderClient )
continue;
bool bReliable = true;
if ( !pClient->SendNetMsg( logoNotify, bReliable ) )
{
Host_Error( "SV_LogoFile_OnConnect: Reliable broadcast message would overflow client" );
return;
}
}
}
// Also, tell this client about all other client CRCs so it can aks for the one it needs.
for ( int i=0; i < sv.clients.Count(); i++ )
{
CGameClient *pClient = sv.Client( i );
if ( !pClient || pClient == pSenderClient || !pClient->m_pLogoInfo->m_bLogoFileCRCValid )
continue;
SVC_LogoFileCRC logoNotify;
logoNotify.m_nLogoFileCRC = pClient->m_pLogoInfo->m_nLogoFileCRC;
bool bReliable = true;
if ( !pSenderClient->SendNetMsg( logoNotify, bReliable ) )
{
Host_Error( "SV_LogoFile_OnConnect: Reliable broadcast message would overflow client" );
return;
}
}
}
|