-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbase_socket_communication.cpp
More file actions
228 lines (163 loc) · 6.04 KB
/
base_socket_communication.cpp
File metadata and controls
228 lines (163 loc) · 6.04 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
220
221
222
223
224
225
226
227
228
// ______ _____ _ ________
// / ____/___ / ___/(_)___ ___ / _/ __ |
// / / / __ \\__ \/ / __ `__ \ / // / / /
// / /___/ /_/ /__/ / / / / / / // // /_/ /
// \____/\____/____/_/_/ /_/ /_/___/\____/
// Kratos CoSimulationApplication
//
// License: BSD License, see license.txt
//
// Main authors: Philipp Bucher (https://github.com/philbucher)
//
// System includes
#ifdef CO_SIM_IO_TIMESTAMP_DEBUG
#include <chrono>
#include <ctime>
#include <iomanip>
#endif
// Project includes
#include "includes/communication/base_socket_communication.hpp"
namespace CoSimIO {
namespace Internals {
template<class TSocketType>
BaseSocketCommunication<TSocketType>::~BaseSocketCommunication<TSocketType>()
{
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
DebugTimeInfo("ConnectDetail", "Before connect");
// required such that asio keeps listening for incoming messages
mContextThread = std::thread([this]() { mAsioContext.run(); });
DebugTimeInfo("ConnectDetail", "After connect");
return Info();
CO_SIM_IO_CATCH
}
template<class TSocketType>
Info BaseSocketCommunication<TSocketType>::DisconnectDetail(const Info& I_Info)
{
CO_SIM_IO_TRY
DebugTimeInfo("DisconnectDetail", "Before disconnect");
// 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 resources (otherwise crashes in Win with release compilation)
DebugTimeInfo("DisconnectDetail", "After disconnect");
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
DebugTimeInfo("SendString", "Before send");
const auto start_time(std::chrono::steady_clock::now());
asio::write(*mpAsioSocket, asio::buffer(rData.data(), rData.size()));
DebugTimeInfo("SendString", "After send");
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
DebugTimeInfo("ReceiveString", "Before receive");
const auto start_time(std::chrono::steady_clock::now());
rData.resize(received_size);
asio::read(*mpAsioSocket, asio::buffer(&(rData.front()), received_size));
DebugTimeInfo("ReceiveString", "After receive");
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
DebugTimeInfo("SendDataContainer", "Before send");
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)));
DebugTimeInfo("SendDataContainer", "After send");
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
DebugTimeInfo("ReceiveDataContainer", "Before receive");
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)));
DebugTimeInfo("ReceiveDataContainer", "After receive");
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
DebugTimeInfo("SendSize", "Before send");
#ifdef CO_SIM_IO_TIMESTAMP_DEBUG
CO_SIM_IO_INFO("CoSimIO::SendSize") << Size << std::endl;
#endif
asio::write(*mpAsioSocket, asio::buffer(&Size, sizeof(Size)));
DebugTimeInfo("SendSize", "After send");
CO_SIM_IO_CATCH
}
template<class TSocketType>
std::uint64_t BaseSocketCommunication<TSocketType>::ReceiveSize()
{
CO_SIM_IO_TRY
DebugTimeInfo("ReceiveSize", "Before receive");
std::uint64_t imp_size_u;
asio::read(*mpAsioSocket, asio::buffer(&imp_size_u, sizeof(imp_size_u)));
DebugTimeInfo("ReceiveSize", "After receive");
#ifdef CO_SIM_IO_TIMESTAMP_DEBUG
CO_SIM_IO_INFO("CoSimIO::ReceiveSize") << imp_size_u << std::endl;
#endif
return imp_size_u;
CO_SIM_IO_CATCH
}
template<class TSocketType>
void BaseSocketCommunication<TSocketType>::DebugTimeInfo(
const std::string& rLabel,
const std::string& rMessage
) const
{
#ifdef CO_SIM_IO_TIMESTAMP_DEBUG
// Get the current time
auto now = std::chrono::system_clock::now();
// Convert to time_t to get calendar time
std::time_t now_time = std::chrono::system_clock::to_time_t(now);
// Convert to local time
std::tm local_time = *std::localtime(&now_time);
// Convert to calendar time to get localtime
CO_SIM_IO_INFO("CoSimIO::" + rLabel) << rMessage << " time: " << std::put_time(&local_time, "%H:%M:%S") << std::endl;
#endif
}
template class BaseSocketCommunication<asio::ip::tcp::socket>;
template class BaseSocketCommunication<asio::local::stream_protocol::socket>;
} // namespace Internals
} // namespace CoSimIO