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.

140 lines
4.4 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. AbstractSocket& operator=(const AbstractSocket& rhs);
  55. protected:
  56. // Methods
  57. virtual void copy(const AbstractSocket& rhs);
  58. // Data
  59. SOCKET_TYPE sock;
  60. SocketState state;
  61. int err;
  62. };
  63. /**
  64. * This class implements client sockets (also called just "sockets").
  65. * A socket is an endpoint for communication between two machines.
  66. */
  67. class LOG4CPLUS_EXPORT Socket : public AbstractSocket {
  68. public:
  69. // ctor and dtor
  70. Socket();
  71. Socket(SOCKET_TYPE sock, SocketState state, int err);
  72. Socket(const tstring& address, unsigned short port, bool udp = false);
  73. virtual ~Socket();
  74. // methods
  75. virtual bool read(SocketBuffer& buffer);
  76. virtual bool write(const SocketBuffer& buffer);
  77. virtual bool write(const std::string & buffer);
  78. };
  79. /**
  80. * This class implements server sockets. A server socket waits for
  81. * requests to come in over the network. It performs some operation
  82. * based on that request, and then possibly returns a result to the
  83. * requester.
  84. */
  85. class LOG4CPLUS_EXPORT ServerSocket : public AbstractSocket {
  86. public:
  87. // ctor and dtor
  88. ServerSocket(unsigned short port);
  89. virtual ~ServerSocket();
  90. Socket accept();
  91. void interruptAccept ();
  92. protected:
  93. std::ptrdiff_t interruptHandles[2];
  94. };
  95. LOG4CPLUS_EXPORT SOCKET_TYPE openSocket(unsigned short port, SocketState& state);
  96. LOG4CPLUS_EXPORT SOCKET_TYPE connectSocket(const log4cplus::tstring& hostn,
  97. unsigned short port, bool udp,
  98. SocketState& state);
  99. LOG4CPLUS_EXPORT SOCKET_TYPE acceptSocket(SOCKET_TYPE sock, SocketState& state);
  100. LOG4CPLUS_EXPORT int closeSocket(SOCKET_TYPE sock);
  101. LOG4CPLUS_EXPORT long read(SOCKET_TYPE sock, SocketBuffer& buffer);
  102. LOG4CPLUS_EXPORT long write(SOCKET_TYPE sock,
  103. const SocketBuffer& buffer);
  104. LOG4CPLUS_EXPORT long write(SOCKET_TYPE sock,
  105. const std::string & buffer);
  106. LOG4CPLUS_EXPORT tstring getHostname (bool fqdn);
  107. LOG4CPLUS_EXPORT int setTCPNoDelay (SOCKET_TYPE, bool);
  108. } // end namespace helpers
  109. } // end namespace log4cplus
  110. #endif // LOG4CPLUS_HELPERS_SOCKET_HEADER_