Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/enet/enet_multiplayer_peer.h
10277 views
1
/**************************************************************************/
2
/* enet_multiplayer_peer.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 "enet_connection.h"
34
35
#include "core/crypto/crypto.h"
36
#include "scene/main/multiplayer_peer.h"
37
38
#include <enet/enet.h>
39
40
class ENetMultiplayerPeer : public MultiplayerPeer {
41
GDCLASS(ENetMultiplayerPeer, MultiplayerPeer);
42
43
private:
44
enum {
45
SYSMSG_ADD_PEER,
46
SYSMSG_REMOVE_PEER
47
};
48
49
enum {
50
SYSCH_RELIABLE = 0,
51
SYSCH_UNRELIABLE = 1,
52
SYSCH_MAX = 2
53
};
54
55
enum Mode {
56
MODE_NONE,
57
MODE_SERVER,
58
MODE_CLIENT,
59
MODE_MESH,
60
};
61
62
Mode active_mode = MODE_NONE;
63
64
uint32_t unique_id = 0;
65
66
int target_peer = 0;
67
68
ConnectionStatus connection_status = CONNECTION_DISCONNECTED;
69
70
HashMap<int, Ref<ENetConnection>> hosts;
71
HashMap<int, Ref<ENetPacketPeer>> peers;
72
73
struct Packet {
74
ENetPacket *packet = nullptr;
75
int from = 0;
76
int channel = 0;
77
TransferMode transfer_mode = TRANSFER_MODE_RELIABLE;
78
};
79
80
List<Packet> incoming_packets;
81
82
Packet current_packet;
83
84
void _store_packet(int32_t p_source, ENetConnection::Event &p_event);
85
void _pop_current_packet();
86
void _disconnect_inactive_peers();
87
void _destroy_unused(ENetPacket *p_packet);
88
_FORCE_INLINE_ bool _is_active() const { return active_mode != MODE_NONE; }
89
90
IPAddress bind_ip;
91
92
protected:
93
static void _bind_methods();
94
95
public:
96
virtual void set_target_peer(int p_peer) override;
97
98
virtual int get_packet_peer() const override;
99
virtual TransferMode get_packet_mode() const override;
100
virtual int get_packet_channel() const override;
101
102
virtual void poll() override;
103
virtual void close() override;
104
virtual void disconnect_peer(int p_peer, bool p_force = false) override;
105
106
virtual bool is_server() const override;
107
virtual bool is_server_relay_supported() const override;
108
109
// Overridden so we can instrument the DTLSServer when needed.
110
virtual void set_refuse_new_connections(bool p_enabled) override;
111
112
virtual ConnectionStatus get_connection_status() const override;
113
114
virtual int get_unique_id() const override;
115
116
virtual int get_max_packet_size() const override;
117
virtual int get_available_packet_count() const override;
118
virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size) override;
119
virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size) override;
120
121
Error create_server(int p_port, int p_max_clients = 32, int p_max_channels = 0, int p_in_bandwidth = 0, int p_out_bandwidth = 0);
122
Error create_client(const String &p_address, int p_port, int p_channel_count = 0, int p_in_bandwidth = 0, int p_out_bandwidth = 0, int p_local_port = 0);
123
Error create_mesh(int p_id);
124
Error add_mesh_peer(int p_id, Ref<ENetConnection> p_host);
125
126
void set_bind_ip(const IPAddress &p_ip);
127
128
Ref<ENetConnection> get_host() const;
129
Ref<ENetPacketPeer> get_peer(int p_id) const;
130
131
ENetMultiplayerPeer();
132
~ENetMultiplayerPeer();
133
};
134
135