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> |
||
| 1315 | runge | 15 | #include "log/Logger.h" |
| 1311 | runge | 16 | #include "utils/BitBuffer.h" |
| 17 | #include "utils/Thread.h" |
||
| 1314 | runge | 18 | #include "utils/convert.h" |
| 1311 | runge | 19 | #include "types.h" |
| 1309 | runge | 20 | |
| 21 | namespace atom { |
||
| 22 | namespace utils { |
||
| 23 | |||
| 24 | using boost::asio::ip::udp; |
||
| 1311 | runge | 25 | using namespace utils; |
| 1309 | runge | 26 | |
| 27 | class UdpServer : protected Thread |
||
| 28 | { |
||
| 29 | typedef boost::signal<void(udp::endpoint & sender, byte_list & bytes)> OnDataSignal; |
||
| 30 | |||
| 31 | public: |
||
| 32 | typedef boost::shared_ptr<UdpServer> pointer; |
||
| 33 | |||
| 34 | ~UdpServer(); |
||
| 35 | |||
| 36 | static pointer getInstance(unsigned int port); |
||
| 37 | |||
| 38 | void sendTo(udp::endpoint receiver, byte_list bytes); |
||
| 39 | void connect(const OnDataSignal::slot_type & slot); |
||
| 40 | |||
| 41 | udp::endpoint getEndpoint(string address); |
||
| 42 | unsigned int getPort(); |
||
| 43 | |||
| 44 | private: |
||
| 45 | UdpServer(unsigned int port); |
||
| 46 | |||
| 47 | void run(); |
||
| 48 | void receiveFrom(); |
||
| 49 | void handleReceiveFrom(const boost::system::error_code& error, size_t bytes_received); |
||
| 50 | |||
| 1315 | runge | 51 | log::Logger LOG; |
| 1309 | runge | 52 | OnDataSignal onNewData; |
| 53 | |||
| 54 | enum { MAX_LENGTH = 64 }; |
||
| 55 | |||
| 56 | boost::asio::io_service myIoService; |
||
| 57 | udp::socket mySocket; |
||
| 58 | |||
| 59 | udp::endpoint mySender; |
||
| 60 | unsigned char myDataBuffer[MAX_LENGTH]; |
||
| 61 | }; |
||
| 62 | |||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | #endif /* UDPSERVER_H_ */ |