src/Network/TCP.cc
author terom
Mon, 08 Dec 2008 01:12:00 +0000
changeset 277 40f4a03917e2
parent 263 8c999cf4c182
child 378 5589abf5e61b
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
186
0738f2949a2b move src/Network% to src/Network/%
terom
parents: 185
diff changeset
     2
#include "TCP.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
200
2dbf40661580 better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents: 186
diff changeset
     5
/*
2dbf40661580 better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents: 186
diff changeset
     6
 * NetworkTCPTransport
2dbf40661580 better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents: 186
diff changeset
     7
 */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     8
NetworkTCPTransport::NetworkTCPTransport (NetworkSocket socket) :
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     9
    socket(socket), in(socket, NETWORK_TCP_INITIAL_IN_BUF), out(socket, NETWORK_TCP_INITIAL_OUT_BUF) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    10
    
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    11
    // connect signals
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    12
    slots.connect(socket.sig_read_triggered(), this, &NetworkTCPTransport::on_read);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    13
    slots.connect(socket.sig_write_triggered(), this, &NetworkTCPTransport::on_write);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    14
    slots.connect(socket.sig_disconnected(), this, &NetworkTCPTransport::on_disconnected);
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
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
void NetworkTCPTransport::on_read (void) {
203
3ec7ab40755f send initial terrain data to clients using a new NETCHAN_TERRAIN_ARRAY channel using raw BigNetworkPacket
terom
parents: 200
diff changeset
    19
    uint32_t length;
200
2dbf40661580 better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents: 186
diff changeset
    20
    char *buf_ptr;
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    21
    
227
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    22
    try { 
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    23
        // let the in stream read length-prefixed packets and pass them on to handle_packet
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    24
        do {
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    25
            // not enough data -> return and wait 
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    26
            if (in.peek_data<uint32_t>(length, buf_ptr) == false)
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    27
                break;
200
2dbf40661580 better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents: 186
diff changeset
    28
227
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    29
            // allocate the NetworkPacketBuffer with the given buf_ptr/length
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    30
            NetworkPacketBuffer packet(buf_ptr, length, length);
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    31
            
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    32
            // pass the packet on
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    33
            _sig_packet(packet);
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    34
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    35
            // flush it
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    36
            in.flush_data<uint32_t>();
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    37
        } while (true);
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    38
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    39
    } catch (NetworkSocketError &e) {
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    40
        // log and disconnect
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    41
        Engine::log(ERROR, "tcp.on_read") << "socket error: " << e.what();
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    42
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    43
        _sig_disconnect();
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    44
    }
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
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    47
void NetworkTCPTransport::on_write (void) {
227
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    48
    try {
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    49
        // just flush the output buffer
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    50
        out.flush_write();
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    51
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    52
    } catch (NetworkSocketError &e) {
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    53
        // log and disconnect
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    54
        Engine::log(ERROR, "tcp.on_write") << "socket error: " << e.what();
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    55
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    56
        _sig_disconnect();
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    57
    }
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
void NetworkTCPTransport::on_disconnected (void) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    61
    // pass right through
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    62
    _sig_disconnect();
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    63
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    64
        
200
2dbf40661580 better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents: 186
diff changeset
    65
void NetworkTCPTransport::write_packet (const NetworkPacketBuffer &packet) {
203
3ec7ab40755f send initial terrain data to clients using a new NETCHAN_TERRAIN_ARRAY channel using raw BigNetworkPacket
terom
parents: 200
diff changeset
    66
    uint32_t prefix = packet.get_data_size();
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    67
    
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    68
    if (prefix != packet.get_data_size())
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    69
        throw CL_Error("send prefix overflow");
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    70
    
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    71
    try {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    72
        // just write to the output buffer
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    73
        out.write_prefix((char *) packet.get_buf(), prefix);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    74
227
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    75
    } catch (NetworkSocketError &e) {
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    76
        const char *err = e.what();
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
        Engine::log(ERROR, "tcp.write_packet") << err;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    79
        
263
8c999cf4c182 weapon projectile radiuses and fix network play (local_player == NULL, Rope releasing upon being hit
terom
parents: 246
diff changeset
    80
        // XXX: these are not handled anywhere :(
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    81
        throw;    
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    82
    }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    83
}
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
NetworkTCPServer::NetworkTCPServer (const NetworkAddress &listen_addr) :
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    86
    socket(CL_Socket::tcp, CL_Socket::ipv4) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    87
    
246
687d9896763a change mkcmake.sh to not use /tmp anymore, and fix some ClanLib idiocy
terom
parents: 227
diff changeset
    88
    // wow... I didn't know ClanLib was *this* crap
687d9896763a change mkcmake.sh to not use /tmp anymore, and fix some ClanLib idiocy
terom
parents: 227
diff changeset
    89
    socket.proto = CL_Socket::tcp;
687d9896763a change mkcmake.sh to not use /tmp anymore, and fix some ClanLib idiocy
terom
parents: 227
diff changeset
    90
    
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    91
    // bind
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    92
    socket.bind(listen_addr);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    93
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    94
    // assign slots
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    95
    slots.connect(socket.sig_read_triggered(), this, &NetworkTCPServer::on_accept);
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
    // listen
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    98
    socket.listen(NETWORK_LISTEN_BACKLOG);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    99
    
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   100
    // use nonblocking sockets
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   101
    socket.set_nonblocking(true);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   102
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   103
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
void NetworkTCPServer::on_accept (void) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   106
    // accept a new socket
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   107
    NetworkSocket client_sock = socket.accept();
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   108
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   109
    // create a new NetworkTCPTransport
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   110
    NetworkTCPTransport *client = buildTransport(client_sock);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   111
        
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   112
    // let our user handle it
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   113
    _sig_client(client);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   114
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   115
        
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   116
NetworkTCPTransport* NetworkTCPServer::buildTransport (CL_Socket &socket) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   117
    return new NetworkTCPTransport(socket);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   118
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   119
        
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   120
NetworkTCPClient::NetworkTCPClient (const NetworkAddress &connect_addr) :
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   121
    NetworkTCPTransport(NetworkSocket(CL_Socket::tcp, CL_Socket::ipv4)) {
246
687d9896763a change mkcmake.sh to not use /tmp anymore, and fix some ClanLib idiocy
terom
parents: 227
diff changeset
   122
    
687d9896763a change mkcmake.sh to not use /tmp anymore, and fix some ClanLib idiocy
terom
parents: 227
diff changeset
   123
    // wow... I didn't know ClanLib was *this* crap
687d9896763a change mkcmake.sh to not use /tmp anymore, and fix some ClanLib idiocy
terom
parents: 227
diff changeset
   124
    socket.proto = CL_Socket::tcp;
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   125
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   126
    // connect
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   127
    socket.connect(connect_addr);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   128
    
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   129
    // use nonblocking sockets
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   130
    socket.set_nonblocking(true);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   131
}