blob: 2d824377cb8a2e6ab08004f0cd35ed98352d0aa5 [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
David Reiss0c90f6f2008-02-06 22:18:40 +000010 method opn =
iproctor9a41a0c2007-07-16 21:59:24 +000011 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)))
David Reiss0c90f6f2008-02-06 22:18:40 +000014 with
iproctor94112d62007-08-17 21:34:15 +000015 Unix.Unix_error (e,fn,_) -> raise (T.E (T.NOT_OPEN, ("TSocket: Could not connect to "^host^":"^(string_of_int port)^" because: "^fn^":"^(Unix.error_message e))))
16 | _ -> raise (T.E (T.NOT_OPEN, ("TSocket: Could not connect to "^host^":"^(string_of_int port))))
iproctore470aa32007-08-10 20:48:12 +000017
David Reiss0c90f6f2008-02-06 22:18:40 +000018 method close =
19 match chans with
20 None -> ()
21 | Some(inc,out) -> (Unix.shutdown_connection inc;
22 close_in inc;
iproctore470aa32007-08-10 20:48:12 +000023 chans <- None)
iproctor9a41a0c2007-07-16 21:59:24 +000024 method read buf off len = match chans with
iproctore470aa32007-08-10 20:48:12 +000025 None -> raise (T.E (T.NOT_OPEN, "TSocket: Socket not open"))
David Reiss0c90f6f2008-02-06 22:18:40 +000026 | Some(i,o) ->
27 try
iproctor9a41a0c2007-07-16 21:59:24 +000028 really_input i buf off len; len
iproctor94112d62007-08-17 21:34:15 +000029 with
30 Unix.Unix_error (e,fn,_) -> raise (T.E (T.UNKNOWN, ("TSocket: Could not read "^(string_of_int len)^" from "^host^":"^(string_of_int port)^" because: "^fn^":"^(Unix.error_message e))))
31 | _ -> raise (T.E (T.UNKNOWN, ("TSocket: Could not read "^(string_of_int len)^" from "^host^":"^(string_of_int port))))
David Reiss0c90f6f2008-02-06 22:18:40 +000032 method write buf off len = 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) -> output o buf off len
35 method flush = match chans with
iproctore470aa32007-08-10 20:48:12 +000036 None -> raise (T.E (T.NOT_OPEN, "TSocket: Socket not open"))
iproctor9a41a0c2007-07-16 21:59:24 +000037 | Some(i,o) -> flush o
38end
David Reiss0c90f6f2008-02-06 22:18:40 +000039
40