Class: RubyWolf::Connection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(socket) ⇒ Connection

Returns a new instance of Connection.



5
6
7
8
9
10
11
# File 'lib/ruby_wolf/connection.rb', line 5

def initialize(socket)
  @socket = socket
  @read_data = ''
  @write_data = ''

  @reading = true
end

Instance Attribute Details

#read_dataObject (readonly)

Returns the value of attribute read_data.



3
4
5
# File 'lib/ruby_wolf/connection.rb', line 3

def read_data
  @read_data
end

#socketObject (readonly)

Returns the value of attribute socket.



3
4
5
# File 'lib/ruby_wolf/connection.rb', line 3

def socket
  @socket
end

#write_dataObject (readonly)

Returns the value of attribute write_data.



3
4
5
# File 'lib/ruby_wolf/connection.rb', line 3

def write_data
  @write_data
end

Instance Method Details

#closeObject



41
42
43
# File 'lib/ruby_wolf/connection.rb', line 41

def close
  @socket.close
end

#enqueue_write(data) ⇒ Object



24
25
26
# File 'lib/ruby_wolf/connection.rb', line 24

def enqueue_write(data)
  @write_data += data
end

#need_to_read?Boolean

Returns:

  • (Boolean)


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

def need_to_read?
  @reading
end

#need_to_write?Boolean

Returns:

  • (Boolean)


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

def need_to_write?
  !@write_data.length.zero?
end

#readObject



17
18
19
20
21
22
# File 'lib/ruby_wolf/connection.rb', line 17

def read
  @read_data << socket.read_nonblock(RubyWolf::READ_SIZE)
  @reading = false if @read_data.end_with?(RubyWolf::CRLF)
rescue EOFError
  @reading = false
end

#to_ioObject



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

def to_io
  @socket
end

#writeObject



28
29
30
31
# File 'lib/ruby_wolf/connection.rb', line 28

def write
  writen = socket.write_nonblock(@write_data)
  @write_data[0..writen] = ''
end