Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/merger/src/network/dummyclient.cc @ 486

Last change on this file since 486 was 298, checked in by nicolasc, 17 years ago

some cleanups
eNet code is currently enet-1.1 compatible (incompatible enet-1.0!)

File size: 2.5 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 "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 = 55556;
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    exit(EXIT_FAILURE);
55  }
56
57  for(int i=0; i<10; i++){
58        // weihnachtsmann bringt packete
59    //ENetPacket *packet = enet_packet_create ("packet1234", strlen("packet1234") + 1, ENET_PACKET_FLAG_RELIABLE);
60        // extend the packet and append the string foo to it
61        // send packet to peer on channel id 0
62    enet_peer_send(peer, 1, pck.chatMessage("test2"));
63        // keep the timeout very small for low delay
64    if(enet_host_service(client, &event, 1)==0){
65      cout << "successfully sent: " << event.type << endl;
66    }else{
67      cout << "failed sending" << endl;
68    }
69    usleep(1000000);
70  }
71
72        // now disconnect
73//   enet_peer_disconnect (peer);
74  enet_peer_disconnect (peer, 0);
75        // 3 seconds timeout
76  while(enet_host_service(client, &event, 3000) > 0){
77    switch (event.type)
78    {
79      case ENET_EVENT_TYPE_RECEIVE:
80        enet_packet_destroy(event.packet);
81        break;
82      case ENET_EVENT_TYPE_DISCONNECT:
83        puts("Disconnection succeeded.");
84        return 0;
85    }
86  }
87        // if disconnect failed
88  enet_peer_reset(peer);
89
90
91
92  return 0;
93}
Note: See TracBrowser for help on using the repository browser.