-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbase_socket_communication.cpp
More file actions
159 lines (119 loc) · 4.23 KB
/
base_socket_communication.cpp
File metadata and controls
159 lines (119 loc) · 4.23 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
// ______ _____ _ ________
// / ____/___ / ___/(_)___ ___ / _/ __ |
// / / / __ \\__ \/ / __ `__ \ / // / / /
// / /___/ /_/ /__/ / / / / / / // // /_/ /
// \____/\____/____/_/_/ /_/ /_/___/\____/
// Kratos CoSimulationApplication
//
// License: BSD License, see license.txt
//
// Main authors: Philipp Bucher (https://github.com/philbucher)
//
// System includes
// Project includes
#include "includes/communication/base_socket_communication.hpp"
namespace CoSimIO {
namespace Internals {
template<class TSocketType>
BaseSocketCommunication<TSocketType>::~BaseSocketCommunication()
{
CO_SIM_IO_TRY
if (GetIsConnected()) {
CO_SIM_IO_INFO("CoSimIO") << "Warning: Disconnect was not performed, attempting automatic disconnection!" << std::endl;
Info tmp;
Disconnect(tmp);
}
CO_SIM_IO_CATCH
}
template<class TSocketType>
Info BaseSocketCommunication<TSocketType>::ConnectDetail(const Info& I_Info)
{
CO_SIM_IO_TRY
// required such that asio keeps listening for incoming messages
mContextThread = std::thread([this]() { mAsioContext.run(); });
return Info();
CO_SIM_IO_CATCH
}
template<class TSocketType>
Info BaseSocketCommunication<TSocketType>::DisconnectDetail(const Info& I_Info)
{
CO_SIM_IO_TRY
// Request the context to close
mAsioContext.stop();
// Tidy up the context thread
if (mContextThread.joinable()) mContextThread.join();
mpAsioSocket->close();
mpAsioSocket.reset(); // important to release the resouces (otherwise crashes in Win with release compilation)
return Info();
CO_SIM_IO_CATCH
}
template<class TSocketType>
double BaseSocketCommunication<TSocketType>::SendString(
const Info& I_Info,
const std::string& rData)
{
CO_SIM_IO_TRY
SendSize(rData.size()); // serves also as synchronization for time measurement
const auto start_time(std::chrono::steady_clock::now());
asio::write(*mpAsioSocket, asio::buffer(rData.data(), rData.size()));
return Utilities::ElapsedSeconds(start_time);
CO_SIM_IO_CATCH
}
template<class TSocketType>
double BaseSocketCommunication<TSocketType>::ReceiveString(
const Info& I_Info,
std::string& rData)
{
CO_SIM_IO_TRY
std::size_t received_size = ReceiveSize(); // serves also as synchronization for time measurement
const auto start_time(std::chrono::steady_clock::now());
rData.resize(received_size);
asio::read(*mpAsioSocket, asio::buffer(&(rData.front()), received_size));
return Utilities::ElapsedSeconds(start_time);
CO_SIM_IO_CATCH
}
template<class TSocketType>
double BaseSocketCommunication<TSocketType>::SendDataContainer(
const Info& I_Info,
const Internals::DataContainer<double>& rData)
{
CO_SIM_IO_TRY
SendSize(rData.size()); // serves also as synchronization for time measurement
const auto start_time(std::chrono::steady_clock::now());
asio::write(*mpAsioSocket, asio::buffer(rData.data(), rData.size()*sizeof(double)));
return Utilities::ElapsedSeconds(start_time);
CO_SIM_IO_CATCH
}
template<class TSocketType>
double BaseSocketCommunication<TSocketType>::ReceiveDataContainer(
const Info& I_Info,
Internals::DataContainer<double>& rData)
{
CO_SIM_IO_TRY
std::size_t received_size = ReceiveSize(); // serves also as synchronization for time measurement
const auto start_time(std::chrono::steady_clock::now());
rData.resize(received_size);
asio::read(*mpAsioSocket, asio::buffer(rData.data(), rData.size()*sizeof(double)));
return Utilities::ElapsedSeconds(start_time);
CO_SIM_IO_CATCH
}
template<class TSocketType>
void BaseSocketCommunication<TSocketType>::SendSize(const std::uint64_t Size)
{
CO_SIM_IO_TRY
asio::write(*mpAsioSocket, asio::buffer(&Size, sizeof(Size)));
CO_SIM_IO_CATCH
}
template<class TSocketType>
std::uint64_t BaseSocketCommunication<TSocketType>::ReceiveSize()
{
CO_SIM_IO_TRY
std::uint64_t imp_size_u;
asio::read(*mpAsioSocket, asio::buffer(&imp_size_u, sizeof(imp_size_u)));
return imp_size_u;
CO_SIM_IO_CATCH
}
template class BaseSocketCommunication<asio::ip::tcp::socket>;
template class BaseSocketCommunication<asio::local::stream_protocol::socket>;
} // namespace Internals
} // namespace CoSimIO