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.

135 lines
4.2 KiB

  1. // -*- C++ -*-
  2. // Module: Log4CPLUS
  3. // File: socket.h
  4. // Created: 4/2003
  5. // Author: Tad E. Smith
  6. //
  7. //
  8. // Copyright 2003-2010 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. };
  40. typedef std::ptrdiff_t SOCKET_TYPE;
  41. extern LOG4CPLUS_EXPORT SOCKET_TYPE const INVALID_SOCKET_VALUE;
  42. class LOG4CPLUS_EXPORT AbstractSocket {
  43. public:
  44. // ctor and dtor
  45. AbstractSocket();
  46. AbstractSocket(SOCKET_TYPE sock, SocketState state, int err);
  47. AbstractSocket(const AbstractSocket&);
  48. virtual ~AbstractSocket() = 0;
  49. // methods
  50. /// Close socket
  51. virtual void close();
  52. virtual bool isOpen() const;
  53. AbstractSocket& operator=(const AbstractSocket& rhs);
  54. protected:
  55. // Methods
  56. virtual void copy(const AbstractSocket& rhs);
  57. // Data
  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, bool udp = false);
  72. virtual ~Socket();
  73. // methods
  74. virtual bool read(SocketBuffer& buffer);
  75. virtual bool write(const SocketBuffer& buffer);
  76. virtual bool write(const std::string & buffer);
  77. };
  78. /**
  79. * This class implements server sockets. A server socket waits for
  80. * requests to come in over the network. It performs some operation
  81. * based on that request, and then possibly returns a result to the
  82. * requester.
  83. */
  84. class LOG4CPLUS_EXPORT ServerSocket : public AbstractSocket {
  85. public:
  86. // ctor and dtor
  87. ServerSocket(unsigned short port);
  88. virtual ~ServerSocket();
  89. Socket accept();
  90. };
  91. LOG4CPLUS_EXPORT SOCKET_TYPE openSocket(unsigned short port, SocketState& state);
  92. LOG4CPLUS_EXPORT SOCKET_TYPE connectSocket(const log4cplus::tstring& hostn,
  93. unsigned short port, bool udp,
  94. SocketState& state);
  95. LOG4CPLUS_EXPORT SOCKET_TYPE acceptSocket(SOCKET_TYPE sock, SocketState& state);
  96. LOG4CPLUS_EXPORT int closeSocket(SOCKET_TYPE sock);
  97. LOG4CPLUS_EXPORT long read(SOCKET_TYPE sock, SocketBuffer& buffer);
  98. LOG4CPLUS_EXPORT long write(SOCKET_TYPE sock,
  99. const SocketBuffer& buffer);
  100. LOG4CPLUS_EXPORT long write(SOCKET_TYPE sock,
  101. const std::string & buffer);
  102. LOG4CPLUS_EXPORT tstring getHostname (bool fqdn);
  103. LOG4CPLUS_EXPORT int setTCPNoDelay (SOCKET_TYPE, bool);
  104. } // end namespace helpers
  105. } // end namespace log4cplus
  106. #endif // LOG4CPLUS_HELPERS_SOCKET_HEADER_