-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathstreams.m
228 lines (174 loc) · 6.03 KB
/
streams.m
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
%---------------------------------------------------------------------------%
% vim: ft=mercury ts=4 sw=4 et
%---------------------------------------------------------------------------%
% Copyright (C) 2014-2016, 2018 The Mercury Team.
% This file is distributed under the terms specified in COPYING.LIB.
%---------------------------------------------------------------------------%
%
% Module: net.streams.
% Main Author: Paul Bone
% Stability: low
%
% Provide a streams interface for sockets.
%
%---------------------------------------------------------------------------%
:- module net.streams.
:- interface.
:- import_module int.
:- import_module io.
:- import_module stream.
:- import_module string.
:- import_module net.sockets.
%---------------------------------------------------------------------------%
:- type socket_stream.
:- func stream(socket) = socket_stream.
:- func socket(socket_stream) = socket.
:- type byte
---> byte(int).
:- type error.
:- instance error(streams.error).
:- instance stream(socket_stream, io).
%---------------------------------------------------------------------------%
:- instance input(socket_stream, io).
% XXX: This does not buffer reads, it is slow.
%
:- instance reader(socket_stream, streams.byte, io, streams.error).
:- instance reader(socket_stream, line, io, streams.error).
%---------------------------------------------------------------------------%
:- instance output(socket_stream, io).
% XXX: This does not buffer writes, it is slow.
%
:- instance writer(socket_stream, streams.byte, io).
:- instance writer(socket_stream, string, io).
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
:- implementation.
:- import_module char.
:- import_module exception.
:- import_module bitmap.
:- import_module list.
:- import_module maybe.
:- import_module require.
:- type socket_stream
---> socket_stream(socket).
:- type error
---> error(string).
stream(Socket) = socket_stream(Socket).
socket(socket_stream(Socket)) = Socket.
%---------------------------------------------------------------------------%
:- instance error(streams.error) where [
error_message(error(Str)) = Str
].
:- instance stream(socket_stream, io) where [
pred(name/4) is stream_name
].
:- pred stream_name(socket_stream::in, name::out, io::di, io::uo) is det.
stream_name(socket_stream(_Socket), Name, !IO) :-
Name = "a socket".
%---------------------------------------------------------------------------%
:- instance input(socket_stream, io) where [].
:- instance reader(socket_stream, streams.byte, io, streams.error) where [
pred(get/4) is get_byte
].
:- pred get_byte(socket_stream::in, result(streams.byte, streams.error)::out,
io::di, io::uo) is det.
get_byte(socket_stream(Socket), Result, !IO) :-
read(Socket, 1, ReadResult, !IO),
(
ReadResult = ok(Bitmap),
( if num_bytes(Bitmap) = 1 then
Byte = Bitmap ^ byte(0),
Result = ok(byte(Byte))
else if num_bytes(Bitmap) = 0 then
Result = eof
else
unexpected($file, $pred,
"Read returned unexpected number of bytes")
)
;
ReadResult = eof,
Result = eof
;
ReadResult = error(String),
Result = error(error(String))
).
:- instance reader(socket_stream, line, io, streams.error) where [
pred(get/4) is get_line
].
:- pred get_line(socket_stream::in, result(line, streams.error)::out,
io::di, io::uo) is det.
get_line(Stream, Result, !IO) :-
get_chars_until_nl(Stream, [], Result0, !IO),
(
Result0 = ok(RevChars),
Result = ok(line(from_rev_char_list(RevChars)))
;
Result0 = eof,
Result = eof
;
Result0 = error(Error),
Result = error(Error)
).
:- pred get_chars_until_nl(Stream::in, list(char)::in,
result(list(char), Error)::out, State::di, State::uo) is det
<= reader(Stream, streams.byte, State, Error).
get_chars_until_nl(Stream, Chars0, Result, !IO) :-
get(Stream, ResByte, !IO),
(
ResByte = ok(byte(Byte)),
( char.from_int(Byte, Char) ->
( if
( Char = '\n'
; Char = '\r'
)
then
Result = ok(Chars0)
else
Chars1 = [Char | Chars0],
get_chars_until_nl(Stream, Chars1, Result, !IO)
)
;
unexpected($file, $pred, "Encoding error")
)
;
ResByte = eof,
Result = eof
;
ResByte = error(Error),
Result = error(Error)
).
%---------------------------------------------------------------------------%
:- instance output(socket_stream, io) where [
pred(flush/3) is flush_noop
].
:- pred flush_noop(socket_stream::in, io::di, io::uo) is det.
flush_noop(_, !IO).
% XXX: This does not buffer writes, it is slow.
%
:- instance writer(socket_stream, streams.byte, io) where [
pred(put/4) is put_byte
].
:- pred put_byte(socket_stream::in, streams.byte::in, io::di, io::uo)
is det.
put_byte(socket_stream(Socket), byte(Byte), !IO) :-
Bitmap = init(bits_per_byte) ^ byte(0) := Byte,
write(Socket, Bitmap, Result, !IO),
(
Result = ok
;
Result = error(Error),
throw(streams.error(Error))
).
:- instance writer(socket_stream, string, io) where [
pred(put/4) is put_string
].
:- pred put_string(Stream::in, string::in, State::di, State::uo) is det
<= writer(Stream, streams.byte, State).
put_string(Stream, String, !State) :-
foldl(put_char(Stream), String, !State).
:- pred put_char(Stream::in, char::in, State::di, State::uo) is det
<= writer(Stream, streams.byte, State).
put_char(Stream, Char, !State) :-
put(Stream, byte(to_int(Char)), !State).
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%