Rev 1317 | Details | Compare with Previous | 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" |
| 1317 | runge | 16 | #include "thread/Thread.h" |
| 1314 | runge | 17 | #include "utils/convert.h" |
| 1311 | runge | 18 | #include "types.h" |
| 1309 | runge | 19 | |
| 20 | namespace atom { |
||
| 1317 | runge | 21 | namespace net { |
| 1309 | runge | 22 | |
| 23 | using boost::asio::ip::udp; |
||
| 1319 | runge | 24 | using namespace std; |
| 1309 | runge | 25 | |
| 1317 | runge | 26 | class UdpServer : protected thread::Thread |
| 1309 | runge | 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 | |||
| 1315 | runge | 50 | log::Logger LOG; |
| 1309 | runge | 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_ */ |