libvisiontransfer  6.4.0
networking.h
1 /*******************************************************************************
2  * Copyright (c) 2019 Nerian Vision GmbH
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *******************************************************************************/
14 
15 /*******************************************************************************
16  * This header file contains include statements and definitions for simplifying
17  * cross platform network development
18 *******************************************************************************/
19 
20 #ifndef VISIONTRANSFER_NETWORKING_H
21 #define VISIONTRANSFER_NETWORKING_H
22 
23 // Network headers
24 #ifdef _WIN32
25  #ifndef _WIN32_WINNT
26  #define _WIN32_WINNT 0x501
27  #endif
28  #define _WINSOCK_DEPRECATED_NO_WARNINGS
29 
30  #ifndef NOMINMAX
31  #define NOMINMAX
32  #endif
33 
34  #include <string>
35  #include <cstdio>
36  #include <winsock2.h>
37  #include <ws2tcpip.h>
38 
39  #include <Ipmib.h>
40  #include <Iprtrmib.h>
41  #include <Iphlpapi.h>
42 
43  // Some defines to make windows socket look more like
44  // posix sockets.
45  #ifdef EWOULDBLOCK
46  #undef EWOULDBLOCK
47  #endif
48  #ifdef ECONNRESET
49  #undef ECONNRESET
50  #endif
51  #ifdef ETIMEDOUT
52  #undef ETIMEDOUT
53  #endif
54  #ifdef EPIPE
55  #undef EPIPE
56  #endif
57 
58  #define EWOULDBLOCK WSAEWOULDBLOCK
59  #define ECONNRESET WSAECONNRESET
60  #define ETIMEDOUT WSAETIMEDOUT
61  #define EPIPE WSAECONNABORTED
62  #define MSG_DONTWAIT 0
63  #define SHUT_WR SD_BOTH
64 
65  inline int close(SOCKET s) {
66  return closesocket(s);
67  }
68 
69  // Emulate posix errno. Does not work in a throw
70  // statement (WTF?)
71  #undef errno
72  #define errno WSAGetLastError()
73  #define strerror win_strerror
74 
75  // Visual studio does not come with snprintf
76  #ifndef snprintf
77  #define snprintf _snprintf_s
78  #endif
79 
80  inline std::string win_strerror(unsigned long error) {
81  char* str = nullptr;
82  if(FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
83  FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
84  nullptr, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
85  (LPSTR)&str, 0, nullptr) == 0 || str == nullptr) {
86  return "Unknown error";
87  } else {
88  char buffer[512];
89  snprintf(buffer, sizeof(buffer), "%s (%lu)", str, error);
90  LocalFree(str);
91  return std::string(buffer);
92  }
93  }
94 
95  typedef int socklen_t;
96 
97 #else
98  #include <arpa/inet.h>
99  #include <netinet/tcp.h>
100  #include <sys/types.h>
101  #include <sys/socket.h>
102  #include <sys/select.h>
103  #include <netdb.h>
104  #include <netinet/in.h>
105  #include <errno.h>
106  #include <unistd.h>
107  #include <signal.h>
108  #include <ifaddrs.h>
109 
110  // Unfortunately we have to use a winsock like socket type
111  typedef int SOCKET;
112  #define INVALID_SOCKET -1
113 
114  // Also we need some additional winsock defines
115  #define WSA_IO_PENDING 0
116  #define WSAECONNRESET 0
117 #endif
118 
119 namespace visiontransfer {
120 namespace internal {
121 
125 class Networking {
126 public:
127  static void initNetworking();
128  static addrinfo* resolveAddress(const char* address, const char* service);
129  static SOCKET connectTcpSocket(const addrinfo* address);
130  static void setSocketTimeout(SOCKET socket, int timeoutMillisec);
131  static void closeSocket(SOCKET& socket);
132  static void setSocketBlocking(SOCKET socket, bool blocking);
133  static void enableReuseAddress(SOCKET socket, bool reuse);
134  static void bindSocket(SOCKET socket, const addrinfo* addressInfo);
135  static SOCKET acceptConnection(SOCKET socket, sockaddr_in& remoteAddress);
136 };
137 
138 }} // namespace
139 
140 #endif
A collection of helper functions for implementing network communication.
Definition: networking.h:125
Nerian Vision Technologies