Class: Rong::Server::Daemon

Inherits:
Object
  • Object
show all
Defined in:
lib/rong/server/daemon.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#listenerObject

Returns the value of attribute listener.



4
5
6
# File 'lib/rong/server/daemon.rb', line 4

def listener
  @listener
end

Instance Method Details

#open_listening_socketObject



18
19
20
21
22
23
# File 'lib/rong/server/daemon.rb', line 18

def open_listening_socket
  self.listener = Socket.new(Socket::AF_INET, Socket::SOCK_DGRAM)
  address  = Socket.pack_sockaddr_in(7664, Socket::INADDR_LOOPBACK)
  listener.bind(address)
  listener.listen(3)
end

#runObject



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/rong/server/daemon.rb', line 6

def run
  open_listening_socket
  puts "Server is running..."
  loop do
    wait_for_connections do |clients|
      Thread.new do
        Rong::Elements::Game.new(clients).start
      end
    end
  end
end

#wait_for_connections {|clients| ... } ⇒ Object

Yields:

  • (clients)


25
26
27
28
29
30
31
32
33
# File 'lib/rong/server/daemon.rb', line 25

def wait_for_connections
  clients = []
  until clients.count == 2
    client, addr = listener.accept
    clients << client
  end
  yield clients if block_given?
  clients
end