Subversion Repositories HomeAutomation

Rev

Rev 1314 | Go to most recent revision | Blame | 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 "utils/BitBuffer.h"
  17. #include "utils/Thread.h"
  18. #include "utils/convert.h"
  19. #include "types.h"
  20.  
  21. namespace atom {
  22. namespace utils {
  23.  
  24. using boost::asio::ip::udp;
  25. using namespace utils;
  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.  
  51.     log::Logger LOG;
  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_ */
  67.