blocxx
NetworkTypes.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright (C) 2005, Vintela, Inc. All rights reserved.
3 * Copyright (C) 2006, Novell, Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of
14 * Vintela, Inc.,
15 * nor Novell, Inc.,
16 * nor the names of its contributors or employees may be used to
17 * endorse or promote products derived from this software without
18 * specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 *******************************************************************************/
32 
33 
39 #ifndef BLOCXX_NETWORK_TYPES_HPP_INCLUDE_GUARD_
40 #define BLOCXX_NETWORK_TYPES_HPP_INCLUDE_GUARD_
41 #include "blocxx/BLOCXX_config.h"
42 #include "blocxx/Types.hpp"
43 
44 extern "C"
45 {
46 #ifdef BLOCXX_HAVE_UNISTD_H
47  #include <unistd.h>
48 #endif
49 
50 #include <signal.h>
51 
52 #ifdef BLOCXX_HAVE_SYS_SOCKET_H
53  #include <sys/socket.h>
54 #endif
55 
56 #ifdef BLOCXX_HAVE_NETINET_IN_H
57  #include <netinet/in.h>
58 #endif
59 
60 #ifdef BLOCXX_HAVE_SYS_UN_H
61  #include <sys/un.h>
62 #endif
63 
64 #if defined(BLOCXX_WIN32)
65 #include <winsock2.h>
66 #endif
67 }
68 
69 #undef shutdown // On OpenUnix, sys/socket.h defines shutdown to be
70  // _shutdown. (which breaks HTTPServer, etc.)
71 
72 namespace BLOCXX_NAMESPACE
73 {
74 
75 // Platform specific socket address type
76 typedef sockaddr SocketAddress_t;
77 // Platform specific inet socket address type
78 #ifdef BLOCXX_HAVE_IPV6
79 typedef sockaddr_storage InetSocketAddress_t;
80 #else
81 typedef sockaddr_in InetSocketAddress_t;
82 #endif
83 
84 #if !defined(BLOCXX_WIN32)
85 // Platform specific unix socket address type
86 typedef sockaddr_un UnixSocketAddress_t;
87 #endif
88 
89 // Platform specific socket address type
90 typedef in_addr InetAddress_t;
91 
92 #if defined (BLOCXX_WIN32)
93 // Platform specific socket fd type
94 typedef SOCKET SocketHandle_t;
95 #else
96 // Platform specific socket fd type
97 typedef int SocketHandle_t;
98 #endif
99 
100 // Select_t is the type of object that can be used in
101 // synchronous I/O multiplexing (i.e. select).
102 #if defined(BLOCXX_WIN32)
103 struct Select_t
104 {
105  Select_t()
106  : event(NULL)
107  , descriptor(INVALID_HANDLE_VALUE)
108  , sockfd(INVALID_SOCKET)
109  , isSocket(false)
110  , networkevents(0)
111  , doreset(false)
112  {
113  }
114 
115  Select_t(const Select_t& arg)
116  : event(arg.event)
117  , descriptor(arg.descriptor)
118  , sockfd(arg.sockfd)
119  , isSocket(arg.isSocket)
120  , networkevents(arg.networkevents)
121  , doreset(arg.doreset)
122  {
123  }
124 
125  HANDLE event;
126  HANDLE descriptor;
127  SOCKET sockfd;
128  bool isSocket;
129  bool doreset;
130  long networkevents;
131 
132  bool operator<(const Select_t& s1) const
133  {
134  //we can not use the fields 'sockfd' or 'descriptor' because it's possible
135  //the comparison of objects with different types - SOCKET and HANDLE
136  //but the field 'event' is presented for both types
137  return (event < s1.event);
138  }
139 
140  bool operator==(const Select_t& s1) const
141  {
142  return (event == s1.event);
143  }
144 };
145 #else
146 typedef int Select_t;
147 #endif
148 
149 } // end namespace BLOCXX_NAMESPACE
150 
151 #if defined(BLOCXX_WIN32) || defined(BLOCXX_NCR)
152 typedef int socklen_t;
153 #else
154 #ifndef BLOCXX_HAVE_SOCKLEN_T
155 typedef unsigned socklen_t;
156 #endif
157 #endif
158 
159 
160 #endif