class Thrift::Socket
Attributes
handle[RW]
timeout[RW]
Public Class Methods
new(host='localhost', port=9090, timeout=nil)
click to toggle source
# File lib/thrift/transport/socket.rb 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
Public Instance Methods
close()
click to toggle source
# File lib/thrift/transport/socket.rb 128 def close 129 @handle.close unless @handle.nil? or @handle.closed? 130 @handle = nil 131 end
open()
click to toggle source
# File lib/thrift/transport/socket.rb 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
open?()
click to toggle source
# File lib/thrift/transport/socket.rb 57 def open? 58 !@handle.nil? and !@handle.closed? 59 end
read(sz)
click to toggle source
# File lib/thrift/transport/socket.rb 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, but not too long 101 start = Time.now 102 timespent = 0 103 rd = loop do 104 rd, = IO.select([@handle], nil, nil, @timeout - timespent) 105 timespent = Time.now - start 106 break rd if (rd and not rd.empty?) or timespent >= @timeout 107 end 108 if rd.nil? or rd.empty? 109 raise TransportException.new(TransportException::TIMED_OUT, "Socket: Timed out reading #{sz} bytes from #{@desc}") 110 else 111 data = @handle.readpartial(sz) 112 end 113 end 114 rescue TransportException => e 115 # don't let this get caught by the StandardError handler 116 raise e 117 rescue StandardError => e 118 @handle.close unless @handle.closed? 119 @handle = nil 120 raise TransportException.new(TransportException::NOT_OPEN, e.message) 121 end 122 if (data.nil? or data.length == 0) 123 raise TransportException.new(TransportException::UNKNOWN, "Socket: Could not read #{sz} bytes from #{@desc}") 124 end 125 data 126 end
to_io()
click to toggle source
# File lib/thrift/transport/socket.rb 133 def to_io 134 @handle 135 end
write(str)
click to toggle source
# File lib/thrift/transport/socket.rb 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