BuddyThread.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // FILE: BuddyThread.cpp //////////////////////////////////////////////////////
  24. // GameSpy Presence & Messaging (Buddy) thread
  25. // This thread communicates with GameSpy's buddy server
  26. // and talks through a message queue with the rest of
  27. // the game.
  28. // Author: Matthew D. Campbell, June 2002
  29. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  30. #include "GameNetwork/GameSpy/BuddyThread.h"
  31. #include "GameNetwork/GameSpy/PeerThread.h"
  32. #include "GameNetwork/GameSpy/PersistentStorageThread.h"
  33. #include "GameNetwork/GameSpy/ThreadUtils.h"
  34. #include "Common/StackDump.h"
  35. #include "mutex.h"
  36. #include "thread.h"
  37. #ifdef _INTERNAL
  38. // for occasional debugging...
  39. //#pragma optimize("", off)
  40. //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  41. #endif
  42. //-------------------------------------------------------------------------
  43. typedef std::queue<BuddyRequest> RequestQueue;
  44. typedef std::queue<BuddyResponse> ResponseQueue;
  45. class BuddyThreadClass;
  46. class GameSpyBuddyMessageQueue : public GameSpyBuddyMessageQueueInterface
  47. {
  48. public:
  49. virtual ~GameSpyBuddyMessageQueue();
  50. GameSpyBuddyMessageQueue();
  51. virtual void startThread( void );
  52. virtual void endThread( void );
  53. virtual Bool isThreadRunning( void );
  54. virtual Bool isConnected( void );
  55. virtual Bool isConnecting( void );
  56. virtual void addRequest( const BuddyRequest& req );
  57. virtual Bool getRequest( BuddyRequest& req );
  58. virtual void addResponse( const BuddyResponse& resp );
  59. virtual Bool getResponse( BuddyResponse& resp );
  60. virtual GPProfile getLocalProfileID( void );
  61. BuddyThreadClass* getThread( void );
  62. private:
  63. MutexClass m_requestMutex;
  64. MutexClass m_responseMutex;
  65. RequestQueue m_requests;
  66. ResponseQueue m_responses;
  67. BuddyThreadClass *m_thread;
  68. };
  69. GameSpyBuddyMessageQueueInterface* GameSpyBuddyMessageQueueInterface::createNewMessageQueue( void )
  70. {
  71. return NEW GameSpyBuddyMessageQueue;
  72. }
  73. GameSpyBuddyMessageQueueInterface *TheGameSpyBuddyMessageQueue;
  74. #define MESSAGE_QUEUE ((GameSpyBuddyMessageQueue *)TheGameSpyBuddyMessageQueue)
  75. //-------------------------------------------------------------------------
  76. class BuddyThreadClass : public ThreadClass
  77. {
  78. public:
  79. BuddyThreadClass() : ThreadClass() { m_isNewAccount = m_isdeleting = m_isConnecting = m_isConnected = false; m_profileID = 0; m_lastErrorCode = 0; }
  80. void Thread_Function();
  81. void errorCallback( GPConnection *con, GPErrorArg *arg );
  82. void messageCallback( GPConnection *con, GPRecvBuddyMessageArg *arg );
  83. void connectCallback( GPConnection *con, GPConnectResponseArg *arg );
  84. void requestCallback( GPConnection *con, GPRecvBuddyRequestArg *arg );
  85. void statusCallback( GPConnection *con, GPRecvBuddyStatusArg *arg );
  86. Bool isConnecting( void ) { return m_isConnecting; }
  87. Bool isConnected( void ) { return m_isConnected; }
  88. GPProfile getLocalProfileID( void ) { return m_profileID; }
  89. private:
  90. Bool m_isNewAccount;
  91. Bool m_isConnecting;
  92. Bool m_isConnected;
  93. GPProfile m_profileID;
  94. Int m_lastErrorCode;
  95. Bool m_isdeleting;
  96. std::string m_nick, m_email, m_pass;
  97. };
  98. static enum CallbackType
  99. {
  100. CALLBACK_CONNECT,
  101. CALLBACK_ERROR,
  102. CALLBACK_RECVMESSAGE,
  103. CALLBACK_RECVREQUEST,
  104. CALLBACK_RECVSTATUS,
  105. CALLBACK_MAX
  106. };
  107. void callbackWrapper( GPConnection *con, void *arg, void *param )
  108. {
  109. CallbackType info = (CallbackType)(Int)param;
  110. BuddyThreadClass *thread = MESSAGE_QUEUE->getThread() ? MESSAGE_QUEUE->getThread() : NULL /*(TheGameSpyBuddyMessageQueue)?TheGameSpyBuddyMessageQueue->getThread():NULL*/;
  111. if (!thread)
  112. return;
  113. switch (info)
  114. {
  115. case CALLBACK_CONNECT:
  116. thread->connectCallback( con, (GPConnectResponseArg *)arg );
  117. break;
  118. case CALLBACK_ERROR:
  119. thread->errorCallback( con, (GPErrorArg *)arg );
  120. break;
  121. case CALLBACK_RECVMESSAGE:
  122. thread->messageCallback( con, (GPRecvBuddyMessageArg *)arg );
  123. break;
  124. case CALLBACK_RECVREQUEST:
  125. thread->requestCallback( con, (GPRecvBuddyRequestArg *)arg );
  126. break;
  127. case CALLBACK_RECVSTATUS:
  128. thread->statusCallback( con, (GPRecvBuddyStatusArg *)arg );
  129. break;
  130. }
  131. }
  132. //-------------------------------------------------------------------------
  133. GameSpyBuddyMessageQueue::GameSpyBuddyMessageQueue()
  134. {
  135. m_thread = NULL;
  136. }
  137. GameSpyBuddyMessageQueue::~GameSpyBuddyMessageQueue()
  138. {
  139. endThread();
  140. }
  141. void GameSpyBuddyMessageQueue::startThread( void )
  142. {
  143. if (!m_thread)
  144. {
  145. m_thread = NEW BuddyThreadClass;
  146. m_thread->Execute();
  147. }
  148. else
  149. {
  150. if (!m_thread->Is_Running())
  151. {
  152. m_thread->Execute();
  153. }
  154. }
  155. }
  156. void GameSpyBuddyMessageQueue::endThread( void )
  157. {
  158. if (m_thread)
  159. delete m_thread;
  160. m_thread = NULL;
  161. }
  162. Bool GameSpyBuddyMessageQueue::isThreadRunning( void )
  163. {
  164. return (m_thread) ? m_thread->Is_Running() : false;
  165. }
  166. Bool GameSpyBuddyMessageQueue::isConnected( void )
  167. {
  168. return (m_thread) ? m_thread->isConnected() : false;
  169. }
  170. Bool GameSpyBuddyMessageQueue::isConnecting( void )
  171. {
  172. return (m_thread) ? m_thread->isConnecting() : false;
  173. }
  174. void GameSpyBuddyMessageQueue::addRequest( const BuddyRequest& req )
  175. {
  176. MutexClass::LockClass m(m_requestMutex);
  177. if (m.Failed())
  178. return;
  179. m_requests.push(req);
  180. }
  181. Bool GameSpyBuddyMessageQueue::getRequest( BuddyRequest& req )
  182. {
  183. MutexClass::LockClass m(m_requestMutex, 0);
  184. if (m.Failed())
  185. return false;
  186. if (m_requests.empty())
  187. return false;
  188. req = m_requests.front();
  189. m_requests.pop();
  190. return true;
  191. }
  192. void GameSpyBuddyMessageQueue::addResponse( const BuddyResponse& resp )
  193. {
  194. MutexClass::LockClass m(m_responseMutex);
  195. if (m.Failed())
  196. return;
  197. m_responses.push(resp);
  198. }
  199. Bool GameSpyBuddyMessageQueue::getResponse( BuddyResponse& resp )
  200. {
  201. MutexClass::LockClass m(m_responseMutex, 0);
  202. if (m.Failed())
  203. return false;
  204. if (m_responses.empty())
  205. return false;
  206. resp = m_responses.front();
  207. m_responses.pop();
  208. return true;
  209. }
  210. BuddyThreadClass* GameSpyBuddyMessageQueue::getThread( void )
  211. {
  212. return m_thread;
  213. }
  214. GPProfile GameSpyBuddyMessageQueue::getLocalProfileID( void )
  215. {
  216. return (m_thread) ? m_thread->getLocalProfileID() : 0;
  217. }
  218. //-------------------------------------------------------------------------
  219. void BuddyThreadClass::Thread_Function()
  220. {
  221. try {
  222. _set_se_translator( DumpExceptionInfo ); // Hook that allows stack trace.
  223. GPConnection gpCon;
  224. GPConnection *con = &gpCon;
  225. gpInitialize( con, 0 );
  226. m_isConnected = m_isConnecting = false;
  227. gpSetCallback( con, GP_ERROR, callbackWrapper, (void *)CALLBACK_ERROR );
  228. gpSetCallback( con, GP_RECV_BUDDY_MESSAGE, callbackWrapper, (void *)CALLBACK_RECVMESSAGE );
  229. gpSetCallback( con, GP_RECV_BUDDY_REQUEST, callbackWrapper, (void *)CALLBACK_RECVREQUEST );
  230. gpSetCallback( con, GP_RECV_BUDDY_STATUS, callbackWrapper, (void *)CALLBACK_RECVSTATUS );
  231. GPEnum lastStatus = GP_OFFLINE;
  232. std::string lastStatusString;
  233. BuddyRequest incomingRequest;
  234. while ( running )
  235. {
  236. // deal with requests
  237. if (TheGameSpyBuddyMessageQueue->getRequest(incomingRequest))
  238. {
  239. switch (incomingRequest.buddyRequestType)
  240. {
  241. case BuddyRequest::BUDDYREQUEST_LOGIN:
  242. m_isConnecting = true;
  243. m_nick = incomingRequest.arg.login.nick;
  244. m_email = incomingRequest.arg.login.email;
  245. m_pass = incomingRequest.arg.login.password;
  246. m_isConnected = (gpConnect( con, incomingRequest.arg.login.nick, incomingRequest.arg.login.email,
  247. incomingRequest.arg.login.password, (incomingRequest.arg.login.hasFirewall)?GP_FIREWALL:GP_NO_FIREWALL,
  248. GP_BLOCKING, callbackWrapper, (void *)CALLBACK_CONNECT ) == GP_NO_ERROR);
  249. m_isConnecting = false;
  250. break;
  251. case BuddyRequest::BUDDYREQUEST_RELOGIN:
  252. m_isConnecting = true;
  253. m_isConnected = (gpConnect( con, m_nick.c_str(), m_email.c_str(), m_pass.c_str(), GP_FIREWALL,
  254. GP_BLOCKING, callbackWrapper, (void *)CALLBACK_CONNECT ) == GP_NO_ERROR);
  255. m_isConnecting = false;
  256. break;
  257. case BuddyRequest::BUDDYREQUEST_DELETEACCT:
  258. m_isdeleting = true;
  259. gpDeleteProfile( con );
  260. break;
  261. case BuddyRequest::BUDDYREQUEST_LOGOUT:
  262. m_isConnecting = m_isConnected = false;
  263. gpDisconnect( con );
  264. break;
  265. case BuddyRequest::BUDDYREQUEST_MESSAGE:
  266. {
  267. std::string s = WideCharStringToMultiByte( incomingRequest.arg.message.text );
  268. DEBUG_LOG(("Sending a buddy message to %d [%s]\n", incomingRequest.arg.message.recipient, s.c_str()));
  269. gpSendBuddyMessage( con, incomingRequest.arg.message.recipient, s.c_str() );
  270. }
  271. break;
  272. case BuddyRequest::BUDDYREQUEST_LOGINNEW:
  273. {
  274. m_isConnecting = true;
  275. m_nick = incomingRequest.arg.login.nick;
  276. m_email = incomingRequest.arg.login.email;
  277. m_pass = incomingRequest.arg.login.password;
  278. m_isNewAccount = TRUE;
  279. m_isConnected = (gpConnectNewUser( con, incomingRequest.arg.login.nick, incomingRequest.arg.login.email,
  280. incomingRequest.arg.login.password, (incomingRequest.arg.login.hasFirewall)?GP_FIREWALL:GP_NO_FIREWALL,
  281. GP_BLOCKING, callbackWrapper, (void *)CALLBACK_CONNECT ) == GP_NO_ERROR);
  282. if (m_isNewAccount) // if we didn't re-login
  283. {
  284. gpSetInfoMask( con, GP_MASK_NONE ); // don't share info
  285. }
  286. m_isConnecting = false;
  287. }
  288. break;
  289. case BuddyRequest::BUDDYREQUEST_ADDBUDDY:
  290. {
  291. std::string s = WideCharStringToMultiByte( incomingRequest.arg.addbuddy.text );
  292. gpSendBuddyRequest( con, incomingRequest.arg.addbuddy.id, s.c_str() );
  293. }
  294. break;
  295. case BuddyRequest::BUDDYREQUEST_DELBUDDY:
  296. {
  297. gpDeleteBuddy( con, incomingRequest.arg.profile.id );
  298. }
  299. break;
  300. case BuddyRequest::BUDDYREQUEST_OKADD:
  301. {
  302. gpAuthBuddyRequest( con, incomingRequest.arg.profile.id );
  303. }
  304. break;
  305. case BuddyRequest::BUDDYREQUEST_DENYADD:
  306. {
  307. gpDenyBuddyRequest( con, incomingRequest.arg.profile.id );
  308. }
  309. case BuddyRequest::BUDDYREQUEST_SETSTATUS:
  310. {
  311. //don't blast our 'Loading' status with 'Online'.
  312. if (lastStatus == GP_PLAYING && lastStatusString == "Loading" && incomingRequest.arg.status.status == GP_ONLINE)
  313. break;
  314. DEBUG_LOG(("BUDDYREQUEST_SETSTATUS: status is now %d:%s/%s\n",
  315. incomingRequest.arg.status.status, incomingRequest.arg.status.statusString, incomingRequest.arg.status.locationString));
  316. gpSetStatus( con, incomingRequest.arg.status.status, incomingRequest.arg.status.statusString,
  317. incomingRequest.arg.status.locationString );
  318. lastStatus = incomingRequest.arg.status.status;
  319. lastStatusString = incomingRequest.arg.status.statusString;
  320. }
  321. break;
  322. }
  323. }
  324. // update the network
  325. GPEnum isConnected = GP_CONNECTED;
  326. GPResult res = GP_NO_ERROR;
  327. res = gpIsConnected( con, &isConnected );
  328. if ( isConnected == GP_CONNECTED && res == GP_NO_ERROR )
  329. gpProcess( con );
  330. // end our timeslice
  331. Switch_Thread();
  332. }
  333. gpDestroy( con );
  334. } catch ( ... ) {
  335. DEBUG_CRASH(("Exception in buddy thread!"));
  336. }
  337. }
  338. void BuddyThreadClass::errorCallback( GPConnection *con, GPErrorArg *arg )
  339. {
  340. // log the error
  341. DEBUG_LOG(("GPErrorCallback\n"));
  342. m_lastErrorCode = arg->errorCode;
  343. char errorCodeString[256];
  344. char resultString[256];
  345. #define RESULT(x) case x: strcpy(resultString, #x); break;
  346. switch(arg->result)
  347. {
  348. RESULT(GP_NO_ERROR)
  349. RESULT(GP_MEMORY_ERROR)
  350. RESULT(GP_PARAMETER_ERROR)
  351. RESULT(GP_NETWORK_ERROR)
  352. RESULT(GP_SERVER_ERROR)
  353. default:
  354. strcpy(resultString, "Unknown result!");
  355. }
  356. #undef RESULT
  357. #define ERRORCODE(x) case x: strcpy(errorCodeString, #x); break;
  358. switch(arg->errorCode)
  359. {
  360. ERRORCODE(GP_GENERAL)
  361. ERRORCODE(GP_PARSE)
  362. ERRORCODE(GP_NOT_LOGGED_IN)
  363. ERRORCODE(GP_BAD_SESSKEY)
  364. ERRORCODE(GP_DATABASE)
  365. ERRORCODE(GP_NETWORK)
  366. ERRORCODE(GP_FORCED_DISCONNECT)
  367. ERRORCODE(GP_CONNECTION_CLOSED)
  368. ERRORCODE(GP_LOGIN)
  369. ERRORCODE(GP_LOGIN_TIMEOUT)
  370. ERRORCODE(GP_LOGIN_BAD_NICK)
  371. ERRORCODE(GP_LOGIN_BAD_EMAIL)
  372. ERRORCODE(GP_LOGIN_BAD_PASSWORD)
  373. ERRORCODE(GP_LOGIN_BAD_PROFILE)
  374. ERRORCODE(GP_LOGIN_PROFILE_DELETED)
  375. ERRORCODE(GP_LOGIN_CONNECTION_FAILED)
  376. ERRORCODE(GP_LOGIN_SERVER_AUTH_FAILED)
  377. ERRORCODE(GP_NEWUSER)
  378. ERRORCODE(GP_NEWUSER_BAD_NICK)
  379. ERRORCODE(GP_NEWUSER_BAD_PASSWORD)
  380. ERRORCODE(GP_UPDATEUI)
  381. ERRORCODE(GP_UPDATEUI_BAD_EMAIL)
  382. ERRORCODE(GP_NEWPROFILE)
  383. ERRORCODE(GP_NEWPROFILE_BAD_NICK)
  384. ERRORCODE(GP_NEWPROFILE_BAD_OLD_NICK)
  385. ERRORCODE(GP_UPDATEPRO)
  386. ERRORCODE(GP_UPDATEPRO_BAD_NICK)
  387. ERRORCODE(GP_ADDBUDDY)
  388. ERRORCODE(GP_ADDBUDDY_BAD_FROM)
  389. ERRORCODE(GP_ADDBUDDY_BAD_NEW)
  390. ERRORCODE(GP_ADDBUDDY_ALREADY_BUDDY)
  391. ERRORCODE(GP_AUTHADD)
  392. ERRORCODE(GP_AUTHADD_BAD_FROM)
  393. ERRORCODE(GP_AUTHADD_BAD_SIG)
  394. ERRORCODE(GP_STATUS)
  395. ERRORCODE(GP_BM)
  396. ERRORCODE(GP_BM_NOT_BUDDY)
  397. ERRORCODE(GP_GETPROFILE)
  398. ERRORCODE(GP_GETPROFILE_BAD_PROFILE)
  399. ERRORCODE(GP_DELBUDDY)
  400. ERRORCODE(GP_DELBUDDY_NOT_BUDDY)
  401. ERRORCODE(GP_DELPROFILE)
  402. ERRORCODE(GP_DELPROFILE_LAST_PROFILE)
  403. ERRORCODE(GP_SEARCH)
  404. ERRORCODE(GP_SEARCH_CONNECTION_FAILED)
  405. default:
  406. strcpy(errorCodeString, "Unknown error code!");
  407. }
  408. #undef ERRORCODE
  409. if(arg->fatal)
  410. {
  411. DEBUG_LOG(( "-----------\n"));
  412. DEBUG_LOG(( "GP FATAL ERROR\n"));
  413. DEBUG_LOG(( "-----------\n"));
  414. }
  415. else
  416. {
  417. DEBUG_LOG(( "-----\n"));
  418. DEBUG_LOG(( "GP ERROR\n"));
  419. DEBUG_LOG(( "-----\n"));
  420. }
  421. DEBUG_LOG(( "RESULT: %s (%d)\n", resultString, arg->result));
  422. DEBUG_LOG(( "ERROR CODE: %s (0x%X)\n", errorCodeString, arg->errorCode));
  423. DEBUG_LOG(( "ERROR STRING: %s\n", arg->errorString));
  424. if (arg->fatal == GP_FATAL)
  425. {
  426. BuddyResponse errorResponse;
  427. errorResponse.buddyResponseType = BuddyResponse::BUDDYRESPONSE_DISCONNECT;
  428. errorResponse.result = arg->result;
  429. errorResponse.arg.error.errorCode = arg->errorCode;
  430. errorResponse.arg.error.fatal = arg->fatal;
  431. strncpy(errorResponse.arg.error.errorString, arg->errorString, MAX_BUDDY_CHAT_LEN);
  432. errorResponse.arg.error.errorString[MAX_BUDDY_CHAT_LEN-1] = 0;
  433. m_isConnecting = m_isConnected = false;
  434. TheGameSpyBuddyMessageQueue->addResponse( errorResponse );
  435. if (m_isdeleting)
  436. {
  437. PeerRequest req;
  438. req.peerRequestType = PeerRequest::PEERREQUEST_LOGOUT;
  439. TheGameSpyPeerMessageQueue->addRequest( req );
  440. m_isdeleting = false;
  441. }
  442. }
  443. }
  444. static void getNickForMessage( GPConnection *con, GPGetInfoResponseArg *arg, void *param )
  445. {
  446. BuddyResponse *resp = (BuddyResponse *)param;
  447. strcpy(resp->arg.message.nick, arg->nick);
  448. }
  449. void BuddyThreadClass::messageCallback( GPConnection *con, GPRecvBuddyMessageArg *arg )
  450. {
  451. BuddyResponse messageResponse;
  452. messageResponse.buddyResponseType = BuddyResponse::BUDDYRESPONSE_MESSAGE;
  453. messageResponse.profile = arg->profile;
  454. // get info about the person asking to be our buddy
  455. gpGetInfo( con, arg->profile, GP_CHECK_CACHE, GP_BLOCKING, (GPCallback)getNickForMessage, &messageResponse);
  456. std::wstring s = MultiByteToWideCharSingleLine( arg->message );
  457. wcsncpy(messageResponse.arg.message.text, s.c_str(), MAX_BUDDY_CHAT_LEN);
  458. messageResponse.arg.message.text[MAX_BUDDY_CHAT_LEN-1] = 0;
  459. messageResponse.arg.message.date = arg->date;
  460. DEBUG_LOG(("Got a buddy message from %d [%ls]\n", arg->profile, s.c_str()));
  461. TheGameSpyBuddyMessageQueue->addResponse( messageResponse );
  462. }
  463. void BuddyThreadClass::connectCallback( GPConnection *con, GPConnectResponseArg *arg )
  464. {
  465. BuddyResponse loginResponse;
  466. if (arg->result == GP_NO_ERROR)
  467. {
  468. loginResponse.buddyResponseType = BuddyResponse::BUDDYRESPONSE_LOGIN;
  469. loginResponse.result = arg->result;
  470. loginResponse.profile = arg->profile;
  471. TheGameSpyBuddyMessageQueue->addResponse( loginResponse );
  472. m_profileID = arg->profile;
  473. if (!TheGameSpyPeerMessageQueue->isConnected() && !TheGameSpyPeerMessageQueue->isConnecting())
  474. {
  475. DEBUG_LOG(("Buddy connect: trying chat connect\n"));
  476. PeerRequest req;
  477. req.peerRequestType = PeerRequest::PEERREQUEST_LOGIN;
  478. req.nick = m_nick.c_str();
  479. req.password = m_pass;
  480. req.email = m_email;
  481. req.login.profileID = arg->profile;
  482. TheGameSpyPeerMessageQueue->addRequest(req);
  483. }
  484. }
  485. else
  486. {
  487. if (!TheGameSpyPeerMessageQueue->isConnected() && !TheGameSpyPeerMessageQueue->isConnecting())
  488. {
  489. if (m_lastErrorCode == GP_NEWUSER_BAD_NICK)
  490. {
  491. m_isNewAccount = FALSE;
  492. // they just hit 'create account' instead of 'log in'. Fix them.
  493. DEBUG_LOG(("User Error: Create Account instead of Login. Fixing them...\n"));
  494. BuddyRequest req;
  495. req.buddyRequestType = BuddyRequest::BUDDYREQUEST_LOGIN;
  496. strcpy(req.arg.login.nick, m_nick.c_str());
  497. strcpy(req.arg.login.email, m_email.c_str());
  498. strcpy(req.arg.login.password, m_pass.c_str());
  499. req.arg.login.hasFirewall = true;
  500. TheGameSpyBuddyMessageQueue->addRequest( req );
  501. return;
  502. }
  503. DEBUG_LOG(("Buddy connect failed (%d/%d): posting a failed chat connect\n", arg->result, m_lastErrorCode));
  504. PeerResponse resp;
  505. resp.peerResponseType = PeerResponse::PEERRESPONSE_DISCONNECT;
  506. resp.discon.reason = DISCONNECT_COULDNOTCONNECT;
  507. switch (m_lastErrorCode)
  508. {
  509. case GP_LOGIN_TIMEOUT:
  510. resp.discon.reason = DISCONNECT_GP_LOGIN_TIMEOUT;
  511. break;
  512. case GP_LOGIN_BAD_NICK:
  513. resp.discon.reason = DISCONNECT_GP_LOGIN_BAD_NICK;
  514. break;
  515. case GP_LOGIN_BAD_EMAIL:
  516. resp.discon.reason = DISCONNECT_GP_LOGIN_BAD_EMAIL;
  517. break;
  518. case GP_LOGIN_BAD_PASSWORD:
  519. resp.discon.reason = DISCONNECT_GP_LOGIN_BAD_PASSWORD;
  520. break;
  521. case GP_LOGIN_BAD_PROFILE:
  522. resp.discon.reason = DISCONNECT_GP_LOGIN_BAD_PROFILE;
  523. break;
  524. case GP_LOGIN_PROFILE_DELETED:
  525. resp.discon.reason = DISCONNECT_GP_LOGIN_PROFILE_DELETED;
  526. break;
  527. case GP_LOGIN_CONNECTION_FAILED:
  528. resp.discon.reason = DISCONNECT_GP_LOGIN_CONNECTION_FAILED;
  529. break;
  530. case GP_LOGIN_SERVER_AUTH_FAILED:
  531. resp.discon.reason = DISCONNECT_GP_LOGIN_SERVER_AUTH_FAILED;
  532. break;
  533. case GP_NEWUSER_BAD_NICK:
  534. resp.discon.reason = DISCONNECT_GP_NEWUSER_BAD_NICK;
  535. break;
  536. case GP_NEWUSER_BAD_PASSWORD:
  537. resp.discon.reason = DISCONNECT_GP_NEWUSER_BAD_PASSWORD;
  538. break;
  539. case GP_NEWPROFILE_BAD_NICK:
  540. resp.discon.reason = DISCONNECT_GP_NEWPROFILE_BAD_NICK;
  541. break;
  542. case GP_NEWPROFILE_BAD_OLD_NICK:
  543. resp.discon.reason = DISCONNECT_GP_NEWPROFILE_BAD_OLD_NICK;
  544. break;
  545. }
  546. TheGameSpyPeerMessageQueue->addResponse(resp);
  547. }
  548. }
  549. }
  550. // -----------------------
  551. static void getInfoResponseForRequest( GPConnection *con, GPGetInfoResponseArg *arg, void *param )
  552. {
  553. BuddyResponse *resp = (BuddyResponse *)param;
  554. resp->profile = arg->profile;
  555. strcpy(resp->arg.request.nick, arg->nick);
  556. strcpy(resp->arg.request.email, arg->email);
  557. strcpy(resp->arg.request.countrycode, arg->countrycode);
  558. }
  559. void BuddyThreadClass::requestCallback( GPConnection *con, GPRecvBuddyRequestArg *arg )
  560. {
  561. BuddyResponse response;
  562. response.buddyResponseType = BuddyResponse::BUDDYRESPONSE_REQUEST;
  563. response.profile = arg->profile;
  564. // get info about the person asking to be our buddy
  565. gpGetInfo( con, arg->profile, GP_CHECK_CACHE, GP_BLOCKING, (GPCallback)getInfoResponseForRequest, &response);
  566. std::wstring s = MultiByteToWideCharSingleLine( arg->reason );
  567. wcsncpy(response.arg.request.text, s.c_str(), GP_REASON_LEN);
  568. response.arg.request.text[GP_REASON_LEN-1] = 0;
  569. TheGameSpyBuddyMessageQueue->addResponse( response );
  570. }
  571. // -----------------------
  572. static void getInfoResponseForStatus(GPConnection * connection, GPGetInfoResponseArg * arg, void * param)
  573. {
  574. BuddyResponse *resp = (BuddyResponse *)param;
  575. resp->profile = arg->profile;
  576. strcpy(resp->arg.status.nick, arg->nick);
  577. strcpy(resp->arg.status.email, arg->email);
  578. strcpy(resp->arg.status.countrycode, arg->countrycode);
  579. }
  580. void BuddyThreadClass::statusCallback( GPConnection *con, GPRecvBuddyStatusArg *arg )
  581. {
  582. BuddyResponse response;
  583. // get user's name
  584. response.buddyResponseType = BuddyResponse::BUDDYRESPONSE_STATUS;
  585. gpGetInfo( con, arg->profile, GP_CHECK_CACHE, GP_BLOCKING, (GPCallback)getInfoResponseForStatus, &response);
  586. // get user's status
  587. GPBuddyStatus status;
  588. gpGetBuddyStatus( con, arg->index, &status );
  589. strcpy(response.arg.status.location, status.locationString);
  590. strcpy(response.arg.status.statusString, status.statusString);
  591. response.arg.status.status = status.status;
  592. DEBUG_LOG(("Got buddy status for %d(%s) - status %d\n", status.profile, response.arg.status.nick, status.status));
  593. // relay to UI
  594. TheGameSpyBuddyMessageQueue->addResponse( response );
  595. }
  596. //-------------------------------------------------------------------------