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