-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathecho.ml
129 lines (112 loc) · 4.13 KB
/
echo.ml
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
(*---------------------------------------------------------------------------
Copyright (c) 2016 KC Sivaramakrishnan. All rights reserved.
Distributed under the ISC license, see terms at the end of the file.
%%NAME%% %%VERSION%%
---------------------------------------------------------------------------*)
(* A simple echo server.
*
* The server listens on localhost port 9301. It accepts multiple clients and
* echoes back to the client any data sent to the server. This server is a
* direct-style reimplementation of the echo server found at [1], which
* illustrates the same server written in CPS style.
*
* Compiling
* ---------
*
* make
*
* Running
* -------
* The echo server can be tested with a telnet client by starting the server and
* on the same machine, running:
*
* telnet localhost 9301
*
* -----------------------
* [1] http://www.mega-nerd.com/erikd/Blog/CodeHacking/Ocaml/ocaml_select.html
* [2] https://github.com/ocamllabs/opam-repo-dev
*)
open Printf
let send sock str =
let len = Bytes.length str in
let total = ref 0 in
while !total < len do
let write_count = Aeio.send sock str !total (len - !total) [] in
total := write_count + !total
done;
!total
let recv sock maxlen =
let str = Bytes.create maxlen in
let recvlen = Aeio.recv sock str 0 maxlen [] in
Bytes.sub str 0 recvlen
let close sock =
try Aeio.shutdown sock Unix.SHUTDOWN_ALL
with _ -> () ;
Aeio.close sock
let string_of_sockaddr = function
| Unix.ADDR_UNIX s -> s
| Unix.ADDR_INET (inet,port) ->
(Unix.string_of_inet_addr inet) ^ ":" ^ (string_of_int port)
(* Repeat what the client says until the client goes away. *)
let echo_server sock addr =
let rec loop () =
let data = recv sock 1024 in
if Bytes.length data > 0 then
(ignore (send sock data);
loop ())
else
let cn = string_of_sockaddr addr in
(printf "echo_server : client (%s) disconnected.\n%!" cn;
close sock)
in
try loop () with _ -> close sock
let server () =
(* Server listens on localhost at 9301 *)
let addr, port = Unix.inet_addr_loopback, 9301 in
printf "Echo server listening on 127.0.0.1:%d\n%!" port;
let saddr = Unix.ADDR_INET (addr, port) in
let ssock = Aeio.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
let ssock_unix = Aeio.get_unix_fd ssock in
(* configure socket *)
Unix.setsockopt ssock_unix Unix.SO_REUSEADDR true;
Unix.bind ssock_unix saddr;
Unix.listen ssock_unix 20;
Aeio.set_nonblock ssock;
try
(* Wait for clients, and fork off echo servers. *)
while true do
let client_sock, client_addr = Aeio.accept ssock in
let cn = string_of_sockaddr client_addr in
printf "server : client (%s) connected.\n%!" cn;
Aeio.set_nonblock client_sock;
ignore @@ Aeio.async (echo_server client_sock) client_addr
done
with
| _ -> close ssock
(* Main *)
let print_usage_and_exit () =
print_endline @@ "Usage: " ^ Sys.argv.(0) ^ " [select|libev]";
exit(0)
let () =
if Array.length Sys.argv < 2 then
print_usage_and_exit ()
else
match Sys.argv.(1) with
| "select" -> Lwt_engine.set (new Lwt_engine.select)
| "libev" -> Lwt_engine.set (new Lwt_engine.libev ())
| _ -> print_usage_and_exit ()
let () =
Aeio.run server
(*---------------------------------------------------------------------------
Copyright (c) 2016 KC Sivaramakrishnan
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
---------------------------------------------------------------------------*)