log4cplus  2.1.0
internal/socket.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 // Module: Log4CPLUS
3 // File: socket.h
4 // Created: 1/2010
5 // Author: Vaclav Haisman
6 //
7 //
8 // Copyright (C) 2010-2017, Vaclav Haisman. All rights reserved.
9 //
10 // Redistribution and use in source and binary forms, with or without modifica-
11 // tion, are permitted provided that the following conditions are met:
12 //
13 // 1. Redistributions of source code must retain the above copyright notice,
14 // this list of conditions and the following disclaimer.
15 //
16 // 2. Redistributions in binary form must reproduce the above copyright notice,
17 // this list of conditions and the following disclaimer in the documentation
18 // and/or other materials provided with the distribution.
19 //
20 // THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
21 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
22 // FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 // APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24 // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
25 // DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26 // OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
27 // ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
37 #ifndef LOG4CPLUS_INTERNAL_SOCKET_H_
38 #define LOG4CPLUS_INTERNAL_SOCKET_H_
39 
40 #include <log4cplus/config.hxx>
41 
42 #if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
43 #pragma once
44 #endif
45 
46 #if ! defined (INSIDE_LOG4CPLUS)
47 # error "This header must not be be used outside log4cplus' implementation files."
48 #endif
49 
50 #if defined(_WIN32)
52 #endif
54 
55 #include <cerrno>
56 #ifdef LOG4CPLUS_HAVE_ERRNO_H
57 #include <errno.h>
58 #endif
59 
60 #ifdef LOG4CPLUS_HAVE_UNISTD_H
61 #include <unistd.h>
62 #endif
63 
64 #if defined (LOG4CPLUS_HAVE_NETDB_H)
65 #include <netdb.h>
66 #endif
67 
68 
69 namespace log4cplus {
70 
71 namespace helpers {
72 
73 #if defined(_WIN32)
74 typedef SOCKET os_socket_type;
75 os_socket_type const INVALID_OS_SOCKET_VALUE = INVALID_SOCKET;
76 
78 {
79  void
80  operator () (ADDRINFOA * ptr) const
81  {
82  FreeAddrInfoA(ptr);
83  }
84 
85  void
86  operator () (ADDRINFOW * ptr) const
87  {
88  FreeAddrInfoW(ptr);
89  }
90 };
91 
92 
94 {
95  void
96  operator () (SOCKET s)
97  {
98  if (s && s != INVALID_OS_SOCKET_VALUE)
99  {
100  DWORD const eno = WSAGetLastError();
101  ::closesocket(s);
102  WSASetLastError(eno);
103  }
104  }
105 };
106 
107 
108 #else
109 typedef int os_socket_type;
111 
112 
113 struct addrinfo_deleter
114 {
115  void
116  operator () (struct addrinfo * ptr) const
117  {
118  freeaddrinfo(ptr);
119  }
120 };
121 
122 
123 struct socket_closer
124 {
125  void
127  {
128  if (s >= 0)
129  {
130  int const eno = errno;
131  close(s);
132  errno = eno;
133  }
134  }
135 };
136 
137 #endif
138 
139 
141 {
143 
146  { }
147 
149  : sock(s)
150  { }
151 
153  {
154  socket_closer()(sock);
155  }
156 
157  void
159  {
161  socket_closer()(sock);
162 
163  sock = s;
164  }
165 
168  {
169  os_socket_type s = sock;
171  return s;
172  }
173 
175  socket_holder(socket_holder const &) = delete;
176 
179 };
180 
181 
182 static inline
185 {
186  return static_cast<os_socket_type>(x);
187 }
188 
189 
190 static inline
193 {
194  return static_cast<SOCKET_TYPE>(x);
195 }
196 
197 
198 static inline
199 void
201 {
202  errno = err;
203 }
204 
205 
206 static inline
207 int
209 {
210  return errno;
211 }
212 
213 
214 } // namespace helpers {
215 
216 } // namespace log4cplus {
217 
218 
219 #endif // LOG4CPLUS_INTERNAL_SOCKET_H_
os_socket_type const INVALID_OS_SOCKET_VALUE
static void set_last_socket_error(int err)
static SOCKET_TYPE to_log4cplus_socket(os_socket_type const &x)
static os_socket_type to_os_socket(SOCKET_TYPE const &x)
static int get_last_socket_error()
std::ptrdiff_t SOCKET_TYPE
void operator()(ADDRINFOA *ptr) const
socket_holder(socket_holder const &)=delete
void reset(os_socket_type s=INVALID_OS_SOCKET_VALUE)
socket_holder(socket_holder &&)=delete
socket_holder operator=(socket_holder &&)=delete