1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * > www.orxonox.net < |
---|
4 | * |
---|
5 | * |
---|
6 | * License notice: |
---|
7 | * |
---|
8 | * This program is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the GNU General Public License |
---|
10 | * as published by the Free Software Foundation; either version 2 |
---|
11 | * of the License, or (at your option) any later version. |
---|
12 | * |
---|
13 | * This program is distributed in the hope that it will be useful, |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | * GNU General Public License for more details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU General Public License |
---|
19 | * along with this program; if not, write to the Free Software |
---|
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
21 | * |
---|
22 | * Author: |
---|
23 | * Sandro 'smerkli' Merkli |
---|
24 | * Co-authors: |
---|
25 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | #include <cstdlib> |
---|
30 | #include <cstdio> |
---|
31 | #include <cstring> |
---|
32 | #include <enet/enet.h> |
---|
33 | |
---|
34 | int main( int argc, char *argv[] ) |
---|
35 | { |
---|
36 | /* initialize Enet */ |
---|
37 | if (enet_initialize () != 0) |
---|
38 | { fprintf (stderr, "An error occurred while initializing ENet.\n"); |
---|
39 | return EXIT_FAILURE; |
---|
40 | } |
---|
41 | atexit (enet_deinitialize); |
---|
42 | |
---|
43 | /* client handle */ |
---|
44 | ENetHost *client; |
---|
45 | |
---|
46 | /* initiate the client */ |
---|
47 | client = enet_host_create( NULL /* create a client host */, |
---|
48 | 1, |
---|
49 | 2, /* allow up 2 channels to be used, 0 and 1 */ |
---|
50 | 0, |
---|
51 | 0 ); |
---|
52 | |
---|
53 | /* see if it worked */ |
---|
54 | if (client == NULL) |
---|
55 | { fprintf (stderr, |
---|
56 | "An error occurred while trying to create an ENet client host.\n"); |
---|
57 | exit (EXIT_FAILURE); |
---|
58 | } |
---|
59 | |
---|
60 | ENetAddress address; |
---|
61 | ENetEvent event; |
---|
62 | ENetPeer *peer; |
---|
63 | |
---|
64 | /* Connect to some.server.net:1234. */ |
---|
65 | enet_address_set_host (& address, argv[1] ); |
---|
66 | address.port = 1234; |
---|
67 | |
---|
68 | /* Initiate the connection, allocating the two channels 0 and 1. */ |
---|
69 | peer = enet_host_connect(client, &address, 2, 0); |
---|
70 | |
---|
71 | if (peer == NULL ) |
---|
72 | { fprintf( stderr, |
---|
73 | "No available peers for initiating an ENet connection.\n"); |
---|
74 | exit (EXIT_FAILURE); |
---|
75 | } |
---|
76 | |
---|
77 | /* Wait up to 5 seconds for the connection attempt to succeed. */ |
---|
78 | if (enet_host_service (client, & event, 5000) > 0 && |
---|
79 | event.type == ENET_EVENT_TYPE_CONNECT) |
---|
80 | { fprintf( stdout, "Connection to some.server.net:1234 succeeded." ); |
---|
81 | char *theinput = (char *)calloc( 100,1 ); |
---|
82 | while( true ) |
---|
83 | { |
---|
84 | fgets( theinput, 90, stdin ); |
---|
85 | if( !strncmp( theinput, "quit", 4 ) ) |
---|
86 | break; |
---|
87 | |
---|
88 | /* send the data to the friend */ |
---|
89 | /* Create a reliable packet of size 7 containing "packet\0" */ |
---|
90 | ENetPacket * packet = enet_packet_create( theinput, |
---|
91 | strlen( theinput ) + 1, |
---|
92 | ENET_PACKET_FLAG_RELIABLE); |
---|
93 | |
---|
94 | /* Send the packet to the peer over channel id 0. */ |
---|
95 | enet_peer_send (peer, 0, packet); |
---|
96 | |
---|
97 | /* One could just use enet_host_service() instead. */ |
---|
98 | enet_host_flush( client ); |
---|
99 | if( packet ) free( packet ); |
---|
100 | } |
---|
101 | } |
---|
102 | else |
---|
103 | { |
---|
104 | /* Either the 5 seconds are up or a disconnect event was */ |
---|
105 | /* received. Reset the peer in the event the 5 seconds */ |
---|
106 | /* had run out without any significant event. */ |
---|
107 | enet_peer_reset (peer); |
---|
108 | fprintf( stdout, "Connection to some.server.net:1234 failed." ); |
---|
109 | exit(EXIT_FAILURE); |
---|
110 | } |
---|
111 | |
---|
112 | enet_host_destroy(client); |
---|
113 | } |
---|