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
|
/*
gpiOperation.c
GameSpy Presence SDK
Dan "Mr. Pants" Schoenblum
Copyright 1999-2007 GameSpy Industries, Inc
devsupport@gamespy.com
***********************************************************************
Please see the GameSpy Presence SDK documentation for more information
**********************************************************************/
//INCLUDES
//////////
#include <stdlib.h>
#include "gpi.h"
//FUNCTIONS
///////////
GPResult
gpiFailedOpCallback(
GPConnection * connection,
const GPIOperation * operation
)
{
GPICallback callback;
GPIConnection * iconnection = (GPIConnection*)*connection;
assert(connection != NULL);
assert(*connection != NULL);
assert(operation != NULL);
callback = operation->callback;
if(callback.callback != NULL)
{
// Handle based on operation type.
//////////////////////////////////
switch(operation->type)
{
case GPI_CONNECT:
{
GPConnectResponseArg * arg;
arg = (GPConnectResponseArg *)gsimalloc(sizeof(GPConnectResponseArg));
if(arg == NULL)
Error(connection, GP_MEMORY_ERROR, "Out of memory.");
memset(arg, 0, sizeof(GPConnectResponseArg));
arg->result = operation->result;
if(iconnection->errorCode == GP_NEWUSER_BAD_NICK)
{
arg->profile = (GPProfile)iconnection->profileid;
iconnection->profileid = 0;
}
CHECK_RESULT(gpiAddCallback(connection, callback, arg, operation, 0));
break;
}
case GPI_NEW_PROFILE:
{
GPNewProfileResponseArg * arg;
arg = (GPNewProfileResponseArg *)gsimalloc(sizeof(GPNewProfileResponseArg));
if(arg == NULL)
Error(connection, GP_MEMORY_ERROR, "Out of memory.");
memset(arg, 0, sizeof(GPNewProfileResponseArg));
arg->result = operation->result;
CHECK_RESULT(gpiAddCallback(connection, callback, arg, operation, 0));
break;
}
case GPI_DELETE_PROFILE:
{
GPDeleteProfileResponseArg * arg;
arg = (GPDeleteProfileResponseArg *)gsimalloc(sizeof(GPDeleteProfileResponseArg));
if (arg == NULL)
Error(connection, GP_MEMORY_ERROR, "Out of memory.");
memset(arg, 0, sizeof(GPDeleteProfileResponseArg));
arg->result = operation->result;
CHECK_RESULT(gpiAddCallback(connection, callback, arg, operation, 0));
break;
}
case GPI_GET_INFO:
{
GPGetInfoResponseArg * arg;
arg = (GPGetInfoResponseArg *)gsimalloc(sizeof(GPGetInfoResponseArg));
if(arg == NULL)
Error(connection, GP_MEMORY_ERROR, "Out of memory.");
memset(arg, 0, sizeof(GPGetInfoResponseArg));
arg->result = operation->result;
CHECK_RESULT(gpiAddCallback(connection, callback, arg, operation, 0));
break;
}
case GPI_PROFILE_SEARCH:
{
GPProfileSearchResponseArg * arg;
arg = (GPProfileSearchResponseArg *)gsimalloc(sizeof(GPProfileSearchResponseArg));
if(arg == NULL)
Error(connection, GP_MEMORY_ERROR, "Out of memory.");
memset(arg, 0, sizeof(GPProfileSearchResponseArg));
arg->result = operation->result;
((GPProfileSearchResponseArg *)arg)->matches = NULL;
CHECK_RESULT(gpiAddCallback(connection, callback, arg, operation, 0));
break;
}
case GPI_REGISTER_UNIQUENICK:
{
GPRegisterUniqueNickResponseArg * arg;
arg = (GPRegisterUniqueNickResponseArg *)gsimalloc(sizeof(GPRegisterUniqueNickResponseArg));
if(arg == NULL)
Error(connection, GP_MEMORY_ERROR, "Out of memory.");
memset(arg, 0, sizeof(GPRegisterUniqueNickResponseArg));
arg->result = operation->result;
CHECK_RESULT(gpiAddCallback(connection, callback, arg, operation, 0));
break;
}
case GPI_REGISTER_CDKEY:
{
GPRegisterCdKeyResponseArg * arg;
arg = (GPRegisterCdKeyResponseArg *)gsimalloc(sizeof(GPRegisterCdKeyResponseArg));
if(arg == NULL)
Error(connection, GP_MEMORY_ERROR, "Out of memory.");
memset(arg, 0, sizeof(GPRegisterCdKeyResponseArg));
arg->result = operation->result;
CHECK_RESULT(gpiAddCallback(connection, callback, arg, operation, 0));
break;
}
default:
assert(0);
}
}
return GP_NO_ERROR;
}
GPResult
gpiAddOperation(
GPConnection * connection,
int type,
void * data,
GPIOperation ** op,
GPEnum blocking,
GPCallback callback,
void * param
)
{
GPIOperation * operation;
GPIConnection * iconnection = (GPIConnection*)*connection;
// Create a new operation struct.
/////////////////////////////////
operation = (GPIOperation *)gsimalloc(sizeof(GPIOperation));
if(operation == NULL)
Error(connection, GP_MEMORY_ERROR, "Out of memory.");
// Set the data.
////////////////
operation->type = type;
operation->data = data;
operation->blocking = (GPIBool)blocking;
operation->state = GPI_START;
if(type == GPI_CONNECT)
{
// Connect is always ID 1.
//////////////////////////
operation->id = 1;
}
else
{
operation->id = iconnection->nextOperationID++;
if(iconnection->nextOperationID < 2)
iconnection->nextOperationID = 2;
}
operation->result = GP_NO_ERROR;
operation->callback.callback = callback;
operation->callback.param = param;
// Add it to the list.
//////////////////////
operation->pnext = iconnection->operationList;
iconnection->operationList = operation;
*op = operation;
return GP_NO_ERROR;
}
void
gpiDestroyOperation(
GPConnection * connection,
GPIOperation * operation
)
{
GPIConnection * iconnection = (GPIConnection*)*connection;
// Search?
//////////
if(operation->type == GPI_PROFILE_SEARCH)
{
GPISearchData * data = (GPISearchData *)operation->data;
// One less.
////////////
iconnection->numSearches--;
assert(iconnection->numSearches >= 0);
// Close the socket.
////////////////////
shutdown(data->sock, 2);
closesocket(data->sock);
// freeclear the buffers.
////////////////////
freeclear(data->outputBuffer.buffer);
freeclear(data->inputBuffer.buffer);
}
// freeclear the data.
/////////////////
freeclear(operation->data);
// freeclear the operation struct.
/////////////////////////////
freeclear(operation);
}
void
gpiRemoveOperation(
GPConnection * connection,
GPIOperation * operation
)
{
GPIConnection * iconnection = (GPIConnection*)*connection;
GPIOperation * pcurr = iconnection->operationList;
GPIOperation * pprev = NULL;
// Go through the list of operations.
/////////////////////////////////////
while(pcurr != NULL)
{
// Check for a match.
/////////////////////
if(pcurr == operation)
{
// Update the list.
///////////////////
if(pprev == NULL)
iconnection->operationList = pcurr->pnext;
else
pprev->pnext = operation->pnext;
gpiDestroyOperation(connection, operation);
return;
}
pprev = pcurr;
pcurr = pcurr->pnext;
}
}
GPIBool
gpiFindOperationByID(
const GPConnection * connection,
GPIOperation ** operation,
int id
)
{
GPIOperation * op;
GPIConnection * iconnection = (GPIConnection*)*connection;
// Go through the list of operations.
/////////////////////////////////////
for(op = iconnection->operationList ; op != NULL ; op = op->pnext)
{
// Check the id.
////////////////
if(op->id == id)
{
// Found it.
////////////
if(operation != NULL)
*operation = op;
return GPITrue;
}
}
// Didn't find it.
//////////////////
if(operation != NULL)
*operation = NULL;
return GPIFalse;
}
GPIBool
gpiOperationsAreBlocking(
const GPConnection * connection
)
{
GPIOperation * operation;
GPIConnection * iconnection = (GPIConnection*)*connection;
// Loop through the operations.
///////////////////////////////
for(operation = iconnection->operationList ; operation != NULL ; operation = operation->pnext)
{
// Check if it's blocking.
//////////////////////////
if((operation->blocking) && (operation->type != GPI_PROFILE_SEARCH))
return GPITrue;
}
// Nothing was blocking.
////////////////////////
return GPIFalse;
}
GPResult
gpiProcessOperation(
GPConnection * connection,
GPIOperation * operation,
const char * input
)
{
GPResult result = GP_NO_ERROR;
// Check the operation type.
////////////////////////////
switch(operation->type)
{
case GPI_CONNECT:
result = gpiProcessConnect(connection, operation, input);
break;
case GPI_NEW_PROFILE:
result = gpiProcessNewProfile(connection, operation, input);
break;
case GPI_DELETE_PROFILE:
result = gpiProcessDeleteProfle(connection, operation, input);
break;
case GPI_GET_INFO:
result = gpiProcessGetInfo(connection, operation, input);
break;
case GPI_REGISTER_UNIQUENICK:
result = gpiProcessRegisterUniqueNick(connection, operation, input);
break;
case GPI_REGISTER_CDKEY:
result = gpiProcessRegisterCdKey(connection, operation, input);
break;
default:
gsDebugFormat(GSIDebugCat_GP, GSIDebugType_Misc, GSIDebugLevel_HotError,
"gpiProcessOperation was passed an operation with an invalid type (%d)\n", operation->type);
assert(0);
break;
}
if(result != GP_NO_ERROR)
operation->result = result;
return result;
}
|