Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network/src/network/ConnectionManager.cc @ 173

Last change on this file since 173 was 173, checked in by scheusso, 12 years ago

created classes PacketBuffer and ConnectionManager
PacketBuffer: (hopefully) threadsafe implementation of a packet buffer
for communication between threads
ConnectionManager: low-level managment of connection; implementation of
multiple threads

File size: 3.2 KB
Line 
1//
2// C++ Interface: ConnectionManager
3//
4// Description: The Class ConnectionManager manages the servers conenctions to the clients.
5// each connection is provided by a new process. communication between master process and
6// connection processes is provided by ...
7//
8//
9// Author:  Dumeni, Oli
10//
11
12// enet library for networking support
13#include enet/enet.h
14// boost.thread library for multithreading support
15#include boost/thread/thread.hpp
16// headerfile
17#include "network/ConnectionManager.h"
18
19namespace network{
20  ConnectionManager::ConnectionManager(){
21    quit=false;
22    client=NULL;
23    bindAddress.host = std::ENET_HOST_ANY;
24    bindAddress.port = NETWORK_PORT;
25  }
26 
27  ConnectionManager::ConnectionManager(int port, int address){
28    quit=false;
29    client=NULL;
30    bindAddress.host = address;
31    bindAddress.port = NETWORK_PORT;
32  }
33 
34  void ConnectionManager::loop(){
35    if(!quit && !buffer.isEmpty()){
36      // to be implemented =================
37     
38    }
39  }
40 
41  bool ConnectionManager::createListener(){
42    threads.create_thread(&peerLoop);
43    return true;
44  }
45 
46  bool ConnectionManager::quitServer(){
47    quit=true;
48    threads.join_all();
49    return true;
50  }
51 
52  bool ConnectionManager::peerLoop(){
53    ENetEvent event;
54    server = enet_host_create(&bindAddress, NETWORK_MAX_CONNECTIONS, 0, 0);
55    if(server==NULL)
56      // add some error handling here ==========================
57      quit=true;
58   
59    while(!quit){
60      if(enet_host_service(server, &event, NETWORK_WAIT_TIMEOUT)<0){
61        // we should never reach this point
62        quit=true;
63        // add some error handling here ========================
64      }
65      switch(event.type){
66        // log handling ================
67      case std::EVENT_TYPE_CONNECT:
68        addClient(event);
69        break;
70      case std::EVENT_TYPE_RECEIVE:
71        processData(event);
72        break;
73      case std::EVENT_TYPE_DISCONNECT:
74        // add some error/log handling here
75        clientDisconnect(event.peer);
76        break;
77      }
78    }
79    // if we're finishied, destroy server
80    enet_host_destroy(server);
81  }
82 
83  ConnectionManager::processData(ENetEvent event){
84//     insert packetdecode and buffer stuff here
85   
86  }
87 
88  bool ConnectionManager::clientDisconnect(ENetPeer peer){
89    ClientList *temp=client;
90    if(temp->event.peer==peer){
91      if(temp->next==NULL){
92        client=NULL;
93      } else{
94        client=temp->next;
95      }
96      delete temp;
97      return true;
98    } else {
99      while(temp->next!=NULL){
100        if(temp->next->event.peer==peer){
101          ClientList *temp2=temp->next;
102          temp->next=temp2->next;
103          delete temp2;
104          return true;
105        } else
106          temp=temp->next;
107      }
108    }
109    return false;
110  }
111 
112  bool ConnectionManager::addClient(ENetEvent event){
113    if(client==NULL){
114      client =new ClientList;
115      client->ID=1;
116      client->next=NULL;
117      client->event = event;
118    } else {
119      ClienList *temp = client;
120      int i=1;
121      while(temp->next != NULL){
122        temp=temp->next;
123        i++;
124      }
125      temp->next=new ClientList;
126      temp=temp->next;
127      temp->ID=i+1;
128      temp->event=event;
129      temp->next=NULL;
130    }
131    return true;
132  }
133 
134  void ConnectionManager::test(){
135    while(!quit)
136   
137   
138  }
139}
Note: See TracBrowser for help on using the repository browser.