Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network/src/network/dummyclient.cc @ 208

Last change on this file since 208 was 208, checked in by scheusso, 16 years ago

blabla

File size: 2.4 KB
Line 
1//
2// Dummy client to test ConnectionManager and PacketBuffer classes
3//
4// Author: Oliver Scheuss
5
6#include <iostream>
7#include <string>
8#include <enet/enet.h>
9#include "network/PacketManager.h"
10
11using namespace std;
12
13int main(){
14  ENetHost * client;
15  ENetAddress address;
16  ENetEvent event;
17  ENetPeer *peer;
18  network::PacketGenerator pck;
19
20  enet_initialize();
21  atexit(enet_deinitialize);
22 
23  cout << "Enter address of the server xxx.xxx.xxx.xxx (enter for localhost)" << endl;
24  string str;
25  getline(cin, str);
26  cout << "You entered: " << str << endl;
27  if(str.compare("")==0)
28    str="127.0.0.1";
29 
30  enet_address_set_host(&address, str.c_str());
31  address.port = 5555;
32
33        // create client object
34  client = enet_host_create(NULL, 2, 0, 0);
35
36  if(client==NULL){
37    fprintf(stderr, "An error occured");
38    exit(EXIT_FAILURE);
39  }
40        // connect peer
41  peer = enet_host_connect(client, &address, 2);
42  if(peer==NULL){
43    fprintf(stderr, "Peer establishing error");
44    exit(EXIT_FAILURE);
45  }
46        // wait 5 seconds for the connection attempt to succeed
47  if(enet_host_service(client, &event, 5000) > 0 && event.type == ENET_EVENT_TYPE_CONNECT){
48    cout << "Connection to " << str << " succeeded." << endl;
49    //puts("Connection to localhost:5555 succeeded.");
50  }else{
51    enet_peer_reset(peer);
52    cout << "Connection to " << str << " failed." << endl;
53    //puts("Connection to localhost:5555 failed.");
54  }
55
56  for(int i=0; i<10; i++){
57        // weihnachtsmann bringt packete
58    //ENetPacket *packet = enet_packet_create ("packet1234", strlen("packet1234") + 1, ENET_PACKET_FLAG_RELIABLE);
59        // extend the packet and append the string foo to it
60        // send packet to peer on channel id 0
61    enet_peer_send(peer, 1, pck.chatMessage("test"));
62        // keep the timeout very small for low delay
63    if(enet_host_service(client, &event, 1)==0){
64      cout << "successfully sent: " << event.type << endl;
65    }else{
66      cout << "failed sending" << endl;
67    }
68  }
69
70        // now disconnect
71  enet_peer_disconnect (peer, 0);
72        // 3 seconds timeout
73  while(enet_host_service(client, &event, 3000) > 0){
74    switch (event.type)
75    {
76      case ENET_EVENT_TYPE_RECEIVE:
77        enet_packet_destroy(event.packet);
78        break;
79      case ENET_EVENT_TYPE_DISCONNECT:
80        puts("Disconnection succeeded.");
81        return 0;
82    }
83  }
84        // if disconnect failed
85  enet_peer_reset(peer);
86
87 
88 
89  return 0;
90}
Note: See TracBrowser for help on using the repository browser.