udp_socket.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #ifndef __UDP_SOCKET_H
  15. #define __UDP_SOCKET_H 1
  16. #ifndef ASIO_HPP
  17. #error "asio.hpp must be included before including this, see asiolink.h as to why"
  18. #endif
  19. #include <cstddef>
  20. #include <asiolink/io_socket.h>
  21. #include <asiolink/io_service.h>
  22. namespace asiolink {
  23. /// \brief The \c UDPSocket class is a concrete derived class of
  24. /// \c IOSocket that represents a UDP socket.
  25. ///
  26. /// Other notes about \c TCPSocket applies to this class, too.
  27. class UDPSocket : public IOSocket {
  28. private:
  29. /// \brief Class is non-copyable
  30. UDPSocket(const UDPSocket&);
  31. UDPSocket& operator=(const UDPSocket&);
  32. public:
  33. enum {
  34. MAX_SIZE = 4096 // Send and receive size
  35. };
  36. /// \brief Constructor from an ASIO UDP socket.
  37. ///
  38. /// \param socket The ASIO representation of the UDP socket.
  39. UDPSocket(asio::ip::udp::socket& socket) :
  40. socket_ptr_(NULL), socket_(socket)
  41. {}
  42. /// \brief Constructor
  43. ///
  44. /// Used when the UDPSocket is being asked to manage its own internal
  45. /// socket.
  46. UDPSocket(IOService& service);
  47. /// \brief Destructor
  48. virtual ~UDPSocket();
  49. virtual int getNative() const { return (socket_.native()); }
  50. virtual int getProtocol() const { return (IPPROTO_UDP); }
  51. /// \brief Open Socket
  52. ///
  53. /// Opens the UDP socket. In the model for transport-layer agnostic I/O,
  54. /// an "open" operation includes a connection to the remote end (which
  55. /// may take time). This does not happen for UDP, so the method returns
  56. /// "false" to indicate that the operation completed synchronously.
  57. ///
  58. /// \param endpoint Endpoint to which the socket will connect to.
  59. /// \param callback Unused.
  60. ///
  61. /// \return false to indicate that the "operation" completed synchronously.
  62. virtual bool open(const IOEndpoint* endpoint, IOCompletionCallback&);
  63. /// \brief Send Asynchronously
  64. ///
  65. /// This corresponds to async_send_to() for UDP sockets and async_send()
  66. /// for TCP. In both cases an endpoint argument is supplied indicating the
  67. /// target of the send - this is ignored for TCP.
  68. ///
  69. /// \param data Data to send
  70. /// \param length Length of data to send
  71. /// \param endpoint Target of the send
  72. /// \param callback Callback object.
  73. virtual void asyncSend(const void* data, size_t length,
  74. const IOEndpoint* endpoint, IOCompletionCallback& callback);
  75. /// \brief Receive Asynchronously
  76. ///
  77. /// This correstponds to async_receive_from() for UDP sockets and
  78. /// async_receive() for TCP. In both cases, an endpoint argument is
  79. /// supplied to receive the source of the communication. For TCP it will
  80. /// be filled in with details of the connection.
  81. ///
  82. /// \param data Buffer to receive incoming message
  83. /// \param length Length of the data buffer
  84. /// \param cumulative Amount of data that should already be in the buffer.
  85. /// (This is ignored - every UPD receive fills the buffer from the start.)
  86. /// \param endpoint Source of the communication
  87. /// \param callback Callback object
  88. virtual void asyncReceive(void* data, size_t length, size_t cumulative,
  89. IOEndpoint* endpoint, IOCompletionCallback& callback);
  90. /// \brief Checks if the data received is complete.
  91. ///
  92. /// As all the data is received in one I/O, so this is, this is effectively
  93. /// a no-op (although it does update the amount of data received).
  94. ///
  95. /// \param data Data buffer containing data to date. (This is ignored
  96. /// for UDP receives.)
  97. /// \param length Amount of data received in last asynchronous I/O
  98. /// \param cumulative On input, amount of data received before the last
  99. /// I/O. On output, the total amount of data received to date.
  100. ///
  101. /// \return true if the receive is complete, false if another receive is
  102. /// needed.
  103. virtual bool receiveComplete(void*, size_t length, size_t& cumulative) {
  104. cumulative = length;
  105. return (true);
  106. }
  107. /// \brief Cancel I/O On Socket
  108. virtual void cancel();
  109. /// \brief Close socket
  110. virtual void close();
  111. private:
  112. // Two variables to hold the socket - a socket and a pointer to it. This
  113. // handles the case where a socket is passed to the UDPSocket on
  114. // construction, or where it is asked to manage its own socket.
  115. asio::ip::udp::socket* socket_ptr_; ///< Pointer to the socket
  116. asio::ip::udp::socket& socket_; ///< Socket
  117. };
  118. } // namespace asiolink
  119. #endif // __UDP_SOCKET_H