Luanti 5.18.0-dev
Loading...
Searching...
No Matches
socket.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5#pragma once
6
7#include "irrlichttypes.h"
8#include "address.h"
9
10void sockets_init();
11void sockets_cleanup();
12
14{
15public:
16 UDPSocket() = default;
18 m_handle(other.m_handle),
21 {
22 other.m_handle = -1;
23 }
25 {
26 Close();
27 }
29 {
30 Close();
31 m_handle = other.m_handle;
32 m_timeout_ms = other.m_timeout_ms;
33 m_addr_family = other.m_addr_family;
34 other.m_handle = -1;
35 return *this;
36 }
37
40 {
41 UDPSocket socket;
42 socket.Bind(addr);
43 return socket;
44 }
45
46 static UDPSocket CreateEphemeral(bool ipv6)
47 {
48 if (ipv6)
49 return Create(Address(nullptr, 0));
50 else
51 return Create(Address(u32(0), 0));
52 }
53
54 void Init(bool ipv6);
55 void Bind(Address addr);
56 void Close();
57
59
60 void Send(const Address &destination, const void *data, int size);
61 // Returns -1 if there is no data
62 int Receive(Address &sender, void *data, int size);
63 void setTimeoutMs(int timeout_ms);
64 // Returns true if there is data, false if timeout occurred
65 bool WaitData(int timeout_ms);
66
67 // Debugging purposes only
68 int GetHandle() const { return m_handle; };
69
70private:
71 int m_handle = -1;
72 int m_timeout_ms = -1;
73 unsigned short m_addr_family = 0;
74};
Definition address.h:27
void Bind(Address addr)
Definition socket.cpp:98
int m_handle
Definition socket.h:71
static UDPSocket CreateEphemeral(bool ipv6)
Definition socket.h:46
UDPSocket(UDPSocket &&other)
Definition socket.h:17
void Send(const Address &destination, const void *data, int size)
Definition socket.cpp:179
int GetHandle() const
Definition socket.h:68
UDPSocket & operator=(UDPSocket &&other)
Definition socket.h:28
unsigned short m_addr_family
Definition socket.h:73
int m_timeout_ms
Definition socket.h:72
UDPSocket()=default
Address GetLocalAddress()
Definition socket.cpp:154
void Close()
Definition socket.cpp:86
int Receive(Address &sender, void *data, int size)
Definition socket.cpp:219
static UDPSocket Create(Address addr)
create and bind a socket to an address
Definition socket.h:39
void setTimeoutMs(int timeout_ms)
Definition socket.cpp:265
~UDPSocket()
Definition socket.h:24
void Init(bool ipv6)
Definition socket.cpp:62
bool WaitData(int timeout_ms)
Definition socket.cpp:270
void sockets_cleanup()
Definition socket.cpp:49
void sockets_init()
Definition socket.cpp:38