Debian Codegraph @

test/enet/nat_punchthrough/client/main.cpp

raw
1
2
3 #include <stdio.h>
4 #include <enet/enet.h>
5 #include <string.h>
6
7 int main(int argc, char* argv[])
8 {
9 printf("starting client (and server)\n");
10
11 if (enet_initialize() != 0)
12 {
13 fprintf(stderr, "An error occurred while initializing ENet.\n");
14 return EXIT_FAILURE;
15 }
16 atexit(enet_deinitialize);
17
18 ENetAddress selfaddress;
19 selfaddress.host = ENET_HOST_ANY;
20 /* Bind the server to port 1111. */
21 selfaddress.port = 1111;
22
23 ENetHost* client = 0;
24 while (!client)
25 {
26 client = enet_host_create(&selfaddress /* create a client host */,
27 32 /* only 32 connections */,
28 2 /* allow up 2 channels to be used, 0 and 1 */,
29 0 /*57600 / 8 56K modem with 56 Kbps downstream bandwidth */,
30 0 /* 14400 / 8 56K modem with 14 Kbps upstream bandwidth */);
31 if (client == NULL)
32 {
33 selfaddress.port++;
34 }
35 }
36 if (client == NULL)
37 {
38 fprintf(stderr,
39 "An error occurred while trying to create an ENet client host.\n");
40 exit(EXIT_FAILURE);
41 }
42
43 ENetAddress dedicatedserveraddress;
44 ENetEvent event;
45 ENetPeer* dedicatedpeer = 0;
46 ENetPeer* natpeer = 0;
47
48 /* Connect to some.server.net:1234. */
49 enet_address_set_host(&dedicatedserveraddress, "localhost");
50 dedicatedserveraddress.port = 1234;
51 /* Initiate the connection, allocating the two channels 0 and 1. */
52 dedicatedpeer = enet_host_connect(client, &dedicatedserveraddress, 2, 0);
53 if (dedicatedpeer == NULL)
54 {
55 fprintf(stderr, "No available peers for initiating an ENet connection.\n");
56 exit(EXIT_FAILURE);
57 }
58 /* Wait up to 5 seconds for the connection attempt to succeed. */
59 if (enet_host_service(client, &event, 5000) > 0 &&
60 event.type == ENET_EVENT_TYPE_CONNECT)
61 {
62 char servername[1024];
63 enet_address_get_host(&dedicatedserveraddress, servername, 1024);
64 char serverinfo[1024];
65
66 sprintf(serverinfo, "Connection to %s:%d succeeded", servername, dedicatedserveraddress.port);
67 puts(serverinfo);
68
69 /////....
70
71 /* Wait up to 1000 milliseconds for an event. */
72 while (enet_host_service(client, &event, 1000000000) > 0)
73 {
74 if (natpeer)
75 {
76 /* Create a reliable packet of size 7 containing "packet\0" */
77 ENetPacket* packet = enet_packet_create("packet",
78 strlen("packet") + 1,
79 ENET_PACKET_FLAG_RELIABLE);
80 /* Extend the packet so and append the string "foo", so it now */
81 /* contains "packetfoo\0" */
82 enet_packet_resize(packet, strlen("packetfoo") + 1);
83 strcpy((char*)&packet->data[strlen("packet")], "foo");
84 /* Send the packet to the peer over channel id 0. */
85 /* One could also broadcast the packet by */
86 /* enet_host_broadcast (host, 0, packet); */
87 enet_peer_send(natpeer, 0, packet);
88 }
89 switch (event.type)
90 {
91 case ENET_EVENT_TYPE_CONNECT:
92 printf("A new client connected from %x:%u.\n",
93 event.peer->address.host,
94 event.peer->address.port);
95 /* Store any relevant client information here. */
96 event.peer->data = (char*)"Client information";
97 break;
98 case ENET_EVENT_TYPE_RECEIVE:
99 printf("A packet of length %u containing %s was received from %s on channel %u.\n",
100 event.packet->dataLength,
101 event.packet->data,
102 event.peer->data,
103 event.channelID);
104 /* Clean up the packet now that we're done using it. */
105
106 if (event.packet->dataLength == sizeof(ENetAddress))
107 {
108 ENetAddress* address = (ENetAddress*)event.packet->data;
109 printf("received other client's address from server, connecting...\n");
110 natpeer = enet_host_connect(client, address, 2, 0);
111 if (natpeer == NULL)
112 {
113 fprintf(stderr, "No available peers for initiating an ENet connection.\n");
114 exit(EXIT_FAILURE);
115 }
116 /* Wait up to 5 seconds for the connection attempt to succeed. */
117 if (enet_host_service(client, &event, 5000) > 0 &&
118 event.type == ENET_EVENT_TYPE_CONNECT)
119 {
120 puts("Connection to natpeer succeeded.");
121 }
122 else
123 {
124 enet_peer_reset(natpeer);
125 puts("Connection to natpeer failed.");
126 natpeer = 0;
127 exit(0);
128 }
129 }
130
131 enet_packet_destroy(event.packet);
132 break;
133
134 case ENET_EVENT_TYPE_DISCONNECT:
135 printf("%s disconnected.\n", event.peer->data);
136 /* Reset the peer's client information. */
137 event.peer->data = NULL;
138 }
139 }
140 /* One could just use enet_host_service() instead. */
141 enet_host_flush(client); //host);
142 }
143 else
144 {
145 /* Either the 5 seconds are up or a disconnect event was */
146 /* received. Reset the peer in the event the 5 seconds */
147 /* had run out without any significant event. */
148 enet_peer_reset(dedicatedpeer);
149 puts("Connection to some.server.net:1234 failed.");
150 }
151
152 enet_host_destroy(client);
153
154 return 0;
155 }