Subversion Repositories HomeAutomation

Rev

Rev 1987 | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
1592 runge 1
/*
2
 *
3
 *    Copyright (C) 2010  Mattias Runge
4
 *
5
 *    This program is free software; you can redistribute it and/or modify
6
 *    it under the terms of the GNU General Public License as published by
7
 *    the Free Software Foundation; either version 2 of the License, or
8
 *    (at your option) any later version.
9
 *
10
 *    This program is distributed in the hope that it will be useful,
11
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *    GNU General Public License for more details.
14
 *
15
 *    You should have received a copy of the GNU General Public License along
16
 *    with this program; if not, write to the Free Software Foundation, Inc.,
17
 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
 *
19
 */
20
 
21
#include "UdpClient.h"
22
 
23
#include <boost/lexical_cast.hpp>
24
 
1963 runge 25
#include "common/log.h"
26
 
1592 runge 27
namespace atom {
28
namespace net {
1963 runge 29
 
30
static const std::string log_module_ = "net::udpclient";
1592 runge 31
 
1962 runge 32
UdpClient::UdpClient(boost::asio::io_service& io_service, SocketId id, SocketId server_id) : Client(io_service, id, server_id)
1592 runge 33
{
1963 runge 34
  LOG_DEBUG_ENTER;
35
  LOG_DEBUG_EXIT;
1592 runge 36
}
37
 
38
UdpClient::~UdpClient()
39
{
1963 runge 40
  LOG_DEBUG_ENTER;
41
  LOG_DEBUG_EXIT;
1592 runge 42
}
43
 
44
void UdpClient::Connect(std::string address, unsigned int port)
45
{
1963 runge 46
  LOG_DEBUG_ENTER;
47
 
48
  try
49
  {
50
    boost::asio::ip::udp::resolver resolver(this->io_service_);
51
    boost::asio::ip::udp::resolver::query query(boost::asio::ip::udp::v4(), address, boost::lexical_cast<std::string>(port));
52
    boost::asio::ip::udp::resolver::iterator it = resolver.resolve(query);
1592 runge 53
 
1963 runge 54
    this->endpoint_ = *it;
55
 
56
    this->socket_.reset(new boost::asio::ip::udp::socket(this->io_service_, boost::asio::ip::udp::endpoint(boost::asio::ip::udp::v4(), port)));
57
  }
58
  catch (std::exception e)
59
  {
60
    throw std::runtime_error("Error while connecting to " + address + ":" + boost::lexical_cast<std::string>(port) + ", " + e.what());
61
  }
62
 
63
  this->Read();
64
 
65
  LOG_DEBUG_EXIT;
1592 runge 66
}
67
 
1599 runge 68
void UdpClient::Stop()
1592 runge 69
{
1963 runge 70
  LOG_DEBUG_ENTER;
71
 
72
  if (this->socket_.use_count() != 0)
73
  {
74
    this->socket_->cancel();
75
    this->socket_->close();
76
    this->socket_.reset();
77
  }
78
 
79
  LOG_DEBUG_EXIT;
1592 runge 80
}
81
 
1642 runge 82
void UdpClient::Send(common::Byteset data)
1592 runge 83
{
1963 runge 84
  LOG_DEBUG_ENTER;
85
 
1989 runge 86
  //log::Debug(log_module_, "data=\"%s\", size=%u", std::string(data.begin(), data.end()).data(), data.size());
1987 runge 87
 
1963 runge 88
  try
89
  {
90
    if (this->socket_->is_open())
1593 runge 91
    {
1987 runge 92
      this->socket_->send_to(boost::asio::buffer(data.data(), data.size()), this->endpoint_);
1593 runge 93
    }
1963 runge 94
  }
95
  catch (std::exception& e)
96
  {
97
    this->Disconnect();
98
    //throw std::runtime_error(e.what());
99
  }
100
 
101
  LOG_DEBUG_EXIT;
1592 runge 102
}
103
 
104
void UdpClient::Read()
105
{
1963 runge 106
  LOG_DEBUG_ENTER;
107
 
108
  Client::Read();
1987 runge 109
 
1989 runge 110
  //log::Debug(log_module_, "this->buffer_.capacity()=%u", this->buffer_.capacity());
1599 runge 111
 
1987 runge 112
  this->socket_->async_receive(boost::asio::buffer(this->buffer_.data(), this->buffer_.capacity()),
1963 runge 113
                               boost::bind(&UdpClient::ReadHandler,
114
                                           this,
115
                                           boost::asio::placeholders::error,
116
                                           boost::asio::placeholders::bytes_transferred));
117
 
118
  LOG_DEBUG_EXIT;
1592 runge 119
}
120
 
121
}; // namespace net
122
}; // namespace atom