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