iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 1 | open Thrift |
| 2 | |
| 3 | module T = Transport |
| 4 | |
| 5 | class t host port= |
| 6 | object (self) |
| 7 | inherit T.t |
| 8 | val mutable chans = None |
| 9 | method isOpen = chans != None |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame^] | 10 | method opn = |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 11 | try |
iproctor | e470aa3 | 2007-08-10 20:48:12 +0000 | [diff] [blame] | 12 | 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 Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame^] | 14 | with |
iproctor | 94112d6 | 2007-08-17 21:34:15 +0000 | [diff] [blame] | 15 | 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)))) |
iproctor | e470aa3 | 2007-08-10 20:48:12 +0000 | [diff] [blame] | 17 | |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame^] | 18 | method close = |
| 19 | match chans with |
| 20 | None -> () |
| 21 | | Some(inc,out) -> (Unix.shutdown_connection inc; |
| 22 | close_in inc; |
iproctor | e470aa3 | 2007-08-10 20:48:12 +0000 | [diff] [blame] | 23 | chans <- None) |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 24 | method read buf off len = match chans with |
iproctor | e470aa3 | 2007-08-10 20:48:12 +0000 | [diff] [blame] | 25 | None -> raise (T.E (T.NOT_OPEN, "TSocket: Socket not open")) |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame^] | 26 | | Some(i,o) -> |
| 27 | try |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 28 | really_input i buf off len; len |
iproctor | 94112d6 | 2007-08-17 21:34:15 +0000 | [diff] [blame] | 29 | 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 Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame^] | 32 | method write buf off len = match chans with |
iproctor | e470aa3 | 2007-08-10 20:48:12 +0000 | [diff] [blame] | 33 | None -> raise (T.E (T.NOT_OPEN, "TSocket: Socket not open")) |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 34 | | Some(i,o) -> output o buf off len |
| 35 | method flush = match chans with |
iproctor | e470aa3 | 2007-08-10 20:48:12 +0000 | [diff] [blame] | 36 | None -> raise (T.E (T.NOT_OPEN, "TSocket: Socket not open")) |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 37 | | Some(i,o) -> flush o |
| 38 | end |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame^] | 39 | |
| 40 | |