src/proto2/NetworkServer.cc
author terom
Tue, 18 Nov 2008 22:58:50 +0000
branchno-netsession
changeset 35 e21cfda0edde
parent 26 5685602aeb9c
child 36 785d220fc6b7
permissions -rw-r--r--
Merge from at r31:36
#include "NetworkServer.hh"
#include "Engine.hh"
#include "Logger.hh"

#include <cassert>

NetworkServer::NetworkServer (GameState &state, const std::string &listen_port) : 
    NetworkCore(state), pid_pool(0) {
    
    // connect slots
    slots.connect(netsession.sig_computer_connected(), this, &NetworkServer::on_connect);
    slots.connect(netsession.sig_computer_disconnected(), this, &NetworkServer::on_disconnect);
    
    // and then we listen
    netsession.start_listen(listen_port);

    Engine::log(INFO, "server") << "running, listen_port=" << listen_port;
}

void NetworkServer::on_connect (CL_NetComputer &computer) {
    // assign a pid
    uint16_t pid = ++pid_pool;
    
    // create the player object (it logs it)
    NetworkServerPlayer *player = new NetworkServerPlayer(*this, computer, pid);
    
    // map computer to it
    players[computer] = player;

    // add to GameState
    state.newRemotePlayer(player);
}

void NetworkServer::on_disconnect (CL_NetComputer &computer) {
    NetworkServerPlayer *player = players[computer];
    
    // remove from players
    players.erase(computer);

    // remove from state
    state.removePlayer(player);

    // remove from game
    player->disconnected();

    // delete
    delete player;
}
        
NetworkServerPlayer::NetworkServerPlayer (NetworkServer &server, CL_NetComputer &computer, uint16_t pid) : 
    RemotePlayer(server.state, Vector(PLAYER_INITIAL_X, PLAYER_INITIAL_Y), true), server(server), computer(computer), obj(&server.netobjs), pid(pid) {
    
    // log
    Engine::log(INFO, "server_player.connected") << "computer=" << computer << ", obj=" << obj;

    // messages
    slots.connect(obj.sig_received_message(NETMSG_CLIENT_MOVE), this, &NetworkServerPlayer::on_move);   

    // the initial NETMSG_PLAYER_HELLO
    CL_NetPacket hello_pkt;
    writeVector(hello_pkt, position);

    obj.send(computer, NETMSG_SERVER_HELLO, hello_pkt, true);

    // send other player objects
    for (std::map<CL_NetComputer, NetworkServerPlayer*>::iterator it = server.players.begin(); it != server.players.end(); it++) {
        NetworkServerPlayer *player = it->second;
        CL_NetPacket player_pkt;
        
        // player is not in players list yet
        assert(player != this);

        writeVector(player_pkt, player->position);

        player->obj.send(computer, NETMSG_PLAYER_INFO, player_pkt, true);
    }


    // broadcast NETMSG_PLAYER_JOIN to all clients
    obj.send(server.netsession.get_all(), NETMSG_PLAYER_JOIN, hello_pkt, true);
}

void NetworkServerPlayer::disconnected (void) {
    CL_NetPacket pkt;
    
    Engine::log(INFO, "server_player.disconnected") << "computer=" << computer << ", obj=" << obj;

    obj.send(server.netsession.get_all(), NETMSG_PLAYER_QUIT, pkt, true);
}

void NetworkServerPlayer::on_move (CL_NetComputer &from, CL_NetPacket &pkt) {
    // sanity-check
    if (!(from == computer))
        return;
    
    Vector impulse_force = readVector(pkt);    
    uint16_t impulse_ms = pkt.input.read_uint16();

    Engine::log(INFO, "server_player.on_move") << "obj=" << obj << ", old_pos=" << position << ", impulse=" << impulse_force << "@" << impulse_ms << "ms";
    
    // apply force
    applyForce(impulse_force, impulse_ms);

    // send position update
    send_position_update();
}
        
void NetworkServerPlayer::send_position_update (void) {
    CL_NetPacket pkt;
    writeVector(pkt, position);
    writeVector(pkt, velocity);

    Engine::log(INFO, "server_player.send_position_update") << "obj=" << obj << " -> " << position << "+" << velocity;

    obj.send(server.netsession.get_all(), NETMSG_PLAYER_POSITION, pkt, false);
}