Leaked source code of windows server 2003
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.

173 lines
4.4 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2000, Microsoft Corp. All rights reserved.
  4. //
  5. // FILE
  6. //
  7. // udpsock.h
  8. //
  9. // SYNOPSIS
  10. //
  11. // Declares the classes PacketReceiver and UDPSocket.
  12. //
  13. // MODIFICATION HISTORY
  14. //
  15. // 02/05/2000 Original version.
  16. //
  17. ///////////////////////////////////////////////////////////////////////////////
  18. #ifndef UDPSOCK_H
  19. #define UDPSOCK_H
  20. #if _MSC_VER >= 1000
  21. #pragma once
  22. #endif
  23. #include <winsock2.h>
  24. class UDPSocket;
  25. ///////////////////////////////////////////////////////////////////////////////
  26. //
  27. // CLASS
  28. //
  29. // InternetAddress
  30. //
  31. // DESCRIPTION
  32. //
  33. // Simple wrapper around a SOCKADDR_IN
  34. //
  35. ///////////////////////////////////////////////////////////////////////////////
  36. struct InternetAddress : public SOCKADDR_IN
  37. {
  38. InternetAddress(ULONG address = INADDR_ANY, USHORT port = 0)
  39. {
  40. sin_family = AF_INET;
  41. sin_port = port;
  42. sin_addr.s_addr = address;
  43. memset(sin_zero, 0, sizeof(sin_zero));
  44. }
  45. InternetAddress(const SOCKADDR_IN& sin) throw ()
  46. { *this = sin; }
  47. const SOCKADDR_IN& operator=(const SOCKADDR_IN& sin) throw ()
  48. {
  49. sin_port = sin.sin_port;
  50. sin_addr.s_addr = sin.sin_addr.s_addr;
  51. return *this;
  52. }
  53. USHORT port() const throw ()
  54. { return sin_port; }
  55. void port(USHORT newPort) throw ()
  56. { sin_port = newPort; }
  57. ULONG address() const throw ()
  58. { return sin_addr.s_addr; }
  59. void address(ULONG newAddress) throw ()
  60. { sin_addr.s_addr = newAddress; }
  61. bool operator==(const SOCKADDR_IN& sin) const throw ()
  62. {
  63. return sin_port == sin.sin_port &&
  64. sin_addr.s_addr == sin.sin_addr.s_addr;
  65. }
  66. operator const SOCKADDR*() const throw ()
  67. { return (const SOCKADDR*)this; }
  68. };
  69. ///////////////////////////////////////////////////////////////////////////////
  70. //
  71. // CLASS
  72. //
  73. // PacketReceiver
  74. //
  75. // DESCRIPTION
  76. //
  77. // Implemented by classes that receive packets from a UDPSocket.
  78. //
  79. ///////////////////////////////////////////////////////////////////////////////
  80. class PacketReceiver
  81. {
  82. public:
  83. virtual void onReceive(
  84. UDPSocket& socket,
  85. ULONG_PTR key,
  86. const SOCKADDR_IN& remoteAddress,
  87. BYTE* buffer,
  88. ULONG bufferLength
  89. ) throw () = 0;
  90. virtual void onReceiveError(
  91. UDPSocket& socket,
  92. ULONG_PTR key,
  93. ULONG errorCode
  94. ) throw () = 0;
  95. };
  96. ///////////////////////////////////////////////////////////////////////////////
  97. //
  98. // CLASS
  99. //
  100. // UDPSocket
  101. //
  102. // DESCRIPTION
  103. //
  104. // Listens and sends on a UDP socket.
  105. //
  106. ///////////////////////////////////////////////////////////////////////////////
  107. class UDPSocket : public IAS_CALLBACK
  108. {
  109. public:
  110. UDPSocket() throw ();
  111. ~UDPSocket() throw ();
  112. const InternetAddress& getLocalAddress() const throw ()
  113. { return localAddress; }
  114. bool isOpen() const throw ()
  115. { return idle != NULL; }
  116. BOOL open(
  117. PacketReceiver* sink,
  118. ULONG_PTR recvKey = 0,
  119. const SOCKADDR_IN* address = NULL
  120. ) throw ();
  121. void close() throw ();
  122. BOOL send(
  123. const SOCKADDR_IN& to,
  124. const BYTE* buffer,
  125. ULONG bufferLength
  126. ) throw ();
  127. bool operator==(const UDPSocket& socket) const throw ()
  128. { return localAddress == socket.localAddress; }
  129. protected:
  130. // Create a new thread to listen on the port.
  131. BOOL createReceiveThread() throw ();
  132. // Receive data from the port.
  133. bool receive() throw ();
  134. // Receive thread start routine.
  135. static void startRoutine(PIAS_CALLBACK This) throw ();
  136. private:
  137. PacketReceiver* receiver; // Our client.
  138. ULONG_PTR key; // Our client's key.
  139. SOCKET sock; // The UDP socket.
  140. InternetAddress localAddress; // The address the socket is bound to.
  141. BOOL closing; // Signals the receiver that we're closing.
  142. HANDLE idle; // Signals that the receiver has exited.
  143. BYTE buffer[4096]; // Receive buffer.
  144. // Not implemented.
  145. UDPSocket(const UDPSocket&);
  146. UDPSocket& operator=(const UDPSocket&);
  147. };
  148. #endif // UDPSOCK_H