blob: 109e11c5650b38095923c535796a4c6f01902420 [file] [log] [blame]
Gavin McDonald0b75e1a2010-10-28 02:12:01 +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
20open 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
29 method opn =
30 try
31 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)))
33 with
34 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))))
36
37 method close =
38 match chans with
39 None -> ()
40 | Some(inc,out) -> (Unix.shutdown_connection inc;
41 close_in inc;
42 chans <- None)
43 method read buf off len = match chans with
44 None -> raise (T.E (T.NOT_OPEN, "TSocket: Socket not open"))
45 | Some(i,o) ->
46 try
47 really_input i buf off len; len
48 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))))
51 method write buf off len = match chans with
52 None -> raise (T.E (T.NOT_OPEN, "TSocket: Socket not open"))
53 | Some(i,o) -> output o buf off len
54 method flush = match chans with
55 None -> raise (T.E (T.NOT_OPEN, "TSocket: Socket not open"))
56 | Some(i,o) -> flush o
57end
58
59