src/Network/Session.cc
author Tero Marttila <terom@fixme.fi>
Mon, 26 Jan 2009 23:03:47 +0200
changeset 431 c6d7272a164b
parent 381 9b35bc329d23
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?\!
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     1
186
0738f2949a2b move src/Network% to src/Network/%
terom
parents: 185
diff changeset
     2
#include "Session.hh"
0738f2949a2b move src/Network% to src/Network/%
terom
parents: 185
diff changeset
     3
#include "../Engine.hh"
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     4
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     5
#include <cassert>
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     6
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     7
NetworkSession::NetworkSession (uint64_t magic) :
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     8
    magic(magic), tcp_srv(NULL), udp_srv(NULL), udp_client(NULL) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     9
   
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    10
    // nothing
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    11
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    12
        
381
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
    13
void NetworkSession::listen (const NetworkEndpoint &addr) {
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    14
    assert(tcp_srv == NULL && udp_srv == NULL);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    15
    
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    16
    // create TCP/UDP servers
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    17
    tcp_srv = new NetworkTCPServer(addr);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    18
    udp_srv = new NetworkUDP(addr);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    19
    
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    20
    // connect signals
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    21
    slots.connect(tcp_srv->sig_client(), this, &NetworkSession::on_tcp_client);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    22
    slots.connect(udp_srv->sig_packet(), this, &NetworkSession::on_udp_packet);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    23
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    24
        
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    25
NetworkNode *NetworkSession::build_node (NetworkTCPTransport *tcp, NetworkUDP *udp, const NetworkAddress &addr, enum NetworkNodeType type) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    26
    // XXX: unused
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    27
    (void) type;
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
    // create node
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    30
    return new NetworkNode(*this, tcp, udp, addr);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    31
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    32
381
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
    33
NetworkNode* NetworkSession::connect (const NetworkEndpoint &addr) {
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    34
    // XXX: only one connect
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    35
    assert(!udp_client);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    36
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    37
    // connect TCP
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    38
    NetworkTCPClient *tcp_client = new NetworkTCPClient(addr);
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 200
diff changeset
    39
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    40
    // create UDP socket on same address
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 200
diff changeset
    41
    udp_client = new NetworkUDP(tcp_client->get_local_address());
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 200
diff changeset
    42
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 200
diff changeset
    43
    // remote address
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 200
diff changeset
    44
    NetworkAddress remote_address = tcp_client->get_remote_address();
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    45
    
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    46
    // build client
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 200
diff changeset
    47
    NetworkNode *client_node = build_node(tcp_client, udp_client, remote_address, NETWORK_NODE_CLIENT_SERVER);
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    48
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    49
    // add to nodes
431
c6d7272a164b 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?\!
Tero Marttila <terom@fixme.fi>
parents: 381
diff changeset
    50
    node_map[remote_address] = client_node;
c6d7272a164b 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?\!
Tero Marttila <terom@fixme.fi>
parents: 381
diff changeset
    51
    node_list.push_back(client_node);
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    52
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    53
    // connect signals
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    54
    slots.connect(udp_client->sig_packet(), this, &NetworkSession::on_udp_packet);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    55
        
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    56
    // return the "server" node
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    57
    return client_node;
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
void NetworkSession::handle_disconnect (NetworkNode *node) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    61
    // remove from nodes
431
c6d7272a164b 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?\!
Tero Marttila <terom@fixme.fi>
parents: 381
diff changeset
    62
    node_map.erase(node->getRemoteAddress());
c6d7272a164b 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?\!
Tero Marttila <terom@fixme.fi>
parents: 381
diff changeset
    63
    node_list.remove(node);
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    64
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    65
        
200
2dbf40661580 better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents: 186
diff changeset
    66
void NetworkSession::handle_message (NetworkPacketInput &pkt, NetworkNode *node) {
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    67
    // read the channel id
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    68
    NetworkChannelID channel_id = pkt.read_uint16();
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    69
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    70
    // fire signal
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    71
    _map_sig_chan_message[channel_id](pkt, node);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    72
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    73
 
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    74
void NetworkSession::on_tcp_client (NetworkTCPTransport *tcp_client) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    75
    // get remote address manually, because NetworkTCPServer doesn't pass it in to us
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 200
diff changeset
    76
    NetworkAddress addr = tcp_client->get_remote_address();
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    77
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    78
    // build client
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    79
    NetworkNode *client_node = build_node(tcp_client, udp_srv, addr, NETWORK_NODE_SERVER_CLIENT);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    80
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    81
    // add to nodes
431
c6d7272a164b 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?\!
Tero Marttila <terom@fixme.fi>
parents: 381
diff changeset
    82
    node_map[addr] = client_node;
c6d7272a164b 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?\!
Tero Marttila <terom@fixme.fi>
parents: 381
diff changeset
    83
    node_list.push_back(client_node);
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    84
    
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    85
    // fire signals
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    86
    _sig_node_connected(client_node);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    87
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    88
        
200
2dbf40661580 better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents: 186
diff changeset
    89
void NetworkSession::on_udp_packet (NetworkPacketInput &pkt, const NetworkAddress &addr) {
431
c6d7272a164b 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?\!
Tero Marttila <terom@fixme.fi>
parents: 381
diff changeset
    90
    NetworkNode *node = node_map[addr];
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    91
    
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    92
    // drop from unknown sources
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    93
    if (!node) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    94
        Engine::log(WARN, "net_session.on_udp_packet") << "dropping unsolicted UDP packet from " << addr;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    95
        return;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    96
    }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    97
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    98
    // handle
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    99
    handle_message(pkt, node);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   100
}
431
c6d7272a164b 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?\!
Tero Marttila <terom@fixme.fi>
parents: 381
diff changeset
   101
c6d7272a164b 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?\!
Tero Marttila <terom@fixme.fi>
parents: 381
diff changeset
   102
void NetworkSession::write_packet_header (NetworkPacketOutput &pkt, NetworkChannelID channel_id) {
c6d7272a164b 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?\!
Tero Marttila <terom@fixme.fi>
parents: 381
diff changeset
   103
    pkt.write_uint16(channel_id);
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   104
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   105