| [1505] | 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 |  *      Oliver Scheuss, (C) 2007 | 
|---|
 | 24 |  *   Co-authors: | 
|---|
 | 25 |  *      ... | 
|---|
 | 26 |  * | 
|---|
 | 27 |  */ | 
|---|
 | 28 |  | 
|---|
 | 29 | //o | 
|---|
 | 30 | // Dummy client to test ConnectionManager and PacketBuffer classes | 
|---|
 | 31 | // | 
|---|
 | 32 | // Author: Oliver Scheuss | 
|---|
 | 33 |  | 
|---|
 | 34 | #include <iostream> | 
|---|
 | 35 | #include <string> | 
|---|
 | 36 | #include <enet/enet.h> | 
|---|
 | 37 |  | 
|---|
 | 38 | #include "util/Sleep.h" | 
|---|
 | 39 | #include "PacketManager.h" | 
|---|
| [1168] | 40 | #include "PacketTypes.h" | 
|---|
| [1505] | 41 |  | 
|---|
 | 42 | using namespace std; | 
|---|
 | 43 |  | 
|---|
 | 44 | int main(){ | 
|---|
 | 45 |   ENetHost * client; | 
|---|
 | 46 |   ENetAddress address; | 
|---|
 | 47 |   ENetEvent event; | 
|---|
 | 48 |   ENetPeer *peer; | 
|---|
 | 49 |   network::PacketGenerator pck; | 
|---|
 | 50 |  | 
|---|
 | 51 |   enet_initialize(); | 
|---|
 | 52 |   atexit(enet_deinitialize); | 
|---|
 | 53 |  | 
|---|
 | 54 |   cout << "Enter address of the server xxx.xxx.xxx.xxx (enter for localhost)" << endl; | 
|---|
 | 55 |   string str; | 
|---|
 | 56 |   getline(cin, str); | 
|---|
 | 57 |   cout << "You entered: " << str << endl; | 
|---|
 | 58 |   if(str.compare("")==0) | 
|---|
 | 59 |     str="127.0.0.1"; | 
|---|
 | 60 |  | 
|---|
 | 61 |   enet_address_set_host(&address, str.c_str()); | 
|---|
 | 62 |   address.port = 55556; | 
|---|
 | 63 |  | 
|---|
 | 64 |   // create client object | 
|---|
 | 65 |   client = enet_host_create(NULL, 2, 0, 0); | 
|---|
 | 66 |  | 
|---|
 | 67 |   if(client==NULL){ | 
|---|
 | 68 |     fprintf(stderr, "An error occured"); | 
|---|
 | 69 |     exit(EXIT_FAILURE); | 
|---|
 | 70 |   } | 
|---|
 | 71 |   // connect peer | 
|---|
 | 72 |   peer = enet_host_connect(client, &address, 2); | 
|---|
 | 73 |   if(peer==NULL){ | 
|---|
 | 74 |     fprintf(stderr, "Peer establishing error"); | 
|---|
 | 75 |     exit(EXIT_FAILURE); | 
|---|
 | 76 |   } | 
|---|
 | 77 |   // wait 5 seconds for the connection attempt to succeed | 
|---|
 | 78 |   if(enet_host_service(client, &event, 5000) > 0 && event.type == ENET_EVENT_TYPE_CONNECT){ | 
|---|
 | 79 |     cout << "Connection to " << str << " succeeded." << endl; | 
|---|
 | 80 |     //puts("Connection to localhost:5555 succeeded."); | 
|---|
 | 81 |   }else{ | 
|---|
 | 82 |     enet_peer_reset(peer); | 
|---|
 | 83 |     cout << "Connection to " << str << " failed." << endl; | 
|---|
 | 84 |     //puts("Connection to localhost:5555 failed."); | 
|---|
 | 85 |     exit(EXIT_FAILURE); | 
|---|
 | 86 |   } | 
|---|
 | 87 |  | 
|---|
 | 88 |   for(int i=0; i<10; i++){ | 
|---|
 | 89 |     // weihnachtsmann bringt packete | 
|---|
 | 90 |     //ENetPacket *packet = enet_packet_create ("packet1234", strlen("packet1234") + 1, ENET_PACKET_FLAG_RELIABLE); | 
|---|
 | 91 |     // extend the packet and append the string foo to it | 
|---|
 | 92 |     // send packet to peer on channel id 0 | 
|---|
 | 93 |     enet_peer_send(peer, 1, pck.chatMessage("test2")); | 
|---|
 | 94 |     // keep the timeout very small for low delay | 
|---|
 | 95 |     if(enet_host_service(client, &event, 1)==0){ | 
|---|
 | 96 |       cout << "successfully sent: " << event.type << endl; | 
|---|
 | 97 |     }else{ | 
|---|
 | 98 |       cout << "failed sending" << endl; | 
|---|
 | 99 |     } | 
|---|
 | 100 |     usleep(1000000); | 
|---|
 | 101 |   } | 
|---|
 | 102 |  | 
|---|
 | 103 |   // now disconnect | 
|---|
 | 104 |   //   enet_peer_disconnect (peer); | 
|---|
 | 105 |   enet_peer_disconnect (peer, 0); | 
|---|
 | 106 |   // 3 seconds timeout | 
|---|
 | 107 |   while(enet_host_service(client, &event, 3000) > 0){ | 
|---|
 | 108 |     switch (event.type) | 
|---|
 | 109 |     { | 
|---|
 | 110 |     case ENET_EVENT_TYPE_RECEIVE: | 
|---|
 | 111 |       enet_packet_destroy(event.packet); | 
|---|
 | 112 |       break; | 
|---|
 | 113 |     case ENET_EVENT_TYPE_DISCONNECT: | 
|---|
 | 114 |       puts("Disconnection succeeded."); | 
|---|
 | 115 |       return 0; | 
|---|
 | 116 |     } | 
|---|
 | 117 |   } | 
|---|
 | 118 |   // if disconnect failed | 
|---|
 | 119 |   enet_peer_reset(peer); | 
|---|
 | 120 |  | 
|---|
 | 121 |  | 
|---|
 | 122 |  | 
|---|
 | 123 |   return 0; | 
|---|
 | 124 | } | 
|---|