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 |
| 12 | chans <- Some(Unix.open_connection (Unix.ADDR_INET ((Unix.inet_addr_of_string host),port))) |
| 13 | with _ -> |
| 14 | T.raise_TTransportExn |
| 15 | ("Could not connect to "^host^":"^(string_of_int port)) |
| 16 | T.NOT_OPEN |
| 17 | method close = match chans with None -> () | Some(inc,_) -> (Unix.shutdown_connection inc; chans <- None) |
| 18 | method read buf off len = match chans with |
| 19 | None -> T.raise_TTransportExn "Socket not open" T.NOT_OPEN |
| 20 | | Some(i,o) -> |
| 21 | try |
| 22 | really_input i buf off len; len |
| 23 | with _ -> T.raise_TTransportExn ("TSocket: Could not read "^(string_of_int len)^" from "^host^":"^(string_of_int port)) T.UNKNOWN |
| 24 | method write buf off len = match chans with |
| 25 | None -> T.raise_TTransportExn "Socket not open" T.NOT_OPEN |
| 26 | | Some(i,o) -> output o buf off len |
| 27 | method flush = match chans with |
| 28 | None -> T.raise_TTransportExn "Socket not open" T.NOT_OPEN |
| 29 | | Some(i,o) -> flush o |
| 30 | end |
| 31 | |
| 32 | |