-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathudp_socket.cpp
More file actions
149 lines (123 loc) · 3.7 KB
/
Copy pathudp_socket.cpp
File metadata and controls
149 lines (123 loc) · 3.7 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
////////////////////////////////////////////////////////////////////////////////
// Distributed under the Boost Software License, Version 1.0. //
// (See accompanying file LICENSE or copy at //
// https://www.boost.org/LICENSE_1_0.txt) //
////////////////////////////////////////////////////////////////////////////////
#include "networking/udp_socket.h"
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <memory>
#include <optional>
#include <string>
#include "core/data_buffer.h"
#include "core/exception.h"
#include "log/log.h"
#include "networking/networking.h"
#include "networking/socket.h"
namespace iris
{
UdpSocket::UdpSocket(const std::string &address, std::uint16_t port)
: socket_()
, address_()
, address_length_(0)
{
LOG_ENGINE_INFO("udp_socket", "creating socket ({}:{})", address, port);
// create socket
socket_ = {::socket(AF_INET, SOCK_DGRAM, 0), CloseSocket};
if (!socket_)
{
throw Exception("socket failed");
}
// configure address
std::memset(&address_, 0x0, sizeof(address_));
address_length_ = sizeof(address_);
address_.sin_family = AF_INET;
address_.sin_port = htons(port);
// convert address from text to binary
if (::inet_pton(AF_INET, address.c_str(), &address_.sin_addr.s_addr) != 1)
{
throw Exception("failed to convert ip address");
}
// enable socket reuse
int reuse = 1;
if (::setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<const char *>(&reuse), sizeof(reuse)) < 0)
{
throw iris::Exception("setsockopt failed");
}
LOG_ENGINE_INFO("udp_socket", "connected!");
}
UdpSocket::UdpSocket(struct sockaddr_in socket_address, socklen_t socket_length, SocketHandle socket)
: socket_(socket, nullptr)
, address_(socket_address)
, address_length_(socket_length)
{
}
std::optional<DataBuffer> UdpSocket::try_read(std::size_t count)
{
std::optional<DataBuffer> out = DataBuffer(count);
set_blocking(socket_, false);
// perform non-blocking read
auto read = ::recvfrom(
socket_,
reinterpret_cast<char *>(out->data()),
static_cast<int>(out->size()),
0,
reinterpret_cast<struct sockaddr *>(&address_),
&address_length_);
if (read == -1)
{
// read failed but not because there was no data
if (!last_call_blocked())
{
throw iris::Exception("read failed");
}
// no data, so reset optional
out.reset();
}
else
{
// resize buffer to amount of data read
out->resize(read);
}
return out;
}
DataBuffer UdpSocket::read(std::size_t count)
{
DataBuffer buffer(count);
set_blocking(socket_, true);
// perform blocking read
auto read = ::recvfrom(
socket_,
reinterpret_cast<char *>(buffer.data()),
static_cast<int>(buffer.size()),
0,
reinterpret_cast<struct sockaddr *>(&address_),
&address_length_);
if (read == -1)
{
throw Exception("recvfrom failed");
}
// resize buffer to amount of data read
buffer.resize(read);
return buffer;
}
void UdpSocket::write(const DataBuffer &buffer)
{
write(buffer.data(), buffer.size());
}
void UdpSocket::write(const std::byte *data, std::size_t size)
{
const auto size_check = static_cast<int>(size);
if (::sendto(
socket_,
reinterpret_cast<const char *>(data),
static_cast<int>(size),
0,
reinterpret_cast<struct sockaddr *>(&address_),
address_length_) != size_check)
{
throw Exception("sendto failed");
}
}
}