File: gt2Filter.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 (191 lines) | stat: -rw-r--r-- 4,160 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
/*
GameSpy GT2 SDK
Dan "Mr. Pants" Schoenblum
dan@gamespy.com

Copyright 2002 GameSpy Industries, Inc

devsupport@gamespy.com
*/

#include "gt2Filter.h"
#include "gt2Callback.h"
#include "gt2Message.h"
#include "gt2Utility.h"

static int GS_STATIC_CALLBACK gti2SendFiltersCompare
(
	const void * elem1,
	const void * elem2
)
{
	gt2SendFilterCallback * callback1 = (gt2SendFilterCallback *)elem1;
	gt2SendFilterCallback * callback2 = (gt2SendFilterCallback *)elem2;

	if(*callback1 == *callback2)
		return 0;

	return 1;
}

static int GS_STATIC_CALLBACK gti2ReceiveFiltersCompare
(
	const void * elem1,
	const void * elem2
)
{
	gt2ReceiveFilterCallback * callback1 = (gt2ReceiveFilterCallback *)elem1;
	gt2ReceiveFilterCallback * callback2 = (gt2ReceiveFilterCallback *)elem2;

	if(*callback1 == *callback2)
		return 0;

	return 1;
}

GT2Bool gti2AddSendFilter(GT2Connection connection, gt2SendFilterCallback callback)
{
	// Check if we have a send filters list.
	if(!connection->sendFilters)
		return GT2False;

	// Add this callback to the list.
	ArrayAppend(connection->sendFilters, &callback);

	// Return GT2True if it was added.
	return (ArraySearch(connection->sendFilters, &callback, gti2SendFiltersCompare, 0, 0) != NOT_FOUND);
}

GT2Bool gti2AddReceiveFilter(GT2Connection connection, gt2ReceiveFilterCallback callback)
{
	// Check if we have a receive filters list.
	if(!connection->receiveFilters)
		return GT2False;

	// Add this callback to the list.
	ArrayAppend(connection->receiveFilters, &callback);

	// Return GT2True if it was added.
	return (ArraySearch(connection->receiveFilters, &callback, gti2ReceiveFiltersCompare, 0, 0) != NOT_FOUND);
}

void gti2RemoveSendFilter(GT2Connection connection, gt2SendFilterCallback callback)
{
	int index;

	// Check for no filters.
	if(!connection->sendFilters)
		return;

	// check for removing all
	if(!callback)
	{
		// Remove all the filters.
		ArrayClear(connection->sendFilters);
		return;
	}

	// Find it.
	index = ArraySearch(connection->sendFilters, &callback, gti2SendFiltersCompare, 0, 0);
	if(index == NOT_FOUND)
		return;

	// Remove it.
	ArrayRemoveAt(connection->sendFilters, index);
}

void gti2RemoveReceiveFilter(GT2Connection connection, gt2ReceiveFilterCallback callback)
{
	int index;

	// Check for no filters.
	if(!connection->receiveFilters)
		return;

	// check for removing all
	if(!callback)
	{
		// Remove all the filters.
		ArrayClear(connection->receiveFilters);
		return;
	}

	// Find it.
	index = ArraySearch(connection->receiveFilters, &callback, gti2ReceiveFiltersCompare, 0, 0);
	if(index == NOT_FOUND)
		return;

	// Remove it.
	ArrayRemoveAt(connection->receiveFilters, index);
}

GT2Bool gti2FilteredSend(GT2Connection connection, int filterID, const GT2Byte * message, int len, GT2Bool reliable)
{
	int num;

	// Make sure we're connected.
	if(connection->state != GTI2Connected)
		return GT2True;

	// check the message and len
	gti2MessageCheck(&message, &len);

	// Get the number of filters.
	num = ArrayLength(connection->sendFilters);

	// Check if its a valid ID.
	if(filterID < 0)
		return GT2True;
	if(filterID >= num)
		return GT2True;

	// Is it the last one?
	if(filterID == (num - 1))
	{
		// Do the actual send.
		if(!gti2Send(connection, message, len, reliable))
			return GT2False;
	}
	else
	{
		// Filter it.
		if(!gti2SendFilterCallback(connection, ++filterID, message, len, reliable))
			return GT2False;
	}

	return GT2True;
}

GT2Bool gti2FilteredReceive(GT2Connection connection, int filterID, GT2Byte * message, int len, GT2Bool reliable)
{
	int num;

	// Make sure we're connected.
	if(connection->state != GTI2Connected)
		return GT2True;

	// Get the number of filters.
	num = ArrayLength(connection->receiveFilters);

	// Check if its a valid ID.
	if(filterID < 0)
		return GT2True;
	if(filterID >= num)
		return GT2True;

	// Is it the last one?
	if(filterID == (num - 1))
	{
		// call the callback
		if(!gti2ReceivedCallback(connection, message, len, reliable))
			return GT2False;
	}
	else
	{
		// Filter it.
		if(!gti2ReceiveFilterCallback(connection, ++filterID, message, len, reliable))
			return GT2False;
	}

	return GT2True;
}