-
Notifications
You must be signed in to change notification settings - Fork 2
/
Connection_lwt.ml
191 lines (172 loc) · 5.45 KB
/
Connection_lwt.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
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
(*
* Copyright 2017-2018 Cedric LE MOIGNE, cedlemo@gmx.com
* This file is part of OCaml-libmpdclient.
*
* OCaml-libmpdclient is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* OCaml-libmpdclient is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OCaml-libmpdclient. If not, see <http://www.gnu.org/licenses/>.
*)
open Lwt
type t =
{ hostname : string;
port : int;
ip : Unix.inet_addr;
socket : Lwt_unix.file_descr;
mutable buffer : string;
}
exception Lwt_unix_exn of string
let fail_with_message m =
Lwt.fail (Lwt_unix_exn m)
let gethostbyname name =
Lwt.catch
(fun () ->
Lwt_unix.gethostbyname name
>>= fun entry ->
let addrs = Array.to_list entry.Unix.h_addr_list in
Lwt.return addrs
)
(function
| Not_found -> let m = Printf.sprintf "Host not found, \
Lwt_unix.gethostname: no host found\
for %s. Exiting..." name in
fail_with_message m
| e -> Lwt.fail e
)
let open_socket addr port =
Lwt.catch
(fun () ->
let sock = Lwt_unix.socket Lwt_unix.PF_INET Lwt_unix.SOCK_STREAM 0 in
let sockaddr = Lwt_unix.ADDR_INET (addr, port) in
Lwt_unix.connect sock sockaddr
>>= fun () ->
Lwt.return sock
)
(function
| Unix.Unix_error (error, fn_name, param_name) ->
let m = Printf.sprintf "%s, Unix.%s (%s): unable to open socket.
Exiting..."
(Unix.error_message error)
fn_name
param_name in
fail_with_message m
| e -> Lwt.fail e
)
let initialize hostname port =
gethostbyname hostname
>>= fun addrs ->
let addr = List.hd addrs in
open_socket addr port
>>= fun socket ->
let conn = { hostname = hostname;
port = port;
ip = addr;
socket = socket;
buffer = "";
}
in Lwt.return conn
let hostname connection =
Lwt.return connection.hostname
let port connection =
Lwt.return connection.port
let buffer connection =
Lwt.return connection.buffer
let write conn str =
Lwt.catch
(fun () ->
let {socket = socket; _} = conn in
let b = Bytes.of_string str in
let len = Bytes.length b in
Lwt_unix.send socket b 0 len []
)
(function
| Unix.Unix_error (error, fn_name, param_name) ->
let m = Printf.sprintf "%s, Unix.%s (%s): unable to write to socket \
connected to %s:%s. Exiting..."
(Unix.error_message error)
fn_name
param_name
conn.hostname
(string_of_int conn.port) in
fail_with_message m
| e -> Lwt.fail e
)
let recvbytes conn =
Lwt.catch
(fun () ->
let {socket = socket; _} = conn in
let maxlen = 1024 in
let buf = Bytes.create maxlen in
Lwt_unix.recv socket buf 0 maxlen []
>>= fun recvlen ->
Lwt.return Bytes.(sub buf 0 recvlen)
)
(function
| Unix.Unix_error (error, fn_name, param_name) ->
let m = Printf.sprintf "%s, Unix.%s (%s): unable to read from socket \
connected to %s:%s. Exiting..."
(Unix.error_message error)
fn_name
param_name
conn.hostname
(string_of_int conn.port) in
fail_with_message m
| e -> Lwt.fail e
)
let read connection fn_to_check_for_pattern =
let open Protocol in
let rec _read connection =
match fn_to_check_for_pattern connection.buffer with
| Complete (response, u) -> (
let resp_len = (String.length response) + u in
let buff_len = String.length connection.buffer in
let start = resp_len in
let length = buff_len - resp_len in
let () = connection.buffer <- String.sub connection.buffer start length in
Lwt.return response
)
| Incomplete ->(
recvbytes connection
>>= fun b ->
let buf = connection.buffer ^ (Bytes.to_string b) in
let () = connection.buffer <- buf in
_read connection
)
in
_read connection
let read_idle_events connection =
read connection Protocol.full_mpd_idle_event
let read_mpd_banner connection =
read connection Protocol.full_mpd_banner
let read_request_response connection =
read connection Protocol.request_response
let read_command_response connection =
read connection Protocol.command_response
let close conn =
Lwt.catch
(fun () ->
Loggin.debuf "close"
>>= fun () ->
let {socket = socket; _} = conn in
Lwt_unix.close socket
)
(function
| Unix.Unix_error (error, fn_name, param_name) ->
let m = Printf.sprintf "%s, Unix.%s (%s): unable to read from socket \
connected to %s:%s. Exiting..."
(Unix.error_message error)
fn_name
param_name
conn.hostname
(string_of_int conn.port) in
fail_with_message m
| e -> Lwt.fail e
)