aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.Client/Durango/Network/DQRNetworkManager_SendReceive.cpp
blob: eed3e8511f7e03ff268dc198f26e6a4ccf773590 (plain)
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#include "stdafx.h"

#include "DQRNetworkManager.h"
#include "PartyController.h"
#include <collection.h>
#include <ppltasks.h>
#include <ws2tcpip.h>
#include "..\Minecraft.World\StringHelpers.h"
#include "base64.h"

#ifdef _DURANGO
#include "..\Minecraft.World\DurangoStats.h"
#endif

#include "ChatIntegrationLayer.h"

using namespace Concurrency;
using namespace Windows::Foundation::Collections;

// This method is called when bytes have been received that are to be passed on to the game itself. The data is associated with a small id so we can specify which network player
// that it was received for.
void DQRNetworkManager::BytesReceived(int smallId, BYTE *bytes, int byteCount)
{
	DQRNetworkPlayer *host = GetPlayerBySmallId(m_hostSmallId);
	DQRNetworkPlayer *client = GetPlayerBySmallId(smallId);

	if( ( host == nullptr ) || ( client == nullptr ) )
	{
		return;
	}

	if( m_isHosting )
	{
		m_listener->HandleDataReceived(client, host, bytes, byteCount );
	}
	else
	{
		m_listener->HandleDataReceived(host, client, bytes, byteCount );
	}
//	app.DebugPrintf("%d bytes received: %s\n", byteCount, bytes);
}

