Class: CrapServer::ConnectionHandler

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

Instance Method Summary collapse

Constructor Details

#initialize(sockets) ⇒ ConnectionHandler

Returns a new instance of ConnectionHandler.



4
5
6
7
8
9
# File 'lib/crap_server/connection_handler.rb', line 4

def initialize(sockets)
  @sockets = sockets
  @sockets.each do |io|
    add_to_read io
  end
end

Instance Method Details

#add_read_buffer(io, string) ⇒ Object



65
66
67
68
69
# File 'lib/crap_server/connection_handler.rb', line 65

def add_read_buffer(io, string)
  @rbuffer ||= {}
  @rbuffer[io.fileno] ||= ''
  @rbuffer[io.fileno] << string
end

#add_to_read(io) ⇒ Object



40
41
42
43
# File 'lib/crap_server/connection_handler.rb', line 40

def add_to_read(io)
  @to_read ||= {}
  @to_read[io.fileno] = io
end

#add_to_write(io) ⇒ Object



11
12
13
14
# File 'lib/crap_server/connection_handler.rb', line 11

def add_to_write(io)
  @to_write ||= {}
  @to_write[io.fileno] = io
end

#address(io) ⇒ Object



50
51
52
53
# File 'lib/crap_server/connection_handler.rb', line 50

def address(io)
  @address ||= {}
  @address[io.fileno]
end

#buffer(io) ⇒ Object



26
27
28
29
# File 'lib/crap_server/connection_handler.rb', line 26

def buffer(io)
  @buffer ||= {}
  @buffer[io.fileno]
end

#close(io) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/crap_server/connection_handler.rb', line 81

def close(io)
  remove_to_read io
  remove_to_write io
  @closeaw ||= {}
  @closeaw.delete io.fileno
  io.close
end

#close_after_write(io) ⇒ Object



76
77
78
79
# File 'lib/crap_server/connection_handler.rb', line 76

def close_after_write(io)
  @closeaw ||= {}
  @closeaw[io.fileno]
end

#handle(&block) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/crap_server/connection_handler.rb', line 89

def handle(&block)
  # The main loop. Listening IPv4 and IPv6 connections
  accept_loop do |data, remote_socket, address_info|
    instance = CrapServer::ConnectionInstance.new
    instance.socket = remote_socket
    instance.config = config
    instance.address = address_info
    instance.handler = self
    instance.run data, &block
  end
end

#read_buffer(io) ⇒ Object



60
61
62
63
# File 'lib/crap_server/connection_handler.rb', line 60

def read_buffer(io)
  @rbuffer ||= {}
  @rbuffer[io.fileno]
end

#remove_to_read(io) ⇒ Object



45
46
47
48
# File 'lib/crap_server/connection_handler.rb', line 45

def remove_to_read(io)
  @to_read.delete io.fileno
  @address.delete io.fileno
end

#remove_to_write(io) ⇒ Object



20
21
22
23
24
# File 'lib/crap_server/connection_handler.rb', line 20

def remove_to_write(io)
  @buffer ||= {}
  @to_write.delete io.fileno
  @buffer.delete io.fileno
end

#set_address(io, addrs) ⇒ Object



55
56
57
58
# File 'lib/crap_server/connection_handler.rb', line 55

def set_address(io, addrs)
  @address ||= {}
  @address[io.fileno] = addrs
end

#set_buffer(io, string) ⇒ Object



31
32
33
34
# File 'lib/crap_server/connection_handler.rb', line 31

def set_buffer(io, string)
  @buffer ||= {}
  @buffer[io.fileno] = string
end

#set_close_after_write(io) ⇒ Object



71
72
73
74
# File 'lib/crap_server/connection_handler.rb', line 71

def set_close_after_write(io)
  @closeaw ||= {}
  @closeaw[io.fileno] = true
end

#to_readObject



36
37
38
# File 'lib/crap_server/connection_handler.rb', line 36

def to_read
  (@to_read ||= {}).values
end

#to_writeObject



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

def to_write
  (@to_write ||= {}).values
end