File: peerAutoMatch.c

package info (click to toggle)
openmohaa 0.82.1%2Bdfsg-1
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 34,192 kB
  • sloc: cpp: 315,720; ansic: 275,789; sh: 312; xml: 246; asm: 141; makefile: 7
file content (334 lines) | stat: -rw-r--r-- 7,631 bytes parent folder | download | duplicates (2)
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
/*
GameSpy Peer SDK 
Dan "Mr. Pants" Schoenblum
dan@gamespy.com

Copyright 1999-2007 GameSpy Industries, Inc

devsupport@gamespy.com
*/

/*************
** INCLUDES **
*************/
#include "peerAutoMatch.h"
#include "peerMangle.h"
#include "peerOperations.h"
#include "peerSB.h"
#include "peerQR.h"
#include "peerCallbacks.h"
#include "peerPlayers.h"
#include "peerRooms.h"

/**************
** FUNCTIONS **
**************/
static PEERBool piCreateAutoMatchRoom(PEER peer);

static void piCleanAutoMatch(PEER peer)
{
	PEER_CONNECTION;

	// free memory
	gsifree(connection->autoMatchFilter);

	// remove the operation
	piRemoveOperation(peer, connection->autoMatchOperation);
	
	connection->autoMatchOperation = NULL;
}

void piSetAutoMatchStatus(PEER peer, PEERAutoMatchStatus status)
{
	piOperation * operation;
	PEER_CONNECTION;

	// make sure there is an operation
	operation = connection->autoMatchOperation;
	assert(operation);
	if(!operation)
		return;

	// we need to create a room before we can really switch to PEERWaiting
	if((status == PEERWaiting) && !connection->inRoom[StagingRoom])
	{
		if(!piCreateAutoMatchRoom(peer))
			piSetAutoMatchStatus(peer, connection->autoMatchSBFailed?PEERFailed:PEERSearching);
		return;
	}

	// check if this is already the current status
	if(connection->autoMatchStatus != status)
	{
		// set the new status
		connection->autoMatchStatus = status;
		
		// add the callback
		piAddAutoMatchStatusCallback(peer);
	}

	// handle the status
	switch(status)
	{		
	case PEERFailed:
		// stop
		piSBStopListingAutoMatches(peer);
		piStopAutoMatchReporting(peer);
		piLeaveRoom(peer, StagingRoom, "");

		// clean
		piCleanAutoMatch(peer);
		break;

	case PEERSearching:
		// stop
		piStopAutoMatchReporting(peer);
		piLeaveRoom(peer, StagingRoom, "");

		// start
		if(!connection->autoMatchBrowsing)
		{
			if(!piSBStartListingAutoMatches(peer))
			{
				piSetAutoMatchStatus(peer, connection->autoMatchQRFailed?PEERFailed:PEERWaiting);
				return;
			}
		}
		break;

	case PEERWaiting:
		// start
		assert(connection->inRoom[StagingRoom]);
		if(!connection->autoMatchBrowsing && !connection->autoMatchSBFailed)			
			piSBStartListingAutoMatches(peer);

		if(!connection->autoMatchReporting)
		{
			if(!piStartAutoMatchReporting(peer))
			{
				piSetAutoMatchStatus(peer, connection->autoMatchSBFailed?PEERFailed:PEERSearching);
				return;
			}
		}
		break;

	case PEERStaging:
		// stop
		if(!connection->hosting)
		{
			piStopAutoMatchReporting(peer);
		}
		piSBStopListingAutoMatches(peer);

		// start
		if(connection->hosting && !connection->autoMatchReporting)
		{
			if(!piStartAutoMatchReporting(peer))
			{
				piSetAutoMatchStatus(peer, PEERSearching);
				return;
			}
		}
		break;

	case PEERReady:
		// start
		if (connection->hosting && !connection->autoMatchReporting)
			piStartAutoMatchReporting(peer);
		
		break;
	case PEERComplete:
		// stop
		piSBStopListingAutoMatches(peer);
		piStopAutoMatchReporting(peer);

		// clean
		piCleanAutoMatch(peer);
		break;
	}
}

void piStopAutoMatch(PEER peer)
{
	//PEERBool inRoom;

	PEER_CONNECTION;

	// make sure we're AutoMatching
	if(peerIsAutoMatching(peer))
	{
		// we don't want the status callback called
		if(connection->autoMatchOperation)
			connection->autoMatchOperation->callback = (PEERCBType)NULL;

		// trick it into thinking it's not in a staging room (if it is)
		//inRoom = connection->inRoom[StagingRoom];
		//connection->inRoom[StagingRoom] = PEERFalse;
		
		// cleanup
		piSetAutoMatchStatus(peer, PEERFailed);

		// reset the inRoom flag
		//connection->inRoom[StagingRoom] = inRoom;
	}
}

