-
Notifications
You must be signed in to change notification settings - Fork 54
/
echo.m
117 lines (104 loc) · 3.55 KB
/
echo.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
%---------------------------------------------------------------------------%
% 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: echo
% Main Author: Paul Bone <paul@bone.id.au>
%
% A simple echo server.
%
% Because the sockets library can't yet connect to the io module we cannot
% yet read or write to and from sockets.
%
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
:- module echo.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
:- implementation.
:- import_module list.
:- import_module maybe.
:- import_module require.
:- import_module stream.
:- import_module string.
:- import_module net.
:- import_module net.sockets.
:- import_module net.streams.
:- import_module net.types.
%---------------------------------------------------------------------------%
main(!IO) :-
socket(fam_inet, sock_stream, ResSocket, !IO),
(
ResSocket = ok(Socket),
bind(Socket, ipv4_sockaddr(in_addr_any, 6969), ResBind, !IO),
(
ResBind = ok,
listen(Socket, 5, ResListen, !IO),
(
ResListen = ok,
run(Socket, !IO)
;
ResListen = error(Error),
unexpected($file, $pred, "listen failed: " ++ Error)
)
;
ResBind = error(Error),
unexpected($file, $pred, "bind failed: " ++ Error)
),
close(Socket, ResClose, !IO),
(
ResClose = ok
;
ResClose = error(Error),
unexpected($file, $pred, "close failed: " ++ Error)
)
;
ResSocket = error(Error),
unexpected($file, $pred, "create socket failed: " ++ Error)
).
:- pred run(socket::in, io::di, io::uo) is det.
run(Socket, !IO) :-
accept(Socket, Result, !IO),
(
Result = ok(accept_result(NewSocket, Address)),
( if ipv4_sockaddr(InAddr, Port, Address) then
AddrStr = format("%s:%d", [s(to_string(InAddr)), i(Port)])
else
AddrStr = format("Unknown peer (family %s)",
[s(string(family(Address)))])
),
io.format("Connection from %s\n", [s(AddrStr)], !IO),
run_connection(stream(NewSocket), AddrStr, !IO),
close(NewSocket, CloseRes, !IO),
(
CloseRes = ok
;
CloseRes = error(Error),
unexpected($file, $pred, "create socket failed: " ++ Error)
)
;
Result = error(Error),
unexpected($file, $pred, "create socket failed: " ++ Error)
),
run(Socket, !IO).
:- pred run_connection(socket_stream::in, string::in, io::di, io::uo) is det.
run_connection(Stream, AddrStr, !IO) :-
get(Stream, MaybeByte, !IO),
(
MaybeByte = ok(Byte `with_type` streams.byte),
put(Stream, Byte, !IO),
run_connection(Stream, AddrStr, !IO)
;
MaybeByte = eof,
write_string(io.stderr_stream, "EOF", !IO)
;
MaybeByte = error(Error),
io.format(io.stderr_stream, "%s; %s\n",
[s(AddrStr), s(error_message(Error))], !IO)
).