-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathudp_server_socket.cpp
More file actions
95 lines (72 loc) · 2.91 KB
/
Copy pathudp_server_socket.cpp
File metadata and controls
95 lines (72 loc) · 2.91 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
////////////////////////////////////////////////////////////////////////////////
// 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_server_socket.h"
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <memory>
#include "core/auto_release.h"
#include "core/data_buffer.h"
#include "core/error_handling.h"
#include "log/log.h"
#include "networking/networking.h"
#include "networking/server_socket_data.h"
#include "networking/socket.h"
#include "networking/udp_socket.h"
namespace iris
{
UdpServerSocket::UdpServerSocket(const std::string &address, std::uint32_t port)
: connections_()
, socket_()
{
LOG_ENGINE_INFO("udp_server_socket", "creating server socket ({}:{})", address, port);
// create socket
socket_ = {::socket(AF_INET, SOCK_DGRAM, 0), CloseSocket};
ensure(socket_ == INVALID_SOCKET, "socket failed");
// configure address
struct sockaddr_in address_storage = {0};
socklen_t address_length = sizeof(struct sockaddr_in);
memset(&address_storage, 0x0, address_length);
address_storage.sin_family = AF_INET;
inet_pton(AF_INET, address.c_str(), &address_storage.sin_addr.s_addr);
address_storage.sin_port = htons(static_cast<std::uint16_t>(port));
// enable multicast
int reuse = 1;
ensure(
::setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<const char *>(&reuse), sizeof(reuse)) == 0,
"setsockopt failed");
// bind socket so we can accept connections
ensure(::bind(socket_, reinterpret_cast<struct sockaddr *>(&address_storage), address_length) == 0, "bind failed");
LOG_ENGINE_INFO("udp_server_socket", "connected!");
}
ServerSocketData UdpServerSocket::read()
{
struct sockaddr_in address;
socklen_t length = sizeof(address);
static constexpr auto new_connection_size = 1024u;
DataBuffer buffer(new_connection_size);
// block and wait for a new connection
const auto read = ::recvfrom(
socket_,
reinterpret_cast<char *>(buffer.data()),
static_cast<int>(buffer.size()),
0,
reinterpret_cast<struct sockaddr *>(&address),
&length);
ensure(read != -1, "recvfrom failed");
// resize buffer to amount of data read
buffer.resize(read);
const auto byte_address = address.sin_addr.s_addr;
auto new_connection = false;
if (connections_.count(byte_address) == 0u)
{
connections_[byte_address] = std::make_unique<UdpSocket>(address, length, socket_.get());
new_connection = true;
LOG_ENGINE_INFO("udp_server_socket", "new connection");
}
return {connections_[byte_address].get(), buffer, new_connection};
}
}