3
3
// Licensed under: MIT License <http://opensource.org/licenses/MIT>
4
4
5
5
#include " TCP/connection.hpp"
6
+ #include < sys/poll.h>
7
+ #include < sys/socket.h>
6
8
7
9
using namespace MB ::TCP;
8
10
@@ -86,8 +88,11 @@ std::vector<uint8_t> Connection::sendException(const MB::ModbusException &ex) {
86
88
}
87
89
88
90
std::vector<uint8_t > Connection::awaitRawMessage () {
89
- pollfd _pfd = {.fd = _sockfd, .events = POLLIN, .revents = POLLIN};
90
- if (::poll (&_pfd, 1 , 60 * 1000 /* 1 minute means the connection has died */ ) <= 0 ) {
91
+ pollfd pfd;
92
+ pfd.fd = this ->_sockfd ;
93
+ pfd.events = POLLIN;
94
+ pfd.revents = POLLIN;
95
+ if (::poll (&pfd, 1 , 60 * 1000 /* 1 minute means the connection has died */ ) <= 0 ) {
91
96
throw MB::ModbusException (MB::utils::ConnectionClosed);
92
97
}
93
98
@@ -108,8 +113,11 @@ std::vector<uint8_t> Connection::awaitRawMessage() {
108
113
}
109
114
110
115
MB::ModbusRequest Connection::awaitRequest () {
111
- pollfd _pfd = {.fd = _sockfd, .events = POLLIN, .revents = POLLIN};
112
- if (::poll (&_pfd, 1 , 60 * 1000 /* 1 minute means the connection has died */ ) <= 0 ) {
116
+ pollfd pfd;
117
+ pfd.fd = this ->_sockfd ;
118
+ pfd.events = POLLIN;
119
+ pfd.revents = POLLIN;
120
+ if (::poll (&pfd, 1 , 60 * 1000 /* 1 minute means the connection has died */ ) <= 0 ) {
113
121
throw MB::ModbusException (MB::utils::Timeout);
114
122
}
115
123
@@ -136,13 +144,17 @@ MB::ModbusRequest Connection::awaitRequest() {
136
144
}
137
145
138
146
MB::ModbusResponse Connection::awaitResponse () {
139
- pollfd _pfd = {.fd = _sockfd, .events = POLLIN, .revents = POLLIN};
140
- if (::poll (&_pfd, 1 , _timeout) <= 0 ) {
147
+ pollfd pfd;
148
+ pfd.fd = this ->_sockfd ;
149
+ pfd.events = POLLIN;
150
+ pfd.revents = POLLIN;
151
+
152
+ if (::poll (&pfd, 1 , this ->_timeout ) <= 0 ) {
141
153
throw MB::ModbusException (MB::utils::Timeout);
142
154
}
143
155
144
156
std::vector<uint8_t > r (1024 );
145
- auto size = ::recv (_sockfd, r.begin ().base (), r.size (), 0 );
157
+ auto size = ::recv (this -> _sockfd , r.begin ().base (), r.size (), 0 );
146
158
147
159
if (size == -1 )
148
160
throw MB::ModbusException (MB::utils::ProtocolError);
@@ -155,7 +167,7 @@ MB::ModbusResponse Connection::awaitResponse() {
155
167
156
168
const auto resultMessageID = *reinterpret_cast <uint16_t *>(&r[0 ]);
157
169
158
- if (resultMessageID != _messageID)
170
+ if (resultMessageID != this -> _messageID )
159
171
throw MB::ModbusException (MB::utils::InvalidMessageID);
160
172
161
173
r.erase (r.begin (), r.begin () + 6 );
@@ -180,10 +192,10 @@ Connection Connection::with(std::string addr, int port) {
180
192
if (sock == -1 )
181
193
throw std::runtime_error (" Cannot open socket, errno = " + std::to_string (errno));
182
194
183
- sockaddr_in server = {. sin_family = AF_INET,
184
- . sin_port = htons (port),
185
- . sin_addr = { inet_addr (addr. c_str ())},
186
- . sin_zero = {} };
195
+ sockaddr_in server;
196
+ server. sin_family = AF_INET;
197
+ server. sin_port = :: htons (port);
198
+ server. sin_addr = { inet_addr (addr. c_str ()) };
187
199
188
200
if (::connect (sock, reinterpret_cast <struct sockaddr *>(&server), sizeof (server)) < 0 )
189
201
throw std::runtime_error (" Cannot connect, errno = " + std::to_string (errno));
0 commit comments