-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectionsToClients.cpp
More file actions
139 lines (126 loc) · 5.35 KB
/
ConnectionsToClients.cpp
File metadata and controls
139 lines (126 loc) · 5.35 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
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
/******************************************************************************/
// ⚠️ ONE translation unit must *both* `#define ENET_IMPLEMENTATION` *and*
// `#include "enet.h"` – put it right above the rest of your includes.
#define ENET_IMPLEMENTATION
#include "enet.h" // ← Header-only ENet (third_party/enet/enet.h)
#include <vector>
#include <cfloat>
/******************************************************************************/
#if defined(__linux__)
/* Keep a copy for later code that might really need the macros… */
#pragma push_macro("LOCK_READ")
#pragma push_macro("LOCK_WRITE")
#undef LOCK_READ
#undef LOCK_WRITE
#endif
/******************************************************************************/
#include "stdafx.h"
#include "@@headers.h"
#include "ConnectionsToClients.h"
#if defined(__linux__)
/* …and restore them so any <fcntl.h>-style users still compile. */
#pragma pop_macro("LOCK_WRITE")
#pragma pop_macro("LOCK_READ")
#endif
/******************************************************************************/
ENetHost *gServer = nullptr; // listens on 127.0.0.1:12345
Int clientCount = 0;
Int packetsRecv = 0;
const int SERVER_ID = 0; // ID used when sending server state
std::vector<ClientInfo> gClients;
static int gNextId = 1;
/******************************************************************************/
// Local helpers
void SetupEnet()
{
if(enet_initialize() != 0){ LogN("ENet init failed"); return; }
ENetAddress addr{}; // zero-initialise **all** fields
addr.port = 12345;
addr.host = ENET_HOST_ANY; // or enet_address_set_host_ip(&addr, "::")
gServer = enet_host_create(&addr, 32, 2, 0, 0);
if(!gServer){
LogN(S+"ENet server create failed.");
LogN(S+"ENet server create failed error: " + strerror(errno));
enet_deinitialize();
return;
}
LogN("ENet server setup complete, waiting for clients...");
}
void ServiceHost(ENetHost *host)
{
if(!host) return;
ENetEvent e;
while(enet_host_service(host, &e, 0) > 0) // non-blocking pump
{
switch(e.type)
{
case ENET_EVENT_TYPE_CONNECT:
{
LogN(S+"[ENet] Peer connected, ch:" + int(e.channelID));
ClientInfo ci; ci.peer=e.peer; ci.id=gNextId++; ci.pos=Vec2(0,0);
gClients.push_back(ci);
e.peer->data=(Ptr)(intptr_t)ci.id;
// shorten timeouts so dropped clients are detected quickly
enet_peer_timeout(e.peer, 2, 1000, 2000);
++clientCount;
// notify existing clients about new one
DotPacket pkt{ci.id, ci.pos.x, ci.pos.y};
for(auto &c:gClients){
ENetPacket *p=enet_packet_create(&pkt,sizeof(pkt),ENET_PACKET_FLAG_RELIABLE);
enet_peer_send(c.peer,0,p);
}
enet_host_flush(host);
// send all other clients to the new one
for(auto &c:gClients){
if(c.id==ci.id) continue;
DotPacket p2{c.id,c.pos.x,c.pos.y};
ENetPacket *pkt2=enet_packet_create(&p2,sizeof(p2),ENET_PACKET_FLAG_RELIABLE);
enet_peer_send(ci.peer,0,pkt2);
}
// also send server position
{
DotPacket srv{SERVER_ID, dot_pos.x, dot_pos.y};
ENetPacket *pkt2=enet_packet_create(&srv,sizeof(srv),ENET_PACKET_FLAG_RELIABLE);
enet_peer_send(ci.peer,0,pkt2);
enet_host_flush(host);
}
enet_host_flush(host);
}break;
case ENET_EVENT_TYPE_RECEIVE:
{
if(e.packet->dataLength==sizeof(DotPacket))
{
DotPacket pkt; CopyFast(pkt, *(DotPacket*)e.packet->data);
int id=(int)(intptr_t)e.peer->data; pkt.id=id;
for(auto &c:gClients) if(c.id==id){c.pos.set(pkt.x,pkt.y);break;}
for(auto &c:gClients){
ENetPacket *p=enet_packet_create(&pkt,sizeof(pkt),ENET_PACKET_FLAG_UNSEQUENCED);
enet_peer_send(c.peer,0,p);
}
enet_host_flush(host);
++packetsRecv;
}else{
Str8 txt((char8*)e.packet->data); // UTF-8 payload
LogN(S + "[ENet] Got pkt (\"" + txt + "\")");
}
enet_packet_destroy(e.packet);
break;
}
case ENET_EVENT_TYPE_DISCONNECT:
{
LogN("[ENet] Peer disconnected");
int id=(int)(intptr_t)e.peer->data;
for(auto it=gClients.begin(); it!=gClients.end(); ++it) if(it->id==id){ gClients.erase(it); break; }
DotPacket pkt{id, FLT_MAX, FLT_MAX};
for(auto &c:gClients){
ENetPacket *p=enet_packet_create(&pkt,sizeof(pkt),ENET_PACKET_FLAG_RELIABLE);
enet_peer_send(c.peer,0,p);
}
enet_host_flush(host);
--clientCount;
}break;
default: break;
}
}
}
/******************************************************************************/