Skip to content

Commit

Permalink
add TCP_DEFER_ACCEPT and TCP_NO_DELAY socket options
Browse files Browse the repository at this point in the history
  • Loading branch information
avsm committed Jul 22, 2023
1 parent 92a06fe commit 804a18b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
8 changes: 8 additions & 0 deletions lib_eio/unix/net.ml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ type socket_int_option =
| EIO_TCP_KEEPCNT
| EIO_TCP_KEEPIDLE
| EIO_TCP_KEEPINTVL
| EIO_TCP_DEFER_ACCEPT
| EIO_TCP_NODELAY

external setsockopt_int : Unix.file_descr -> socket_int_option -> int -> unit =
"eio_unix_setsockopt_int"
Expand All @@ -64,10 +66,12 @@ module Sockopt = struct
| SO_KEEPALIVE : bool Eio.Net.Sockopt.t
| SO_REUSEADDR : bool Eio.Net.Sockopt.t
| SO_REUSEPORT : bool Eio.Net.Sockopt.t
| TCP_NODELAY : bool Eio.Net.Sockopt.t
| TCP_CORK : int Eio.Net.Sockopt.t
| TCP_KEEPCNT : int Eio.Net.Sockopt.t
| TCP_KEEPIDLE : int Eio.Net.Sockopt.t
| TCP_KEEPINTVL : int Eio.Net.Sockopt.t
| TCP_DEFER_ACCEPT : int Eio.Net.Sockopt.t

let set : type a . Fd.t -> a Eio.Net.Sockopt.t -> a -> unit = fun sock k v ->
Fd.use_exn "Sockopt.set" sock @@ fun fd ->
Expand All @@ -76,6 +80,8 @@ module Sockopt = struct
| TCP_KEEPCNT -> setsockopt_int fd EIO_TCP_KEEPCNT v
| TCP_KEEPIDLE -> setsockopt_int fd EIO_TCP_KEEPIDLE v
| TCP_KEEPINTVL -> setsockopt_int fd EIO_TCP_KEEPINTVL v
| TCP_DEFER_ACCEPT -> setsockopt_int fd EIO_TCP_DEFER_ACCEPT v
| TCP_NODELAY -> setsockopt_int fd EIO_TCP_DEFER_ACCEPT (if v then 1 else 0)
| SO_KEEPALIVE -> Unix.(setsockopt fd SO_KEEPALIVE v)
| SO_REUSEADDR -> Unix.(setsockopt fd SO_REUSEADDR v)
| SO_REUSEPORT -> Unix.(setsockopt fd SO_REUSEPORT v)
Expand All @@ -87,6 +93,8 @@ module Sockopt = struct
| TCP_KEEPCNT -> getsockopt_int fd EIO_TCP_KEEPCNT
| TCP_KEEPIDLE -> getsockopt_int fd EIO_TCP_KEEPIDLE
| TCP_KEEPINTVL -> getsockopt_int fd EIO_TCP_KEEPINTVL
| TCP_DEFER_ACCEPT -> getsockopt_int fd EIO_TCP_DEFER_ACCEPT
| TCP_NODELAY -> getsockopt_int fd EIO_TCP_NODELAY = 1
| SO_KEEPALIVE -> Unix.(getsockopt fd SO_KEEPALIVE)
| SO_REUSEADDR -> Unix.(getsockopt fd SO_REUSEADDR)
| SO_REUSEPORT -> Unix.(getsockopt fd SO_REUSEPORT)
Expand Down
2 changes: 2 additions & 0 deletions lib_eio/unix/net.mli
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ module Sockopt : sig
| TCP_KEEPCNT : int Eio.Net.Sockopt.t
| TCP_KEEPIDLE : int Eio.Net.Sockopt.t
| TCP_KEEPINTVL : int Eio.Net.Sockopt.t
| TCP_DEFER_ACCEPT : int Eio.Net.Sockopt.t
| TCP_NODELAY : bool Eio.Net.Sockopt.t

val set : Fd.t -> 'a Eio.Net.Sockopt.t -> 'a -> unit
(** [set fd opt v] sets the [opt] option to value [v] on socket [fd]. *)
Expand Down
12 changes: 11 additions & 1 deletion lib_eio/unix/sockopt.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@
#define TCP_KEEPINTVL (-1)
#endif

#ifndef TCP_DEFER_ACCEPT
#define TCP_DEFER_ACCEPT (-1)
#endif

#ifndef TCP_NODELAY
#define TCP_NODELAY (-1)
#endif

struct socket_option {
int level;
int option;
Expand All @@ -40,7 +48,9 @@ static struct socket_option sockopt_int[] = {
{ IPPROTO_TCP, TCP_CORK },
{ IPPROTO_TCP, TCP_KEEPCNT },
{ IPPROTO_TCP, TCP_KEEPIDLE },
{ IPPROTO_TCP, TCP_KEEPINTVL }
{ IPPROTO_TCP, TCP_KEEPINTVL },
{ IPPROTO_TCP, TCP_DEFER_ACCEPT },
{ IPPROTO_TCP, TCP_NODELAY },
};

CAMLprim value eio_unix_getsockopt_int(value vsocket, value voption)
Expand Down

0 comments on commit 804a18b

Please sign in to comment.