blob: 109e11c5650b38095923c535796a4c6f01902420 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001(*
2 Licensed to the Apache Software Foundation (ASF) under one
3 or more contributor license agreements. See the NOTICE file
4 distributed with this work for additional information
5 regarding copyright ownership. The ASF licenses this file
6 to you under the Apache License, Version 2.0 (the
7 "License"); you may not use this file except in compliance
8 with the License. You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing,
13 software distributed under the License is distributed on an
14 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 KIND, either express or implied. See the License for the
16 specific language governing permissions and limitations
17 under the License.
18*)
19
iproctor9a41a0c2007-07-16 21:59:24 +000020open Thrift
21
22module T = Transport
23
24class t host port=
25object (self)
26 inherit T.t
27 val mutable chans = None
28 method isOpen = chans != None
David Reiss0c90f6f2008-02-06 22:18:40 +000029 method opn =
iproctor9a41a0c2007-07-16 21:59:24 +000030 try
iproctore470aa32007-08-10 20:48:12 +000031 let addr = (let {Unix.h_addr_list=x} = Unix.gethostbyname host in x.(0)) in
32 chans <- Some(Unix.open_connection (Unix.ADDR_INET (addr,port)))
David Reiss0c90f6f2008-02-06 22:18:40 +000033 with
iproctor94112d62007-08-17 21:34:15 +000034 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))))
35 | _ -> raise (T.E (T.NOT_OPEN, ("TSocket: Could not connect to "^host^":"^(string_of_int port))))
iproctore470aa32007-08-10 20:48:12 +000036
David Reiss0c90f6f2008-02-06 22:18:40 +000037 method close =
38 match chans with
39 None -> ()
40 | Some(inc,out) -> (Unix.shutdown_connection inc;
41 close_in inc;
iproctore470aa32007-08-10 20:48:12 +000042 chans <- None)
iproctor9a41a0c2007-07-16 21:59:24 +000043 method read buf off len = match chans with
iproctore470aa32007-08-10 20:48:12 +000044 None -> raise (T.E (T.NOT_OPEN, "TSocket: Socket not open"))
David Reiss0c90f6f2008-02-06 22:18:40 +000045 | Some(i,o) ->
46 try
iproctor9a41a0c2007-07-16 21:59:24 +000047 really_input i buf off len; len
iproctor94112d62007-08-17 21:34:15 +000048 with
49 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))))
50 | _ -> 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 +000051 method write buf off len = match chans with
iproctore470aa32007-08-10 20:48:12 +000052 None -> raise (T.E (T.NOT_OPEN, "TSocket: Socket not open"))
iproctor9a41a0c2007-07-16 21:59:24 +000053 | Some(i,o) -> output o buf off len
54 method flush = match chans with
iproctore470aa32007-08-10 20:48:12 +000055 None -> raise (T.E (T.NOT_OPEN, "TSocket: Socket not open"))
iproctor9a41a0c2007-07-16 21:59:24 +000056 | Some(i,o) -> flush o
57end
David Reiss0c90f6f2008-02-06 22:18:40 +000058
59