-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.d
54 lines (42 loc) · 1.02 KB
/
server.d
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
module network.server;
import libasync;
import std.stdio;
import network.client;
import vendor;
public class Server {
AsyncTCPListener m_listener;
private Client[int] clients;
this(string host, size_t port, EventLoop evl) {
m_listener = new AsyncTCPListener(evl);
if (m_listener.host(host, port).run(&handler))
writeln("Listening to ", m_listener.local.toString());
}
void delegate(TCPEvent) handler(AsyncTCPConnection conn) {
auto tcpConn = new Client(conn);
this.clients[this.clients.length] = tcpConn;
return &tcpConn.handler;
}
public void send(int clientId, string msg)
{
auto enc = Cerealizer();
enc ~= cast(string)msg;
send(clientId, enc);
}
public void send(int clientId, Cerealizer writer)
{
auto client = this.clients.get(clientId, null);
if(client !is null)
send(client, writer);
}
public void send(Client client, Cerealizer writer)
{
client.send(writer);
}
public void sendBroadcast(Cerealizer writer)
{
foreach(client; this.clients)
{
client.send(writer);
}
}
}