src/Network/UDP.cc
author terom
Mon, 15 Dec 2008 23:56:42 +0000
changeset 378 5589abf5e61b
parent 202 b3f5d766391e
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 "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);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    18
}
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
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
    21
    socket(AF_UNSPEC, SOCK_DGRAM) {
185
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
    // bind socket
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    24
    socket.bind(bind_addr);
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
    // connect signal
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 202
diff changeset
    27
    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
    28
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    29
    // nonblocking
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    30
    socket.set_nonblocking(true);
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
        
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    33
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
    34
    size_t ret;
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    35
    NetworkPacket pkt;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    36
    NetworkAddress src;
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
    // receieve as many packets as possible
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    39
    do {    
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    40
        // 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
    41
        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
    42
        
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 202
diff changeset
    43
        // no more packets?
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 202
diff changeset
    44
        if (ret == 0)
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 202
diff changeset
    45
            return;
185
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
        // set packet data size
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    48
        pkt.set_data_size(ret);
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
        // handle packet
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    51
        _sig_packet(pkt, src);
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
    } while (true);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    54
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    55
        
202
b3f5d766391e support sending of raw packets
terom
parents: 186
diff changeset
    56
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
    57
    size_t ret;
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
    try {
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 202
diff changeset
    60
        // try and send
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 202
diff changeset
    61
        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
    62
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 202
diff changeset
    63
    } catch (NetworkSocketError &e) {
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 202
diff changeset
    64
        // catch and log errors
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 202
diff changeset
    65
        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
    66
        return false;
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
    
378
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 202
diff changeset
    69
    // weird packet size?
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 202
diff changeset
    70
    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
    71
        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
    72
        return false;
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 202
diff changeset
    73
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 202
diff changeset
    74
    } else {
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 202
diff changeset
    75
        // sent
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 202
diff changeset
    76
        return true;
5589abf5e61b break the network code. Too late to set up a branch for this now
terom
parents: 202
diff changeset
    77
    }
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    78
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    79