src/Network/UDP.cc
author terom
Tue, 16 Dec 2008 23:21:26 +0000
changeset 380 d193dd1d8a7e
parent 378 5589abf5e61b
child 381 9b35bc329d23
permissions -rw-r--r--
new NetworkReactor, fix everything to actually work
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 "UDP.hh"
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 202
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 <ClanLib/core.h>
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     6
#include <cassert>
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     7
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     8
NetworkUDP::NetworkUDP (void) : 
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 202
diff changeset
     9
    socket(AF_UNSPEC, SOCK_DGRAM) 
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 202
diff changeset
    10
{
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    11
    // do not bind
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
    // connect signal
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 202
diff changeset
    14
    slots.connect(socket.sig_read(), this, &NetworkUDP::on_recv);
185
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
    // nonblocking
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    17
    socket.set_nonblocking(true);
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    18
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    19
    // activate polling
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    20
    socket.set_poll_read(true);
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    21
}
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
NetworkUDP::NetworkUDP (const NetworkAddress &bind_addr) :
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 202
diff changeset
    24
    socket(AF_UNSPEC, SOCK_DGRAM) {
185
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
    // bind socket
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    27
    socket.bind(bind_addr);
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
    // connect signal
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 202
diff changeset
    30
    slots.connect(socket.sig_read(), this, &NetworkUDP::on_recv);
185
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
    // nonblocking
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    33
    socket.set_nonblocking(true);
380
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    34
    
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    35
    // activate polling
d193dd1d8a7e new NetworkReactor, fix everything to actually work
terom
parents: 378
diff changeset
    36
    socket.set_poll_read(true);
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    37
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    38
        
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    39
void NetworkUDP::on_recv (void) {
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 202
diff changeset
    40
    size_t ret;
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    41
    NetworkPacket pkt;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    42
    NetworkAddress src;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    43
    
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    44
    // receieve as many packets as possible
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    45
    do {    
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    46
        // attempt to recv a packet
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 202
diff changeset
    47
        ret = socket.recv(pkt.get_buf(), pkt.get_buf_size(), &src);
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 202
diff changeset
    48
        
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 202
diff changeset
    49
        // no more packets?
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 202
diff changeset
    50
        if (ret == 0)
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 202
diff changeset
    51
            return;
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
        // set packet data size
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    54
        pkt.set_data_size(ret);
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
        // handle packet
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    57
        _sig_packet(pkt, src);
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
    } while (true);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    60
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    61
        
202
b3f5d766391e support sending of raw packets
terom
parents: 186
diff changeset
    62
bool NetworkUDP::sendto (const NetworkPacketBuffer &packet, const NetworkAddress &dst) {
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 202
diff changeset
    63
    size_t ret;
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
    try {
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 202
diff changeset
    66
        // try and send
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 202
diff changeset
    67
        ret = socket.send(packet.get_buf(), packet.get_data_size(), &dst);
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    68
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 202
diff changeset
    69
    } catch (NetworkSocketError &e) {
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 202
diff changeset
    70
        // catch and log errors
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 202
diff changeset
    71
        Engine::log(WARN, "udp.sendto") << "socket->send raised error: " << e.what();
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    72
        return false;
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
    
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 202
diff changeset
    75
    // weird packet size?
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 202
diff changeset
    76
    if (ret != packet.get_data_size()) {
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 202
diff changeset
    77
        Engine::log(ERROR, "udp.sendto") << "socket->send returned weird length: " << ret << ", packet was " << packet.get_data_size();
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 202
diff changeset
    78
        return false;
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 202
diff changeset
    79
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 202
diff changeset
    80
    } else {
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 202
diff changeset
    81
        // sent
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 202
diff changeset
    82
        return true;
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 202
diff changeset
    83
    }
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