// This method is called when network data is received, that is to be processed by the DQRNetworkManager itself. This is for handling internal
// updates such as assigning & unassigning of small Ids, transmission of the table of players currently in the session etc.
// Processing of these things is handled as a state machine so that we can receive a message split over more than one call to this method should
// the underlying communcation layer split data up somehow.
void DQRNetworkManager::BytesReceivedInternal(DQRConnectionInfo *connectionInfo, unsigned int sessionAddress, BYTE *bytes, int byteCount)
{
	BYTE *pNextByte = bytes;
	BYTE *pEndByte = pNextByte + byteCount;

	do
	{
		BYTE byte = *pNextByte;
		switch( connectionInfo->m_internalDataState )
		{
			case DQRConnectionInfo::ConnectionState_InternalHeaderByte:
				switch( byte )
				{
					case DQR_INTERNAL_ASSIGN_SMALL_IDS:
						connectionInfo->m_internalDataState = DQRConnectionInfo::ConnectionState_InternalAssignSmallIdMask;
						break;
					case DQR_INTERNAL_UNASSIGN_SMALL_ID:
						// Host only
						if( connectionInfo->m_channelActive[connectionInfo->m_currentChannel] )
						{
							int smallId = connectionInfo->m_smallId[connectionInfo->m_currentChannel];
							connectionInfo->m_channelActive[connectionInfo->m_currentChannel] = false;
							m_sessionAddressFromSmallId[smallId] = 0;
							DQRNetworkPlayer *pPlayer = GetPlayerBySmallId(smallId);
							if( pPlayer )
							{
								RemoveRoomSyncPlayer(pPlayer);
								SendRoomSyncInfo();
							}
						}
						break;
					case DQR_INTERNAL_PLAYER_TABLE:
						connectionInfo->m_internalDataState = DQRConnectionInfo::ConnectionState_InternalRoomSyncData;
						connectionInfo->m_pucRoomSyncData = new unsigned char[4];
						connectionInfo->m_roomSyncDataBytesToRead = 0;
						connectionInfo->m_roomSyncDataBytesRead = 0;
						break;
					case DQR_INTERNAL_ADD_PLAYER_FAILED:
						connectionInfo->m_internalDataState = DQRConnectionInfo::ConnectionState_InternalAddPlayerFailedData;
						connectionInfo->m_pucAddFailedPlayerData = new unsigned char[4];
						connectionInfo->m_addFailedPlayerDataBytesToRead = 0;
						connectionInfo->m_addFailedPlayerDataBytesRead = 0;
						break;
					default:
						break;
				}
				pNextByte++;
				break;
			case DQRConnectionInfo::ConnectionState_InternalAssignSmallIdMask:
				// Up to 4 smallIds are assigned at once, with the ones that are being assigned dictated by a mask byte which is passed in first.
				// The small Ids themselves follow, always 4 bytes, and any that are masked as being assigned are processed, the other bytes ignored.
				// In order work around a bug with the networking library, this particular packet (being the first this that is sent from a client)
				// is at first sent unreliably, with retries, until a message is received back to the client, or it times out. We therefore have to be able
				// to handle (and ignore) this being received more than once
				DQRNetworkManager::LogCommentFormat(L"Small Ids being received");
				connectionInfo->m_smallIdReadMask = byte;
				// Create a uniquely allocated byte to which names have been resolved, as another one of these packets could be received whilst that asyncronous process is going o n
				connectionInfo->m_pucsmallIdReadMaskResolved = new unsigned char;
				*connectionInfo->m_pucsmallIdReadMaskResolved = 0;
				connectionInfo->m_internalDataState = DQRConnectionInfo::ConnectionState_InternalAssignSmallId0;
				connectionInfo->m_initialPacketReceived = true;

				pNextByte++;
				break;
			case DQRConnectionInfo::ConnectionState_InternalAssignSmallId0:
			case DQRConnectionInfo::ConnectionState_InternalAssignSmallId1:
			case DQRConnectionInfo::ConnectionState_InternalAssignSmallId2:
			case DQRConnectionInfo::ConnectionState_InternalAssignSmallId3:
				{
					int channel = static_cast<int>(connectionInfo->m_internalDataState) - DQRConnectionInfo::ConnectionState_InternalAssignSmallId0;

					if( ( connectionInfo->m_smallIdReadMask & ( 1 << channel ) ) && ( !connectionInfo->m_channelActive[channel] ) )
					{
						// HOST ONLY
						// Store the small Id that is associated with this send channel. In order work around a bug with the networking library, this particular packet
						// (being the first this that is sent from a client) is sent unreliably, with retries, until a message is received back to the client, or it times out.
						// We therefore have to be able to handle (and ignore) this being received more than once - hence the check of the bool above.
						// At this point, the connection is considered properly active from the point of view of the host.

						int sessionIndex = GetSessionIndexForSmallId(byte);
						if( sessionIndex != -1 )
						{
							connectionInfo->m_channelActive[channel] = true;
							connectionInfo->m_smallId[channel] = byte;
					
							m_sessionAddressFromSmallId[byte] = sessionAddress;
							m_channelFromSmallId[byte] = channel;

							auto pAsyncOp = m_primaryUserXboxLiveContext->ProfileService->GetUserProfileAsync(m_multiplayerSession->Members->GetAt(sessionIndex)->XboxUserId);
							DQRNetworkManager::LogCommentFormat(L"Session index of %d found for player with small id %d - attempting to resolve display name\n",sessionIndex,byte);

							DQRNetworkPlayer *pPlayer = new DQRNetworkPlayer(this, DQRNetworkPlayer::DNP_TYPE_REMOTE, true, 0, sessionAddress);
							pPlayer->SetSmallId(byte);
							pPlayer->SetUID(static_cast<PlayerUID>(m_multiplayerSession->Members->GetAt(sessionIndex)->XboxUserId->Data()));

							HostGamertagResolveDetails *resolveDetails = new HostGamertagResolveDetails();
							resolveDetails->m_pPlayer = pPlayer;
							resolveDetails->m_sessionAddress = sessionAddress;
							resolveDetails->m_channel = channel;
							resolveDetails->m_sync = false;

							int mask = 1 << channel;
							unsigned char *pucsmallIdReadMaskResolved = connectionInfo->m_pucsmallIdReadMaskResolved;
							unsigned char ucsmallIdReadMask = connectionInfo->m_smallIdReadMask;
							create_task( pAsyncOp ).then( [this,resolveDetails,mask,pucsmallIdReadMaskResolved,ucsmallIdReadMask] (task<Microsoft::Xbox::Services::Social::XboxUserProfile^> resultTask)
							{
								try
								{
									Microsoft::Xbox::Services::Social::XboxUserProfile^ result = resultTask.get();

									resolveDetails->m_name.assign(result->Gamertag->Data());		// Use the gamertag for this data, as it is synchronised round all the machines and so we can't use a display name that could be a real name

									EnterCriticalSection(&m_csHostGamertagResolveResults);
									// Update flags for which names have been resolved, and if this completes this set, then set the flag to say that we should synchronise these out to the clients
									*pucsmallIdReadMaskResolved |= mask;
									LogCommentFormat(L"<<>> Compare %d to %d",*pucsmallIdReadMaskResolved,ucsmallIdReadMask);
									if(ucsmallIdReadMask == *pucsmallIdReadMaskResolved)
									{
										resolveDetails->m_sync = true;
										delete pucsmallIdReadMaskResolved;
									}
									m_hostGamertagResolveResults.push(resolveDetails);
									LeaveCriticalSection(&m_csHostGamertagResolveResults);
								}

								catch (Platform::Exception^ ex)
								{
									LogComment("Name resolve exception raised");
									// TODO - handle errors more usefully than just not setting the name...
									EnterCriticalSection(&m_csHostGamertagResolveResults);
									// Update flags for which names have been resolved, and if this completes this set, then set the flag to say that we should synchronise these out to the clients
									*pucsmallIdReadMaskResolved |= mask;
									if(ucsmallIdReadMask == *pucsmallIdReadMaskResolved)
									{
										resolveDetails->m_sync = true;
										delete pucsmallIdReadMaskResolved;
									}
									m_hostGamertagResolveResults.push(resolveDetails);
									LeaveCriticalSection(&m_csHostGamertagResolveResults);
								}
							});
						}
					}
				}

				switch(connectionInfo->m_internalDataState)
				{
					case DQRConnectionInfo::ConnectionState_InternalAssignSmallId0:
						connectionInfo->m_internalDataState = DQRConnectionInfo::ConnectionState_InternalAssignSmallId1;
						break;
					case DQRConnectionInfo::ConnectionState_InternalAssignSmallId1:
						connectionInfo->m_internalDataState = DQRConnectionInfo::ConnectionState_InternalAssignSmallId2;
						break;
					case DQRConnectionInfo::ConnectionState_InternalAssignSmallId2:
						connectionInfo->m_internalDataState = DQRConnectionInfo::ConnectionState_InternalAssignSmallId3;
						break;
					case DQRConnectionInfo::ConnectionState_InternalAssignSmallId3:
						connectionInfo->m_internalDataState = DQRConnectionInfo::ConnectionState_InternalHeaderByte;
						break;
				}

				pNextByte++;
				break;
			case DQRConnectionInfo::ConnectionState_InternalRoomSyncData:
				connectionInfo->m_pucRoomSyncData[connectionInfo->m_roomSyncDataBytesRead++] = byte;
				// The room sync info is sent as a 4 byte count of the length of XUID strings, then the RoomSyncData, then the XUID strings
				if( connectionInfo->m_roomSyncDataBytesToRead == 0 )
				{
					// At first stage of reading the 4 byte count
					if( connectionInfo->m_roomSyncDataBytesRead == 4 )
					{
						memcpy( &connectionInfo->m_roomSyncDataBytesToRead, connectionInfo->m_pucRoomSyncData, 4);
						delete [] connectionInfo->m_pucRoomSyncData;
						connectionInfo->m_roomSyncDataBytesToRead += sizeof(RoomSyncData);
						connectionInfo->m_pucRoomSyncData = new unsigned char[ connectionInfo->m_roomSyncDataBytesToRead ];
						connectionInfo->m_roomSyncDataBytesRead = 0;
					}
				}
				else if( connectionInfo->m_roomSyncDataBytesRead == connectionInfo->m_roomSyncDataBytesToRead )
				{
					// Second stage of reading the variable length data - when we've read this all, we can created storage for the XUID strings and copy them all in
					RoomSyncData *roomSyncData = (RoomSyncData *)connectionInfo->m_pucRoomSyncData;
					wchar_t *pwcsData = (wchar_t *)((unsigned char *)connectionInfo->m_pucRoomSyncData + sizeof(RoomSyncData));
					for( int i = 0; i < roomSyncData->playerCount; i++ )
					{
						unsigned int thisWchars = ( wcslen(pwcsData) + 1 );
						roomSyncData->players[i].m_XUID = new wchar_t[thisWchars];
						wcsncpy(roomSyncData->players[i].m_XUID, pwcsData, thisWchars);
						pwcsData += thisWchars;
					}
					// Update the room sync data with this new data. This will handle notification of new and removed players
					UpdateRoomSyncPlayers((RoomSyncData *)connectionInfo->m_pucRoomSyncData);
					
					delete connectionInfo->m_pucRoomSyncData;
					connectionInfo->m_pucRoomSyncData = nullptr;
					connectionInfo->m_internalDataState = DQRConnectionInfo::ConnectionState_InternalHeaderByte;

					// If we haven't actually established a connection yet for this channel, then this is the point where we can consider this active
					if( !connectionInfo->m_channelActive[connectionInfo->m_currentChannel] )
					{
						DQRNetworkManager::LogCommentFormat(L"Received data from host, channel %d considered active (%d bytes)\n",connectionInfo->m_currentChannel,connectionInfo->m_bytesRemaining);
						connectionInfo->m_channelActive[connectionInfo->m_currentChannel] = true;

						// This is also the time (as a client) to inform the chat integration layer of the host's session address, since we can now (reliably) send data to it
						if(connectionInfo->m_currentChannel == 0)
						{
							if( m_chat )
							{
								m_chat->OnNewSessionAddressAdded(m_hostSessionAddress);
							}
						}
					}

					// Move to starting & playing states, if we are still joining rather than adding an additional player from this client, and we have all the local players here.
					// We need to check that they are all here because we could have received a broadcast room sync data caused by another machine joining, and and so we can't assume
					// that we're ready to go just yet.
					if( m_state == DQRNetworkManager::DNM_INT_STATE_JOINING_SENDING_UNRELIABLE )
					{
						bool allLocalPlayersHere = true;
						for( int i = 0; i < MAX_LOCAL_PLAYERS; i++ )
						{
							if( m_currentUserMask & ( 1 << i ) )
							{
								if( GetLocalPlayerByUserIndex(i) == nullptr )
								{
									allLocalPlayersHere = false;
								}
							}
						}
						if( allLocalPlayersHere )
						{
							DQRNetworkManager::LogComment(L"All local players present");
							SetState(DQRNetworkManager::DNM_INT_STATE_STARTING);
							SetState(DQRNetworkManager::DNM_INT_STATE_PLAYING);
						}
						else
						{
							// Our players aren't all here yet. Going to keep on sending unreliable packets though until the connection is up and running
							DQRNetworkManager::LogComment(L"All local players not yet present");
						}
					}
				}
				pNextByte++;
				break;
			case DQRConnectionInfo::ConnectionState_InternalAddPlayerFailedData:
				connectionInfo->m_pucAddFailedPlayerData[connectionInfo->m_addFailedPlayerDataBytesRead++] = byte;
				// The failed player info is sent as a 4 byte count of the length of XUID string, then the string itself
				if( connectionInfo->m_addFailedPlayerDataBytesToRead == 0 )
				{
					// At first stage of reading the 4 byte count
					if( connectionInfo->m_addFailedPlayerDataBytesRead == 4 )
					{
						memcpy( &connectionInfo->m_addFailedPlayerDataBytesToRead, connectionInfo->m_pucAddFailedPlayerData, 4);
						delete [] connectionInfo->m_pucAddFailedPlayerData;
						connectionInfo->m_pucAddFailedPlayerData = new unsigned char[ connectionInfo->m_addFailedPlayerDataBytesToRead ];
						connectionInfo->m_addFailedPlayerDataBytesRead = 0;
					}
				}
				else if( connectionInfo->m_addFailedPlayerDataBytesRead == connectionInfo->m_addFailedPlayerDataBytesToRead )
				{
					// XUID fully read, can now handle what to do with it
					AddPlayerFailed(ref new Platform::String( (wchar_t *)connectionInfo->m_pucAddFailedPlayerData ) );
					delete [] connectionInfo->m_pucAddFailedPlayerData;
					connectionInfo->m_pucAddFailedPlayerData = nullptr;
					connectionInfo->m_internalDataState = DQRConnectionInfo::ConnectionState_InternalHeaderByte;
				}

				pNextByte++;
				break;
		}
	} while (pNextByte != pEndByte);
}

