~ubuntu-branches/ubuntu/trusty/libhdhomerun/trusty

« back to all changes in this revision

Viewing changes to hdhomerun_sock_windows.c

  • Committer: Package Import Robot
  • Author(s): Francois Marier
  • Date: 2012-04-11 12:15:15 UTC
  • mfrom: (1.1.15) (0.1.13 sid)
  • Revision ID: package-import@ubuntu.com-20120411121515-20saju5jbwqeqvgc
Tags: 20120405-1
* New upstream release
* Improve orig-changelog script to remove the footer

* Bump Standards-Version up to 3.9.3
* Bump debhelper compatibility to 9

Show diffs side-by-side

added added

removed removed

Lines of Context:
269
269
 
270
270
bool_t hdhomerun_sock_connect(hdhomerun_sock_t sock, uint32_t remote_addr, uint16_t remote_port, uint64_t timeout)
271
271
{
272
 
        WSAEVENT wsa_event = WSACreateEvent();
273
 
        if (wsa_event == WSA_INVALID_EVENT) {
274
 
                return FALSE;
275
 
        }
276
 
 
277
 
        if (WSAEventSelect(sock, wsa_event, FD_CONNECT) == SOCKET_ERROR) {
278
 
                WSACloseEvent(wsa_event);
279
 
                return FALSE;
280
 
        }
281
 
 
282
272
        /* Connect (non-blocking). */
283
273
        struct sockaddr_in sock_addr;
284
274
        memset(&sock_addr, 0, sizeof(sock_addr));
288
278
 
289
279
        if (connect(sock, (struct sockaddr *)&sock_addr, sizeof(sock_addr)) != 0) {
290
280
                if (WSAGetLastError() != WSAEWOULDBLOCK) {
291
 
                        WSACloseEvent(wsa_event);
292
281
                        return FALSE;
293
282
                }
294
283
        }
295
284
 
296
285
        /* Wait for connect to complete (both success and failure will signal). */
 
286
        WSAEVENT wsa_event = WSACreateEvent();
 
287
        if (wsa_event == WSA_INVALID_EVENT) {
 
288
                return FALSE;
 
289
        }
 
290
 
 
291
        if (WSAEventSelect(sock, wsa_event, FD_WRITE | FD_CLOSE) == SOCKET_ERROR) {
 
292
                WSACloseEvent(wsa_event);
 
293
                return FALSE;
 
294
        }
 
295
 
297
296
        DWORD ret = WaitForSingleObjectEx(wsa_event, (DWORD)timeout, FALSE);
298
297
        WSACloseEvent(wsa_event);
299
 
 
300
298
        if (ret != WAIT_OBJECT_0) {
301
299
                return FALSE;
302
300
        }
303
301
 
304
302
        /* Detect success/failure. */
305
 
        int sockaddr_size = sizeof(sock_addr);
306
 
        if (getpeername(sock, (struct sockaddr *)&sock_addr, &sockaddr_size) != 0) {
 
303
        wsa_event = WSACreateEvent();
 
304
        if (wsa_event == WSA_INVALID_EVENT) {
 
305
                return FALSE;
 
306
        }
 
307
 
 
308
        if (WSAEventSelect(sock, wsa_event, FD_CLOSE) == SOCKET_ERROR) {
 
309
                WSACloseEvent(wsa_event);
 
310
                return FALSE;
 
311
        }
 
312
 
 
313
        ret = WaitForSingleObjectEx(wsa_event, 0, FALSE);
 
314
        WSACloseEvent(wsa_event);
 
315
        if (ret == WAIT_OBJECT_0) {
307
316
                return FALSE;
308
317
        }
309
318
 
344
353
 
345
354
        while (1) {
346
355
                int ret = send(sock, (char *)ptr, (int)length, 0);
347
 
                if (ret >= (int)length) {
348
 
                        return TRUE;
 
356
                if (ret <= 0) {
 
357
                        if (WSAGetLastError() != WSAEWOULDBLOCK) {
 
358
                                return FALSE;
 
359
                        }
 
360
                        if (!hdhomerun_sock_wait_for_event(sock, FD_WRITE | FD_CLOSE, stop_time)) {
 
361
                                return FALSE;
 
362
                        }
 
363
                        continue;
349
364
                }
350
365
 
351
 
                if (ret > 0) {
 
366
                if (ret < (int)length) {
352
367
                        ptr += ret;
353
368
                        length -= ret;
354
 
                }
355
 
 
356
 
                if (WSAGetLastError() != WSAEWOULDBLOCK) {
357
 
                        return FALSE;
358
 
                }
359
 
 
360
 
                if (!hdhomerun_sock_wait_for_event(sock, FD_WRITE | FD_CLOSE, stop_time)) {
361
 
                        return FALSE;
362
 
                }
 
369
                        continue;
 
370
                }
 
371
 
 
372
                return TRUE;
363
373
        }
364
374
}
365
375
 
376
386
                sock_addr.sin_port = htons(remote_port);
377
387
 
378
388
                int ret = sendto(sock, (char *)ptr, (int)length, 0, (struct sockaddr *)&sock_addr, sizeof(sock_addr));
379
 
                if (ret >= (int)length) {
380
 
                        return TRUE;
 
389
                if (ret <= 0) {
 
390
                        if (WSAGetLastError() != WSAEWOULDBLOCK) {
 
391
                                return FALSE;
 
392
                        }
 
393
                        if (!hdhomerun_sock_wait_for_event(sock, FD_WRITE | FD_CLOSE, stop_time)) {
 
394
                                return FALSE;
 
395
                        }
 
396
                        continue;
381
397
                }
382
398
 
383
 
                if (ret > 0) {
 
399
                if (ret < (int)length) {
384
400
                        ptr += ret;
385
401
                        length -= ret;
386
 
                }
387
 
 
388
 
                if (WSAGetLastError() != WSAEWOULDBLOCK) {
389
 
                        return FALSE;
390
 
                }
391
 
 
392
 
                if (!hdhomerun_sock_wait_for_event(sock, FD_WRITE | FD_CLOSE, stop_time)) {
393
 
                        return FALSE;
394
 
                }
 
402
                        continue;
 
403
                }
 
404
 
 
405
                return TRUE;
395
406
        }
396
407
}
397
408
 
401
412
 
402
413
        while (1) {
403
414
                int ret = recv(sock, (char *)data, (int)(*length), 0);
404
 
                if (ret > 0) {
405
 
                        *length = ret;
406
 
                        return TRUE;
407
 
                }
408
 
 
409
 
                if (WSAGetLastError() != WSAEWOULDBLOCK) {
410
 
                        return FALSE;
411
 
                }
412
 
 
413
 
                if (!hdhomerun_sock_wait_for_event(sock, FD_READ | FD_CLOSE, stop_time)) {
414
 
                        return FALSE;
415
 
                }
 
415
                if (ret < 0) {
 
416
                        if (WSAGetLastError() != WSAEWOULDBLOCK) {
 
417
                                return FALSE;
 
418
                        }
 
419
                        if (!hdhomerun_sock_wait_for_event(sock, FD_READ | FD_CLOSE, stop_time)) {
 
420
                                return FALSE;
 
421
                        }
 
422
                        continue;
 
423
                }
 
424
 
 
425
                if (ret == 0) {
 
426
                        return FALSE;
 
427
                }
 
428
 
 
429
                *length = ret;
 
430
                return TRUE;
416
431
        }
417
432
}
418
433
 
