libvisiontransfer  10.6.0
networking.h
1 /*******************************************************************************
2  * Copyright (c) 2023 Allied Vision Technologies 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  // Visual studio does not come with snprintf
70  #ifndef snprintf
71  #define snprintf _snprintf_s
72  #endif
73 
74  typedef int socklen_t;
75 
76  typedef unsigned long error_int_type;
77 
78 #else
79  #include <arpa/inet.h>
80  #include <netinet/tcp.h>
81  #include <sys/types.h>
82  #include <sys/socket.h>
83  #include <sys/select.h>
84  #include <netdb.h>
85  #include <netinet/in.h>
86  #include <errno.h>
87  #include <unistd.h>
88  #include <signal.h>
89  #include <ifaddrs.h>
90  #include <poll.h>
91 
92  #include <string>
93 
94  // Unfortunately we have to use a winsock like socket type
95  typedef int SOCKET;
96  #define INVALID_SOCKET -1
97 
98  // Also we need some additional winsock defines
99  #define WSA_IO_PENDING 0
100  #define WSAECONNRESET 0
101 
102  typedef int error_int_type;
103 
104 #endif
105 
106 namespace visiontransfer {
107 namespace internal {
108 
112 class Networking {
113 public:
114  static void initNetworking();
115  static addrinfo* resolveAddress(const char* address, const char* service);
116  static SOCKET connectTcpSocket(const addrinfo* address);
117  static void setSocketTimeout(SOCKET socket, int timeoutMillisec);
118  static void closeSocket(SOCKET& socket);
119  static void setSocketBlocking(SOCKET socket, bool blocking);
120  static void enableReuseAddress(SOCKET socket, bool reuse);
121  static void bindSocket(SOCKET socket, const addrinfo* addressInfo);
122  static SOCKET acceptConnection(SOCKET socket, sockaddr_in& remoteAddress);
123  static error_int_type getErrno();
124  static std::string getErrorString(error_int_type error);
125  static std::string getLastErrorString();
126 
127 };
128 
129 }} // namespace
130 
131 #endif
visiontransfer::internal::Networking
A collection of helper functions for implementing network communication.
Definition: networking.h:112
Allied Vision