Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 364 was 355, checked in by landauf, 17 years ago

changed retos usleep hack to a more generic solution:
#ifdef WIN32
#include <windows.h>
#define usleep(x) Sleep((x)/1000)

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