Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 380


Ignore:
Timestamp:
Dec 4, 2007, 8:22:24 PM (17 years ago)
Author:
scheusso
Message:

problem in connectionmanager

Location:
code/branches/FICN/src/network
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • code/branches/FICN/src/network/ConnectionManager.cc

    r369 r380  
    1616  boost::thread_group network_threads;
    1717
    18   void test(){
    19     return;
    20   }
    21 
    2218  ConnectionManager::ConnectionManager(){
    2319    quit=false;
    24     client=NULL;
    2520    bindAddress.host = ENET_HOST_ANY;
    2621    bindAddress.port = NETWORK_PORT;
     
    2924  ConnectionManager::ConnectionManager(int port, std::string address){
    3025    quit=false;
    31     client=NULL;
    3226    enet_address_set_host (& bindAddress, address.c_str());
    3327    bindAddress.port = NETWORK_PORT;
     
    3630  ConnectionManager::ConnectionManager(int port, const char *address){
    3731    quit=false;
    38     client=NULL;
    3932    enet_address_set_host (& bindAddress, address);
    4033    bindAddress.port = NETWORK_PORT;
     
    4740        return NULL;
    4841  }
     42 
     43  ENetPacket *ConnectionManager::getPacket(int &clientID){
     44    ENetAddress address;
     45    ENetPacket *packet=getPacket(address);
     46    clientID=clientMap.find(address)->second;
     47    return packet;
     48  }
    4949
    5050  bool ConnectionManager::queueEmpty(){
     
    6565
    6666  bool ConnectionManager::addPacket(ENetPacket *packet, ENetPeer *peer){
    67     if(client==NULL)
    68       return false;
    69     ClientList *temp=client;
    70     while(peer->host != temp->event->peer->host){
    71       temp=temp->next;
    72       if(temp==NULL)
    73         return false;
    74     }
    75     if(enet_peer_send(temp->event->peer, temp->ID, packet)!=0)
    76       return false;
    77     return true;
    78   }
    79 
    80   bool ConnectionManager::addPacket(ENetPacket *packet, int ID){
    81     if(client==NULL)
    82       return false;
    83     ClientList *temp=client;
    84     while(ID != temp->ID){
    85       temp=temp->next;
    86       if(temp==NULL)
    87         return false;
    88     }
    89     if(enet_peer_send(temp->event->peer, temp->ID, packet)!=0)
    90       return false;
    91     else
    92       return true;
    93   }
    94 
     67    if(clientVector.size()==0)
     68      return false;
     69    if(enet_peer_send(peer, clientMap.find(peer->address)->second , packet)!=0)
     70      return false;
     71    return true;
     72  }
     73 
     74  bool ConnectionManager::addPacket(ENetPacket *packet, int clientID){
     75    if(clientVector.size()==0)
     76      return false;
     77    if(enet_peer_send(&(peerMap.find(clientVector[clientID])->second), clientID, packet)!=0)
     78      return false;
     79    return true;
     80  }
     81 
    9582  bool ConnectionManager::addPacketAll(ENetPacket *packet){
    96     ClientList *temp=client;
    97     while(temp!=NULL){
    98       if(enet_peer_send(temp->event->peer, temp->ID, packet)!=0)
     83    for(int i=0; i<clientVector.size(); i++){
     84      if(enet_peer_send(&(peerMap.find(clientVector[i])->second), i, packet)!=0)
    9985         return false;
    10086    }
     
    163149    bool disconnected=false;
    164150    ENetEvent event;
    165     ClientList *temp=client;
    166     while(temp!=NULL){
    167       enet_peer_disconnect(temp->event->peer, 0);
     151    for(int i=0; i<clientVector.size(); i++){
     152      enet_peer_disconnect(&(peerMap.find(clientVector[i])->second), 0);
    168153      while( !disconnected && enet_host_service(server, &event, NETWORK_WAIT_TIMEOUT) > 0){
    169154        switch (event.type)
     
    179164        }
    180165      }
    181       temp = temp->next;
    182166      disconnected=false;
    183167    }
     
    192176
    193177  bool ConnectionManager::clientDisconnect(ENetPeer *peer){
    194     ClientList *temp=client;
    195     // do we have to remove the first item ?
    196     if(temp->event->peer->host==peer->host){
    197       if(temp->next==NULL){
    198         client=NULL;
    199       } else{
    200         client=temp->next;
    201       }
    202       delete temp;
    203       return true;
    204     } else {
    205       while(temp->next!=NULL){
    206         if(temp->next->event->peer->host==peer->host){
    207           // do a correct relink and delete the disconnected client
    208           ClientList *temp2=temp->next;
    209           temp->next=temp2->next;
    210           delete temp2;
    211           return true;
    212         } else
    213           //otherwise keep on searching ;)
    214           temp=temp->next;
    215       }
    216     }
    217     return false;
     178    return clientDisconnect(*peer);
     179  }
     180 
     181  bool ConnectionManager::clientDisconnect(ENetPeer peer){
     182    std::map<ENetAddress, int>::iterator it;
     183    it=clientMap.find(peer.address);
     184    clientVector.erase(clientVector.begin()+it->second);
     185    clientMap.erase(it);
     186    peerMap.erase(peerMap.find(peer.address));
     187    return true;
    218188  }
    219189
    220190  bool ConnectionManager::addClient(ENetEvent *event){
    221 
    222     // first client?
    223     if(client==NULL){
    224       client =new ClientList;
    225       client->ID=1;
    226       client->next=NULL;
    227       client->event = event;
    228     } else {
    229       // otherwise add a new element to clientlist
    230       ClientList *temp = client;
    231       int i=1;
    232       while(temp->next != NULL){
    233         temp=temp->next;
    234         i++;
    235       }
    236       temp->next=new ClientList;
    237       temp=temp->next;
    238       temp->ID=i+1;
    239       temp->event=event;
    240       temp->next=NULL;
    241     }
    242     return true;
    243   }
    244 
    245 
     191    clientVector.push_back((event->peer->address));
     192    clientMap[event->peer->address]=clientVector.size()-1;
     193    peerMap[event->peer->address]=*(event->peer);
     194    return true;
     195  }
     196 
     197  int ConnectionManager::getClientID(ENetPeer peer){
     198    return clientMap.find(peer.address)->second;
     199  }
     200 
     201  int ConnectionManager::getClientID(ENetAddress address){
     202    return clientMap.find(address)->second;
     203  }
     204 
     205  ENetPeer ConnectionManager::getClientPeer(int clientID){
     206    return peerMap.find(clientVector[clientID])->second;
     207  }
     208 
    246209}
  • code/branches/FICN/src/network/ConnectionManager.h

    r369 r380  
    1515#include <iostream>
    1616#include <string>
     17#include <map>
     18#include <vector>
    1719// enet library for networking support
    1820#include <enet/enet.h>
     
    2022#include <boost/thread/thread.hpp>
    2123#include <boost/bind.hpp>
    22 // headerfile
     24// headerfiles
    2325#include "ConnectionManager.h"
    2426#include "PacketBuffer.h"
     
    3032#define NETWORK_WAIT_TIMEOUT 5000
    3133#define NETWORK_SEND_WAIT 5
    32   //just some defines to make life easier ;)
    33 // #define ENetEvent std::ENetEvent
    34 // #define ENetHost std::ENetHost
    35 // #define ENetAddress std::ENetAddress
    36 // #define ENetPeer std::ENetPeer
    3734
    3835  struct ClientList{
     
    4845    ConnectionManager(int port, std::string address);
    4946    ENetPacket *getPacket(ENetAddress &address); // thread1
    50     // check wheter the packet queue is empty
     47    ENetPacket *getPacket(int &clientID);
    5148    bool queueEmpty();
    52     // create a new listener thread
    5349    void createListener();
    5450    bool quitListener();
    55     // add a packet to queue for a specific client peer
    5651    bool addPacket(ENetPacket *packet, ENetPeer *peer);
    57     // add ad packet to queue for a specific client ID
    5852    bool addPacket(ENetPacket *packet, int ID);
    59     // add a packet to queue for all clients
    6053    bool addPacketAll(ENetPacket *packet);
    61     // send out all queued packets
    6254    bool sendPackets(ENetEvent *event);
    6355    bool sendPackets();
    6456    private:
    6557    bool clientDisconnect(ENetPeer *peer);
     58    bool clientDisconnect(ENetPeer peer);
    6659    bool processData(ENetEvent *event);
    6760    bool addClient(ENetEvent *event);
    68     // implementation of the listener
    69     void receiverThread(); //thread2
     61    void receiverThread();
    7062    void disconnectClients();
    71     //packetbuffer
     63    int getClientID(ENetPeer peer);
     64    int getClientID(ENetAddress address);
     65    ENetPeer getClientPeer(int clientID);
    7266    PacketBuffer buffer;
    73     // enet stuff
     67   
    7468    ENetHost *server;
    7569    ENetAddress bindAddress;
    76     // quit-variable (communication with threads)
    77     bool quit;
    78     // clientlist
    79     ClientList *client;
    80 //     thread group
    81 //     boost::thread_group threads;
     70   
     71    bool quit; // quit-variable (communication with threads)
     72    std::map<ENetAddress, int> clientMap;
     73    std::map<ENetAddress, ENetPeer> peerMap;
     74    std::vector<ENetAddress> clientVector;
    8275  };
    8376
  • code/branches/FICN/src/network/Server.cc

    r369 r380  
    8585  }
    8686 
     87  /**
     88   * Run this function once every tick
     89   * calls processQueue and updateGamestate
     90   */
    8791  void Server::tick(){
     92    processQueue();
     93    updateGamestate();
     94    return;
     95  }
     96 
     97  /**
     98   * processes all the packets waiting in the queue
     99   */
     100  void Server::processQueue(){
     101    ENetPacket *packet;
     102    while(!connection.queueEmpty()){
     103      packet = connection.getPacket()
     104    }
     105  }
     106 
     107  /**
     108   * takes a new snapshot of the gamestate and sends it to the clients
     109   */
     110  void Server::updateGamestate(){
    88111  }
    89112 
  • code/branches/FICN/src/network/Server.h

    r369 r380  
    4040    GameStateManager gamestates;
    4141    PacketGenerator packet_gen;
    42 
     42   
     43    void processQueue();
     44    void updateGamestate();
    4345  };
    4446
Note: See TracChangeset for help on using the changeset viewer.