iosocket.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright (C) 2010 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. // $Id$
  15. #ifndef __IOSOCKET_H
  16. #define __IOSOCKET_H 1
  17. // IMPORTANT NOTE: only very few ASIO headers files can be included in
  18. // this file. In particular, asio.hpp should never be included here.
  19. // See the description of the namespace below.
  20. #include <unistd.h> // for some network system calls
  21. #include <functional>
  22. #include <string>
  23. #include <exceptions/exceptions.h>
  24. namespace asiolink {
  25. /// \brief The \c IOSocket class is an abstract base class to represent
  26. /// various types of network sockets.
  27. ///
  28. /// This class is a wrapper for the ASIO socket classes such as
  29. /// \c ip::tcp::socket and \c ip::udp::socket.
  30. ///
  31. /// Derived class implementations are completely hidden within the
  32. /// implementation. User applications only get access to concrete
  33. /// \c IOSocket objects via the abstract interfaces.
  34. ///
  35. /// We may revisit this decision when we generalize the wrapper and more
  36. /// modules use it. Also, at that point we may define a separate (visible)
  37. /// derived class for testing purposes rather than providing factory methods
  38. /// (i.e., getDummy variants below).
  39. class IOSocket {
  40. ///
  41. /// \name Constructors and Destructor
  42. ///
  43. /// Note: The copy constructor and the assignment operator are
  44. /// intentionally defined as private, making this class non-copyable.
  45. //@{
  46. private:
  47. IOSocket(const IOSocket& source);
  48. IOSocket& operator=(const IOSocket& source);
  49. protected:
  50. /// \brief The default constructor.
  51. ///
  52. /// This is intentionally defined as \c protected as this base class
  53. /// should never be instantiated (except as part of a derived class).
  54. IOSocket() {}
  55. public:
  56. /// The destructor.
  57. virtual ~IOSocket() {}
  58. //@}
  59. /// \brief Return the "native" representation of the socket.
  60. ///
  61. /// In practice, this is the file descriptor of the socket for
  62. /// UNIX-like systems so the current implementation simply uses
  63. /// \c int as the type of the return value.
  64. /// We may have to need revisit this decision later.
  65. ///
  66. /// In general, the application should avoid using this method;
  67. /// it essentially discloses an implementation specific "handle" that
  68. /// can change the internal state of the socket (consider the
  69. /// application closes it, for example).
  70. /// But we sometimes need to perform very low-level operations that
  71. /// requires the native representation. Passing the file descriptor
  72. /// to a different process is one example.
  73. /// This method is provided as a necessary evil for such limited purposes.
  74. ///
  75. /// This method never throws an exception.
  76. ///
  77. /// \return The native representation of the socket. This is the socket
  78. /// file descriptor for UNIX-like systems.
  79. virtual int getNative() const = 0;
  80. /// \brief Return the transport protocol of the socket.
  81. ///
  82. /// Currently, it returns \c IPPROTO_UDP for UDP sockets, and
  83. /// \c IPPROTO_TCP for TCP sockets.
  84. ///
  85. /// This method never throws an exception.
  86. ///
  87. /// \return IPPROTO_UDP for UDP sockets
  88. /// \return IPPROTO_TCP for TCP sockets
  89. virtual int getProtocol() const = 0;
  90. /// \brief Return a non-usable "dummy" UDP socket for testing.
  91. ///
  92. /// This is a class method that returns a "mock" of UDP socket.
  93. /// This is not associated with any actual socket, and its only
  94. /// responsibility is to return \c IPPROTO_UDP from \c getProtocol().
  95. /// The only feasible usage of this socket is for testing so that
  96. /// the test code can prepare some "UDP data" even without opening any
  97. /// actual socket.
  98. ///
  99. /// This method never throws an exception.
  100. ///
  101. /// \return A reference to an \c IOSocket object whose \c getProtocol()
  102. /// returns \c IPPROTO_UDP.
  103. static IOSocket& getDummyUDPSocket();
  104. /// \brief Return a non-usable "dummy" TCP socket for testing.
  105. ///
  106. /// See \c getDummyUDPSocket(). This method is its TCP version.
  107. ///
  108. /// \return A reference to an \c IOSocket object whose \c getProtocol()
  109. /// returns \c IPPROTO_TCP.
  110. static IOSocket& getDummyTCPSocket();
  111. };
  112. } // asiolink
  113. #endif // __IOSOCKET_H
  114. // Local Variables:
  115. // mode: c++
  116. // End: