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.

164 lines
5.4 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: acestl/network/socket_functions.hpp
  3. *
  4. * Purpose: Helper functions for ACE_SOCK (and derived) classes
  5. *
  6. * Created: 28th November 2004
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2004-2009, Matthew Wilson and Synesis Software
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright notice, this
  18. * list of conditions and the following disclaimer.
  19. * - Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
  23. * any contributors may be used to endorse or promote products derived from
  24. * this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  30. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. * ////////////////////////////////////////////////////////////////////// */
  39. /** \file acestl/network/socket_functions.hpp
  40. *
  41. * \brief [C++ only; requires ACE] Socket helper functions
  42. * (\ref group__library__network "Network" Library).
  43. */
  44. #ifndef ACESTL_INCL_ACESTL_NETWORK_HPP_SOCKET_FUNCTIONS
  45. #define ACESTL_INCL_ACESTL_NETWORK_HPP_SOCKET_FUNCTIONS
  46. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  47. # define ACESTL_VER_ACESTL_NETWORK_HPP_SOCKET_FUNCTIONS_MAJOR 2
  48. # define ACESTL_VER_ACESTL_NETWORK_HPP_SOCKET_FUNCTIONS_MINOR 0
  49. # define ACESTL_VER_ACESTL_NETWORK_HPP_SOCKET_FUNCTIONS_REVISION 2
  50. # define ACESTL_VER_ACESTL_NETWORK_HPP_SOCKET_FUNCTIONS_EDIT 24
  51. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  52. /* /////////////////////////////////////////////////////////////////////////
  53. * Includes
  54. */
  55. #ifndef ACESTL_INCL_ACESTL_HPP_ACESTL
  56. # include <acestl/acestl.hpp>
  57. #endif /* !ACESTL_INCL_ACESTL_HPP_ACESTL */
  58. #ifndef STLSOFT_INCL_ACE_H_SOCK
  59. # define STLSOFT_INCL_ACE_H_SOCK
  60. # include <ace/SOCK.h>
  61. #endif /* !STLSOFT_INCL_ACE_H_SOCK */
  62. #ifndef STLSOFT_INCL_ACE_H_REACTOR
  63. # define STLSOFT_INCL_ACE_H_REACTOR
  64. # include <ace/Reactor.h>
  65. #endif /* !STLSOFT_INCL_ACE_H_REACTOR */
  66. /* /////////////////////////////////////////////////////////////////////////
  67. * Namespace
  68. */
  69. #ifndef _ACESTL_NO_NAMESPACE
  70. # if defined(_STLSOFT_NO_NAMESPACE) || \
  71. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  72. /* There is no stlsoft namespace, so must define ::acestl */
  73. namespace acestl
  74. {
  75. # else
  76. /* Define stlsoft::acestl_project */
  77. namespace stlsoft
  78. {
  79. namespace acestl_project
  80. {
  81. # endif /* _STLSOFT_NO_NAMESPACE */
  82. #endif /* !_ACESTL_NO_NAMESPACE */
  83. /* /////////////////////////////////////////////////////////////////////////
  84. * Functions
  85. */
  86. /** \brief Closes a socket and deregisters it from the reactor
  87. *
  88. * \ingroup group__library__network
  89. *
  90. * \param sk Reference to the socket to close
  91. * \param reactor Reactor from which the socket will be deregistered. May be
  92. * NULL.
  93. *
  94. * \return true if the socket was previously closed, false otherwise. There
  95. * is no error return
  96. *
  97. * Usage is simple. Just specify the socket to close and, optionally, the
  98. * reactor with which it is registered.
  99. *
  100. \code
  101. // close and deregister m_upstreamPeer and m_downstreamPeer (both ACE_SOCK_Stream)
  102. acestl::close_and_deregister(m_peer, reactor());
  103. \endcode
  104. */
  105. inline as_bool_t close_and_deregister(ACE_SOCK &sk, ACE_Reactor *reactor)
  106. {
  107. if(ACE_INVALID_HANDLE == sk.get_handle())
  108. {
  109. return false;
  110. }
  111. if(NULL != reactor)
  112. {
  113. const ACE_Reactor_Mask close_mask = ACE_Event_Handler::ALL_EVENTS_MASK
  114. | ACE_Event_Handler::DONT_CALL;
  115. reactor->remove_handler(sk.get_handle(), close_mask);
  116. }
  117. sk.close();
  118. return true;
  119. }
  120. ////////////////////////////////////////////////////////////////////////////
  121. // Unit-testing
  122. #ifdef STLSOFT_UNITTEST
  123. # include "./unittest/socket_functions_unittest_.h"
  124. #endif /* STLSOFT_UNITTEST */
  125. /* ////////////////////////////////////////////////////////////////////// */
  126. #ifndef _ACESTL_NO_NAMESPACE
  127. # if defined(_STLSOFT_NO_NAMESPACE) || \
  128. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  129. } // namespace acestl
  130. # else
  131. } // namespace acestl_project
  132. } // namespace stlsoft
  133. # endif /* _STLSOFT_NO_NAMESPACE */
  134. #endif /* !_ACESTL_NO_NAMESPACE */
  135. /* ////////////////////////////////////////////////////////////////////// */
  136. #endif /* ACESTL_INCL_ACESTL_NETWORK_HPP_SOCKET_FUNCTIONS */
  137. /* ///////////////////////////// end of file //////////////////////////// */