Subversion Repositories Games.Chess Giants

Rev

Rev 85 | Rev 89 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 85 Rev 88
Line 13... Line 13...
13
 
13
 
14
 
14
 
15
// prototypes of local functions
15
// prototypes of local functions
16
static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam);
16
static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam);
17
static unsigned __int32 RetrieveActivationCode (wchar_t *candidate_email, wchar_t **failure_reason);
17
static unsigned __int32 RetrieveActivationCode (wchar_t *candidate_email, wchar_t **failure_reason);
-
 
18
static int recv_with_timeout (int socket_id, float timeout_in_seconds, char *outbuf, size_t outbuf_size, int flags);
18
 
19
 
19
 
20
 
20
void DialogBox_Registration (void)
21
void DialogBox_Registration (void)
21
{
22
{
22
   // helper function to fire up the modal dialog box
23
   // helper function to fire up the modal dialog box
Line 227... Line 228...
227
   {
228
   {
228
      *failure_reason = LOCALIZE (L"Registration_NetworkFailure");
229
      *failure_reason = LOCALIZE (L"Registration_NetworkFailure");
229
      return (0); // unable to send HTTP query, return an error condition
230
      return (0); // unable to send HTTP query, return an error condition
230
   }
231
   }
231
 
232
 
232
   // read the reply
233
   // read the reply (10 seconds timeout)
-
 
234
   http_buffer[0] = 0;
233
   read_index = recv (s, http_buffer, sizeof (http_buffer), 0);
235
   read_index = recv_with_timeout (s, 10.0f, http_buffer, sizeof (http_buffer), 0);
234
   if (read_index < 1)
236
   if (read_index < 1)
235
   {
237
   {
236
      *failure_reason = LOCALIZE (L"Registration_NetworkFailure");
238
      *failure_reason = LOCALIZE (L"Registration_NetworkFailure");
237
      return (0); // empty or erroneous HTTP reply, return an error condition
239
      return (0); // empty or erroneous HTTP reply, return an error condition
238
   }
240
   }
Line 254... Line 256...
254
      if (strchr (data_start, '\r') != NULL) *strchr (data_start, '\r') = 0;
256
      if (strchr (data_start, '\r') != NULL) *strchr (data_start, '\r') = 0;
255
      if (strchr (data_start, '\n') != NULL) *strchr (data_start, '\n') = 0;
257
      if (strchr (data_start, '\n') != NULL) *strchr (data_start, '\n') = 0;
256
      if      (_stricmp (data_start, "WrongEmail") == 0)         *failure_reason = LOCALIZE (L"Registration_WrongEmail");
258
      if      (_stricmp (data_start, "WrongEmail") == 0)         *failure_reason = LOCALIZE (L"Registration_WrongEmail");
257
      else if (_stricmp (data_start, "UnknownEmail") == 0)       *failure_reason = LOCALIZE (L"Registration_UnknownEmail");
259
      else if (_stricmp (data_start, "UnknownEmail") == 0)       *failure_reason = LOCALIZE (L"Registration_UnknownEmail");
258
      else if (_stricmp (data_start, "TooManyActivations") == 0) *failure_reason = LOCALIZE (L"Registration_TooManyActivations");;
260
      else if (_stricmp (data_start, "TooManyActivations") == 0) *failure_reason = LOCALIZE (L"Registration_TooManyActivations");;
259
      return (0); // and return the candidate code we got
261
      return (0); // server returned an error, return an error condition
260
   }
262
   }
261
 
263
 
262
   *failure_reason = L"Unknown error"; // FIXME: this should never happen
264
   *failure_reason = LOCALIZE (L"Registration_NetworkFailure"); // this should never happen
263
   return (0); // server returned an error, return an error condition
265
   return (0); // server returned an error, return an error condition
-
 
266
}
-
 
267
 
-
 
268
 
-
 
269
static int recv_with_timeout (int socket_id, float timeout_in_seconds, char *outbuf, size_t outbuf_size, int flags)
-
 
270
{
-
 
271
   unsigned long nonblocking_mode;
-
 
272
   unsigned long msec_start;
-
 
273
   float timediff;
-
 
274
   int total_size;
-
 
275
   int recv_size;
-
 
276
 
-
 
277
   // make socket non blocking
-
 
278
   nonblocking_mode = 1;
-
 
279
   ioctlsocket (socket_id, FIONBIO, &nonblocking_mode);
-
 
280
 
-
 
281
   // loop endlessly, noting the time at which we start
-
 
282
   msec_start = GetTickCount ();
-
 
283
   total_size = 0;
-
 
284
   for (;;)
-
 
285
   {
-
 
286
      // see how much time elapsed since the last time we received data
-
 
287
      timediff = (GetTickCount () - msec_start) / 1000.0f;
-
 
288
      if (timediff > timeout_in_seconds)
-
 
289
         break; // if we've waited long enough, give up
-
 
290
 
-
 
291
      // see if we have something to read from the socket
-
 
292
      recv_size = recv (socket_id, &outbuf[total_size], outbuf_size - total_size, flags);
-
 
293
      if (recv_size == 0)
-
 
294
         break; // on TCP disconnection, give up too
-
 
295
      else if (recv_size < 0)
-
 
296
      {
-
 
297
         Sleep (100); // if nothing was received then we want to wait a little before trying again, 0.1 seconds
-
 
298
         continue;
-
 
299
      }
-
 
300
 
-
 
301
      total_size += recv_size; // increase the received bytes count
-
 
302
      outbuf[total_size] = 0; // terminate outbuf ourselves
-
 
303
      if (total_size == outbuf_size)
-
 
304
         break; // if the output buffer is full, give up
-
 
305
      msec_start = GetTickCount (); // and remember when we last received data (i.e. now)
-
 
306
   }
-
 
307
 
-
 
308
   return (total_size); // and return the number of bytes received
264
}
309
}