static void piJoinAutoMatchRoomCallback
(
	PEER peer,
	PEERBool success,
	PEERJoinResult result,
	RoomType roomType,
	void * param
)
{
	PEER_CONNECTION;

	// if we're the only one, or if there's no host, leave
	if(success && ((connection->numPlayers[StagingRoom] <= 1) || !piCountRoomOps(peer, StagingRoom, connection->nick)))
	{
		piLeaveRoom(peer, StagingRoom, "");
		success = PEERFalse;
	}

	// check if it succeeded
	if(success)
	{
		// we're now staging
		piSetAutoMatchStatus(peer, PEERStaging);

		// if we've got maxplayers, we're now Ready
		if((connection->autoMatchStatus == PEERStaging) && (connection->numPlayers[StagingRoom] >= connection->maxPlayers))
			piSetAutoMatchStatus(peer, PEERReady);
	}

	// if we were waiting, and this failed, restart waiting
	if(!success && (connection->autoMatchStatus == PEERWaiting))
		piSetAutoMatchStatus(peer, PEERWaiting);
		
	GSI_UNUSED(result);
	GSI_UNUSED(roomType);
	GSI_UNUSED(param);
}

PEERBool piJoinAutoMatchRoom(PEER peer, SBServer server)
{
	unsigned int publicIP;
	unsigned int privateIP;
	unsigned short privatePort;
	char room[PI_ROOM_MAX_LEN];

	PEER_CONNECTION;

	// Get the public and private IPs and ports.
	publicIP = SBServerGetPublicInetAddress(server);
	privateIP = SBServerGetPrivateInetAddress(server);
	if(SBServerHasPrivateAddress(server))
		privatePort = SBServerGetPrivateQueryPort(server);
	else
		privatePort = SBServerGetPublicQueryPort(server);

	if(!publicIP)
		return PEERFalse;

	// get the staging room.
	piMangleStagingRoom(room, connection->title, publicIP, privateIP, privatePort);

	// start the operation.
	if(!piNewJoinRoomOperation(peer, StagingRoom, room, NULL, piJoinAutoMatchRoomCallback, NULL, piGetNextID(peer)))
		return PEERFalse;

	// clone the server
	connection->hostServer = piSBCloneServer(server);

	return PEERTrue;
}

// GetChannelModeCallback used to get the channel modes 
// and fix the modes: Limit and OpsObeyChannelLimit
static void piAutoMatchGetChannelCallback(CHAT chat, CHATBool success, const gsi_char *channel, CHATChannelMode *mode, void *param)
{
	piConnection *connection = (piConnection *)param;
	if (success)
	{

		mode->Limit = connection->maxPlayers;
		mode->OpsObeyChannelLimit = CHATTrue;
		
		// Don't let ops bypass channel limit on the number of players in room
		chatSetChannelMode(chat, channel, mode);
	}
	else
	{
		// handle the failure
		connection->autoMatchQRFailed = PEERTrue;
		piSetAutoMatchStatus((PEER *)&connection, connection->autoMatchSBFailed?PEERFailed:PEERSearching);
	}
}

static void piCreateAutoMatchRoomCallback
(
	PEER peer,
	PEERBool success,
	PEERJoinResult result,
	RoomType roomType,
	void * param
)
{
	PEERAutoMatchStatus status;

	PEER_CONNECTION;

	if(success)
	{
		// set the status based on the number of people in the room
		if(connection->numPlayers[StagingRoom] <= 1)
		{
			status = PEERWaiting;

			// get the channel modes so we can set the limits on our staging channel
			chatGetChannelMode(connection->chat, peerGetRoomChannel(peer, StagingRoom), piAutoMatchGetChannelCallback, 
				(void *)connection, CHATFalse);
		}
		else if(connection->numPlayers[StagingRoom] >= connection->maxPlayers)
		{
			status = PEERReady;
		}
		else
		{
			status = PEERStaging;
		}

		// set the Waiting status
		piSetAutoMatchStatus(peer, status);
	}
	else
	{
		// handle the failure
		connection->autoMatchQRFailed = PEERTrue;
		piSetAutoMatchStatus(peer, connection->autoMatchSBFailed?PEERFailed:PEERSearching);
	}
	
	GSI_UNUSED(result);
	GSI_UNUSED(roomType);
	GSI_UNUSED(param);
}

static PEERBool piCreateAutoMatchRoom(PEER peer)
{
	piOperation * operation;

	PEER_CONNECTION;

	// get the AutoMatch operation
	operation = connection->autoMatchOperation;

	// start the operations
	if(!piNewCreateStagingRoomOperation(peer, connection->nick, "", connection->maxPlayers, operation->socket, operation->port, piCreateAutoMatchRoomCallback, NULL, piGetNextID(peer)))
	{
		connection->autoMatchQRFailed = PEERTrue;
		return PEERFalse;
	}

	return PEERTrue;
}