Class: SocketChannel
- Inherits:
-
Object
- Object
- SocketChannel
- Defined in:
- lib/socket_channel.rb
Instance Attribute Summary collapse
-
#socket ⇒ Object
readonly
Returns the value of attribute socket.
Class Method Summary collapse
Instance Method Summary collapse
- #close ⇒ Object
- #connect(*args) ⇒ Object
- #connected? ⇒ Boolean
-
#initialize ⇒ SocketChannel
constructor
A new instance of SocketChannel.
- #read(destination_buffer) ⇒ Object
- #write(source_buffer) ⇒ Object
Constructor Details
#initialize ⇒ SocketChannel
Returns a new instance of SocketChannel.
33 34 35 |
# File 'lib/socket_channel.rb', line 33 def initialize @connected = false end |
Instance Attribute Details
#socket ⇒ Object (readonly)
Returns the value of attribute socket.
14 15 16 |
# File 'lib/socket_channel.rb', line 14 def socket @socket end |
Class Method Details
.open ⇒ Object
16 17 18 |
# File 'lib/socket_channel.rb', line 16 def self.open SocketChannel.new end |
Instance Method Details
#close ⇒ Object
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
37 38 39 |
# File 'lib/socket_channel.rb', line 37 def connected? @connected end |
#read(destination_buffer) ⇒ Object
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
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 |