| Class | Thrift::Socket |
| In: |
ruby/lib/thrift/transport/socket.rb
|
| Parent: | BaseTransport |
| handle | [RW] | |
| timeout | [RW] |
# File ruby/lib/thrift/transport/socket.rb, line 25
25: def initialize(host='localhost', port=9090, timeout=nil)
26: @host = host
27: @port = port
28: @timeout = timeout
29: @desc = "#{host}:#{port}"
30: @handle = nil
31: end
# File ruby/lib/thrift/transport/socket.rb, line 127
127: def close
128: @handle.close unless @handle.nil? or @handle.closed?
129: @handle = nil
130: end
# File ruby/lib/thrift/transport/socket.rb, line 35
35: def open
36: begin
37: addrinfo = ::Socket::getaddrinfo(@host, @port).first
38: @handle = ::Socket.new(addrinfo[4], ::Socket::SOCK_STREAM, 0)
39: sockaddr = ::Socket.sockaddr_in(addrinfo[1], addrinfo[3])
40: begin
41: @handle.connect_nonblock(sockaddr)
42: rescue Errno::EINPROGRESS
43: unless IO.select(nil, [ @handle ], nil, @timeout)
44: raise TransportException.new(TransportException::NOT_OPEN, "Connection timeout to #{@desc}")
45: end
46: begin
47: @handle.connect_nonblock(sockaddr)
48: rescue Errno::EISCONN
49: end
50: end
51: @handle
52: rescue StandardError => e
53: raise TransportException.new(TransportException::NOT_OPEN, "Could not connect to #{@desc}: #{e}")
54: end
55: end
# File ruby/lib/thrift/transport/socket.rb, line 57
57: def open?
58: !@handle.nil? and !@handle.closed?
59: end
# File ruby/lib/thrift/transport/socket.rb, line 92
92: def read(sz)
93: raise IOError, "closed stream" unless open?
94:
95: begin
96: if @timeout.nil? or @timeout == 0
97: data = @handle.readpartial(sz)
98: else
99: # it's possible to interrupt select for something other than the timeout
100: # so we need to ensure we've waited long enough
101: start = Time.now
102: rd = nil # scoping
103: loop do
104: rd, = IO.select([@handle], nil, nil, @timeout)
105: break if (rd and not rd.empty?) or Time.now - start >= @timeout
106: end
107: if rd.nil? or rd.empty?
108: raise TransportException.new(TransportException::TIMED_OUT, "Socket: Timed out reading #{sz} bytes from #{@desc}")
109: else
110: data = @handle.readpartial(sz)
111: end
112: end
113: rescue TransportException => e
114: # don't let this get caught by the StandardError handler
115: raise e
116: rescue StandardError => e
117: @handle.close unless @handle.closed?
118: @handle = nil
119: raise TransportException.new(TransportException::NOT_OPEN, e.message)
120: end
121: if (data.nil? or data.length == 0)
122: raise TransportException.new(TransportException::UNKNOWN, "Socket: Could not read #{sz} bytes from #{@desc}")
123: end
124: data
125: end
# File ruby/lib/thrift/transport/socket.rb, line 61
61: def write(str)
62: raise IOError, "closed stream" unless open?
63: begin
64: if @timeout.nil? or @timeout == 0
65: @handle.write(str)
66: else
67: len = 0
68: start = Time.now
69: while Time.now - start < @timeout
70: rd, wr, = IO.select(nil, [@handle], nil, @timeout)
71: if wr and not wr.empty?
72: len += @handle.write_nonblock(str[len..-1])
73: break if len >= str.length
74: end
75: end
76: if len < str.length
77: raise TransportException.new(TransportException::TIMED_OUT, "Socket: Timed out writing #{str.length} bytes to #{@desc}")
78: else
79: len
80: end
81: end
82: rescue TransportException => e
83: # pass this on
84: raise e
85: rescue StandardError => e
86: @handle.close
87: @handle = nil
88: raise TransportException.new(TransportException::NOT_OPEN, e.message)
89: end
90: end