Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Dec 3, 2007, 12:28:19 AM (17 years ago)
Author:
scheusso
Message:

PacketDecoder:

Extended Class to make inheriting easier…

-added virtual function, that can be implemented by lower classes

Client:

Added some function, changed some things (input, gamestate, connectionhandling)

Server:

same as client

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

Legend:

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

    r337 r369  
    2121  Client::Client(): client_connection(NETWORK_PORT,"127.0.0.1"){
    2222    // set server address to localhost
    23     //server_address="127.0.0.1";
    24     //port = NETWORK_PORT;
    2523    isConnected=false;
     24    pck_gen = PacketGenerator();
     25    gamestate = GameStateManager();
    2626  }
    2727
     
    3232   */
    3333  Client::Client(std::string address, int port) : client_connection(port, address){
    34     //server_address=address.c_str();
    35     //this->port = port;
    3634    isConnected=false;
     35    pck_gen = PacketGenerator();
     36    gamestate = GameStateManager();
    3737  }
    3838
     
    4343   */
    4444  Client::Client(const char *address, int port) : client_connection(port, address){
    45     //server_address = address;
    46     //this->port = port;
    4745    isConnected=false;
     46    pck_gen = PacketGenerator();
     47    gamestate = GameStateManager();
    4848  }
    4949
     
    7878        return false;
    7979    // send packets
    80     client_connection.sendPackets(&event);
    81     if(event.type==ENET_EVENT_TYPE_NONE)
    82       return true;
    83     else
    84       return false;
     80    return client_connection.sendPackets();
    8581  }
    8682
     
    9692        return false;
    9793    // send packets
    98     client_connection.sendPackets(&event);
    99     if(event.type==ENET_EVENT_TYPE_NONE)
    100       return true;
    101     else
    102       return false;
     94    return client_connection.sendPackets();
    10395  }
    10496
     
    131123  }
    132124
     125  /**
     126   * Sends out all the packets queued by addXXX
     127   */
     128  bool Client::sendPackets(){
     129    ENetEvent event;
     130    // send packets
     131    client_connection.sendPackets(&event);
     132    if(event.type==ENET_EVENT_TYPE_NONE)
     133      return true;
     134    else
     135      return false;
     136  }
     137 
    133138  /**
    134139   * Performs a GameState update
    135140   */
    136141  void Client::update(){
    137     // to be implemented ==================
    138 
    139 
     142    ENetPacket *packet;
     143    // stop if the packet queue is empty
     144    while(!client_connection.queueEmpty()){
     145      packet = client_connection.getPacket();
     146      elaborate(packet, 0); // ================= i guess we got to change this .... (client_ID is always same = server)
     147    }
     148    return;
    140149  }
    141 
     150 
     151  void Client::processGamestate( GameState *data){
     152    gamestate.loadSnapshot( *data );
     153    return;
     154  }
     155 
    142156}
  • code/branches/FICN/src/network/Client.h

    r337 r369  
    1818#include "ClientConnection.h"
    1919#include "PacketManager.h"
     20#include "GameStateManager.h"
    2021
    2122
     
    4344  bool addKeyboard(char key_code);
    4445
     46  bool sendPackets();
     47 
    4548  void update();
    4649
     
    4851  ClientConnection client_connection;
    4952  PacketGenerator pck_gen;
    50 
    51   //const char *server_address;
    52   //int port;
     53  GameStateManager gamestate;
    5354  bool isConnected;
     55 
     56  // implement data processing functions of PacketDecoder
     57  void processGamestate( GameState *data);
    5458};
    5559
  • code/branches/FICN/src/network/ClientConnection.cc

    r355 r369  
    5454  }
    5555
     56  ENetPacket *ClientConnection::getPacket(){
     57    ENetAddress address;
     58    return getPacket(address);
     59  }
     60 
    5661  bool ClientConnection::queueEmpty(){
    5762    return buffer.isEmpty();
     
    110115      quit=true;
    111116    //connect to the server
    112     if(!establishConnection())
     117    if(!establishConnection()){
    113118      quit=true;
     119      return;
     120    }
    114121    //main loop
    115122    while(!quit){
     
    126133        break;
    127134      case ENET_EVENT_TYPE_DISCONNECT:
    128         // add some error/log handling here
    129         // extend =====================
     135        quit=true;
     136        // server closed the connection
     137        return;
    130138        break;
    131139      case ENET_EVENT_TYPE_NONE:
     
    143151  bool ClientConnection::disconnectConnection(){
    144152    ENetEvent event;
    145 //     enet_peer_disconnect(server);
    146153    enet_peer_disconnect(server, 0);
    147154    while(enet_host_service(client, &event, NETWORK_WAIT_TIMEOUT) > 0){
  • code/branches/FICN/src/network/ClientConnection.h

    r337 r369  
    3838    ClientConnection(int port, const char* address);
    3939    ENetPacket *getPacket(ENetAddress &address); // thread1
     40    ENetPacket *getPacket(); // thread1
    4041    // check wheter the packet queue is empty
    4142    bool queueEmpty();
  • code/branches/FICN/src/network/ConnectionManager.cc

    r352 r369  
    106106      return false;
    107107    if(enet_host_service(server, event, NETWORK_SEND_WAIT)>=0)
     108      return true;
     109    else
     110      return false;
     111  }
     112 
     113  bool ConnectionManager::sendPackets(){
     114    ENetEvent event;
     115    if(server==NULL)
     116      return false;
     117    if(enet_host_service(server, &event, NETWORK_SEND_WAIT)>=0)
    108118      return true;
    109119    else
     
    117127    ENetEvent event;
    118128    server = enet_host_create(&bindAddress, NETWORK_MAX_CONNECTIONS, 0, 0);
    119     if(server==NULL)
     129    if(server==NULL){
    120130      // add some error handling here ==========================
    121131      quit=true;
     132      return;
     133    }
    122134
    123135    while(!quit){
     
    143155      }
    144156    }
     157    disconnectClients();
    145158    // if we're finishied, destroy server
    146159    enet_host_destroy(server);
     160  }
     161 
     162  void ConnectionManager::disconnectClients(){
     163    bool disconnected=false;
     164    ENetEvent event;
     165    ClientList *temp=client;
     166    while(temp!=NULL){
     167      enet_peer_disconnect(temp->event->peer, 0);
     168      while( !disconnected && enet_host_service(server, &event, NETWORK_WAIT_TIMEOUT) > 0){
     169        switch (event.type)
     170        {
     171          case ENET_EVENT_TYPE_NONE:
     172          case ENET_EVENT_TYPE_CONNECT:
     173          case ENET_EVENT_TYPE_RECEIVE:
     174            enet_packet_destroy(event.packet);
     175            break;
     176          case ENET_EVENT_TYPE_DISCONNECT:
     177            disconnected=true;
     178            break;
     179        }
     180      }
     181      temp = temp->next;
     182      disconnected=false;
     183    }
     184    return;
    147185  }
    148186
  • code/branches/FICN/src/network/ConnectionManager.h

    r337 r369  
    6161    // send out all queued packets
    6262    bool sendPackets(ENetEvent *event);
     63    bool sendPackets();
    6364    private:
    6465    bool clientDisconnect(ENetPeer *peer);
     
    6768    // implementation of the listener
    6869    void receiverThread(); //thread2
     70    void disconnectClients();
    6971    //packetbuffer
    7072    PacketBuffer buffer;
  • code/branches/FICN/src/network/GameStateManager.cc

    r346 r369  
    9595      }
    9696      if(it==0){  // add the new object
    97        
     97        // =================== factory command to add object
     98        // can we be sure the object really was added?
     99        it=orxonox::ObjectList<Synchronisable>::end();
     100        it->objectID=sync.objectID;
     101        it->classID=sync.classID;
    98102      }
    99103    } else {
  • code/branches/FICN/src/network/PacketDecoder.cc

    r341 r369  
    100100        enet_packet_destroy( packet );
    101101       
    102         printChat( chatting ); //debug info
     102        processChat( chatting ); //debug info
    103103       
    104104}
     
    118118        //copy the gamestate data
    119119        memcpy( (void*)(currentState->data), (const void*)(data+3*sizeof( int )), currentState->size );
    120        
     120 
    121121        //clean memory
    122122        enet_packet_destroy( packet );
     123  //run processGamestate
     124  processGamestate(currentState);
     125}
     126
     127// now the data processing functions:
     128
     129void PacketDecoder::processChat( chat *data){
     130  printChat(data);
    123131}
    124132
  • code/branches/FICN/src/network/PacketManager.h

    r341 r369  
    9292        void gstate( ENetPacket* packet );
    9393       
     94  //process data
     95  virtual void processGamestate(GameState *state);
     96  virtual void processChat( chat *data);
     97  virtual void processAck( ack *data);
     98 
    9499        //print functions
    95100        void printAck( ack* data );
  • code/branches/FICN/src/network/Server.cc

    r337 r369  
    2121   */
    2222  Server::Server(){
    23 
    24 
     23    connection = ConnectionManager();
     24    gamestates = GameStateManager();
     25    packet_gen = PacketGenerator();
    2526  }
    2627
     
    3031   * @param bindAddress Address to listen on
    3132   */
    32   Server::Server(int port, std::string bindAddress) : connection(port, bindAddress){
    33 
    34 
     33  Server::Server(int port, std::string bindAddress){
     34    connection = ConnectionManager(port, bindAddress);
     35    gamestates = GameStateManager();
     36    packet_gen = PacketGenerator();
    3537  }
    3638
     
    4042   * @param bindAddress Address to listen on
    4143   */
    42   Server::Server(int port, const char *bindAddress) : connection(port, bindAddress){
    43 
    44 
    45 
    46 
     44  Server::Server(int port, const char *bindAddress){
     45    connection = ConnectionManager(port, bindAddress);
     46    gamestates = GameStateManager();
     47    packet_gen = PacketGenerator();
    4748  }
    48 
    49 
    50 
    51 
    52 
     49 
     50  /**
     51   * This function opens the server by creating the listener thread
     52   */
     53  void Server::open(){
     54    connection.createListener();
     55    return;
     56  }
     57 
     58  /**
     59   * This function closes the server
     60   */
     61  void Server::close(){
     62    connection.quitListener();
     63    return;
     64  }
     65 
     66  /**
     67   * This function sends out a message to all clients
     68   * @param msg message
     69   * @return true/false
     70   */
     71  bool Server::sendMSG(std::string msg){
     72    ENetPacket *packet = packet_gen.chatMessage(msg.c_str());
     73    connection.addPacketAll(packet);
     74    return connection.sendPackets();
     75  }
     76  /**
     77   * This function sends out a message to all clients
     78   * @param msg message
     79   * @return true/false
     80   */
     81  bool Server::sendMSG(const char *msg){
     82    ENetPacket *packet = packet_gen.chatMessage(msg);
     83    connection.addPacketAll(packet);
     84    return connection.sendPackets();
     85  }
     86 
     87  void Server::tick(){
     88  }
     89 
    5390}
  • code/branches/FICN/src/network/Server.h

    r337 r369  
    1818#include "ConnectionManager.h"
    1919#include "PacketManager.h"
    20 
     20#include "enet/enet.h"
    2121
    2222namespace network{
     
    3131    Server(int port, std::string bindAddress);
    3232    Server(int port, const char *bindAddress);
    33 
     33    void open();
     34    void close();
     35    bool sendMSG(std::string msg);
     36    bool sendMSG(const char *msg);
     37    void tick();
    3438    private:
    3539    ConnectionManager connection;
    36 
    37 
    38 
    39 
    40 
    41 
     40    GameStateManager gamestates;
     41    PacketGenerator packet_gen;
    4242
    4343  };
Note: See TracChangeset for help on using the changeset viewer.