-
Notifications
You must be signed in to change notification settings - Fork 0
/
SocketAddress.cpp
74 lines (61 loc) · 2.07 KB
/
SocketAddress.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//
// Created by floweryclover on 2024-02-06.
//
#include "SocketAddress.h"
#include <cassert>
#ifdef _WIN32
#include <ws2tcpip.h>
#elifdef linux
#include <arpa/inet.h>
#endif
SocketAddress::SocketAddress() noexcept : mpRawSockAddrIn(nullptr) {}
SocketAddress::SocketAddress(const char *hostIpAddress, unsigned short hostPort) noexcept
{
#ifdef _WIN32
in_addr inAddr;
ZeroMemory(&inAddr, sizeof(in_addr));
#elifdef linux
in_addr_t inAddr;
#endif
assert (inet_pton(AF_INET, hostIpAddress, &inAddr) != -1);
auto pRawSockAddrIn = new struct sockaddr_in;
memset(pRawSockAddrIn, 0, sizeof(struct sockaddr_in));
pRawSockAddrIn->sin_family = AF_INET;
pRawSockAddrIn->sin_port = htons(hostPort);
pRawSockAddrIn->sin_addr = inAddr;
mpRawSockAddrIn = std::unique_ptr<struct sockaddr_in>(pRawSockAddrIn);
}
SocketAddress::SocketAddress(const struct sockaddr_in &rawSockAddrIn) noexcept
{
auto pRawSockAddrIn = new struct sockaddr_in;
memcpy(pRawSockAddrIn, &rawSockAddrIn, sizeof(struct sockaddr_in));
mpRawSockAddrIn = std::unique_ptr<struct sockaddr_in>(pRawSockAddrIn);
}
SocketAddress::SocketAddress(const SocketAddress &rhs) noexcept : SocketAddress(*rhs.mpRawSockAddrIn) {}
SocketAddress::SocketAddress(SocketAddress &&rhs) noexcept : mpRawSockAddrIn(std::move(rhs.mpRawSockAddrIn)) {}
SocketAddress::~SocketAddress() = default;
SocketAddress& SocketAddress::operator=(const SocketAddress &rhs) noexcept
{
this->mpRawSockAddrIn->sin_addr = rhs.mpRawSockAddrIn->sin_addr;
this->mpRawSockAddrIn->sin_port = rhs.mpRawSockAddrIn->sin_port;
return *this;
}
SocketAddress& SocketAddress::operator=(SocketAddress &&rhs) noexcept
{
this->mpRawSockAddrIn.reset();
this->mpRawSockAddrIn = std::move(rhs.mpRawSockAddrIn);
return *this;
}
std::string SocketAddress::ToString() const
{
if (mpRawSockAddrIn == nullptr)
{
return {};
}
return {inet_ntoa(mpRawSockAddrIn->sin_addr)};
}
const struct sockaddr_in& SocketAddress::GetRawSockAddrIn() const
{
assert(mpRawSockAddrIn != nullptr);
return *mpRawSockAddrIn;
}