Skip to content

Commit 1212f7c

Browse files
only use linux specific socet options when compiling for linux
1 parent 3809d64 commit 1212f7c

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/Modbus_TCP_Slave.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Slave::Slave(const std::string &ip, unsigned short port, modbus_mapping_t *mappi
5656
throw std::system_error(errno, std::generic_category(), "Failed to set socket option SO_KEEPALIVE");
5757
}
5858

59-
// this block makes this source file linux only :(
59+
#ifdef OS_LINUX
6060
if (tcp_timeout) {
6161
// set user timeout (~= timeout for tcp connection)
6262
unsigned user_timeout = static_cast<unsigned>(tcp_timeout) * 1000;
@@ -86,6 +86,9 @@ Slave::Slave(const std::string &ip, unsigned short port, modbus_mapping_t *mappi
8686
throw std::system_error(errno, std::generic_category(), "Failed to set socket option TCP_KEEPCNT");
8787
}
8888
}
89+
#else
90+
static_cast<void>(tcp_timeout);
91+
#endif
8992
}
9093

9194
Slave::~Slave() {

src/main.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,16 @@ int main(int argc, char **argv) {
9494
("response-timeout",
9595
"set the timeout interval in seconds used to wait for a response. "
9696
"When a byte timeout is set, if elapsed time for the first byte of response is longer than "
97-
"the given timeout, the a timeout is detected."
97+
"the given timeout, a timeout is detected."
9898
"When byte timeout is disabled, the full confirmation response must be received before "
9999
"expiration of the response timeout."
100100
"Fractional values are possible.",
101101
cxxopts::value<double>())
102+
#ifdef OS_LINUX
102103
("t,tcp-timeout",
103104
"tcp timeout in seconds. Set to 0 to use the system defaults (not recommended).",
104105
cxxopts::value<std::size_t>()->default_value("5"))
106+
#endif
105107
("h,help",
106108
"print usage")
107109
("version",
@@ -140,7 +142,11 @@ int main(int argc, char **argv) {
140142

141143
// print usage
142144
if (args.count("version")) {
143-
std::cout << PROJECT_NAME << ' ' << PROJECT_VERSION << std::endl;
145+
std::cout << PROJECT_NAME << ' ' << PROJECT_VERSION
146+
#ifndef OS_LINUX
147+
<< "-nonlinux"
148+
#endif
149+
<< std::endl;
144150
exit(EX_OK);
145151
}
146152

@@ -184,7 +190,11 @@ int main(int argc, char **argv) {
184190
slave = std::make_unique<Modbus::TCP::Slave>(args["ip"].as<std::string>(),
185191
args["port"].as<uint16_t>(),
186192
mapping.get_mapping(),
193+
#ifdef OS_LINUX
187194
args["tcp-timeout"].as<std::size_t>());
195+
#else
196+
0);
197+
#endif
188198
slave->set_debug(args.count("monitor"));
189199
} catch (const std::runtime_error &e) {
190200
std::cerr << e.what() << std::endl;
@@ -196,7 +206,7 @@ int main(int argc, char **argv) {
196206
try {
197207
if (args.count("response-timeout")) { slave->set_response_timeout(args["response-timeout"].as<double>()); }
198208

199-
if (args.count("byte-timeout")) { slave->set_response_timeout(args["byte-timeout"].as<double>()); }
209+
if (args.count("byte-timeout")) { slave->set_byte_timeout(args["byte-timeout"].as<double>()); }
200210
} catch (const std::runtime_error &e) {
201211
std::cerr << e.what() << std::endl;
202212
exit(EX_SOFTWARE);

0 commit comments

Comments
 (0)