Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/tutorial/src/network/dummyclient.cc @ 859

Last change on this file since 859 was 859, checked in by landauf, 16 years ago

more or less a copy of the trunk

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