1 module gfm.enet.address;
2 
3 import std.string,
4        core.stdc.string;
5 
6 import derelict.enet.enet;
7 
8 import gfm.enet.enet;
9 
10 /// A wrapper for ENetAddress.
11 struct Address
12 {
13     ENetAddress address;
14 
15     alias address this;
16 
17     this(ENetAddress other) pure const nothrow
18     {
19         address = other;
20     }
21 
22     this(string hostName, ushort port)
23     {
24         auto errCode = enet_address_set_host(&address, hostName.toStringz());
25         if(errCode < 0)
26             throw new ENetException("enet_address_set_host failed");
27         address.port = port;
28     }
29 
30     this(uint host, ushort port) pure nothrow
31     {
32         address.host = host;
33         address.port = port;
34     }
35 
36     string host() const
37     {
38         enum MAX_LEN = 39; // Maximum ipv6 length 
39         char[MAX_LEN] buffer; 
40             
41         auto errCode = enet_address_get_host_ip(&address, buffer.ptr, MAX_LEN);
42         if(errCode < 0)
43             throw new ENetException("enet_address_get_host failed");
44 
45         size_t len = strlen(buffer.ptr);
46         return buffer[0..len].idup;
47     }
48 
49     void setHost(const char* hostName)
50     {
51         auto errCode = enet_address_set_host(&address, hostName);
52         if(errCode < 0)
53             throw new ENetException("enet_address_set_host failed");
54     }    
55 }
56 
57 static assert(ENetAddress.sizeof == Address.sizeof);