File: ghttpc.c

package info (click to toggle)
openmohaa 0.81.1%2Bdfsg-2
  • links: PTS, VCS
  • area: contrib
  • in suites: trixie
  • size: 29,124 kB
  • sloc: ansic: 270,865; cpp: 250,173; sh: 234; asm: 141; xml: 64; makefile: 7
file content (313 lines) | stat: -rw-r--r-- 7,206 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
 /*
GameSpy GHTTP SDK 
Dan "Mr. Pants" Schoenblum
dan@gamespy.com

Copyright 1999-2007 GameSpy Industries, Inc

devsupport@gamespy.com
*/

#include "../../common/gsCommon.h"
#include "../ghttp.h"

#ifdef UNDER_CE
	void RetailOutputA(CHAR *tszErr, ...);
	#define printf RetailOutputA
#elif defined(_NITRO)
	#include "../../common/nitro/screen.h"
	#define printf Printf
	#define vprintf VPrintf
#endif

#define MAX_REQUESTS 20

typedef struct Result
{
	GHTTPBool started;
	gsi_time startTime;
	gsi_time stopTime;
	GHTTPResult result;
} Result;

static int pendingRequests;
static Result results[MAX_REQUESTS];

static gsi_char * stateStrings[] =
{
	_T("Socket Init"),
	_T("Host Lookup"),
	_T("Lookup Pending"),
	_T("Connecting"),
	_T("Securing Session"),
	_T("Sending Request"),
	_T("Posting"),
	_T("Waiting"),
	_T("Receiving Status"),
	_T("Receiving Headers"),
	_T("Receiving File")
};

static gsi_char * resultStrings[] =
{
	_T("GHTTPSuccess"),
	_T("GHTTPOutOfMemory"),
	_T("GHTTPBufferOverflow"),
	_T("GHTTPParseURLFailed"),
	_T("GHTTPHostLookupFailed"),
	_T("GHTTPSocketFailed"),
	_T("GHTTPConnectFailed"),
	_T("GHTTPBadResponse"),
	_T("GHTTPRequestRejected"),
	_T("GHTTPUnauthorized"),
	_T("GHTTPForbidden"),
	_T("GHTTPFileNotFound"),
	_T("GHTTPServerError"),
	_T("GHTTPFileWriteFailed"),
	_T("GHTTPFileReadFailed"),
	_T("GHTTPFileIncomplete"),
	_T("GHTTPFileToBig"),
	_T("GHTTPEncryptionError"),
	_T("GHTTPRequestCancelled")
};


#ifdef __MWERKS__  // CodeWarrior will warn if function not prototyped
int test_main(int argc, char **argv);
#endif

#ifdef GSI_COMMON_DEBUG
	static void DebugCallback(GSIDebugCategory theCat, GSIDebugType theType,
	                          GSIDebugLevel theLevel, const char * theTokenStr,
	                          va_list theParamList)
	{
		GSI_UNUSED(theLevel);

		printf("[%s][%s] ", 
				gGSIDebugCatStrings[theCat], 
				gGSIDebugTypeStrings[theType]);
		vprintf(theTokenStr, theParamList);

#if defined(_WIN32) && !defined(_XBOX)
		{
			static char buffer[4098];
			sprintf(buffer, "[%s][%s] ", 
					gGSIDebugCatStrings[theCat], 
					gGSIDebugTypeStrings[theType]);
			OutputDebugString(buffer);
			vsprintf(buffer, theTokenStr, theParamList);
			OutputDebugString(buffer);
		}
#endif
	}
#endif

static GHTTPBool CompletedCallback
(
	GHTTPRequest request,
	GHTTPResult result,
	char * buffer,
	GHTTPByteCount bufferLen,
	void * param
)
{
	int index = (int)param;

	pendingRequests--;

	// Save off the result.
	///////////////////////
	results[index].result = result;

	// Save the time.
	/////////////////
	results[index].stopTime = current_time();

	// Check the result and print out some info.
	////////////////////////////////////////////
	if(result == GHTTPSuccess)
		_tprintf(_T("%d finished\n"), index);
	else
		_tprintf(_T("%d failed: %s\n"), index, resultStrings[result]);

	// Don't free this buffer, its from the stack.
	//////////////////////////////////////////////
	if(index == 1)
		return GHTTPFalse;

	// Free the buffer (if there is one).
	/////////////////////////////////////
	GSI_UNUSED(request);
	GSI_UNUSED(buffer);
	GSI_UNUSED(bufferLen);

	return GHTTPTrue;
}

