Subversion Repositories HomeAutomation

Rev

Details | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
1309 runge 1
/*
2
 * UdpServer.h
3
 *
4
 *  Created on: Jul 20, 2009
5
 *      Author: Mattias Runge
6
 */
7
 
8
#ifndef UDPSERVER_H_
9
#define UDPSERVER_H_
10
 
11
#include <boost/bind.hpp>
12
#include <boost/asio.hpp>
13
#include <boost/signal.hpp>
14
#include <boost/make_shared.hpp>
1311 runge 15
#include "utils/Logger.h"
16
#include "utils/BitBuffer.h"
17
#include "utils/Thread.h"
18
#include "types.h"
1309 runge 19
 
20
namespace atom {
21
namespace utils {
22
 
23
using boost::asio::ip::udp;
1311 runge 24
using namespace utils;
1309 runge 25
 
26
class UdpServer : protected Thread
27
{
28
    typedef boost::signal<void(udp::endpoint & sender, byte_list & bytes)> OnDataSignal;
29
 
30
public:
31
    typedef boost::shared_ptr<UdpServer> pointer;
32
 
33
    ~UdpServer();
34
 
35
    static pointer getInstance(unsigned int port);
36
 
37
    void sendTo(udp::endpoint receiver, byte_list bytes);
38
    void connect(const OnDataSignal::slot_type & slot);
39
 
40
    udp::endpoint getEndpoint(string address);
41
    unsigned int getPort();
42
 
43
private:
44
    UdpServer(unsigned int port);
45
 
46
    void run();
47
    void receiveFrom();
48
    void handleReceiveFrom(const boost::system::error_code& error, size_t bytes_received);
49
 
50
    Logger LOG;
51
    OnDataSignal onNewData;
52
 
53
    enum { MAX_LENGTH = 64 };
54
 
55
    boost::asio::io_service myIoService;
56
    udp::socket mySocket;
57
 
58
    udp::endpoint mySender;
59
    unsigned char myDataBuffer[MAX_LENGTH];
60
};
61
 
62
}
63
}
64
 
65
#endif /* UDPSERVER_H_ */