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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
#include "networked_multiplayer_enet.h"
void NetworkedMultiplayerENet::set_transfer_mode(TransferMode p_mode) {
transfer_mode=p_mode;
}
void NetworkedMultiplayerENet::set_target_peer(const StringName &p_peer){
target_peer=p_peer;
}
void NetworkedMultiplayerENet::set_channel(int p_channel){
send_channel=p_channel;
}
StringName NetworkedMultiplayerENet::get_packet_peer() const{
ERR_FAIL_COND_V(!active,StringName());
ERR_FAIL_COND_V(incoming_packets.size()==0,StringName());
return incoming_packets.front()->get().from;
}
int NetworkedMultiplayerENet::get_packet_channel() const{
ERR_FAIL_COND_V(!active,0);
ERR_FAIL_COND_V(incoming_packets.size()==0,0);
return incoming_packets.front()->get().from_channel;
}
Error NetworkedMultiplayerENet::create_server(int p_port, int p_max_clients, int p_max_channels, int p_in_bandwidth, int p_out_bandwidth){
ERR_FAIL_COND_V(active,ERR_ALREADY_IN_USE);
ENetAddress address;
address.host = ENET_HOST_ANY;
/* Bind the server to port 1234. */
address.port = 1234;
host = enet_host_create (& address /* the address to bind the server host to */,
p_max_clients /* allow up to 32 clients and/or outgoing connections */,
p_max_channels /* allow up to 2 channels to be used, 0 and 1 */,
p_in_bandwidth /* assume any amount of incoming bandwidth */,
p_out_bandwidth /* assume any amount of outgoing bandwidth */);
ERR_FAIL_COND_V(!host,ERR_CANT_CREATE);
active=true;
server=true;
connection_status=CONNECTION_CONNECTED;
return OK;
}
Error NetworkedMultiplayerENet::create_client(const IP_Address& p_ip,int p_port, int p_max_channels, int p_in_bandwidth, int p_out_bandwidth){
ERR_FAIL_COND_V(active,ERR_ALREADY_IN_USE);
host = enet_host_create (NULL /* create a client host */,
1 /* only allow 1 outgoing connection */,
p_max_channels /* allow up 2 channels to be used, 0 and 1 */,
p_in_bandwidth /* 56K modem with 56 Kbps downstream bandwidth */,
p_out_bandwidth /* 56K modem with 14 Kbps upstream bandwidth */);
ERR_FAIL_COND_V(!host,ERR_CANT_CREATE);
ENetAddress address;
address.host=p_ip.host;
address.port=p_port;
/* Initiate the connection, allocating the two channels 0 and 1. */
ENetPeer *peer = enet_host_connect (host, & address, p_max_channels, 0);
if (peer == NULL) {
enet_host_destroy(host);
ERR_FAIL_COND_V(!peer,ERR_CANT_CREATE);
}
//technically safe to ignore the peer or anything else.
connection_status=CONNECTION_CONNECTING;
active=true;
server=false;
return OK;
}
void NetworkedMultiplayerENet::poll(){
ERR_FAIL_COND(!active);
_pop_current_packet();
ENetEvent event;
/* Wait up to 1000 milliseconds for an event. */
while (enet_host_service (host, & event, 1000) > 0)
{
switch (event.type)
{
case ENET_EVENT_TYPE_CONNECT: {
/* Store any relevant client information here. */
IP_Address ip;
ip.host=event.peer -> address.host;
StringName *new_id = memnew( StringName );
*new_id = String(ip) +":"+ itos(event.peer -> address.port);
peer_map[*new_id]=event.peer;
connection_status=CONNECTION_CONNECTED; //if connecting, this means it connected t something!
emit_signal("peer_connected",*new_id);
} break;
case ENET_EVENT_TYPE_DISCONNECT: {
/* Reset the peer's client information. */
StringName *id = (StringName*)event.peer -> data;
emit_signal("peer_disconnected",*id);
peer_map.erase(*id);
memdelete( id );
} break;
case ENET_EVENT_TYPE_RECEIVE: {
Packet packet;
packet.packet = event.packet;
StringName *id = (StringName*)event.peer -> data;
packet.from_channel=event.channelID;
packet.from=*id;
incoming_packets.push_back(packet);
//destroy packet later..
}break;
case ENET_EVENT_TYPE_NONE: {
//do nothing
} break;
}
}
}
bool NetworkedMultiplayerENet::is_server() const {
ERR_FAIL_COND_V(!active,false);
return server;
}
void NetworkedMultiplayerENet::close_connection() {
ERR_FAIL_COND(!active);
_pop_current_packet();
enet_host_destroy(host);
active=false;
incoming_packets.clear();
connection_status=CONNECTION_DISCONNECTED;
}
int NetworkedMultiplayerENet::get_available_packet_count() const {
return incoming_packets.size();
}
Error NetworkedMultiplayerENet::get_packet(const uint8_t **r_buffer,int &r_buffer_size) const{
ERR_FAIL_COND_V(incoming_packets.size()==0,ERR_UNAVAILABLE);
_pop_current_packet();
current_packet = incoming_packets.front()->get();
incoming_packets.pop_front();
r_buffer=(const uint8_t**)¤t_packet.packet->data;
r_buffer_size=current_packet.packet->dataLength;
return OK;
}
Error NetworkedMultiplayerENet::put_packet(const uint8_t *p_buffer,int p_buffer_size){
ERR_FAIL_COND_V(incoming_packets.size()==0,ERR_UNAVAILABLE);
Map<StringName,ENetPeer*>::Element *E=NULL;
if (target_peer!=StringName()) {
peer_map.find(target_peer);
if (!E) {
ERR_EXPLAIN("Invalid Target Peer: "+String(target_peer));
ERR_FAIL_V(ERR_INVALID_PARAMETER);
}
}
int packet_flags=0;
switch(transfer_mode) {
case TRANSFER_MODE_UNRELIABLE: {
packet_flags=ENET_PACKET_FLAG_UNSEQUENCED;
} break;
case TRANSFER_MODE_RELIABLE: {
packet_flags=ENET_PACKET_FLAG_RELIABLE;
} break;
case TRANSFER_MODE_ORDERED: {
packet_flags=ENET_PACKET_FLAG_RELIABLE;
} break;
}
/* Create a reliable packet of size 7 containing "packet\0" */
ENetPacket * packet = enet_packet_create (p_buffer,p_buffer_size,packet_flags);
if (target_peer==StringName()) {
enet_host_broadcast(host,send_channel,packet);
} else {
enet_peer_send (E->get(), send_channel, packet);
}
enet_host_flush(host);
return OK;
}
int NetworkedMultiplayerENet::get_max_packet_size() const {
return 1<<24; //anything is good
}
void NetworkedMultiplayerENet::_pop_current_packet() const {
if (current_packet.packet) {
enet_packet_destroy(current_packet.packet);
current_packet.packet=NULL;
current_packet.from=StringName();
}
}
NetworkedMultiplayerPeer::ConnectionStatus NetworkedMultiplayerENet::get_connection_status() const {
return connection_status;
}
void NetworkedMultiplayerENet::_bind_methods() {
ObjectTypeDB::bind_method(_MD("create_server","port","max_clients","max_channels","in_bandwidth","out_bandwidth"),&NetworkedMultiplayerENet::create_server,DEFVAL(32),DEFVAL(1),DEFVAL(0),DEFVAL(0));
ObjectTypeDB::bind_method(_MD("create_client","ip","port","max_channels","in_bandwidth","out_bandwidth"),&NetworkedMultiplayerENet::create_client,DEFVAL(1),DEFVAL(0),DEFVAL(0));
ObjectTypeDB::bind_method(_MD("disconnect"),&NetworkedMultiplayerENet::disconnect);
}
NetworkedMultiplayerENet::NetworkedMultiplayerENet(){
active=false;
server=false;
send_channel=0;
current_packet.packet=NULL;
transfer_mode=TRANSFER_MODE_ORDERED;
connection_status=CONNECTION_DISCONNECTED;
}
NetworkedMultiplayerENet::~NetworkedMultiplayerENet(){
if (active) {
close_connection();
}
}
|