// This method directly sends bytes via the network communication layer, used to send both game data & data internal to the DQRNetworkManager itself.
// This is used by higher level sending methods that wrap communications up with headers that can be processed at the receiving end. 
void DQRNetworkManager::SendBytesRaw(int smallId, BYTE *bytes, int byteCount, bool reliableAndSequential)
{
	bool broadcast;
	unsigned int sessionAddress;

//	app.DebugPrintf("{%d,%d - %d}\n",smallId,reliableAndSequential,byteCount);

	if( smallId == -1 )
	{
		LogCommentFormat(L"Attempting broadcast, exception of address m_XRNS_Session->LocalSessionAddress %d %d %d", smallId, byteCount, reliableAndSequential);
		// Broadcast, used from host only
		broadcast = true;
		sessionAddress = 0;
	}
	else
	{
		// Send to individual session address
		broadcast = false;
		if( m_isHosting )
		{
			sessionAddress = m_sessionAddressFromSmallId[ smallId ];
		}
		else
		{
			sessionAddress = m_hostSessionAddress;
		}
	}
	RTS_SendData(bytes, byteCount, sessionAddress, reliableAndSequential, reliableAndSequential, reliableAndSequential, broadcast, true);
}

// This method is called by the chat integration layer to be able to send data
void DQRNetworkManager::SendBytesChat(unsigned int address, BYTE *bytes, int byteCount, bool reliable, bool sequential, bool broadcast)
{
	unsigned int sessionAddress;

	if( broadcast )
	{
		sessionAddress = 0;
	}
	else
	{
		// Send to individual session address
		sessionAddress = address;
	}
	
	RTS_SendData(bytes, byteCount, sessionAddress, reliable, sequential, false, broadcast, false);
}

