src/Network/Node.cc
author terom
Mon, 08 Dec 2008 01:12:00 +0000
changeset 277 40f4a03917e2
parent 202 b3f5d766391e
child 431 c6d7272a164b
permissions -rw-r--r--
fix NetworkClient handleInput
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     1
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     2
#include <cassert>
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     3
186
0738f2949a2b move src/Network% to src/Network/%
terom
parents: 185
diff changeset
     4
#include "Node.hh"
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     5
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     6
NetworkNode::NetworkNode (NetworkSession &session, NetworkTCPTransport *tcp, NetworkUDP *udp, const NetworkAddress &address) :
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     7
    session(session), tcp(tcp), udp(udp), address(address) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     8
    
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     9
    // connect signals
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    10
    slots.connect(tcp->sig_disconnect(), this, &NetworkNode::on_disconnect);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    11
    slots.connect(tcp->sig_packet(), &session, &NetworkSession::handle_message, this);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    12
    
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    13
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    14
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    15
NetworkNode::~NetworkNode (void) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    16
    delete tcp;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    17
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    18
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    19
void NetworkNode::on_disconnect (void) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    20
    // tell session
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    21
    session.handle_disconnect(this);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    22
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    23
    // fire signal
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    24
    _sig_disconnected();
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    25
    
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    26
    // delete
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    27
//    delete this;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    28
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    29
202
b3f5d766391e support sending of raw packets
terom
parents: 200
diff changeset
    30
void NetworkNode::write_packet_header (NetworkPacketOutput &pkt, NetworkChannelID channel_id) {
b3f5d766391e support sending of raw packets
terom
parents: 200
diff changeset
    31
    pkt.write_uint16(channel_id);
b3f5d766391e support sending of raw packets
terom
parents: 200
diff changeset
    32
}
b3f5d766391e support sending of raw packets
terom
parents: 200
diff changeset
    33
b3f5d766391e support sending of raw packets
terom
parents: 200
diff changeset
    34
void NetworkNode::send_raw (const NetworkPacketBuffer &pkt, bool reliable) {
b3f5d766391e support sending of raw packets
terom
parents: 200
diff changeset
    35
    // either tcp or udp
b3f5d766391e support sending of raw packets
terom
parents: 200
diff changeset
    36
    if (reliable) {
b3f5d766391e support sending of raw packets
terom
parents: 200
diff changeset
    37
        assert(tcp);
b3f5d766391e support sending of raw packets
terom
parents: 200
diff changeset
    38
b3f5d766391e support sending of raw packets
terom
parents: 200
diff changeset
    39
        tcp->write_packet(pkt);
b3f5d766391e support sending of raw packets
terom
parents: 200
diff changeset
    40
b3f5d766391e support sending of raw packets
terom
parents: 200
diff changeset
    41
    } else {
b3f5d766391e support sending of raw packets
terom
parents: 200
diff changeset
    42
        udp->sendto(pkt, address);
b3f5d766391e support sending of raw packets
terom
parents: 200
diff changeset
    43
    }
b3f5d766391e support sending of raw packets
terom
parents: 200
diff changeset
    44
b3f5d766391e support sending of raw packets
terom
parents: 200
diff changeset
    45
}
b3f5d766391e support sending of raw packets
terom
parents: 200
diff changeset
    46
200
2dbf40661580 better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents: 186
diff changeset
    47
void NetworkNode::send (NetworkChannelID channel_id, const NetworkPacketBuffer &pkt, bool reliable) {
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    48
    assert(channel_id > 0);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    49
    
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    50
    // add our header
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    51
    NetworkPacket pkt2;
202
b3f5d766391e support sending of raw packets
terom
parents: 200
diff changeset
    52
    
b3f5d766391e support sending of raw packets
terom
parents: 200
diff changeset
    53
    write_packet_header(pkt2, channel_id);
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    54
    pkt2.write_packet(pkt);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    55
    
202
b3f5d766391e support sending of raw packets
terom
parents: 200
diff changeset
    56
    // send
b3f5d766391e support sending of raw packets
terom
parents: 200
diff changeset
    57
    send_raw(pkt2, reliable);
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    58
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    59
        
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    60
const NetworkAddress& NetworkNode::getRemoteAddress (void) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    61
    return address;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    62
}