Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion capnp-rpc-unix.opam
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ depends: [
"astring"
"fmt" {>= "0.8.7"}
"logs"
"extunix"
"base64" {>= "3.0.0"}
"dune" {>= "3.16"}
"ipaddr" {>= "5.3.0" }
Expand Down
5 changes: 4 additions & 1 deletion unix/dune
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
(library
(name capnp_rpc_unix)
(public_name capnp-rpc-unix)
(libraries eio.unix astring capnp-rpc capnp-rpc-net fmt logs mirage-crypto-rng.unix cmdliner cstruct extunix))
(libraries eio.unix astring capnp-rpc capnp-rpc-net fmt logs mirage-crypto-rng.unix cmdliner cstruct)
(foreign_stubs
(language c)
(names sockopt_stubs)))
9 changes: 1 addition & 8 deletions unix/keepalive.ml
Original file line number Diff line number Diff line change
@@ -1,8 +1 @@
let have_tcp_keepidle =
try ExtUnix.All.have_sockopt_int ExtUnix.All.TCP_KEEPIDLE
with ExtUnix.All.Not_available _ -> false

let try_set_idle socket i =
if have_tcp_keepidle then (
ExtUnix.All.setsockopt_int socket ExtUnix.All.TCP_KEEPIDLE i
)
external try_set_idle : Unix.file_descr -> int -> unit = "capnp_rpc_set_keepidle"
1 change: 1 addition & 0 deletions unix/keepalive.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
external try_set_idle : Unix.file_descr -> int -> unit = "capnp_rpc_set_keepidle"
40 changes: 40 additions & 0 deletions unix/sockopt_stubs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <caml/mlvalues.h>
#include <caml/memory.h>

#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#define KEEPIDLE_OPT (-1)
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>

/* macOS uses TCP_KEEPALIVE instead of TCP_KEEPIDLE */
#if defined(TCP_KEEPIDLE)
#define KEEPIDLE_OPT TCP_KEEPIDLE
#elif defined(TCP_KEEPALIVE)
#define KEEPIDLE_OPT TCP_KEEPALIVE
#else
#define KEEPIDLE_OPT (-1)
#endif
#endif

CAMLprim value capnp_rpc_set_keepidle(value v_fd, value v_time)
{
CAMLparam2(v_fd, v_time);
#if KEEPIDLE_OPT == (-1)
(void)v_fd;
(void)v_time;
#else
#ifdef _WIN32
SOCKET fd = Handle_val(v_fd);
#else
int fd = Int_val(v_fd);
#endif
int time = Int_val(v_time);
setsockopt(fd, IPPROTO_TCP, KEEPIDLE_OPT, &time, sizeof(time));
#endif
CAMLreturn(Val_unit);
}