Files
mercury/extras/net/echo.m
Zoltan Somogyi 9b6390b17e Bring the programming style of extras up to date.
extras/curs/curs.m:
extras/curs/curs.panel.m:
    Make panel a separate submodule of curs, not a nested submodule.

extras/base64/base64.m:
extras/curses/mcurses.basics.m:
extras/curses/mcurses.m:
extras/curses/mcurses.misc.m:
extras/curses/mcurses.user.m:
extras/gator/evolve.m:
extras/gator/genotype.m:
extras/gator/phenotype.m:
extras/gator/tausworthe3.m:
extras/monte/dots.m:
extras/monte/geom.m:
extras/monte/hg.m:
extras/monte/monte.m:
extras/monte/rnd.m:
extras/moose/grammar.m:
extras/moose/moose.m:
extras/mopenssl/mopenssl.m:
extras/net/echo.m:
extras/net/errno.m:
extras/net/getaddrinfo.m:
extras/net/net.m:
extras/net/netdb.m:
extras/net/sockets.m:
extras/net/streams.m:
extras/net/tcp.m:
extras/net/test_lookups.m:
extras/net/types.m:
extras/odbc/odbc.m:
extras/odbc/odbc_test.m:
extras/references/README:
extras/references/reference.m:
extras/references/scoped_update.m:
extras/solver_types/library/any.m:
extras/solver_types/library/any_array.m:
extras/solver_types/library/any_assoc_list.m:
extras/solver_types/library/any_list.m:
extras/solver_types/library/any_map.m:
extras/solver_types/library/any_tree234.m:
extras/solver_types/library/any_util.m:
extras/trail/trail.m:
extras/trailed_update/samples/interpreter.m:
extras/trailed_update/samples/vqueens.m:
extras/trailed_update/tests/var_test.m:
extras/trailed_update/tr_array.m:
extras/trailed_update/tr_store.m:
extras/trailed_update/trailed_update.m:
extras/trailed_update/unsafe.m:
extras/trailed_update/var.m:
    Bring programming style up to date.
2023-03-30 21:48:10 +11:00

118 lines
3.5 KiB
Mathematica

%---------------------------------------------------------------------------%
% 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)
).