log4cplus  2.1.0
helpers/socket.h
Go to the documentation of this file.
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 
24 #ifndef LOG4CPLUS_HELPERS_SOCKET_HEADER_
25 #define LOG4CPLUS_HELPERS_SOCKET_HEADER_
26 
27 #include <log4cplus/config.hxx>
28 
29 #if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
30 #pragma once
31 #endif
32 
33 #include <array>
34 
35 #include <log4cplus/tstring.h>
37 
38 
39 namespace log4cplus {
40  namespace helpers {
41 
42  enum SocketState { ok,
50  };
51 
52  typedef std::ptrdiff_t SOCKET_TYPE;
53 
55 
57  public:
59  AbstractSocket(SOCKET_TYPE sock, SocketState state, int err);
60  AbstractSocket(AbstractSocket const &) = delete;
62  virtual ~AbstractSocket() = 0;
63 
65  virtual void close();
66  virtual bool isOpen() const;
67  virtual void shutdown();
69 
70  void swap (AbstractSocket &);
71 
72  protected:
74  SocketState state;
75  int err;
76  };
77 
78 
79 
85  public:
86  // ctor and dtor
87  Socket();
88  Socket(SOCKET_TYPE sock, SocketState state, int err);
89  Socket(const tstring& address, unsigned short port,
90  bool udp = false, bool ipv6 = false);
92  virtual ~Socket();
93 
94  Socket & operator = (Socket &&) LOG4CPLUS_NOEXCEPT;
95 
96  // methods
97  virtual bool read(SocketBuffer& buffer);
98  virtual bool write(const SocketBuffer& buffer);
99  virtual bool write(const std::string & buffer);
100  virtual bool write(std::size_t bufferCount,
101  SocketBuffer const * const * buffers);
102 
103  template <typename... Args>
104  static bool write(Socket & socket, Args &&... args)
105  {
106  SocketBuffer const * const buffers[sizeof... (args)] {
107  (&args)... };
108  return socket.write (sizeof... (args), buffers);
109  }
110  };
111 
112 
113 
121  public:
122  ServerSocket(unsigned short port, bool udp = false,
123  bool ipv6 = false, tstring const & host = tstring ());
125  virtual ~ServerSocket();
126 
128 
129  Socket accept();
130  void interruptAccept ();
131  void swap (ServerSocket &);
132 
133  protected:
134  std::array<std::ptrdiff_t, 2> interruptHandles;
135  };
136 
137 
138  LOG4CPLUS_EXPORT SOCKET_TYPE openSocket(unsigned short port, bool udp,
139  bool ipv6, SocketState& state);
141  unsigned short port, bool udp, bool ipv6, SocketState& state);
142 
144  unsigned short port, bool udp, bool ipv6, SocketState& state);
148 
151  const SocketBuffer& buffer);
152  LOG4CPLUS_EXPORT long write(SOCKET_TYPE sock, std::size_t bufferCount,
153  SocketBuffer const * const * buffers);
155  const std::string & buffer);
156 
159 
160  } // end namespace helpers
161 } // end namespace log4cplus
162 
163 #endif // LOG4CPLUS_HELPERS_SOCKET_HEADER_
AbstractSocket(AbstractSocket &&) LOG4CPLUS_NOEXCEPT
AbstractSocket(SOCKET_TYPE sock, SocketState state, int err)
AbstractSocket(AbstractSocket const &)=delete
This class implements server sockets.
ServerSocket(unsigned short port, bool udp=false, bool ipv6=false, tstring const &host=tstring())
ServerSocket(ServerSocket &&) LOG4CPLUS_NOEXCEPT
This class implements client sockets (also called just "sockets").
Socket(Socket &&) LOG4CPLUS_NOEXCEPT
Socket(const tstring &address, unsigned short port, bool udp=false, bool ipv6=false)
Socket(SOCKET_TYPE sock, SocketState state, int err)
#define LOG4CPLUS_NOEXCEPT
Definition: config.hxx:101
LOG4CPLUS_EXPORT SOCKET_TYPE const INVALID_SOCKET_VALUE
LOG4CPLUS_EXPORT long read(SOCKET_TYPE sock, SocketBuffer &buffer)
LOG4CPLUS_EXPORT int closeSocket(SOCKET_TYPE sock)
LOG4CPLUS_EXPORT SOCKET_TYPE acceptSocket(SOCKET_TYPE sock, SocketState &state)
LOG4CPLUS_EXPORT int setTCPNoDelay(SOCKET_TYPE, bool)
LOG4CPLUS_EXPORT tstring getHostname(bool fqdn)
LOG4CPLUS_EXPORT SOCKET_TYPE connectSocket(const log4cplus::tstring &hostn, unsigned short port, bool udp, bool ipv6, SocketState &state)
LOG4CPLUS_EXPORT long write(SOCKET_TYPE sock, const SocketBuffer &buffer)
LOG4CPLUS_EXPORT SOCKET_TYPE openSocket(unsigned short port, bool udp, bool ipv6, SocketState &state)
LOG4CPLUS_EXPORT int shutdownSocket(SOCKET_TYPE sock)
std::ptrdiff_t SOCKET_TYPE
std::basic_string< tchar > tstring
Definition: tstring.h:39
#define LOG4CPLUS_EXPORT
Definition: win32.h:141