src/Network/TCP.cc
author terom
Wed, 17 Dec 2008 00:40:22 +0000
changeset 381 9b35bc329d23
parent 380 d193dd1d8a7e
child 431 c6d7272a164b
permissions -rw-r--r--
separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
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);
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    14
    
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    15
    // activate read polling, but leave write polling for later
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    16
    socket->set_poll_read(true);
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    17
}
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 263
diff changeset
    18
        
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 263
diff changeset
    19
NetworkTCPTransport::~NetworkTCPTransport (void) {
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 263
diff changeset
    20
    // release socket
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 263
diff changeset
    21
    delete socket;
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 263
diff changeset
    22
}
185
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
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
    25
    uint32_t length;
200
2dbf40661580 better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents: 186
diff changeset
    26
    char *buf_ptr;
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    27
    
227
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    28
    try { 
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    29
        // 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
    30
        do {
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    31
            // not enough data -> return and wait 
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    32
            if (in.peek_data<uint32_t>(length, buf_ptr) == false)
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    33
                break;
200
2dbf40661580 better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents: 186
diff changeset
    34
227
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    35
            // allocate the NetworkPacketBuffer with the given buf_ptr/length
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    36
            NetworkPacketBuffer packet(buf_ptr, length, length);
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
            // pass the packet on
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    39
            _sig_packet(packet);
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    40
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    41
            // flush it
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    42
            in.flush_data<uint32_t>();
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    43
        } while (true);
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    44
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    45
    } catch (NetworkSocketError &e) {
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    46
        // log and disconnect
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    47
        Engine::log(ERROR, "tcp.on_read") << "socket error: " << e.what();
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    48
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    49
        handle_disconnect();
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    50
    }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    51
}
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
void NetworkTCPTransport::on_write (void) {
227
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    54
    try {
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    55
        // just flush the output buffer, and deactivate output polling if done
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    56
        if (!out.flush_write())
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    57
            socket->set_poll_write(false);
227
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
    } catch (NetworkSocketError &e) {
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    60
        // log and disconnect
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    61
        Engine::log(ERROR, "tcp.on_write") << "socket error: " << e.what();
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    62
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    63
        handle_disconnect();
227
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    64
    }
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
        
200
2dbf40661580 better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents: 186
diff changeset
    67
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
    68
    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
    69
    
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    70
    if (prefix != packet.get_data_size())
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    71
        throw CL_Error("send prefix overflow");
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
    try {
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    74
        // just write to the output buffer, but activate output polling if needed
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    75
        if (out.write_prefix((char *) packet.get_buf(), prefix))
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    76
            socket->set_poll_write(true);
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    77
227
39cd6861e43e handle errors a bit more gracefully in NetworkTCP
terom
parents: 203
diff changeset
    78
    } catch (NetworkSocketError &e) {
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    79
        const char *err = e.what();
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
        Engine::log(ERROR, "tcp.write_packet") << err;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    82
        
263
8c999cf4c182 weapon projectile radiuses and fix network play (local_player == NULL, Rope releasing upon being hit
terom
parents: 246
diff changeset
    83
        // XXX: these are not handled anywhere :(
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    84
        throw;    
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    85
    }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    86
}
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    87
        
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    88
void NetworkTCPTransport::handle_disconnect (void) {
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    89
    // disable events on our socket
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    90
    socket->set_poll_read(false);
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    91
    socket->set_poll_write(false);
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    92
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    93
    // signal
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    94
    _sig_disconnect();
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    95
}
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    96
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 263
diff changeset
    97
/*
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 263
diff changeset
    98
 * NetworkTCPServer
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 263
diff changeset
    99
 */
381
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
   100
NetworkTCPServer::NetworkTCPServer (const NetworkEndpoint &listen_addr) :
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 263
diff changeset
   101
    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
   102
    
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   103
    // bind
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   104
    socket.bind(listen_addr);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   105
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   106
    // assign slots
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 263
diff changeset
   107
    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
   108
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   109
    // listen
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   110
    socket.listen(NETWORK_LISTEN_BACKLOG);
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
    // use nonblocking sockets
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   113
    socket.set_nonblocking(true);
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   114
    
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   115
    // activate polling
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
   116
    socket.set_poll_read(true);
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   117
}
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
void NetworkTCPServer::on_accept (void) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   120
    // 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
   121
    NetworkSocket *client_sock = socket.accept(NULL);
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   122
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   123
    // create a new NetworkTCPTransport
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   124
    NetworkTCPTransport *client = buildTransport(client_sock);
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
    // let our user handle it
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   127
    _sig_client(client);
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
        
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 263
diff changeset
   130
NetworkTCPTransport* NetworkTCPServer::buildTransport (NetworkSocket *socket) {
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   131
    return new NetworkTCPTransport(socket);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   132
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   133
        
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 263
diff changeset
   134
/*
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 263
diff changeset
   135
 * NetworkTCPClient
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 263
diff changeset
   136
 */
381
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 380
diff changeset
   137
NetworkTCPClient::NetworkTCPClient (const NetworkEndpoint &connect_addr) :
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 263
diff changeset
   138
    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
   139
    
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   140
    // connect
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 263
diff changeset
   141
    socket->connect(connect_addr);
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   142
    
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   143
    // use nonblocking sockets
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 263
diff changeset
   144
    socket->set_nonblocking(true);
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   145
}