-
Notifications
You must be signed in to change notification settings - Fork 118
/
stream.ex
280 lines (220 loc) · 7.35 KB
/
stream.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# Version 2, December 2004
#
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
#
# 0. You just DO WHAT THE FUCK YOU WANT TO.
defprotocol Socket.Stream.Protocol do
@doc """
Send data through the socket.
"""
@spec send(t, iodata) :: :ok | { :error, term }
def send(self, data)
@doc """
Send a file through the socket, using non-copying operations where available.
"""
@spec file(t, String.t) :: :ok | { :error, term }
@spec file(t, String.t, Keyword.t) :: :ok | { :error, term }
def file(self, path, options \\ [])
@doc """
Receive data from the socket compatible with the packet type.
"""
@spec recv(t) :: { :ok, term } | { :error, term }
def recv(self)
@doc """
Receive data from the socket with the given length or options.
"""
@spec recv(t, non_neg_integer | Keyword.t) :: { :ok, term } | { :error, term }
def recv(self, length_or_options)
@doc """
Receive data from the socket with the given length and options.
"""
@spec recv(t, non_neg_integer, Keyword.t) :: { :ok, term } | { :error, term }
def recv(self, length, options)
@doc """
Shutdown the socket in the given mode, either `:both`, `:read`, or `:write`.
"""
@spec shutdown(t, :both | :read | :write) :: :ok | { :error, term }
def shutdown(self, how \\ :both)
@doc """
Close the socket.
"""
@spec close(t) :: :ok | {:error, term}
def close(self)
end
defmodule Socket.Stream do
@type t :: Socket.Stream.Protocol.t
use Socket.Helpers
import Kernel, except: [send: 2]
defdelegate send(self, data), to: Socket.Stream.Protocol
defbang send(self, data), to: Socket.Stream.Protocol
defdelegate file(self, path), to: Socket.Stream.Protocol
defbang file(self, path), to: Socket.Stream.Protocol
defdelegate file(self, path, options), to: Socket.Stream.Protocol
defbang file(self, path, options), to: Socket.Stream.Protocol
defdelegate recv(self), to: Socket.Stream.Protocol
defbang recv(self), to: Socket.Stream.Protocol
defdelegate recv(self, length_or_options), to: Socket.Stream.Protocol
defbang recv(self, length_or_options), to: Socket.Stream.Protocol
defdelegate recv(self, length, options), to: Socket.Stream.Protocol
defbang recv(self, length, options), to: Socket.Stream.Protocol
defdelegate shutdown(self), to: Socket.Stream.Protocol
defbang shutdown(self), to: Socket.Stream.Protocol
defdelegate shutdown(self, how), to: Socket.Stream.Protocol
defbang shutdown(self, how), to: Socket.Stream.Protocol
defdelegate close(self), to: Socket.Stream.Protocol
defbang close(self), to: Socket.Stream.Protocol
@doc """
Read from the IO device and send to the socket following the given options.
## Options
- `:size` is the amount of bytes to read from the IO device, if omitted it
will read until EOF
- `:offset` is the amount of bytes to read from the IO device before
starting to send what's being read
- `:chunk_size` is the size of the chunks read from the IO device at a time
"""
@spec io(t, :io.device) :: :ok | { :error, term }
@spec io(t, :io.device, Keyword.t) :: :ok | { :error, term }
def io(self, io, options \\ []) do
if offset = options[:offset] do
case IO.binread(io, offset) do
:eof ->
:ok
{ :error, reason } ->
{ :error, reason }
_ ->
io(0, self, io, options[:size] || -1, options[:chunk_size] || 4096)
end
else
io(0, self, io, options[:size] || -1, options[:chunk_size] || 4096)
end
end
defp io(total, self, io, size, chunk_size) when size > 0 and total + chunk_size > size do
case IO.binread(io, size - total) do
:eof ->
:ok
{ :error, reason } ->
{ :error, reason }
data ->
self |> send(data)
end
end
defp io(total, self, io, size, chunk_size) do
case IO.binread(io, chunk_size) do
:eof ->
:ok
{ :error, reason } ->
{ :error, reason }
data ->
self |> send(data)
io(total + chunk_size, self, io, size, chunk_size)
end
end
defbang io(self, io)
defbang io(self, io, options)
end
defimpl Socket.Stream.Protocol, for: Port do
def send(self, data) do
:gen_tcp.send(self, data)
end
def file(self, path, options \\ []) do
cond do
options[:size] && options[:chunk_size] ->
:file.sendfile(path, self, options[:offset] || 0, options[:size], chunk_size: options[:chunk_size])
options[:size] ->
:file.sendfile(path, self, options[:offset] || 0, options[:size], [])
true ->
:file.sendfile(path, self)
end
end
def recv(self) do
recv(self, 0, [])
end
def recv(self, length) when length |> is_integer do
recv(self, length, [])
end
def recv(self, options) when options |> is_list do
recv(self, 0, options)
end
def recv(self, length, options) do
timeout = options[:timeout] || :infinity
case :gen_tcp.recv(self, length, timeout) do
{ :ok, _ } = ok ->
ok
{ :error, :closed } ->
{ :ok, nil }
{ :error, reason } ->
{ :error, reason }
end
end
def shutdown(self, how \\ :both) do
:gen_tcp.shutdown(self, case how do
:read -> :read
:write -> :write
:both -> :read_write
end)
end
def close(self) do
:gen_tcp.close(self)
end
end
defimpl Socket.Stream.Protocol, for: Tuple do
require Record
def send(self, data) when self |> Record.is_record(:sslsocket) do
:ssl.send(self, data)
end
def file(self, path, options \\ []) when self |> Record.is_record(:sslsocket) do
cond do
options[:size] && options[:chunk_size] ->
file(self, path, options[:offset] || 0, options[:size], options[:chunk_size])
options[:size] ->
file(self, path, options[:offset] || 0, options[:size], 4096)
true ->
file(self, path, 0, -1, 4096)
end
end
defp file(self, path, offset, -1, chunk_size) when path |> is_binary do
file(self, path, offset, File.stat!(path).size, chunk_size)
end
defp file(self, path, offset, size, chunk_size) when path |> is_binary do
case File.open!(path, [:read], &Socket.Stream.io(self, &1, offset: offset, size: size, chunk_size: chunk_size)) do
{ :ok, :ok } ->
:ok
{ :ok, { :error, reason } } ->
{ :error, reason }
{ :error, reason } ->
{ :error, reason }
end
end
def recv(self) when self |> Record.is_record(:sslsocket) do
recv(self, 0, [])
end
def recv(self, length) when self |> Record.is_record(:sslsocket) and length |> is_integer do
recv(self, length, [])
end
def recv(self, options) when self |> Record.is_record(:sslsocket) and options |> is_list do
recv(self, 0, options)
end
def recv(self, length, options) when self |> Record.is_record(:sslsocket) do
timeout = options[:timeout] || :infinity
case :ssl.recv(self, length, timeout) do
{ :ok, _ } = ok ->
ok
{ :error, :closed } ->
{ :ok, nil }
{ :error, reason } ->
{ :error, reason }
end
end
def shutdown(self, how \\ :both) do
:ssl.shutdown(self, case how do
:read -> :read
:write -> :write
:both -> :read_write
end)
end
def close(self) do
:ssl.close(self)
end
end