Subversion Repositories HomeAutomation

Rev

Rev 1599 | Rev 1939 | Go to most recent revision | 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
 
25
namespace atom {
26
namespace net {
27
 
28
UdpClient::UdpClient(boost::asio::io_service& io_service, ClientId id, ServerId server_id) : io_service_(io_service), Client(io_service, id, server_id)
29
{
30
}
31
 
32
UdpClient::~UdpClient()
33
{
34
}
35
 
36
void UdpClient::Connect(std::string address, unsigned int port)
37
{
38
    try
39
    {
40
        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));
42
        boost::asio::ip::udp::resolver::iterator it = resolver.resolve(query);
43
 
44
        this->endpoint_ = *it;
45
 
46
        this->socket_.reset(new boost::asio::ip::udp::socket(this->io_service_, boost::asio::ip::udp::endpoint(boost::asio::ip::udp::v4(), port)));
47
    }
48
    catch (std::exception e)
49
    {
50
        throw std::runtime_error("Error while connecting to " + address + ":" + boost::lexical_cast<std::string>(port) + ", " + e.what());
51
    }
52
 
53
    this->Read();
54
}
55
 
1599 runge 56
void UdpClient::Stop()
1592 runge 57
{
58
    if (this->socket_.use_count() != 0)
59
    {
1595 runge 60
        this->socket_->cancel();
1592 runge 61
        this->socket_->close();
62
        this->socket_.reset();
63
    }
64
}
65
 
1642 runge 66
void UdpClient::Send(common::Byteset data)
1592 runge 67
{
1597 runge 68
    try
1593 runge 69
    {
1597 runge 70
        if (this->socket_->is_open())
71
        {
72
            this->socket_->send_to(boost::asio::buffer(data.Get(), data.GetMaxSize()), this->endpoint_);
73
        }
1593 runge 74
    }
1597 runge 75
    catch (std::exception& e)
1593 runge 76
    {
77
        this->Disconnect();
1597 runge 78
        //throw std::runtime_error(e.what());
1593 runge 79
    }
1592 runge 80
}
81
 
82
void UdpClient::Read()
83
{
84
    Client::Read();
1599 runge 85
 
1596 runge 86
    this->socket_->async_receive(boost::asio::buffer(this->buffer_.Get(), this->buffer_.GetMaxSize()),
1592 runge 87
                                 boost::bind(&UdpClient::ReadHandler,
88
                                             this,
89
                                             boost::asio::placeholders::error,
90
                                             boost::asio::placeholders::bytes_transferred));
91
}
92
 
93
}; // namespace net
94
}; // namespace atom