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