flashpolicyd.rb
# === Synopsis
# Starts the main loop of the server and handles connections, logic is more or less:
#
# 1. Opens the port for listening
# 1. Create a new thread so the connection handling happens seperate from the main loop
# 1. Create a loop to accept new sessions from the socket, each new sesison gets a new thread
# 1. Increment the totalclient variable for stats handling
# 1. Create a OpenStruct structure with detail about the current connection and put it in the @connections array
# 1. Pass the connection to the serve method for handling
# 1. Once handling completes, remove the connection from the active list and close the socket
Synopsis
Starts the main loop of the server and handles connections, logic is more or less.

1. Opens the port for listening
1. Create a new thread so the connection handling happens seperate from the main loop
1. Create a loop to accept new sessions from the socket, each new sesison gets a new thread
1. Increment the totalclient variable for stats handling
1. Create a OpenStruct structure with detail about the current connection and put it in the @connections array
1. Pass the connection to the serve method for handling
1. Once handling completes, remove the connection from the active list and close the socket
Synopsis Starts the main loop of the server and handles connections logic is more or less . 1 . Opens the port for listening 1 . Create a new thread so the connection handling happens seperate from the main loop 1 . Create a loop to accept new sessions from the socket each new sesison gets a new thread 1 . Increment the totalclient variable for stats handling 1 . Create a OpenStruct structure with detail about the current connection and put it in the @connections array 1 . Pass the connection to the serve method for handling 1 . Once handling completes remove the connection from the active list and close the socket
def start
    begin
      BasicSocket::do_not_reverse_lookup=true
      server = TCPServer.new(@host, @port)
    rescue Exception => e
      fatal("Can't open server: #{e.class} #{e}")
      exit
    end
    begin
      @serverThread = Thread.new {
        while (session = server.accept)
          Thread.new(session) do |client|
            begin
              debug("Handling new connection from #{client.peeraddr[2]}, #{Thread.list.size} total threads ")
              @@clientsMutex.synchronize {
                @@totalclients += 1
              }
              connection = OpenStruct.new
              connection.client = client
              connection.timecreated = Time.new
              connection.thread = Thread.current
              connection.addr = client.peeraddr
              @@connMutex.synchronize {
                @connections << connection
                debug("Pushed connection thread to @connections, now #{@connections.size} connections")
              }
              debug("Calling serve on connection")
              serve(connection)
              client.close
              @@connMutex.synchronize {
                @connections.delete(connection)
                debug("Removed connection from @connections, now #{@connections.size} connections")
              }
            rescue Errno::ENOTCONN => e
              warn("Unexpected disconnection while handling request")
            rescue Errno::ECONNRESET => e
              warn("Connection reset by peer")
            rescue Exception => e
              error("Unexpected #{e.class} exception while handling client connection: #{e}")
              error("Unexpected #{e.class} exception while handling client connection: #{e.backtrace.join("\n")}")
              client.close
            end 
          end 
        end 
      } 
    rescue Exception => e
      fatal("Got #{e.class} exception in main listening thread: #{e}")
    end
  end
def start begin BasicSocket :: do_not_reverse_lookup = true server = TCPServer . new ( @host , @port ) rescue Exception => e fatal ( "Can't open server: #{e.class} #{e}" ) exit end begin @serverThread = Thread . new { while ( session = server . accept ) Thread . new ( session ) do | client | begin debug ( "Handling new connection from #{client.peeraddr[2]}, #{Thread.list.size} total threads " ) @@clientsMutex . synchronize { @@totalclients += 1 } connection = OpenStruct . new connection . client = client connection . timecreated = Time . new connection . thread = Thread . current connection . addr = client . peeraddr @@connMutex . synchronize { @connections << connection debug ( "Pushed connection thread to @connections, now #{@connections.size} connections" ) } debug ( "Calling serve on connection" ) serve ( connection ) client . close @@connMutex . synchronize { @connections . delete ( connection ) debug ( "Removed connection from @connections, now #{@connections.size} connections" ) } rescue Errno :: ENOTCONN => e warn ( "Unexpected disconnection while handling request" ) rescue Errno :: ECONNRESET => e warn ( "Connection reset by peer" ) rescue Exception => e error ( "Unexpected #{e.class} exception while handling client connection: #{e}" ) error ( "Unexpected #{e.class} exception while handling client connection: #{e.backtrace.join("\n")}" ) client . close end end end } rescue Exception => e fatal ( "Got #{e.class} exception in main listening thread: #{e}" ) end end
Synopsis
Starts the main loop of the server and handles connections, logic is more or less:
Synopsis Starts the main loop of the server and handles connections logic is more or less :
# Disable reverse lookups, makes it all slow down # block around main logic # while # around Thread.new for client connections # @serverThread