-
Notifications
You must be signed in to change notification settings - Fork 940
Expand file tree
/
Copy pathTCPChannelResourceBasic.cpp
More file actions
219 lines (185 loc) · 5.92 KB
/
Copy pathTCPChannelResourceBasic.cpp
File metadata and controls
219 lines (185 loc) · 5.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Copyright 2018 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <rtps/transport/TCPChannelResourceBasic.h>
#include <future>
#include <array>
#include <asio.hpp>
#include <fastdds/utils/IPLocator.hpp>
#include <rtps/transport/TCPTransportInterface.h>
using namespace asio;
namespace eprosima {
namespace fastdds {
namespace rtps {
using Log = fastdds::dds::Log;
TCPChannelResourceBasic::TCPChannelResourceBasic(
TCPTransportInterface* parent,
asio::io_context& context,
const Locator& locator,
uint32_t maxMsgSize)
: TCPChannelResource(parent, locator, maxMsgSize)
, context_(context)
{
}
TCPChannelResourceBasic::TCPChannelResourceBasic(
TCPTransportInterface* parent,
asio::io_context& context,
std::shared_ptr<asio::ip::tcp::socket> socket,
uint32_t maxMsgSize)
: TCPChannelResource(parent, maxMsgSize)
, context_(context)
, socket_(socket)
{
}
TCPChannelResourceBasic::~TCPChannelResourceBasic()
{
}
void TCPChannelResourceBasic::connect(
const std::shared_ptr<TCPChannelResource>& myself)
{
assert(TCPConnectionType::TCP_CONNECT_TYPE == tcp_connection_type_);
eConnectionStatus expected = eConnectionStatus::eDisconnected;
if (connection_status_.compare_exchange_strong(expected, eConnectionStatus::eConnecting))
{
try
{
ip::tcp::resolver resolver(context_);
auto endpoints = resolver.resolve(
IPLocator::hasWan(locator_) ? IPLocator::toWanstring(locator_) : IPLocator::ip_to_string(
locator_),
std::to_string(IPLocator::getPhysicalPort(locator_)));
socket_ = std::make_shared<asio::ip::tcp::socket>(context_);
std::weak_ptr<TCPChannelResource> channel_weak_ptr = myself;
asio::async_connect(
*socket_,
endpoints,
[this, channel_weak_ptr](std::error_code ec
#if ASIO_VERSION >= 101200
, ip::tcp::endpoint
#else
, ip::tcp::resolver::iterator
#endif // if ASIO_VERSION >= 101200
)
{
if (!channel_weak_ptr.expired())
{
parent_->SocketConnected(channel_weak_ptr, ec);
}
}
);
}
catch (const std::system_error& error)
{
EPROSIMA_LOG_ERROR(RTCP, "Openning socket " << error.what());
}
}
}
void TCPChannelResourceBasic::disconnect()
{
if (eConnecting < change_status(eConnectionStatus::eDisconnected) && alive())
{
std::lock_guard<std::mutex> read_lock(read_mutex_);
auto socket = socket_;
std::error_code ec;
socket->shutdown(asio::ip::tcp::socket::shutdown_both, ec);
asio::post(context_, [&, socket]()
{
try
{
socket->cancel();
socket->close();
}
catch (std::exception&)
{
}
});
}
}
uint32_t TCPChannelResourceBasic::read(
octet* buffer,
std::size_t size,
asio::error_code& ec)
{
std::unique_lock<std::mutex> read_lock(read_mutex_);
if (eConnecting < connection_status_)
{
return static_cast<uint32_t>(asio::read(*socket_, asio::buffer(buffer, size), transfer_exactly(size), ec));
}
return 0;
}
size_t TCPChannelResourceBasic::send(
const octet* header,
size_t header_size,
const std::vector<NetworkBuffer>& buffers,
uint32_t total_bytes,
asio::error_code& ec)
{
size_t bytes_sent = 0;
if (eConnecting < connection_status_)
{
std::lock_guard<std::mutex> send_guard(send_mutex_);
if (parent_->configuration()->non_blocking_send &&
!check_socket_send_buffer(header_size + total_bytes, socket_->native_handle()))
{
return 0;
}
// Use a list of const_buffers to send the message
std::list<asio::const_buffer> asio_buffers;
if (header_size > 0)
{
asio_buffers.push_back(asio::buffer(header, header_size));
}
asio_buffers.insert(asio_buffers.end(), buffers.begin(), buffers.end());
bytes_sent = asio::write(*socket_.get(), asio_buffers, ec);
}
return bytes_sent;
}
asio::ip::tcp::endpoint TCPChannelResourceBasic::remote_endpoint() const
{
return socket_->remote_endpoint();
}
asio::ip::tcp::endpoint TCPChannelResourceBasic::local_endpoint() const
{
return socket_->local_endpoint();
}
asio::ip::tcp::endpoint TCPChannelResourceBasic::remote_endpoint(
asio::error_code& ec) const
{
return socket_->remote_endpoint(ec);
}
asio::ip::tcp::endpoint TCPChannelResourceBasic::local_endpoint(
asio::error_code& ec) const
{
return socket_->local_endpoint(ec);
}
void TCPChannelResourceBasic::set_options(
const TCPTransportDescriptor* options)
{
TCPChannelResource::set_socket_options(*socket_, options);
}
void TCPChannelResourceBasic::cancel()
{
socket_->cancel();
}
void TCPChannelResourceBasic::close()
{
socket_->close();
}
void TCPChannelResourceBasic::shutdown(
asio::socket_base::shutdown_type what)
{
socket_->shutdown(what);
}
} // namespace rtps
} // namespace fastdds
} // namespace eprosima