static void ProgressCallback
(
	GHTTPRequest request,
	GHTTPState state,
	const char * buffer,
	GHTTPByteCount bufferLen,
	GHTTPByteCount bytesReceived,
	GHTTPByteCount totalSize,
	void * param
)
{
	int index = (int)param;

	// Show the current state.
	//////////////////////////
	_tprintf(_T("%d state: %s"), index, stateStrings[state]);

	// If we're receiving the file, show the progress.
	//////////////////////////////////////////////////
	if(state == GHTTPReceivingFile)
	{
		// Display based on if we know the total size.
		//////////////////////////////////////////////
		if(totalSize != -1)
			_tprintf(_T(" (%d / %d bytes)\n"), bytesReceived, totalSize);
		else
			_tprintf(_T(" (%d bytes)\n"), bytesReceived);
	}
	else
		_tprintf(_T("\n"));
		
	GSI_UNUSED(request);
	GSI_UNUSED(buffer);
	GSI_UNUSED(bufferLen);
}

static void CheckRequest(GHTTPRequest request, int index)
{
	assert(index < MAX_REQUESTS);
	results[index].started = (request < 0)?GHTTPFalse:GHTTPTrue;
	if(results[index].started)
		results[index].startTime = current_time();
}

int test_main(int argc, char **argv)
{
	int i;
	static char buffer[10000] = "";
	GHTTPRequest request;
	int numRequests;

#ifdef GSI_COMMON_DEBUG
	// Define GSI_COMMON_DEBUG if you want to view the SDK debug output
	// Set the SDK debug log file, or set your own handler using gsSetDebugCallback
	//gsSetDebugFile(stdout); // output to console
	gsSetDebugCallback(DebugCallback);

	// Set some debug levels
	gsSetDebugLevel(GSIDebugCat_All, GSIDebugType_All, GSIDebugLevel_Hardcore);
	//gsSetDebugLevel(GSIDebugCat_QR2, GSIDebugType_Network, GSIDebugLevel_Verbose);   // Show Detailed data on network traffic
	//gsSetDebugLevel(GSIDebugCat_App, GSIDebugType_All, GSIDebugLevel_Hardcore);  // Show All app comment
#endif

	// get a file
	request = ghttpGet(
		_T("http://www.gamespy.net/images/dev_serv_main.jpg"),
		GHTTPFalse,
		CompletedCallback,
		(void *)pendingRequests);
	CheckRequest(request, pendingRequests);
	pendingRequests++;

	// put a file into our own buffer, with progress updates
	// also gets redirected
	request = ghttpGetEx(
		_T("http://google.com/intl/en_ALL/images/logo.gif"),
		NULL,
		buffer, sizeof(buffer),
		NULL,
		GHTTPFalse,
		GHTTPFalse,
		ProgressCallback,
		CompletedCallback,
		(void *)pendingRequests);
	CheckRequest(request, pendingRequests);
	pendingRequests++;

	// download a file
#if !defined(NOFILE)
	request = ghttpSave(
		_T("http://www.gamespy.net/images/dev_serv_logo.jpg"),
		_T("logo.jpg"),
		GHTTPFalse,
		CompletedCallback,
		(void *)pendingRequests);
	CheckRequest(request, pendingRequests);
	pendingRequests++;
#endif

	// stream a page
	request = ghttpStreamEx(
		_T("http://www.gamespy.net"),
		NULL,
		NULL,
		GHTTPFalse,
		GHTTPFalse,
		ProgressCallback,
		CompletedCallback,
		(void *)pendingRequests);
	CheckRequest(request, pendingRequests);
	pendingRequests++;

	// get a header
	request = ghttpHead(
		_T("http://sdkdev.gamespy.com/games/st_ladder/web/index.html"),
		GHTTPFalse,
		CompletedCallback,
		(void *)pendingRequests);
	CheckRequest(request, pendingRequests);
	pendingRequests++;

	// stream a secure page	
	request = ghttpStreamEx(
#if defined(_REVOLUTION)
		_T("https://mariokartwii.race.gs.nintendowifi.net/RaceService/test.txt"),
#else
		_T("https://www.gamespyid.com/"),
#endif
		NULL,
		NULL,
		GHTTPFalse,
		GHTTPFalse,
		ProgressCallback,
		CompletedCallback,
		(void *)pendingRequests);

	//if(!IS_GHTTP_ERROR(request))
	//	ghttpSetRequestEncryptionEngine(request, GHTTPEncryptionEngine_GameSpy);
	CheckRequest(request, pendingRequests);
	pendingRequests++;

	// store the number of requests
	numRequests = pendingRequests;

	do
	{
		ghttpThink();
		msleep(20);
	}
	while(pendingRequests);

	ghttpCleanup();

	_tprintf(_T("Results:\n"));
	for(i = 0 ; i < numRequests ; i++)
	{
		if(results[i].started == GHTTPFalse)
			_tprintf(_T("%d: Failed to start\n"), i);
		else
			_tprintf(_T("%d: %s [%dms]\n"), i, resultStrings[results[i].result], results[i].stopTime - results[i].startTime);
	}

	GSI_UNUSED(argc);
	GSI_UNUSED(argv);

	return 0;
}