Class: Pixelflut::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/pixelflut/server.rb,
lib/pixelflut/server/connection.rb,
lib/pixelflut/server/configuration.rb

Defined Under Namespace

Classes: Configuration

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(canvas, config = Configuration.default) ⇒ Server

Returns a new instance of Server.



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/pixelflut/server.rb', line 11

def initialize(canvas, config = Configuration.default)
  @canvas, @config = canvas, config
  @socket, @connections = nil, {}
  @peers = Hash.new{ |h, k| h[k] = 0 }
  @ccfg = Connection::Configuration.new(
    keep_alive_time: config.keep_alive_time,
    read_buffer_size: config.read_buffer_size,
    command_limit: config.command_limit,
    canvas: canvas,
    size_result: "SIZE #{canvas.width} #{canvas.height}\n".freeze,
    on_end: ->(conn){ @peers[conn.peeraddr] -= 1 if @connections.delete(conn) }
  ).freeze
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



9
10
11
# File 'lib/pixelflut/server.rb', line 9

def config
  @config
end

Instance Method Details

#connection_countObject



25
26
27
# File 'lib/pixelflut/server.rb', line 25

def connection_count
  @connections.size
end

#runObject



36
37
38
39
40
# File 'lib/pixelflut/server.rb', line 36

def run
  Thread.new do
    loop{ update }
  end
end

#updateObject



29
30
31
32
33
34
# File 'lib/pixelflut/server.rb', line 29

def update
  return create_socket unless @socket
  incoming = @socket.accept_nonblock(exception: false)
  create_connection(incoming) unless Symbol === incoming
  @connections.keys.each(&:update)
end