Class: Wine::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/wine/connection.rb

Constant Summary collapse

DEFAULT_TIMEOUT =
5000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(socket) ⇒ Connection

Returns a new instance of Connection.



11
12
13
# File 'lib/wine/connection.rb', line 11

def initialize(socket)
  @socket = socket
end

Instance Attribute Details

#socketObject (readonly)

Returns the value of attribute socket.



9
10
11
# File 'lib/wine/connection.rb', line 9

def socket
  @socket
end

Instance Method Details

#closeObject



40
41
42
# File 'lib/wine/connection.rb', line 40

def close
  @socket.close
end

#recv(timeout = DEFAULT_TIMEOUT) ⇒ Object

Raises:



19
20
21
22
23
24
25
26
27
# File 'lib/wine/connection.rb', line 19

def recv(timeout = DEFAULT_TIMEOUT)
  readable = IO.select([ @socket ], nil, nil, timeout / 1000.0)
  return nil unless readable

  msg_type = @socket.recv(1, Socket::MSG_PEEK)
  raise ConnectionClosed unless msg_type.length == 1

  Message.read(@socket)
end

#recv_nonblockObject



29
30
31
32
33
34
35
36
37
38
# File 'lib/wine/connection.rb', line 29

def recv_nonblock
  begin
    msg_type = @socket.recv_nonblock(1, Socket::MSG_PEEK)
    raise ConnectionClosed unless msg_type.length == 1

    Message.read(@socket)
  rescue IO::WaitReadable
    nil
  end
end

#send(message) ⇒ Object



15
16
17
# File 'lib/wine/connection.rb', line 15

def send(message)
  message.write(@socket)
end