// This is the higher level sending method for sending game data - this prefixes the send with a header so that it will get routed to the correct player.
void DQRNetworkManager::SendBytes(int smallId, BYTE *bytes, int byteCount)
{
	EnterCriticalSection(&m_csSendBytes);
	unsigned char *tempSendBuffer = static_cast<unsigned char *>(malloc(8191 + 2));

	BYTE *data = bytes;
	BYTE *dataEnd = bytes + byteCount;

	// Data to be sent has a header to say which of our own internal channels it is on (2 bits), and number of bytes that are in the message.
	// The number of bytes has to be stored in 13 bits, and so a maximum of 8191 bytes can be send at a time. Split up longer messages into
	// blocks of this size.
	do
	{
		int bytesToSend = static_cast<int>(dataEnd - data);
		if( bytesToSend > 8191 ) bytesToSend = 8191;

		// Send header with data sending mode - see full comment in DQRNetworkManagerEventHandlers::DataReceivedHandler
		tempSendBuffer[0] = ( m_channelFromSmallId[smallId] << 5 ) | ( bytesToSend >> 8 );
		tempSendBuffer[1] = bytesToSend & 0xff;
		memcpy(&tempSendBuffer[2], data, bytesToSend);

		SendBytesRaw(smallId, tempSendBuffer, bytesToSend + 2, true);

		data += bytesToSend;
	} while (data != dataEnd);

	free(tempSendBuffer);
	LeaveCriticalSection(&m_csSendBytes);
}

int DQRNetworkManager::GetQueueSizeBytes()
{
	return m_RTS_Stat_totalBytes;
}

int DQRNetworkManager::GetQueueSizeMessages()
{
	return m_RTS_Stat_totalSends;
}