| 1 | /* |
|---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
|---|
| 3 | * |
|---|
| 4 | * |
|---|
| 5 | * License notice: |
|---|
| 6 | * |
|---|
| 7 | * This program is free software; you can redistribute it and/or |
|---|
| 8 | * modify it under the terms of the GNU General Public License |
|---|
| 9 | * as published by the Free Software Foundation; either version 2 |
|---|
| 10 | * of the License, or (at your option) any later version. |
|---|
| 11 | * |
|---|
| 12 | * This program is distributed in the hope that it will be useful, |
|---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 15 | * GNU General Public License for more details. |
|---|
| 16 | * |
|---|
| 17 | * You should have received a copy of the GNU General Public License |
|---|
| 18 | * along with this program; if not, write to the Free Software |
|---|
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|---|
| 20 | * |
|---|
| 21 | * Author: |
|---|
| 22 | * Oliver Scheuss, (C) 2007 |
|---|
| 23 | * Co-authors: |
|---|
| 24 | * ... |
|---|
| 25 | * |
|---|
| 26 | */ |
|---|
| 27 | |
|---|
| 28 | //o |
|---|
| 29 | // Dummy client to test ConnectionManager and PacketBuffer classes |
|---|
| 30 | // |
|---|
| 31 | // Author: Oliver Scheuss |
|---|
| 32 | |
|---|
| 33 | #include <iostream> |
|---|
| 34 | #include <string> |
|---|
| 35 | #include <enet/enet.h> |
|---|
| 36 | #include "PacketManager.h" |
|---|
| 37 | |
|---|
| 38 | #ifdef WIN32 |
|---|
| 39 | #include <windows.h> |
|---|
| 40 | #define usleep(x) Sleep((x)/1000) |
|---|
| 41 | #else |
|---|
| 42 | #include <unistd.h> |
|---|
| 43 | #endif |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | using namespace std; |
|---|
| 47 | |
|---|
| 48 | int main(){ |
|---|
| 49 | ENetHost * client; |
|---|
| 50 | ENetAddress address; |
|---|
| 51 | ENetEvent event; |
|---|
| 52 | ENetPeer *peer; |
|---|
| 53 | network::PacketGenerator pck; |
|---|
| 54 | |
|---|
| 55 | enet_initialize(); |
|---|
| 56 | atexit(enet_deinitialize); |
|---|
| 57 | |
|---|
| 58 | cout << "Enter address of the server xxx.xxx.xxx.xxx (enter for localhost)" << endl; |
|---|
| 59 | string str; |
|---|
| 60 | getline(cin, str); |
|---|
| 61 | cout << "You entered: " << str << endl; |
|---|
| 62 | if(str.compare("")==0) |
|---|
| 63 | str="127.0.0.1"; |
|---|
| 64 | |
|---|
| 65 | enet_address_set_host(&address, str.c_str()); |
|---|
| 66 | address.port = 55556; |
|---|
| 67 | |
|---|
| 68 | // create client object |
|---|
| 69 | client = enet_host_create(NULL, 2, 0, 0); |
|---|
| 70 | |
|---|
| 71 | if(client==NULL){ |
|---|
| 72 | fprintf(stderr, "An error occured"); |
|---|
| 73 | exit(EXIT_FAILURE); |
|---|
| 74 | } |
|---|
| 75 | // connect peer |
|---|
| 76 | peer = enet_host_connect(client, &address, 2); |
|---|
| 77 | if(peer==NULL){ |
|---|
| 78 | fprintf(stderr, "Peer establishing error"); |
|---|
| 79 | exit(EXIT_FAILURE); |
|---|
| 80 | } |
|---|
| 81 | // wait 5 seconds for the connection attempt to succeed |
|---|
| 82 | if(enet_host_service(client, &event, 5000) > 0 && event.type == ENET_EVENT_TYPE_CONNECT){ |
|---|
| 83 | cout << "Connection to " << str << " succeeded." << endl; |
|---|
| 84 | //puts("Connection to localhost:5555 succeeded."); |
|---|
| 85 | }else{ |
|---|
| 86 | enet_peer_reset(peer); |
|---|
| 87 | cout << "Connection to " << str << " failed." << endl; |
|---|
| 88 | //puts("Connection to localhost:5555 failed."); |
|---|
| 89 | exit(EXIT_FAILURE); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | for(int i=0; i<10; i++){ |
|---|
| 93 | // weihnachtsmann bringt packete |
|---|
| 94 | //ENetPacket *packet = enet_packet_create ("packet1234", strlen("packet1234") + 1, ENET_PACKET_FLAG_RELIABLE); |
|---|
| 95 | // extend the packet and append the string foo to it |
|---|
| 96 | // send packet to peer on channel id 0 |
|---|
| 97 | enet_peer_send(peer, 1, pck.chatMessage("test2")); |
|---|
| 98 | // keep the timeout very small for low delay |
|---|
| 99 | if(enet_host_service(client, &event, 1)==0){ |
|---|
| 100 | cout << "successfully sent: " << event.type << endl; |
|---|
| 101 | }else{ |
|---|
| 102 | cout << "failed sending" << endl; |
|---|
| 103 | } |
|---|
| 104 | usleep(1000000); |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | // now disconnect |
|---|
| 108 | // enet_peer_disconnect (peer); |
|---|
| 109 | enet_peer_disconnect (peer, 0); |
|---|
| 110 | // 3 seconds timeout |
|---|
| 111 | while(enet_host_service(client, &event, 3000) > 0){ |
|---|
| 112 | switch (event.type) |
|---|
| 113 | { |
|---|
| 114 | case ENET_EVENT_TYPE_RECEIVE: |
|---|
| 115 | enet_packet_destroy(event.packet); |
|---|
| 116 | break; |
|---|
| 117 | case ENET_EVENT_TYPE_DISCONNECT: |
|---|
| 118 | puts("Disconnection succeeded."); |
|---|
| 119 | return 0; |
|---|
| 120 | } |
|---|
| 121 | } |
|---|
| 122 | // if disconnect failed |
|---|
| 123 | enet_peer_reset(peer); |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | return 0; |
|---|
| 128 | } |
|---|