5 void runServer (string
[] args
, bool compress
) {
9 /* Bind the server to the default localhost. */
10 /* A specific host address can be specified by */
11 /* enet_address_set_host (& address, "x.x.x.x"); */
12 address
.host
= ENET_HOST_ANY
;
13 /* Bind the server to port 1234. */
15 server
= enet_host_create(
16 &address
, /* the address to bind the server host to */
17 32, /* allow up to 32 clients and/or outgoing connections */
18 2, /* allow up to 2 channels to be used, 0 and 1 */
19 0, /* assume any amount of incoming bandwidth */
20 0, /* assume any amount of outgoing bandwidth */);
21 if (server
is null) throw new Exception("An error occurred while trying to create an ENet server host.");
22 scope(exit
) enet_host_destroy(server
);
23 if (compress
) enet_host_compress_with_range_coder(server
);
28 /* Wait up to 1000 milliseconds for an event. */
29 while (enet_host_service(server
, &event
, 1000) > 0) {
31 case ENET_EVENT_TYPE_CONNECT
:
32 writefln
!"A new client connected from %x:%s."(event
.peer
.address
.host
, event
.peer
.address
.port
);
33 /* Store any relevant client information here. */
34 event
.peer
.data
= "Client information\0".dup
.ptr
;
36 /* Create a reliable packet of size 7 containing "packet\0" */
37 ENetPacket
* packet
= enet_packet_create("packet\0".ptr
, 7, ENET_PACKET_FLAG_RELIABLE
);
38 /* Extend the packet so and append the string "foo", so it now */
39 /* contains "packetfoo\0" */
40 //enet_packet_resize(packet, strlen("packetfoo")+1);
41 //strcpy(&packet.data[strlen("packet")], "foo");
43 /* Send the packet to the peer over channel id 0. */
44 /* One could also broadcast the packet by */
45 /* enet_host_broadcast(host, 0, packet); */
46 enet_peer_send(event
.peer
, 0, packet
);
48 /* One could just use enet_host_service() instead. */
49 //enet_host_flush (host);
51 case ENET_EVENT_TYPE_RECEIVE
:
52 writefln
!"A packet of length %s containing %s was received from %s on channel %s."(event
.packet
.dataLength
, event
.packet
.data
, event
.peer
.data
, event
.channelID
);
53 write(" ["); for (int f
= 0; f
< event
.packet
.dataLength
; ++f
) write(cast(char)event
.packet
.data
[f
]); write("]\n");
54 /* Clean up the packet now that we're done using it. */
55 enet_packet_destroy(event
.packet
);
57 case ENET_EVENT_TYPE_DISCONNECT
:
58 writefln
!"%s disconnected."(event
.peer
.data
);
59 /* Reset the peer's client information. */
60 event
.peer
.data
= null;
69 ENetPeer
*connectToServer (ENetHost
* client
) {
74 /* Connect to localhost:1234. */
75 enet_address_set_host(&address
, "localhost");
77 /* Initiate the connection, allocating the two channels 0 and 1. */
78 peer
= enet_host_connect(client
, &address
, 2, 0);
79 if (peer
is null) throw new Exception("No available peers for initiating an ENet connection.");
81 /* Wait up to 5 seconds for the connection attempt to succeed. */
82 if (enet_host_service(client
, &event
, 5000) > 0 && event
.type
== ENET_EVENT_TYPE_CONNECT
) {
86 /* Either the 5 seconds are up or a disconnect event was */
87 /* received. Reset the peer in the event the 5 seconds */
88 /* had run out without any significant event. */
89 enet_peer_reset(peer
);
94 void runClient (string
[] args
, bool compress
) {
97 client
= enet_host_create(
98 null, /* create a client host */
99 1, /* only allow 1 outgoing connection */
100 2, /* allow up 2 channels to be used, 0 and 1 */
101 57600/8, /* 56K modem with 56 Kbps downstream bandwidth */
102 14400/8, /* 56K modem with 14 Kbps upstream bandwidth */);
103 if (client
is null) throw new Exception("An error occurred while trying to create an ENet client host.");
104 scope(exit
) enet_host_destroy(client
);
105 if (compress
) enet_host_compress_with_range_coder(client
);
107 writeln("connecting to server...");
108 auto peer
= connectToServer(client
);
110 writeln("connection failed!");
114 /* Create a reliable packet of size 6 containing "hello\0" */
115 ENetPacket
* packet
= enet_packet_create("hello\0".ptr
, 6, ENET_PACKET_FLAG_RELIABLE
);
117 /* Send the packet to the peer over channel id 0. */
118 enet_peer_send(peer
, 0, packet
);
123 /* Wait up to 1000 milliseconds for an event. */
124 while (enet_host_service(client
, &event
, 1000) > 0) {
125 switch (event
.type
) {
126 case ENET_EVENT_TYPE_CONNECT
:
128 writefln
!"WTF?! A new client connected from %x:%s."(event
.peer
.address
.host
, event
.peer
.address
.port
);
129 /* Store any relevant client information here. */
130 //event.peer.data = "Client information\0".dup.ptr;
131 enet_peer_reset(event
.peer
);
133 case ENET_EVENT_TYPE_RECEIVE
:
134 writefln
!"A packet of length %s containing %s was received from %s on channel %s."(event
.packet
.dataLength
, event
.packet
.data
, event
.peer
.data
, event
.channelID
);
135 write(" ["); for (int f
= 0; f
< event
.packet
.dataLength
; ++f
) write(cast(char)event
.packet
.data
[f
]); write("]\n");
136 /* Clean up the packet now that we're done using it. */
137 enet_packet_destroy(event
.packet
);
139 case ENET_EVENT_TYPE_DISCONNECT
:
140 writefln
!"%s disconnected."(event
.peer
.data
);
141 /* Reset the peer's client information. */
142 event
.peer
.data
= null;
151 void main (string
[] args
) {
152 enum Mode
{ unknown
, client
, server
}
153 auto mode
= Mode
.unknown
;
154 bool compress
= false;
156 foreach (string arg
; args
[1..$]) {
157 if (arg
== "client") mode
= Mode
.client
;
158 else if (arg
== "server") mode
= Mode
.server
;
159 else if (arg
== "compress") compress
= true;
160 else throw new Exception("'"~arg
~"': what?");
162 if (mode
== Mode
.unknown
) throw new Exception("client or server?");
163 if (enet_initialize() != 0) throw new Exception("An error occurred while initializing ENet.");
164 scope(exit
) enet_deinitialize();
166 writeln("compression: ", (compress ?
"tan" : "ona"));
167 if (mode
== Mode
.client
) runClient(args
[2..$], compress
);
168 else runServer(args
[2..$], compress
);