Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
1.7 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 1999, Microsoft Corp. All rights reserved.
  4. //
  5. // FILE
  6. //
  7. // sockevt.cpp
  8. //
  9. // SYNOPSIS
  10. //
  11. // Defines the class SocketEvent.
  12. //
  13. // MODIFICATION HISTORY
  14. //
  15. // 02/12/1999 Original version.
  16. //
  17. ///////////////////////////////////////////////////////////////////////////////
  18. #include <radcommon.h>
  19. #include <sockevt.h>
  20. DWORD SocketEvent::initialize() throw ()
  21. {
  22. // Create a socket.
  23. s = socket(AF_INET, SOCK_DGRAM, 0);
  24. if (s == INVALID_SOCKET)
  25. {
  26. return WSAGetLastError();
  27. }
  28. int error;
  29. // Bind to an arbitrary port on the loopback interface.
  30. sin.sin_family = AF_INET;
  31. sin.sin_port = 0;
  32. sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  33. error = bind(s, (sockaddr*)&sin, sizeof(sin));
  34. if (!error)
  35. {
  36. // Find out which port we bound to.
  37. int namelen = sizeof(sin);
  38. error = getsockname(s, (sockaddr*)&sin, &namelen);
  39. if (!error)
  40. {
  41. // Set the socket to non-blocking.
  42. u_long argp = 1;
  43. error = ioctlsocket(s, FIONBIO, &argp);
  44. }
  45. }
  46. // Clean-up if anything went wrong.
  47. if (error)
  48. {
  49. closesocket(s);
  50. s = INVALID_SOCKET;
  51. return WSAGetLastError();
  52. }
  53. return NO_ERROR;
  54. }
  55. void SocketEvent::finalize() throw ()
  56. {
  57. if (s != INVALID_SOCKET)
  58. {
  59. closesocket(s);
  60. s = INVALID_SOCKET;
  61. }
  62. }
  63. DWORD SocketEvent::set() throw ()
  64. {
  65. if (sendto(s, NULL, 0, 0, (sockaddr*)&sin, sizeof(sin)))
  66. {
  67. return WSAGetLastError();
  68. }
  69. return NO_ERROR;
  70. }
  71. void SocketEvent::reset() throw ()
  72. {
  73. // Loop until we've read all the zero-byte sends.
  74. char buf[1];
  75. while (!recv(s, buf, 1, 0)) {}
  76. }