426
441
                int sockaddr_size = sizeof(sock_addr);
427
442
 
428
443
                int ret = recvfrom(sock, (char *)data, (int)(*length), 0, (struct sockaddr *)&sock_addr, &sockaddr_size);
429
 
                if (ret > 0) {
430
 
                        *remote_addr = ntohl(sock_addr.sin_addr.s_addr);
431
 
                        *remote_port = ntohs(sock_addr.sin_port);
432
 
                        *length = ret;
433
 
                        return TRUE;
434
 
                }
435
 
 
436
 
                if (WSAGetLastError() != WSAEWOULDBLOCK) {
437
 
                        return FALSE;
438
 
                }
439
 
 
440
 
                if (!hdhomerun_sock_wait_for_event(sock, FD_READ | FD_CLOSE, stop_time)) {
441
 
                        return FALSE;
442
 
                }
 
444
                if (ret < 0) {
 
445
                        if (WSAGetLastError() != WSAEWOULDBLOCK) {
 
446
                                return FALSE;
 
447
                        }
 
448
                        if (!hdhomerun_sock_wait_for_event(sock, FD_READ | FD_CLOSE, stop_time)) {
 
449
                                return FALSE;
 
450
                        }
 
451
                        continue;
 
452
                }
 
453
 
 
454
                if (ret == 0) {
 
455
                        return FALSE;
 
456
                }
 
457
 
 
458
                *remote_addr = ntohl(sock_addr.sin_addr.s_addr);
 
459
                *remote_port = ntohs(sock_addr.sin_port);
 
460
                *length = ret;
 
461
                return TRUE;
443
462
        }
444
463
}