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 |
| 10 | method opn = |
| 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))) |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 14 | with _ -> |
iproctor | e470aa3 | 2007-08-10 20:48:12 +0000 | [diff] [blame^] | 15 | 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) |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 23 | method read buf off len = match chans with |
iproctor | e470aa3 | 2007-08-10 20:48:12 +0000 | [diff] [blame^] | 24 | None -> raise (T.E (T.NOT_OPEN, "TSocket: Socket not open")) |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 25 | | Some(i,o) -> |
| 26 | try |
| 27 | really_input i buf off len; len |
iproctor | e470aa3 | 2007-08-10 20:48:12 +0000 | [diff] [blame^] | 28 | with _ -> raise (T.E (T.UNKNOWN, ("TSocket: Could not read "^(string_of_int len)^" from "^host^":"^(string_of_int port)))) |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 29 | method write buf off len = match chans with |
iproctor | e470aa3 | 2007-08-10 20:48:12 +0000 | [diff] [blame^] | 30 | None -> raise (T.E (T.NOT_OPEN, "TSocket: Socket not open")) |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 31 | | Some(i,o) -> output o buf off len |
| 32 | method flush = 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) -> flush o |
| 35 | end |
| 36 | |
| 37 | |