Class Thrift::Socket
In: lib/thrift/transport/socket.rb
lib/thrift/transport/socket.rb
Parent: BaseTransport

Methods

close   close   new   new   open   open   open?   open?   read   read   to_io   to_io   write   write  

Attributes

handle  [RW] 
handle  [RW] 
timeout  [RW] 
timeout  [RW] 

Public Class methods

[Source]

    # File 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

[Source]

    # File 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

Public Instance methods

[Source]

     # File lib/thrift/transport/socket.rb, line 130
130:     def close
131:       @handle.close unless @handle.nil? or @handle.closed?
132:       @handle = nil
133:     end

[Source]

     # File lib/thrift/transport/socket.rb, line 130
130:     def close
131:       @handle.close unless @handle.nil? or @handle.closed?
132:       @handle = nil
133:     end

[Source]

    # File lib/thrift/transport/socket.rb, line 35
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

[Source]

    # File lib/thrift/transport/socket.rb, line 35
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

[Source]

    # File lib/thrift/transport/socket.rb, line 58
58:     def open?
59:       !@handle.nil? and !@handle.closed?
60:     end

[Source]

    # File lib/thrift/transport/socket.rb, line 58
58:     def open?
59:       !@handle.nil? and !@handle.closed?
60:     end

[Source]

     # File lib/thrift/transport/socket.rb, line 94
 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

[Source]

     # File lib/thrift/transport/socket.rb, line 94
 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

[Source]

     # File lib/thrift/transport/socket.rb, line 135
135:     def to_io
136:       @handle
137:     end

[Source]

     # File lib/thrift/transport/socket.rb, line 135
135:     def to_io
136:       @handle
137:     end

[Source]

    # File lib/thrift/transport/socket.rb, line 62
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

[Source]

    # File lib/thrift/transport/socket.rb, line 62
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

[Validate]