-
Notifications
You must be signed in to change notification settings - Fork 1
/
ex_sctp.ex
133 lines (107 loc) · 4.43 KB
/
ex_sctp.ex
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
defmodule ExSCTP do
@moduledoc """
Utilities for establishing SCTP connection and sending data over it.
"""
use Rustler, otp_app: :ex_sctp
@typedoc """
Reference to mutable SCTP state.
Operate on it only using functions from this module.
"""
@opaque t() :: reference()
@typedoc """
ID of the SCTP stream.
"""
@type stream_id() :: 0..255
@typedoc """
SCTP Payload Protocol Identifier.
"""
@type ppi() :: non_neg_integer()
@typedoc """
SCTP stream reliability types.
"""
@type reliability_type() :: :reliable | :rexmit | :timed
@typedoc """
Event obtained by calling `poll/1`.
Meaning of the events:
* `:none` - there's no more events to handle and no actions need to be taken
* `:connected` and `:disconnected` - STCP connection has been established or closed
* `{:stream_opened, id}` and `{:stream_closed, id}` - remote side either opened or closed a
stream with provided `id`. This message is not created for locally opened or closed streams
(by calling `open_stream/2` or `close_stream/2`)
* `{:data, id, ppi, data}` - data was received on stream with `id` and is marked with `ppi` PPI
* `{:transmit, [data]}` - data needs to be transmited to the other peer. You need to send the packets
using appropriate means, e.g. DTLS over ICE in case of WebRTC
* `{:timeout, val}` - informs that `handle_timeout/1` needs to be called after `val` milliseconds.
If `val` is `nil`, `handle_timeout/1` won't need to be called and awaiting timeouts can be canceled
"""
@type event() ::
:none
| :connected
| :disconnected
| {:stream_opened, stream_id()}
| {:stream_closed, stream_id()}
| {:data, stream_id(), ppi(), binary()}
| {:transmit, [binary()]}
| {:timeout, non_neg_integer() | nil}
@doc """
Initializes new SCTP connection state.
"""
@spec new() :: t()
def new(), do: error()
@doc """
Triggers connection establishment.
Calling this function will result in new events. See `poll/1` for more information.
Calling this function after `:connected` event was received will result in an error.
"""
@spec connect(t()) :: :ok
def connect(_resource), do: error()
@doc """
Opens a new stream with specified `id`.
Calling this function will result in new events. See `poll/1` for more information.
Stream IDs need to be unique. Calling this function with already existing stream `id`
will rsult in an error.
"""
@spec open_stream(t(), stream_id()) :: :ok | {:error, atom()}
def open_stream(_resource, _id), do: error()
@doc """
Closes the stream with `id`.
Calling this function will result in new events. See `poll/1` for more information.
"""
@spec close_stream(t(), stream_id()) :: :ok | {:error, atom()}
def close_stream(_resource, _id), do: error()
@doc """
Sets stream's order and reliability parameters.
"""
@spec configure_stream(t(), stream_id(), boolean(), reliability_type(), non_neg_integer()) ::
:ok | {:error, atom()}
def configure_stream(_resource, _id, _ordered?, _rel_param, _rel_value), do: error()
@doc """
Sends data over specified stream using provided `ppi`.
Calling this function will result in new events. See `poll/1` for more information.
"""
@spec send(t(), stream_id(), ppi(), binary()) :: :ok | {:error, atom()}
def send(_resource, _id, _ppi, _data), do: error()
@doc """
Handles data received from other peer.
Calling this function will result in new events. See `poll/1` for more information.
"""
@spec handle_data(t(), binary()) :: :ok
def handle_data(_resource, _data), do: error()
@doc """
Handles timeout.
After receiving `{:timeout, ms}` event from `poll/1`, you must call this function after `ms` milliseconds.
Calling this function will result in new events. See `poll/1` for more information.
"""
@spec handle_timeout(t()) :: :ok | {:error, atom()}
def handle_timeout(_resource), do: error()
@doc """
Produces event that needs to be handled by the library user.
New events can happen after IO is performed or timeout occurs. Related functions in
this module specify explicitly that they result in new events. If such a function is called,
the user needs to call `poll/1` repeatedly and handle the events until the `:none` event is received.
See `t:event/0` to learn how to handle events.
"""
@spec poll(t()) :: event()
def poll(_resource), do: error()
defp error, do: :erlang.nif_error(:nif_not_loaded)
end