Skip to content

Instantly share code, notes, and snippets.

@havenwood
Created April 5, 2018 18:18
Show Gist options
  • Save havenwood/bdf4aaec6824840caf53226264636333 to your computer and use it in GitHub Desktop.
Save havenwood/bdf4aaec6824840caf53226264636333 to your computer and use it in GitHub Desktop.
require 'rmodbus'
module ModBus
class Client
def set_keepalive keepalive: true, keepidle: 50, keepintvl: 10, keepcnt: 5
@io.setsockopt Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, keepalive
@io.setsockopt Socket::SOL_TCP, Socket::TCP_KEEPIDLE, keepidle
@io.setsockopt Socket::SOL_TCP, Socket::TCP_KEEPINTVL, keepintvl
@io.setsockopt Socket::SOL_TCP, Socket::TCP_KEEPCNT, keepcnt
rescue
warn 'TCP keepalive parameters not set. Continuing.'
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment