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.

141 lines
4.5 KiB

  1. // -*- C++ -*-
  2. // Module: Log4CPLUS
  3. // File: socket.h
  4. // Created: 4/2003
  5. // Author: Tad E. Smith
  6. //
  7. //
  8. // Copyright 2003-2013 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 <log4cplus/tstring.h>
  29. #include <log4cplus/helpers/socketbuffer.h>
  30. namespace log4cplus {
  31. namespace helpers {
  32. enum SocketState { ok,
  33. not_opened,
  34. bad_address,
  35. connection_failed,
  36. broken_pipe,
  37. invalid_access_mode,
  38. message_truncated,
  39. accept_interrupted
  40. };
  41. typedef std::ptrdiff_t SOCKET_TYPE;
  42. extern LOG4CPLUS_EXPORT SOCKET_TYPE const INVALID_SOCKET_VALUE;
  43. class LOG4CPLUS_EXPORT AbstractSocket {
  44. public:
  45. // ctor and dtor
  46. AbstractSocket();
  47. AbstractSocket(SOCKET_TYPE sock, SocketState state, int err);
  48. AbstractSocket(const AbstractSocket&);
  49. virtual ~AbstractSocket() = 0;
  50. // methods
  51. /// Close socket
  52. virtual void close();
  53. virtual bool isOpen() const;
  54. virtual void shutdown();
  55. AbstractSocket& operator=(const AbstractSocket& rhs);
  56. protected:
  57. // Methods
  58. virtual void copy(const AbstractSocket& rhs);
  59. // Data
  60. SOCKET_TYPE sock;
  61. SocketState state;
  62. int err;
  63. };
  64. /**
  65. * This class implements client sockets (also called just "sockets").
  66. * A socket is an endpoint for communication between two machines.
  67. */
  68. class LOG4CPLUS_EXPORT Socket : public AbstractSocket {
  69. public:
  70. // ctor and dtor
  71. Socket();
  72. Socket(SOCKET_TYPE sock, SocketState state, int err);
  73. Socket(const tstring& address, unsigned short port, bool udp = false);
  74. virtual ~Socket();
  75. // methods
  76. virtual bool read(SocketBuffer& buffer);
  77. virtual bool write(const SocketBuffer& buffer);
  78. virtual bool write(const std::string & buffer);
  79. };
  80. /**
  81. * This class implements server sockets. A server socket waits for
  82. * requests to come in over the network. It performs some operation
  83. * based on that request, and then possibly returns a result to the
  84. * requester.
  85. */
  86. class LOG4CPLUS_EXPORT ServerSocket : public AbstractSocket {
  87. public:
  88. // ctor and dtor
  89. ServerSocket(unsigned short port);
  90. virtual ~ServerSocket();
  91. Socket accept();
  92. void interruptAccept ();
  93. protected:
  94. std::ptrdiff_t interruptHandles[2];
  95. };
  96. LOG4CPLUS_EXPORT SOCKET_TYPE openSocket(unsigned short port, SocketState& state);
  97. LOG4CPLUS_EXPORT SOCKET_TYPE connectSocket(const log4cplus::tstring& hostn,
  98. unsigned short port, bool udp,
  99. SocketState& state);
  100. LOG4CPLUS_EXPORT SOCKET_TYPE acceptSocket(SOCKET_TYPE sock, SocketState& state);
  101. LOG4CPLUS_EXPORT int closeSocket(SOCKET_TYPE sock);
  102. LOG4CPLUS_EXPORT int shutdownSocket(SOCKET_TYPE sock);
  103. LOG4CPLUS_EXPORT long read(SOCKET_TYPE sock, SocketBuffer& buffer);
  104. LOG4CPLUS_EXPORT long write(SOCKET_TYPE sock,
  105. const SocketBuffer& buffer);
  106. LOG4CPLUS_EXPORT long write(SOCKET_TYPE sock,
  107. const std::string & buffer);
  108. LOG4CPLUS_EXPORT tstring getHostname (bool fqdn);
  109. LOG4CPLUS_EXPORT int setTCPNoDelay (SOCKET_TYPE, bool);
  110. } // end namespace helpers
  111. } // end namespace log4cplus
  112. #endif // LOG4CPLUS_HELPERS_SOCKET_HEADER_