Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/FICN/src/network/dummyclient.cc @ 777

Last change on this file since 777 was 777, checked in by rgrieder, 16 years ago
  • added dll support to the network library
  • improved header file dependency in network
File size: 3.4 KB
Line 
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
37#include "util/Sleep.h"
38#include "PacketManager.h"
39
40using namespace std;
41
42int main(){
43  ENetHost * client;
44  ENetAddress address;
45  ENetEvent event;
46  ENetPeer *peer;
47  network::PacketGenerator pck;
48
49  enet_initialize();
50  atexit(enet_deinitialize);
51
52  cout << "Enter address of the server xxx.xxx.xxx.xxx (enter for localhost)" << endl;
53  string str;
54  getline(cin, str);
55  cout << "You entered: " << str << endl;
56  if(str.compare("")==0)
57    str="127.0.0.1";
58
59  enet_address_set_host(&address, str.c_str());
60  address.port = 55556;
61
62  // create client object
63  client = enet_host_create(NULL, 2, 0, 0);
64
65  if(client==NULL){
66    fprintf(stderr, "An error occured");
67    exit(EXIT_FAILURE);
68  }
69  // connect peer
70  peer = enet_host_connect(client, &address, 2);
71  if(peer==NULL){
72    fprintf(stderr, "Peer establishing error");
73    exit(EXIT_FAILURE);
74  }
75  // wait 5 seconds for the connection attempt to succeed
76  if(enet_host_service(client, &event, 5000) > 0 && event.type == ENET_EVENT_TYPE_CONNECT){
77    cout << "Connection to " << str << " succeeded." << endl;
78    //puts("Connection to localhost:5555 succeeded.");
79  }else{
80    enet_peer_reset(peer);
81    cout << "Connection to " << str << " failed." << endl;
82    //puts("Connection to localhost:5555 failed.");
83    exit(EXIT_FAILURE);
84  }
85
86  for(int i=0; i<10; i++){
87    // weihnachtsmann bringt packete
88    //ENetPacket *packet = enet_packet_create ("packet1234", strlen("packet1234") + 1, ENET_PACKET_FLAG_RELIABLE);
89    // extend the packet and append the string foo to it
90    // send packet to peer on channel id 0
91    enet_peer_send(peer, 1, pck.chatMessage("test2"));
92    // keep the timeout very small for low delay
93    if(enet_host_service(client, &event, 1)==0){
94      cout << "successfully sent: " << event.type << endl;
95    }else{
96      cout << "failed sending" << endl;
97    }
98    usleep(1000000);
99  }
100
101  // now disconnect
102  //   enet_peer_disconnect (peer);
103  enet_peer_disconnect (peer, 0);
104  // 3 seconds timeout
105  while(enet_host_service(client, &event, 3000) > 0){
106    switch (event.type)
107    {
108    case ENET_EVENT_TYPE_RECEIVE:
109      enet_packet_destroy(event.packet);
110      break;
111    case ENET_EVENT_TYPE_DISCONNECT:
112      puts("Disconnection succeeded.");
113      return 0;
114    }
115  }
116  // if disconnect failed
117  enet_peer_reset(peer);
118
119
120
121  return 0;
122}
Note: See TracBrowser for help on using the repository browser.