Subversion Repositories HomeAutomation

Rev

Rev 1317 | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  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>
  15. #include "log/Logger.h"
  16. #include "thread/Thread.h"
  17. #include "utils/convert.h"
  18. #include "types.h"
  19.  
  20. namespace atom {
  21. namespace net {
  22.  
  23. using boost::asio::ip::udp;
  24. using namespace std;
  25.  
  26. class UdpServer : protected thread::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.     log::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_ */
  66.