Class: SocketChannel

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSocketChannel

Returns a new instance of SocketChannel.



33
34
35
# File 'lib/socket_channel.rb', line 33

def initialize
  @connected = false
end

Instance Attribute Details

#socketObject (readonly)

Returns the value of attribute socket.



14
15
16
# File 'lib/socket_channel.rb', line 14

def socket
  @socket
end

Class Method Details

.openObject



16
17
18
# File 'lib/socket_channel.rb', line 16

def self.open
  SocketChannel.new
end

Instance Method Details

#closeObject



20
21
22
# File 'lib/socket_channel.rb', line 20

def close
  @socket.close
end

#connect(*args) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/socket_channel.rb', line 24

def connect(*args)
  timeout(1) do
    @socket = TCPSocket.new args[0][0][3], args[0][0][1]
    @connected = true
  end

  self
end

#connected?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/socket_channel.rb', line 37

def connected?
  @connected
end

#read(destination_buffer) ⇒ Object

Raises:

  • (ArgumentError)


41
42
43
44
45
46
47
# File 'lib/socket_channel.rb', line 41

def read(destination_buffer)
  raise ArgumentError unless destination_buffer.is_a? StringIO

  data = @socket.recv destination_buffer.remaining
  length = destination_buffer.write data
  destination_buffer.truncate length
end

#write(source_buffer) ⇒ Object

Raises:

  • (ArgumentError)


49
50
51
52
53
# File 'lib/socket_channel.rb', line 49

def write(source_buffer)
  raise ArgumentError unless source_buffer.is_a? StringIO

  @socket.send(source_buffer.get, 0)
end