-
Notifications
You must be signed in to change notification settings - Fork 6
/
tcp.gleam
85 lines (68 loc) · 2.5 KB
/
tcp.gleam
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import gleam/bit_builder.{type BitBuilder}
import gleam/dynamic.{type Dynamic}
import gleam/erlang/atom.{type Atom}
import gleam/erlang/process.{type Pid}
import gleam/list
import gleam/dict.{type Dict}
import glisten/socket.{type ListenSocket, type Socket, type SocketReason}
import glisten/socket/options.{type TcpOption}
@external(erlang, "glisten_tcp_ffi", "controlling_process")
pub fn controlling_process(socket: Socket, pid: Pid) -> Result(Nil, Atom)
@external(erlang, "gen_tcp", "listen")
fn do_listen_tcp(
port: Int,
options: List(TcpOption),
) -> Result(ListenSocket, SocketReason)
@external(erlang, "gen_tcp", "accept")
pub fn accept_timeout(
socket: ListenSocket,
timeout: Int,
) -> Result(Socket, SocketReason)
@external(erlang, "gen_tcp", "accept")
pub fn accept(socket: ListenSocket) -> Result(Socket, SocketReason)
@external(erlang, "gen_tcp", "recv")
pub fn receive_timeout(
socket: Socket,
length: Int,
timeout: Int,
) -> Result(BitArray, SocketReason)
@external(erlang, "gen_tcp", "recv")
pub fn receive(socket: Socket, length: Int) -> Result(BitArray, SocketReason)
@external(erlang, "glisten_tcp_ffi", "send")
pub fn send(socket: Socket, packet: BitBuilder) -> Result(Nil, SocketReason)
@external(erlang, "socket", "info")
pub fn socket_info(socket: Socket) -> Dict(a, b)
@external(erlang, "glisten_tcp_ffi", "close")
pub fn close(socket: a) -> Result(Nil, SocketReason)
@external(erlang, "glisten_tcp_ffi", "shutdown")
pub fn do_shutdown(socket: Socket, write: Atom) -> Result(Nil, SocketReason)
pub fn shutdown(socket: Socket) -> Result(Nil, SocketReason) {
let assert Ok(write) = atom.from_string("write")
do_shutdown(socket, write)
}
@external(erlang, "glisten_tcp_ffi", "set_opts")
fn do_set_opts(socket: Socket, opts: List(Dynamic)) -> Result(Nil, Nil)
/// Update the optons for a socket (mutates the socket)
pub fn set_opts(socket: Socket, opts: List(TcpOption)) -> Result(Nil, Nil) {
opts
|> options.to_dict
|> dict.to_list
|> list.map(dynamic.from)
|> do_set_opts(socket, _)
}
/// Start listening over TCP on a port with the given options
pub fn listen(
port: Int,
opts: List(TcpOption),
) -> Result(ListenSocket, SocketReason) {
opts
|> options.merge_with_defaults
|> do_listen_tcp(port, _)
}
pub fn handshake(socket: Socket) -> Result(Socket, Nil) {
Ok(socket)
}
@external(erlang, "tcp", "negotiated_protocol")
pub fn negotiated_protocol(socket: Socket) -> a
@external(erlang, "inet", "peername")
pub fn peername(socket: Socket) -> Result(#(#(Int, Int, Int, Int), Int), Nil)