socket.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // -*- C++ -*-
  2. // Module: Log4CPLUS
  3. // File: socket.h
  4. // Created: 4/2003
  5. // Author: Tad E. Smith
  6. //
  7. //
  8. // Copyright 2003-2017 Tad E. Smith
  9. //
  10. // Licensed under the Apache License, Version 2.0 (the "License");
  11. // you may not use this file except in compliance with the License.
  12. // You may obtain a copy of the License at
  13. //
  14. // http://www.apache.org/licenses/LICENSE-2.0
  15. //
  16. // Unless required by applicable law or agreed to in writing, software
  17. // distributed under the License is distributed on an "AS IS" BASIS,
  18. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. // See the License for the specific language governing permissions and
  20. // limitations under the License.
  21. /** @file */
  22. #ifndef LOG4CPLUS_HELPERS_SOCKET_HEADER_
  23. #define LOG4CPLUS_HELPERS_SOCKET_HEADER_
  24. #include <log4cplus/config.hxx>
  25. #if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
  26. #pragma once
  27. #endif
  28. #include <array>
  29. #include <log4cplus/tstring.h>
  30. #include <log4cplus/helpers/socketbuffer.h>
  31. namespace log4cplus {
  32. namespace helpers {
  33. enum SocketState { ok,
  34. not_opened,
  35. bad_address,
  36. connection_failed,
  37. broken_pipe,
  38. invalid_access_mode,
  39. message_truncated,
  40. accept_interrupted
  41. };
  42. typedef std::ptrdiff_t SOCKET_TYPE;
  43. extern LOG4CPLUS_EXPORT SOCKET_TYPE const INVALID_SOCKET_VALUE;
  44. class LOG4CPLUS_EXPORT AbstractSocket {
  45. public:
  46. AbstractSocket();
  47. AbstractSocket(SOCKET_TYPE sock, SocketState state, int err);
  48. AbstractSocket(AbstractSocket const &) = delete;
  49. AbstractSocket(AbstractSocket &&) LOG4CPLUS_NOEXCEPT;
  50. virtual ~AbstractSocket() = 0;
  51. /// Close socket
  52. virtual void close();
  53. virtual bool isOpen() const;
  54. virtual void shutdown();
  55. AbstractSocket & operator = (AbstractSocket && rhs) LOG4CPLUS_NOEXCEPT;
  56. void swap (AbstractSocket &);
  57. protected:
  58. SOCKET_TYPE sock;
  59. SocketState state;
  60. int err;
  61. };
  62. /**
  63. * This class implements client sockets (also called just "sockets").
  64. * A socket is an endpoint for communication between two machines.
  65. */
  66. class LOG4CPLUS_EXPORT Socket : public AbstractSocket {
  67. public:
  68. // ctor and dtor
  69. Socket();
  70. Socket(SOCKET_TYPE sock, SocketState state, int err);
  71. Socket(const tstring& address, unsigned short port,
  72. bool udp = false, bool ipv6 = false);
  73. Socket(Socket &&) LOG4CPLUS_NOEXCEPT;
  74. virtual ~Socket();
  75. Socket & operator = (Socket &&) LOG4CPLUS_NOEXCEPT;
  76. // methods
  77. virtual bool read(SocketBuffer& buffer);
  78. virtual bool write(const SocketBuffer& buffer);
  79. virtual bool write(const std::string & buffer);
  80. virtual bool write(std::size_t bufferCount,
  81. SocketBuffer const * const * buffers);
  82. template <typename... Args>
  83. static bool write(Socket & socket, Args &&... args)
  84. {
  85. SocketBuffer const * const buffers[sizeof... (args)] {
  86. (&args)... };
  87. return socket.write (sizeof... (args), buffers);
  88. }
  89. };
  90. /**
  91. * This class implements server sockets. A server socket waits for
  92. * requests to come in over the network. It performs some operation
  93. * based on that request, and then possibly returns a result to the
  94. * requester.
  95. */
  96. class LOG4CPLUS_EXPORT ServerSocket : public AbstractSocket {
  97. public:
  98. ServerSocket(unsigned short port, bool udp = false,
  99. bool ipv6 = false, tstring const & host = tstring ());
  100. ServerSocket(ServerSocket &&) LOG4CPLUS_NOEXCEPT;
  101. virtual ~ServerSocket();
  102. ServerSocket & operator = (ServerSocket &&) LOG4CPLUS_NOEXCEPT;
  103. Socket accept();
  104. void interruptAccept ();
  105. void swap (ServerSocket &);
  106. protected:
  107. std::array<std::ptrdiff_t, 2> interruptHandles;
  108. };
  109. LOG4CPLUS_EXPORT SOCKET_TYPE openSocket(unsigned short port, bool udp,
  110. bool ipv6, SocketState& state);
  111. LOG4CPLUS_EXPORT SOCKET_TYPE openSocket(tstring const & host,
  112. unsigned short port, bool udp, bool ipv6, SocketState& state);
  113. LOG4CPLUS_EXPORT SOCKET_TYPE connectSocket(const log4cplus::tstring& hostn,
  114. unsigned short port, bool udp, bool ipv6, SocketState& state);
  115. LOG4CPLUS_EXPORT SOCKET_TYPE acceptSocket(SOCKET_TYPE sock, SocketState& state);
  116. LOG4CPLUS_EXPORT int closeSocket(SOCKET_TYPE sock);
  117. LOG4CPLUS_EXPORT int shutdownSocket(SOCKET_TYPE sock);
  118. LOG4CPLUS_EXPORT long read(SOCKET_TYPE sock, SocketBuffer& buffer);
  119. LOG4CPLUS_EXPORT long write(SOCKET_TYPE sock,
  120. const SocketBuffer& buffer);
  121. LOG4CPLUS_EXPORT long write(SOCKET_TYPE sock, std::size_t bufferCount,
  122. SocketBuffer const * const * buffers);
  123. LOG4CPLUS_EXPORT long write(SOCKET_TYPE sock,
  124. const std::string & buffer);
  125. LOG4CPLUS_EXPORT tstring getHostname (bool fqdn);
  126. LOG4CPLUS_EXPORT int setTCPNoDelay (SOCKET_TYPE, bool);
  127. } // end namespace helpers
  128. } // end namespace log4cplus
  129. #endif // LOG4CPLUS_HELPERS_SOCKET_HEADER_