Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/io/net_socket.h
20951 views
1
/**************************************************************************/
2
/* net_socket.h */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#pragma once
32
33
#include "core/io/ip.h"
34
#include "core/object/ref_counted.h"
35
36
class NetSocket : public RefCounted {
37
GDSOFTCLASS(NetSocket, RefCounted);
38
39
protected:
40
static NetSocket *(*_create)();
41
42
public:
43
static NetSocket *create();
44
45
enum PollType : int32_t {
46
POLL_TYPE_IN,
47
POLL_TYPE_OUT,
48
POLL_TYPE_IN_OUT
49
};
50
51
enum Type : int32_t {
52
TYPE_NONE,
53
TYPE_TCP,
54
TYPE_UDP,
55
};
56
57
enum class Family {
58
NONE,
59
INET,
60
UNIX,
61
};
62
63
class Address {
64
Family _family = Family::NONE;
65
CharString _path;
66
IPAddress _ip;
67
uint16_t _port = 0;
68
69
public:
70
_FORCE_INLINE_ Family get_family() const { return _family; }
71
_FORCE_INLINE_ bool is_inet() const { return _family == Family::INET; }
72
_FORCE_INLINE_ bool is_unix() const { return _family == Family::UNIX; }
73
_FORCE_INLINE_ bool is_valid() const { return is_inet() || is_unix(); }
74
75
_FORCE_INLINE_ const IPAddress &ip() const { return _ip; }
76
_FORCE_INLINE_ const uint16_t &port() const { return _port; }
77
78
_FORCE_INLINE_ const CharString &get_path() const { return _path; }
79
80
Address() {}
81
82
Address(const IPAddress &p_addr, uint16_t p_port) :
83
_family(Family::INET) {
84
_ip = p_addr;
85
_port = p_port;
86
}
87
88
Address(const String &p_path) :
89
_family(Family::UNIX), _path(p_path.utf8()) {
90
}
91
92
Address(const CharString &p_path) :
93
_family(Family::UNIX), _path(p_path) {
94
}
95
};
96
97
virtual Error open(Family p_family, Type p_type, IP::Type &r_ip_type) = 0;
98
virtual void close() = 0;
99
virtual Error bind(Address p_addr) = 0;
100
virtual Error listen(int p_max_pending) = 0;
101
virtual Error connect_to_host(Address p_addr) = 0;
102
virtual Error poll(PollType p_type, int timeout) const = 0;
103
virtual Error recv(uint8_t *p_buffer, int p_len, int &r_read) = 0;
104
virtual Error recvfrom(uint8_t *p_buffer, int p_len, int &r_read, IPAddress &r_ip, uint16_t &r_port, bool p_peek = false) = 0;
105
virtual Error send(const uint8_t *p_buffer, int p_len, int &r_sent) = 0;
106
virtual Error sendto(const uint8_t *p_buffer, int p_len, int &r_sent, IPAddress p_ip, uint16_t p_port) = 0;
107
virtual Ref<NetSocket> accept(Address &r_addr) = 0;
108
109
virtual bool is_open() const = 0;
110
virtual int get_available_bytes() const = 0;
111
virtual Error get_socket_address(Address *r_addr) const = 0;
112
113
virtual Error set_broadcasting_enabled(bool p_enabled) = 0; // Returns OK if the socket option has been set successfully.
114
virtual void set_blocking_enabled(bool p_enabled) = 0;
115
virtual void set_ipv6_only_enabled(bool p_enabled) = 0;
116
virtual void set_tcp_no_delay_enabled(bool p_enabled) = 0;
117
virtual void set_reuse_address_enabled(bool p_enabled) = 0;
118
virtual Error join_multicast_group(const IPAddress &p_multi_address, const String &p_if_name) = 0;
119
virtual Error leave_multicast_group(const IPAddress &p_multi_address, const String &p_if_name) = 0;
120
121
virtual ~NetSocket() {}
122
};
123
124