DAVID4 SDK  1.8.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
NetworkHelper.h
1 //=============================================================================
2 // See License in Related Pages
3 //=============================================================================
4 
5 #pragma once
6 
7 #ifndef DAVID_SDK_NETWORK_HELPER_H
8 #define DAVID_SDK_NETWORK_HELPER_H
9 
10 #include <string>
11 
12 #ifdef _WIN32
13  #include <stdint.h>
14  #include <winsock2.h>
15  #include <ws2tcpip.h>
16 
17  /* to use getaddrinfo, _WIN32_WINNT have to
18  * equal at least 0x0501
19  */
20 
21  #define OLD_WIN32_WINNT _WIN32_WINNT
22 
23  #if (_WIN32_WINNT < 0x0501)
24  #undef _WIN32_WINNT
25  #define _WIN32_WINNT 0x501
26  #endif
27 
28  #include <ws2tcpip.h>
29 
30  #if (_WIN32_WINNT != OLD_WIN32_WINNT)
31  #undef _WIN32_WINNT
32  #define _WIN32_WINNT OLD_WIN32_WINNT
33  #endif
34 
35  typedef int socklen_t;
36 
37  #ifndef QT_VERSION
38  #define close closesocket
39  #endif
40 
41 #else // not _WIN32
42  #include <stdint.h>
43 
44  #include <sys/types.h>
45  #include <sys/socket.h>
46  #include <sys/time.h>
47 
48  #include <unistd.h>
49 
50  #include <netinet/in.h>
51 
52  #include <netdb.h>
53 
54  #define SOCKET int
55  #define closesocket close
56 #endif
57 
58 
59 namespace david {
60 
61 //=============================================================================
62 // NetworkHelper
63 //=============================================================================
64 
65 /// Helper class for initialization and cleanup of network usage.
66 /// On Windows WSAStartup is called within constructor and WSACleanup within destructor.
67 /// Define DAVID_EXTERNAL_WSA_STARTUP, if you want to call WSAStartup externally.
69 {
70 public:
71  /// Constructor. Calls WSAStartup on Windows.
72  NetworkHelper();
73 
74  /// Destructor. Calls WSACleanup on Windows in case of successful WSAStartup.
75  virtual ~NetworkHelper();
76 
77  /// Connect to a remote machine.
78  /// @param[out] sockaddr Gets the sockaddr if the function succeeds.
79  /// @param[out] addrlen Length of sockaddr if the functions succeeds.
80  /// @param[in] address Remote address.
81  /// @param[in] port Remote port.
82  /// @return Socket descriptor if successful, -1 otherwise.
83  static SOCKET Connect(sockaddr_storage* sockaddr, socklen_t* addrlen, const std::string& address, uint16_t port);
84 
85  /// Bind on a local address.
86  /// @param[out] sockaddr Gets the sockaddr if the function succeeds.
87  /// @param[out] addrlen Length of sockaddr if the functions succeeds.
88  /// @param[in] address Remote address.
89  /// @param[in] port Remote port.
90  /// @return Socket descriptor if successful, -1 otherwise.
91  static SOCKET Bind(sockaddr_storage* sockaddr, socklen_t* addrlen, const std::string& address, uint16_t port);
92 
93 private:
94  bool m_initOk; ///< Initialization ok?
95 };
96 
97 
98 } // namespace
99 
100 #endif // DAVID_SDK_NETWORK_HELPER_H