flashpolicyd.rb
def start
    if @@serverThread.nil? 
      begin
        # Disable reverse lookups, makes it all slow down
        BasicSocket::do_not_reverse_lookup=true
        server = TCPServer.new(@host, @port)
      rescue Exception => e
        log("Can't open server: #{e.class} #{e}")
        return 
      end
    
      begin
        @@serverThread = Thread.new {
          while (session = server.accept)
            Thread.new(session) do |client|
              begin
                log("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
                  log("Pushed connection thread to @connections, now #{@connections.size} connections")
                }

                log("Calling serve on connection")
                serve(connection)

                client.close

                @@connMutex.synchronize {
                  @connections.delete(connection)
                  log("Removed connection from @connections, now #{@connections.size} connections")
                }

              rescue Errno::ENOTCONN => e
                log("Unexpected disconnection while handling request")
              rescue Errno::ECONNRESET => e
                log("Connection reset by peer")
              rescue Exception => e
                log("Unexpected #{e.class} exception while handling client connection: #{e}")
                log("Unexpected #{e.class} exception while handling client connection: #{e.backtrace.join("\n")}")
                client.close
              end # block around main logic
            end # while
          end # around Thread.new for client connections
        } # @serverThread
      rescue Exception => e
        log("Got #{e.class} exception in main listening thread: #{e}")
      end
    end
  end
# === 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
    if @@serverThread.nil? 
      begin
        BasicSocket::do_not_reverse_lookup=true
        server = TCPServer.new(@host, @port)
      rescue Exception => e
        log("Can't open server: #{e.class} #{e}")
        return 
      end
      begin
        @@serverThread = Thread.new {
          while (session = server.accept)
            Thread.new(session) do |client|
              begin
                log("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
                  log("Pushed connection thread to @connections, now #{@connections.size} connections")
                }
                log("Calling serve on connection")
                serve(connection)
                client.close
                @@connMutex.synchronize {
                  @connections.delete(connection)
                  log("Removed connection from @connections, now #{@connections.size} connections")
                }
              rescue Errno::ENOTCONN => e
                log("Unexpected disconnection while handling request")
              rescue Errno::ECONNRESET => e
                log("Connection reset by peer")
              rescue Exception => e
                log("Unexpected #{e.class} exception while handling client connection: #{e}")
                log("Unexpected #{e.class} exception while handling client connection: #{e.backtrace.join("\n")}")
                client.close
              end 
            end 
          end 
        } 
      rescue Exception => e
        log("Got #{e.class} exception in main listening thread: #{e}")
      end
    end
  end
def start if @@serverThread . nil? begin BasicSocket :: do_not_reverse_lookup = true server = TCPServer . new ( @host , @port ) rescue Exception => e log ( "Can't open server: #{e.class} #{e}" ) return end begin @@serverThread = Thread . new { while ( session = server . accept ) Thread . new ( session ) do | client | begin log ( "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 log ( "Pushed connection thread to @connections, now #{@connections.size} connections" ) } log ( "Calling serve on connection" ) serve ( connection ) client . close @@connMutex . synchronize { @connections . delete ( connection ) log ( "Removed connection from @connections, now #{@connections.size} connections" ) } rescue Errno :: ENOTCONN => e log ( "Unexpected disconnection while handling request" ) rescue Errno :: ECONNRESET => e log ( "Connection reset by peer" ) rescue Exception => e log ( "Unexpected #{e.class} exception while handling client connection: #{e}" ) log ( "Unexpected #{e.class} exception while handling client connection: #{e.backtrace.join("\n")}" ) client . close end end end } rescue Exception => e log ( "Got #{e.class} exception in main listening thread: #{e}" ) end 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