Class: Thrift::Socket

Inherits:
BaseTransport show all
Defined in:
lib/thrift/transport/socket.rb

Direct Known Subclasses

SSLSocket, UNIXSocket

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseTransport

#flush, #read_all, #read_byte, #read_into_buffer

Constructor Details

#initialize(host = 'localhost', port = 9090, timeout = nil) ⇒ Socket

Returns a new instance of Socket.



26
27
28
29
30
31
32
# File 'lib/thrift/transport/socket.rb', line 26

def initialize(host = 'localhost', port = 9090, timeout = nil)
  @host = host
  @port = port
  @timeout = timeout
  @desc = "#{host}:#{port}"
  @handle = nil
end

Instance Attribute Details

#handleObject

Returns the value of attribute handle.



34
35
36
# File 'lib/thrift/transport/socket.rb', line 34

def handle
  @handle
end

#timeoutObject

Returns the value of attribute timeout.



34
35
36
# File 'lib/thrift/transport/socket.rb', line 34

def timeout
  @timeout
end

Instance Method Details

#closeObject



110
111
112
113
# File 'lib/thrift/transport/socket.rb', line 110

def close
  close_socket(@handle)
  @handle = nil
end

#openObject



36
37
38
39
# File 'lib/thrift/transport/socket.rb', line 36

def open
  deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + @timeout unless @timeout.nil? || @timeout == 0
  @handle = connect_socket(deadline)
end

#open?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/thrift/transport/socket.rb', line 41

def open?
  !@handle.nil? && !@handle.closed?
end

#read(sz) ⇒ Object

Raises:



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/thrift/transport/socket.rb', line 77

def read(sz)
  raise TransportException.new(TransportException::NOT_OPEN, "closed stream") unless open?

  begin
    if @timeout.nil? || @timeout == 0
      data = @handle.readpartial(sz)
    else
      deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + @timeout

      data = loop do
        begin
          break @handle.read_nonblock(sz)
        rescue IO::WaitReadable
          wait_for(:read, deadline, sz)
        rescue IO::WaitWritable
          wait_for(:write, deadline, sz)
        end
      end
    end
  rescue TransportException => e
    # don't let this get caught by the StandardError handler
    raise e
  rescue StandardError => e
    close_socket(@handle)
    @handle = nil
    raise TransportException.new(TransportException::NOT_OPEN, e.message)
  end
  if (data.nil? || data.length == 0)
    raise TransportException.new(TransportException::UNKNOWN, "Socket: Could not read #{sz} bytes from #{@desc}")
  end
  data
end

#to_ioObject



115
116
117
# File 'lib/thrift/transport/socket.rb', line 115

def to_io
  @handle&.to_io || raise(IOError, 'closed stream')
end

#to_sObject



119
120
121
# File 'lib/thrift/transport/socket.rb', line 119

def to_s
  "socket(#{@host}:#{@port})"
end

#write(str) ⇒ Object

Raises:



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/thrift/transport/socket.rb', line 45

def write(str)
  raise TransportException.new(TransportException::NOT_OPEN, "closed stream") unless open?
  str = Bytes.force_binary_encoding(str)
  begin
    if @timeout.nil? || @timeout == 0
      @handle.write(str)
    else
      deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + @timeout
      len = 0

      while len < str.length
        begin
          len += @handle.write_nonblock(str[len..-1])
        rescue IO::WaitWritable
          wait_for(:write, deadline, str.length)
        rescue IO::WaitReadable
          wait_for(:read, deadline, str.length)
        end
      end

      len
    end
  rescue TransportException => e
    # pass this on
    raise e
  rescue StandardError => e
    close_socket(@handle)
    @handle = nil
    raise TransportException.new(TransportException::NOT_OPEN, e.message)
  end
end