Subversion Repositories HomeAutomation

Rev

Rev 1963 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

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