Module: OSC::VNC::Listenable

Included in:
Session
Defined in:
lib/osc/vnc/listenable.rb

Overview

Mixin that adds the ability to create and read from a TCP server.

Instance Method Summary collapse

Instance Method Details

#create_listenTCPServer

Generate a TCP server that listens on a random port.

Returns:

  • (TCPServer)

    ruby TCPServer object listening on random port



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/osc/vnc/listenable.rb', line 11

def create_listen
  listen_host = Socket.gethostname
  listen_port = _get_port
  begin
    server = TCPServer.new(listen_host, listen_port)
  rescue Errno::EADDRINUSE
    listen_port = _get_port
    retry
  end
  server
end

#read_listen(args) ⇒ String

Read the data received by the TCP server.

Parameters:

  • args (Hash)

    the arguments to read data received by TCP server with

Options Hash (args):

  • :server (TCPServer)

    the TCP server that is currently listening

Returns:

  • (String)

    the contents of the data received by the server



28
29
30
31
32
# File 'lib/osc/vnc/listenable.rb', line 28

def read_listen(args)
  server = args[:server]
  client = server.accept  # wait for connection
  client.read             # read complete response
end