blob: c74864a975aaf7d1edda85d686796431af8b0abd [file] [log] [blame]
iproctor9a41a0c2007-07-16 21:59:24 +00001open Thrift
2
3module T = Transport
4
5class t host port=
6object (self)
7 inherit T.t
8 val mutable chans = None
9 method isOpen = chans != None
10 method opn =
11 try
iproctore470aa32007-08-10 20:48:12 +000012 let addr = (let {Unix.h_addr_list=x} = Unix.gethostbyname host in x.(0)) in
13 chans <- Some(Unix.open_connection (Unix.ADDR_INET (addr,port)))
iproctor9a41a0c2007-07-16 21:59:24 +000014 with _ ->
iproctore470aa32007-08-10 20:48:12 +000015 raise (T.E (T.NOT_OPEN, ("TSocket: Could not connect to "^host^":"^(string_of_int port))))
16
17 method close =
18 match chans with
19 None -> ()
20 | Some(inc,out) -> (Unix.shutdown_connection inc;
21 close_in inc;
22 chans <- None)
iproctor9a41a0c2007-07-16 21:59:24 +000023 method read buf off len = match chans with
iproctore470aa32007-08-10 20:48:12 +000024 None -> raise (T.E (T.NOT_OPEN, "TSocket: Socket not open"))
iproctor9a41a0c2007-07-16 21:59:24 +000025 | Some(i,o) ->
26 try
27 really_input i buf off len; len
iproctore470aa32007-08-10 20:48:12 +000028 with _ -> raise (T.E (T.UNKNOWN, ("TSocket: Could not read "^(string_of_int len)^" from "^host^":"^(string_of_int port))))
iproctor9a41a0c2007-07-16 21:59:24 +000029 method write buf off len = match chans with
iproctore470aa32007-08-10 20:48:12 +000030 None -> raise (T.E (T.NOT_OPEN, "TSocket: Socket not open"))
iproctor9a41a0c2007-07-16 21:59:24 +000031 | Some(i,o) -> output o buf off len
32 method flush = match chans with
iproctore470aa32007-08-10 20:48:12 +000033 None -> raise (T.E (T.NOT_OPEN, "TSocket: Socket not open"))
iproctor9a41a0c2007-07-16 21:59:24 +000034 | Some(i,o) -> flush o
35end
36
37