CVB++ 15.0
network_connection.hpp
1#pragma once
2
3#include "global.hpp"
4
5#include "string.hpp"
6
7namespace Cvb
8{
9 CVB_BEGIN_INLINE_NS
10
12
14 struct NetworkConnection
15 {
16 typedef String IPAddress;
17 typedef std::uint32_t IPAddressUInt;
18 typedef std::uint16_t Port;
19
20 IPAddressUInt ipAddressUInt_{0u};
21 IPAddress ipAddress_{CVB_LIT("0.0.0.0")};
22 Port port_{0u};
23
24 NetworkConnection() noexcept {}
25
26 NetworkConnection(const IPAddress &ip, const Port &port)
27 : ipAddress_(ip)
28 , port_(port)
29 {
30 ipAddressUInt_ = ToIPAddressUInt(ipAddress_);
31 }
32
33 NetworkConnection(const IPAddressUInt &ipUInt, const Port &port)
34 : ipAddressUInt_(ipUInt)
35 , port_(port)
36 {
37 ipAddress_ = ToIPAddress(ipAddressUInt_);
38 }
39
46 static IPAddress ToIPAddress(const IPAddressUInt &ipAddressInt)
47 {
48 std::array<uint8_t, 5> bytes = {};
49 bytes[3] = ipAddressInt & 0xFF;
50 bytes[2] = (ipAddressInt >> 8) & 0xFF;
51 bytes[1] = (ipAddressInt >> 16) & 0xFF;
52 bytes[0] = (ipAddressInt >> 24) & 0xFF;
53
54 std::string str = std::to_string((int)bytes[0]);
55 str += "." + std::to_string(static_cast<int>(bytes[1]));
56 str += "." + std::to_string(static_cast<int>(bytes[2]));
57 str += "." + std::to_string(static_cast<int>(bytes[3]));
58
59 return IPAddress(str.begin(), str.end());
60 }
61
68 static IPAddressUInt ToIPAddressUInt(const IPAddress &ip)
69 {
70 std::string _ip(Internal::CastToAscii(ip));
71 const char *runner = _ip.c_str();
72 unsigned long factor = 1;
73 IPAddressUInt temp = 0;
74 unsigned int reset = 0;
75
76 while (*runner)
77 runner++;
78
79 while ((_ip.c_str() - runner--))
80 {
81 if (*runner == '.')
82 {
83 factor <<= 8;
84 reset = 0;
85 runner--;
86 }
87 temp += (factor * static_cast<unsigned long>(*(runner) - '0') * (reset == 1 ? 10 : (reset == 2 ? 100 : 1)));
88 reset++;
89 }
90 return temp;
91 }
92 };
93
94 CVB_END_INLINE_NS
95} // namespace Cvb
Root namespace for the Image Manager interface.
Definition c_bayer_to_rgb.h:17
std::string String
String for wide characters or unicode characters.
Definition string.hpp:49
static IPAddress ToIPAddress(const IPAddressUInt &ipAddressInt)
Creates an IPAddress from the given uint ip .
Definition network_connection.hpp:46
static IPAddressUInt ToIPAddressUInt(const IPAddress &ip)
Creates an uint ip . from the given IPAddress
Definition network_connection.hpp:68
T to_string(T... args)