src/Network/TCP.cc
author terom
Mon, 15 Dec 2008 23:56:42 +0000
changeset 378 5589abf5e61b
parent 263 8c999cf4c182
child 380 d193dd1d8a7e
permissions -rw-r--r--
break the network code. Too late to set up a branch for this now
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
 */
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 263
diff changeset
     8
NetworkTCPTransport::NetworkTCPTransport (NetworkSocket *socket) :
185
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
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 263
diff changeset
    12
    slots.connect(socket->sig_read(), this, &NetworkTCPTransport::on_read);
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 263
diff changeset
    13
    slots.connect(socket->sig_write(), this, &NetworkTCPTransport::on_write);
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    14
}
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 263
diff changeset
    15
        
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 263
diff changeset
    16
NetworkTCPTransport::~NetworkTCPTransport (void) {
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 263
diff changeset
    17
    // release socket
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 263
diff changeset
    18
    delete socket;
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 263
diff changeset
    19
}
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    20
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    21
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
    22
    uint32_t length;
200
2dbf40661580 better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents: 186
diff changeset
    23
    char *buf_ptr;
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    24
    
227
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    25
    try { 
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    26
        // 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
    27
        do {
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    28
            // not enough data -> return and wait 
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    29
            if (in.peek_data<uint32_t>(length, buf_ptr) == false)
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    30
                break;
200
2dbf40661580 better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents: 186
diff changeset
    31
227
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    32
            // allocate the NetworkPacketBuffer with the given buf_ptr/length
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    33
            NetworkPacketBuffer packet(buf_ptr, length, length);
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
            // pass the packet on
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    36
            _sig_packet(packet);
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    37
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    38
            // flush it
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    39
            in.flush_data<uint32_t>();
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    40
        } while (true);
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    41
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    42
    } catch (NetworkSocketError &e) {
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    43
        // log and disconnect
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    44
        Engine::log(ERROR, "tcp.on_read") << "socket error: " << e.what();
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    45
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    46
        _sig_disconnect();
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    47
    }
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
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    50
void NetworkTCPTransport::on_write (void) {
227
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    51
    try {
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    52
        // just flush the output buffer
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    53
        out.flush_write();
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    54
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    55
    } catch (NetworkSocketError &e) {
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    56
        // log and disconnect
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    57
        Engine::log(ERROR, "tcp.on_write") << "socket error: " << e.what();
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    58
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    59
        _sig_disconnect();
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    60
    }
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    61
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    62
        
200
2dbf40661580 better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents: 186
diff changeset
    63
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
    64
    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
    65
    
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    66
    if (prefix != packet.get_data_size())
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    67
        throw CL_Error("send prefix overflow");
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    68
    
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    69
    try {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    70
        // just write to the output buffer
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    71
        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
    72
227
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    73
    } catch (NetworkSocketError &e) {
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    74
        const char *err = e.what();
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    75
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    76
        Engine::log(ERROR, "tcp.write_packet") << err;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    77
        
263
8c999cf4c182 weapon projectile radiuses and fix network play (local_player == NULL, Rope releasing upon being hit
terom
parents: 246
diff changeset
    78
        // XXX: these are not handled anywhere :(
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    79
        throw;    
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
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    82
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 263
diff changeset
    83
/*
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 263
diff changeset
    84
 * NetworkTCPServer
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 263
diff changeset
    85
 */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    86
NetworkTCPServer::NetworkTCPServer (const NetworkAddress &listen_addr) :
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 263
diff changeset
    87
    socket(AF_UNSPEC, SOCK_STREAM) {
246
687d9896763a change mkcmake.sh to not use /tmp anymore, and fix some ClanLib idiocy
terom
parents: 227
diff changeset
    88
    
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    89
    // bind
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    90
    socket.bind(listen_addr);
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
    // assign slots
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 263
diff changeset
    93
    slots.connect(socket.sig_read(), this, &NetworkTCPServer::on_accept);
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    94
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    95
    // listen
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    96
    socket.listen(NETWORK_LISTEN_BACKLOG);
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
    // use nonblocking sockets
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    99
    socket.set_nonblocking(true);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   100
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   101
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
void NetworkTCPServer::on_accept (void) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   104
    // accept a new socket
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 263
diff changeset
   105
    NetworkSocket *client_sock = socket.accept(NULL);
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   106
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   107
    // create a new NetworkTCPTransport
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   108
    NetworkTCPTransport *client = buildTransport(client_sock);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   109
        
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   110
    // let our user handle it
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   111
    _sig_client(client);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   112
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   113
        
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 263
diff changeset
   114
NetworkTCPTransport* NetworkTCPServer::buildTransport (NetworkSocket *socket) {
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   115
    return new NetworkTCPTransport(socket);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   116
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   117
        
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 263
diff changeset
   118
/*
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 263
diff changeset
   119
 * NetworkTCPClient
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 263
diff changeset
   120
 */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   121
NetworkTCPClient::NetworkTCPClient (const NetworkAddress &connect_addr) :
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 263
diff changeset
   122
    NetworkTCPTransport(new NetworkSocket(AF_UNSPEC, SOCK_STREAM)) {
246
687d9896763a change mkcmake.sh to not use /tmp anymore, and fix some ClanLib idiocy
terom
parents: 227
diff changeset
   123
    
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   124
    // connect
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 263
diff changeset
   125
    socket->connect(connect_addr);
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   126
    
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   127
    // use nonblocking sockets
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 263
diff changeset
   128
    socket->set_nonblocking(true);
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   129
}