1 /**
2 Copyright: Copyright (c) 2014 Andrey Penechko.
3 License: a$(WEB boost.org/LICENSE_1_0.txt, Boost License 1.0).
4 Authors: Andrey Penechko.
5 */
6 
7 module clientstorage;
8 
9 import derelict.enet.enet;
10 import connection;
11 
12 // Needs at least peer field.
13 // struct Client
14 // {
15 //     ENetPeer* peer;
16 // }
17 
18 struct ClientStorage(Client)
19 {
20 	import std.traits : hasMember;
21 	static assert(hasMember!(Client, "peer") &&
22 		is(typeof(Client.peer) == ENetPeer*),
23 		"Client type must have peer member");
24 
25 	Client*[ClientId] clients;
26 
27 	ClientId addClient(ENetPeer* peer)
28 	{
29 		ClientId id = nextPeerId;
30 		Client* client = new Client;
31 		client.peer = peer;
32 		clients[id] = client;
33 		return id;
34 	}
35 
36 	Client* opIndex(ClientId id)
37 	{
38 		return clients.get(id, null);
39 	}
40 
41 	void removeClient(ClientId id)
42 	{
43 		clients.remove(id);
44 	}
45 
46 	ENetPeer* clientPeer(ClientId id)
47 	{
48 		return clients[id].peer;
49 	}
50 
51 	size_t length()
52 	{
53 		return clients.length;
54 	}
55 
56 	ClientId nextPeerId() @property
57 	{
58 		return _nextClientId++;
59 	}
60 
61 	// 0 is reserved for server.
62 	private ClientId _nextClientId = 1;
63 }