Class: DatagramChannel

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#socketObject (readonly)

Returns the value of attribute socket.



13
14
15
# File 'lib/datagram_channel.rb', line 13

def socket
  @socket
end

Class Method Details

.openObject



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

def self.open
  DatagramChannel.new
end

Instance Method Details

#closeObject



19
20
21
# File 'lib/datagram_channel.rb', line 19

def close
  @socket.close
end

#configure_blocking(do_block) ⇒ Object



43
44
# File 'lib/datagram_channel.rb', line 43

def configure_blocking(do_block)
end

#connect(*args) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/datagram_channel.rb', line 23

def connect(*args)
  if args[0].is_a? IPAddr
    ip_address = args[0].to_s
  elsif args[0].is_a? String
    ip_address = IPSocket.getaddress args[0]
  else
    raise ArgumentError
  end

  if args[1].is_a? Numeric
    port_number = args[1]
  else
    port_number = args[1].to_i
  end

  @socket.connect ip_address, port_number

  self
end

#read(destination_io) ⇒ Object

Raises:

  • (ArgumentError)


46
47
48
49
50
51
52
# File 'lib/datagram_channel.rb', line 46

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

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

#write(source_io) ⇒ Object

Raises:

  • (ArgumentError)


54
55
56
57
58
# File 'lib/datagram_channel.rb', line 54

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

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