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
12
# File 'lib/ruby_wolf/connection.rb', line 5

def initialize(socket)
  @socket = socket
  @read_chunk = ''
  @write_chunk = ''
  @reading = true

  @headers = {}
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



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

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

#methodObject (readonly)

Returns the value of attribute method.



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

def method
  @method
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

#queryObject (readonly)

Returns the value of attribute query.



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

def query
  @query
end

#read_chunkObject (readonly)

Returns the value of attribute read_chunk.



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

def read_chunk
  @read_chunk
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_chunkObject (readonly)

Returns the value of attribute write_chunk.



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

def write_chunk
  @write_chunk
end

Instance Method Details

#closeObject



46
47
48
# File 'lib/ruby_wolf/connection.rb', line 46

def close
  @socket.close
end

#enqueue_write(data) ⇒ Object



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

def enqueue_write(data)
  @write_chunk += data
end

#need_to_read?Boolean

Returns:

  • (Boolean)


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

def need_to_read?
  @reading
end

#need_to_write?Boolean

Returns:

  • (Boolean)


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

def need_to_write?
  !@write_chunk.bytesize.zero?
end

#readObject



18
19
20
21
22
23
24
25
26
27
# File 'lib/ruby_wolf/connection.rb', line 18

def read
  @read_chunk << socket.read_nonblock(RubyWolf::READ_SIZE)
  if @content_length.nil?
    read_headers
  else
    read_body
  end
rescue EOFError
  @reading = false
end

#to_ioObject



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

def to_io
  @socket
end

#writeObject



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

def write
  writen = socket.write_nonblock(@write_chunk)
  @write_chunk = @write_chunk.byteslice(writen, @write_chunk.bytesize)
end