Subversion Repositories HomeAutomation

Rev

Rev 1962 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1962 Rev 1963
Line 19... Line 19...
19
 */
19
 */
20
 
20
 
21
#include "UdpClient.h"
21
#include "UdpClient.h"
22
 
22
 
23
#include <boost/lexical_cast.hpp>
23
#include <boost/lexical_cast.hpp>
-
 
24
 
-
 
25
#include "common/log.h"
24
 
26
 
25
namespace atom {
27
namespace atom {
26
namespace net {
28
namespace net {
-
 
29
 
-
 
30
static const std::string log_module_ = "net::udpclient";
27
 
31
 
28
UdpClient::UdpClient(boost::asio::io_service& io_service, SocketId id, SocketId server_id) : Client(io_service, id, server_id)
32
UdpClient::UdpClient(boost::asio::io_service& io_service, SocketId id, SocketId server_id) : Client(io_service, id, server_id)
29
{
33
{
-
 
34
  LOG_DEBUG_ENTER;
-
 
35
  LOG_DEBUG_EXIT;
30
}
36
}
31
 
37
 
32
UdpClient::~UdpClient()
38
UdpClient::~UdpClient()
33
{
39
{
-
 
40
  LOG_DEBUG_ENTER;
-
 
41
  LOG_DEBUG_EXIT;
34
}
42
}
35
 
43
 
36
void UdpClient::Connect(std::string address, unsigned int port)
44
void UdpClient::Connect(std::string address, unsigned int port)
37
{
45
{
-
 
46
  LOG_DEBUG_ENTER;
-
 
47
 
38
    try
48
  try
39
    {
49
  {
40
        boost::asio::ip::udp::resolver resolver(this->io_service_);
50
    boost::asio::ip::udp::resolver resolver(this->io_service_);
41
        boost::asio::ip::udp::resolver::query query(boost::asio::ip::udp::v4(), address, boost::lexical_cast<std::string>(port));
51
    boost::asio::ip::udp::resolver::query query(boost::asio::ip::udp::v4(), address, boost::lexical_cast<std::string>(port));
42
        boost::asio::ip::udp::resolver::iterator it = resolver.resolve(query);
52
    boost::asio::ip::udp::resolver::iterator it = resolver.resolve(query);
Line 44... Line 54...
44
        this->endpoint_ = *it;
54
    this->endpoint_ = *it;
45
       
55
   
46
        this->socket_.reset(new boost::asio::ip::udp::socket(this->io_service_, boost::asio::ip::udp::endpoint(boost::asio::ip::udp::v4(), port)));
56
    this->socket_.reset(new boost::asio::ip::udp::socket(this->io_service_, boost::asio::ip::udp::endpoint(boost::asio::ip::udp::v4(), port)));
47
    }
57
  }
48
    catch (std::exception e)
58
  catch (std::exception e)
49
    {
59
  {
50
        throw std::runtime_error("Error while connecting to " + address + ":" + boost::lexical_cast<std::string>(port) + ", " + e.what());
60
    throw std::runtime_error("Error while connecting to " + address + ":" + boost::lexical_cast<std::string>(port) + ", " + e.what());
51
    }
61
  }
52
   
62
 
53
    this->Read();
63
  this->Read();
-
 
64
 
-
 
65
  LOG_DEBUG_EXIT;
54
}
66
}
55
 
67
 
56
void UdpClient::Stop()
68
void UdpClient::Stop()
57
{
69
{
-
 
70
  LOG_DEBUG_ENTER;
-
 
71
 
58
    if (this->socket_.use_count() != 0)
72
  if (this->socket_.use_count() != 0)
59
    {
73
  {
60
        this->socket_->cancel();
74
    this->socket_->cancel();
61
        this->socket_->close();
75
    this->socket_->close();
62
        this->socket_.reset();
76
    this->socket_.reset();
63
    }
77
  }
-
 
78
 
-
 
79
  LOG_DEBUG_EXIT;
64
}
80
}
65
 
81
 
66
void UdpClient::Send(common::Byteset data)
82
void UdpClient::Send(common::Byteset data)
67
{
83
{
-
 
84
  LOG_DEBUG_ENTER;
-
 
85
 
68
    try
86
  try
69
    {
87
  {
70
        if (this->socket_->is_open())
88
    if (this->socket_->is_open())
71
        {
89
    {
72
            this->socket_->send_to(boost::asio::buffer(data.Get(), data.GetMaxSize()), this->endpoint_);
90
      this->socket_->send_to(boost::asio::buffer(data.Get(), data.GetMaxSize()), this->endpoint_);
73
        }
91
    }
74
    }
92
  }
75
    catch (std::exception& e)
93
  catch (std::exception& e)
76
    {
94
  {
77
        this->Disconnect();
95
    this->Disconnect();
78
        //throw std::runtime_error(e.what());
96
    //throw std::runtime_error(e.what());
79
    }
97
  }
-
 
98
 
-
 
99
  LOG_DEBUG_EXIT;
80
}
100
}
81
 
101
 
82
void UdpClient::Read()
102
void UdpClient::Read()
83
{
103
{
-
 
104
  LOG_DEBUG_ENTER;
-
 
105
 
84
    Client::Read();
106
  Client::Read();
85
 
107
 
86
    this->socket_->async_receive(boost::asio::buffer(this->buffer_.Get(), this->buffer_.GetMaxSize()),
108
  this->socket_->async_receive(boost::asio::buffer(this->buffer_.Get(), this->buffer_.GetMaxSize()),
87
                                 boost::bind(&UdpClient::ReadHandler,
109
                               boost::bind(&UdpClient::ReadHandler,
88
                                             this,
110
                                           this,
89
                                             boost::asio::placeholders::error,
111
                                           boost::asio::placeholders::error,
90
                                             boost::asio::placeholders::bytes_transferred));
112
                                           boost::asio::placeholders::bytes_transferred));
-
 
113
 
-
 
114
  LOG_DEBUG_EXIT;
91
}
115
}
92
 
116
 
93
}; // namespace net
117
}; // namespace net
94
}; // namespace atom
118
}; // namespace atom