src/Network/Object.cc
author Tero Marttila <terom@fixme.fi>
Mon, 26 Jan 2009 23:03:47 +0200
changeset 431 c6d7272a164b
parent 367 5114fbb7ec03
child 434 a8ba81432ddd
permissions -rw-r--r--
rework Network send() code to use NetworkTarget/Node/Group::send classes, add a NetworkMessage class for sending NetworkObject messages, and fix a bug whereby the server's client TCP sockets weren't nonblocking.... I wonder how this has worked before?\!

#include <cassert>

#include "Object.hh"

/* 
 * NetworkObject_Controller 
 */
NetworkObjectController::NetworkObjectController (NetworkSession &session, NetworkChannelID channel_id) :
    session(session), channel_id(channel_id) {
    
    // setup signals
    slot_message = session.sig_chan_message(channel_id).connect(this, &NetworkObjectController::on_message);
}

void NetworkObjectController::on_message (NetworkPacketInput &pkt, NetworkNode *node) {
    uint32_t obj_id = pkt.read_uint32();
    uint16_t msg_id = pkt.read_uint16();

    // lookup object
    NetworkObject *obj = objects[obj_id];
    
    if (obj) {
        obj->handle_packet(node, msg_id, pkt);

    } else {
        handle_create(obj_id, msg_id, pkt, node);
    }
}
        
NetworkObject* NetworkObjectController::read_object (NetworkPacketInput &pkt) {
    uint32_t obj_id = pkt.read_uint32();

    // lookup object
    NetworkObject *obj = obj_id ? objects[obj_id] : NULL;
    
    // return
    return obj;
}
        
void NetworkObjectController::write_object (NetworkPacketOutput &pkt, NetworkObject *obj) {
    pkt.write_uint32(obj ? obj->obj_id : 0);
}

/* 
 * NetworkObject_ServerController 
 */
NetworkObject_ServerController::NetworkObject_ServerController (NetworkSession &session, NetworkChannelID channel_id) :
    NetworkObjectController(session, channel_id), id_pool(0) {

}

NetworkObjectID NetworkObject_ServerController::getObjectID (void) {
    return ++id_pool;
}
        
void NetworkObject_ServerController::handle_create (NetworkObjectID obj_id, NetworkMessageID msg_id, NetworkPacketInput &pkt, NetworkNode *node) {
    (void) obj_id;
    (void) msg_id;
    (void) pkt;
    (void) node;

    // XXX: fail
    throw CL_Error("clients cannot create objects");
}
        
/* 
 * NetworkObject_ClientController *
 */
NetworkObject_ClientController::NetworkObject_ClientController (NetworkSession &session, NetworkChannelID channel_id, NetworkNode &server) :
    NetworkObjectController(session, channel_id), server(server) 
{


}
        
void NetworkObject_ClientController::handle_create (NetworkObjectID obj_id, NetworkMessageID msg_id, NetworkPacketInput &pkt, NetworkNode *node) {
    // we only communicate with the server
    if (node != &server)
        assert(false);
    
    // create new object
    NetworkObject_Client *obj = new NetworkObject_Client(*this, obj_id);
    
    // signal
    // XXX: this leaks if it's not handled
   _map_sig_create[msg_id](obj, pkt); 
}

/* 
 * NetworkObject 
 */
NetworkObject::NetworkObject (NetworkObjectController &controller, NetworkObjectID obj_id) :
    controller(controller), obj_id(obj_id) {
    
    assert(obj_id);

    controller.objects[obj_id] = this;
}
        
NetworkObject::~NetworkObject (void) {
    controller.objects.erase(obj_id);
}
        
std::ostream& operator<< (std::ostream &s, const NetworkObject &obj) {
    return s << "<NetworkObject #" << obj.obj_id << ">";
}

/* 
 * NetworkObject_Server 
 */
NetworkObject_Server::NetworkObject_Server (NetworkObject_ServerController &controller) :
    NetworkObject(controller, controller.getObjectID()), controller(controller) {
    
}

void NetworkObject_Server::handle_packet (NetworkNode *node, NetworkMessageID msg_id, NetworkPacketInput &pkt) {
    _map_sig_message[msg_id](node, pkt);
}

/* 
 * NetworkObject_Client 
 */
NetworkObject_Client::NetworkObject_Client (NetworkObject_ClientController &controller, NetworkObjectID id) :
    NetworkObject(controller, id), controller(controller) { 
    
    // nothing
}

void NetworkObject_Client::handle_packet (NetworkNode *node, NetworkMessageID msg_id, NetworkPacketInput &pkt) {
    if (node != &controller.server)
        throw Error("Reject NetworkObject packet from non-server");

    _map_sig_message[msg_id](pkt);
}