Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 196


Ignore:
Timestamp:
Nov 11, 2007, 5:11:36 PM (17 years ago)
Author:
scheusso
Message:

created a dummyserver and dummyclient in order to test ConnectionManager and PacketBuffer with enet and boost_thread\n Makefile is used to build server and client

Location:
code/branches/network/src/network
Files:
3 added
4 edited

Legend:

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

    r173 r196  
    77//
    88//
    9 // Author:  Dumeni, Oli
     9// Author:  Oliver Scheuss
    1010//
    1111
    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
    1712#include "network/ConnectionManager.h"
    1813
    1914namespace network{
     15 
     16  boost::thread_group network_threads;
     17 
     18  void test(){
     19    return;
     20  }
     21 
    2022  ConnectionManager::ConnectionManager(){
    2123    quit=false;
    2224    client=NULL;
    23     bindAddress.host = std::ENET_HOST_ANY;
     25    bindAddress.host = ENET_HOST_ANY;
    2426    bindAddress.port = NETWORK_PORT;
    2527  }
     
    3234  }
    3335 
    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(){
     36 
     37  ENetPacket *ConnectionManager::getPacket(){
     38    if(!buffer.isEmpty())
     39      return buffer.pop();
     40    else
     41        return NULL;
     42  }
     43 
     44  bool ConnectionManager::queueEmpty(){
     45    return buffer.isEmpty();
     46  }
     47 
     48  void ConnectionManager::createListener(){
     49    network_threads.create_thread(boost::bind(boost::mem_fn(&ConnectionManager::receiverThread), this));
     50//     boost::thread thr(boost::bind(boost::mem_fn(&ConnectionManager::receiverThread), this));
     51    return;
     52  }
     53 
     54  bool ConnectionManager::quitListener(){
    4755    quit=true;
    48     threads.join_all();
    49     return true;
    50   }
    51  
    52   bool ConnectionManager::peerLoop(){
     56    network_threads.join_all();
     57    return true;
     58  }
     59 
     60  bool ConnectionManager::addPacket(ENetPacket *packet, ENetPeer *peer){
     61    if(client=NULL)
     62      return false;
     63    ClientList *temp=client;
     64    while(peer->host != temp->event->peer->host){
     65      temp=temp->next;
     66      if(temp==NULL)
     67        return false;
     68    }
     69    if(enet_peer_send(temp->event->peer, temp->ID, packet)!=0)
     70      return false;
     71    return true;
     72  }
     73 
     74  bool ConnectionManager::addPacket(ENetPacket *packet, int ID){
     75    if(client=NULL)
     76      return false;
     77    ClientList *temp=client;
     78    while(ID != temp->ID){
     79      temp=temp->next;
     80      if(temp==NULL)
     81        return false;
     82    }
     83    if(enet_peer_send(temp->event->peer, temp->ID, packet)!=0)
     84      return false;
     85    else
     86      return true;
     87  }
     88 
     89  bool ConnectionManager::addPacketAll(ENetPacket *packet){
     90    ClientList *temp=client;
     91    while(temp!=NULL){
     92      if(enet_peer_send(temp->event->peer, temp->ID, packet)!=0)
     93         return false;
     94    }
     95    return true;
     96  }
     97 
     98  bool ConnectionManager::sendPackets(ENetEvent *event){
     99    if(server==NULL)
     100      return false;
     101    if(enet_host_service(server, event, NETWORK_SEND_WAIT)>=0)
     102      return true;
     103    else
     104      return false;
     105  }
     106 
     107  void ConnectionManager::receiverThread(){
     108    // what about some error-handling here ?
     109    enet_initialize();
     110    atexit(enet_deinitialize);
    53111    ENetEvent event;
    54112    server = enet_host_create(&bindAddress, NETWORK_MAX_CONNECTIONS, 0, 0);
     
    65123      switch(event.type){
    66124        // log handling ================
    67       case std::EVENT_TYPE_CONNECT:
    68         addClient(event);
     125        case ENET_EVENT_TYPE_CONNECT:
     126        addClient(&event);
    69127        break;
    70       case std::EVENT_TYPE_RECEIVE:
    71         processData(event);
     128      case ENET_EVENT_TYPE_RECEIVE:
     129        std::cout << event.packet->data << std::endl;
     130        processData(&event);
    72131        break;
    73       case std::EVENT_TYPE_DISCONNECT:
     132      case ENET_EVENT_TYPE_DISCONNECT:
    74133        // add some error/log handling here
    75134        clientDisconnect(event.peer);
     
    81140  }
    82141 
    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){
     142  bool ConnectionManager::processData(ENetEvent *event){
     143    // just add packet to the buffer
     144    // this can be extended with some preprocessing
     145    return buffer.push(event->packet);
     146  }
     147 
     148  bool ConnectionManager::clientDisconnect(ENetPeer *peer){
     149    ClientList *temp=client;
     150    // do we have to remove the first item ?
     151    if(temp->event->peer->host==peer->host){
    91152      if(temp->next==NULL){
    92153        client=NULL;
     
    98159    } else {
    99160      while(temp->next!=NULL){
    100         if(temp->next->event.peer==peer){
     161        if(temp->next->event->peer->host==peer->host){
     162          // do a correct relink and delete the disconnected client
    101163          ClientList *temp2=temp->next;
    102164          temp->next=temp2->next;
     
    104166          return true;
    105167        } else
     168          //otherwise keep on searching ;)
    106169          temp=temp->next;
    107170      }
     
    110173  }
    111174 
    112   bool ConnectionManager::addClient(ENetEvent event){
     175  bool ConnectionManager::addClient(ENetEvent *event){
     176   
     177    // first client?
    113178    if(client==NULL){
    114179      client =new ClientList;
     
    117182      client->event = event;
    118183    } else {
    119       ClienList *temp = client;
     184      // otherwise add a new element to clientlist
     185      ClientList *temp = client;
    120186      int i=1;
    121187      while(temp->next != NULL){
     
    132198  }
    133199 
    134   void ConnectionManager::test(){
    135     while(!quit)
    136    
    137    
    138   }
     200 
    139201}
  • code/branches/network/src/network/ConnectionManager.h

    r187 r196  
    55//
    66//
    7 // Author:  <>, (C) 2007
     7// Author:  Oliver Scheuss, (C) 2007
    88//
    99// Copyright: See COPYING file that comes with this distribution
     
    1313#define NETWORK_CONNECTIONMANAGER_H
    1414
    15 #include PacketBuffer.h
     15#include <iostream>
     16// enet library for networking support
     17#include <enet/enet.h>
     18// boost.thread library for multithreading support
     19#include <boost/thread/thread.hpp>
     20#include <boost/bind.hpp>
     21// headerfile
     22#include "network/ConnectionManager.h"
     23#include "network/PacketBuffer.h"
    1624
    1725namespace network{
     
    2028#define NETWORK_MAX_CONNECTIONS 50
    2129#define NETWORK_WAIT_TIMEOUT 5000
     30#define NETWORK_SEND_WAIT 5
    2231  //just some defines to make life easier ;)
    23 #define ENetEvent std::ENetEvent
    24 #define ENetHost std::ENetHost
    25 #define ENetAddress std::ENetAddress
    26 #define ENetPeer std::ENetPeer
     32// #define ENetEvent std::ENetEvent
     33// #define ENetHost std::ENetHost
     34// #define ENetAddress std::ENetAddress
     35// #define ENetPeer std::ENetPeer
    2736 
    2837  struct ClientList{
    29     ENetEvent event;
     38    ENetEvent *event;
    3039    int ID;
    3140    ClientList *next;
     
    3544    public:
    3645    ConnectionManager();
    37     ~ConnectionManager();
    38     void loop(); // thread1
    39     bool createListener();
    40     bool quitServer();
    41     bool sendEventAll(/*.....*/);
    42     bool sendEvent(int clientID/*.....*/);
    43     void test();
     46    ConnectionManager(int port, int address);
     47    ENetPacket *getPacket(); // thread1
     48    // check wheter the packet queue is empty
     49    bool queueEmpty();
     50    // create a new listener thread
     51    void createListener();
     52    bool quitListener();
     53    // add a packet to queue for a specific client peer
     54    bool addPacket(ENetPacket *packet, ENetPeer *peer);
     55    // add ad packet to queue for a specific client ID
     56    bool addPacket(ENetPacket *packet, int ID);
     57    // add a packet to queue for all clients
     58    bool addPacketAll(ENetPacket *packet);
     59    // send out all queued packets
     60    bool sendPackets(ENetEvent *event);
    4461    private:
    45     void peerLoop(int clientNumber); //thread2
    46     bool clientDisconnect(ENetPeer peer);
    47     bool processData(ENetEvent event);
    48     bool addClient(ENetEvent event);
     62    bool clientDisconnect(ENetPeer *peer);
     63    bool processData(ENetEvent *event);
     64    bool addClient(ENetEvent *event);
     65    // implementation of the listener
     66    void receiverThread(); //thread2
    4967    //packetbuffer
    5068    PacketBuffer buffer;
     
    5472    // quit-variable (communication with threads)
    5573    bool quit;
    56     // thread group
    57     boost::thread_group threads;
    5874    // clientlist
    5975    ClientList *client;
     76//     thread group
     77//     boost::thread_group threads;
    6078  };
    6179 
  • code/branches/network/src/network/PacketBuffer.cc

    r188 r196  
    11// C++ PacketBuffer
    2 //
     2// d
    33// Author Oliver Scheuss
     4
     5#ifndef NETWORK_PACKETBUFFER_CC
     6#define NETWORK_PACKETBUFFER_CC
     7
    48#include <iostream>
    59#include "network/PacketBuffer.h"
    610
    711namespace network{
    8 
     12 
     13   boost::mutex networkPacketBufferMutex;
     14 
    915PacketBuffer::PacketBuffer(){
    1016  closed=false;
     
    1218  last=NULL;
    1319}
     20    //this is needed in order to make the packetbuffer threadsafe
     21 
    1422
    1523bool PacketBuffer::push(ENetPacket *pck){
     
    7987
    8088}// namespace network
     89
     90#endif
  • code/branches/network/src/network/PacketBuffer.h

    r188 r196  
    1919#include <boost/bind.hpp>
    2020#include <boost/thread/mutex.hpp>
    21 #include <boost/thread/mutex.hpp>
    2221
    23 //this is needed in order to make the packetbuffer threadsafe
    24 boost::mutex networkPacketBufferMutex;
    2522
    2623namespace network{
     24
    2725
    2826struct PacketEnvelope{
     
    4745  // pushs a packet to the queue
    4846  bool push(ENetPacket *pck);
     47 
    4948private:
    5049  QueueItem *first;
     
    5251  bool closed;
    5352 
    54   //make it threadsafe
    55 //   boost::mutex mutex;
    5653};
    5754
Note: See TracChangeset for help on using the changeset viewer.