Skip to content

Instantly share code, notes, and snippets.

@davidlee
Created March 19, 2009 12:32
Show Gist options
  • Save davidlee/81790 to your computer and use it in GitHub Desktop.
Save davidlee/81790 to your computer and use it in GitHub Desktop.
eventmachine policy file server for flash
equire 'rubygems'
require 'eventmachine'
module Socky
module Policy
DEBUG = true
HOST = 'localhost'
PORT = 843
EOF = "\r\000"
REQUEST = "<policy-file-request/>\000"
DOCUMENT = File.read(FIle.join(File.dirname(__FILE__),'policy.xml'))
module Server
def packet?(data)
req_size = REQUEST.size
return false if data.size < req_size
if data[0...req_size] == REQUEST
send_data(DOCUMENT)
return req_size
else
STDERR.puts( "\n[ Malformed request: #{ data } ]" ) if DEBUG
end
end
def receive_data(data)
(@saved_data ||= "") << data
while true
case i = packet?(saved_data)
when false, 0; return
when true; @saved_data = ""
when Numeric; @saved_data.slice!(0...i)
end
end
end
end
end
end
if __FILE__ == $0
EventMachine::run do
EventMachine::start_server( Socky::Policy::HOST,
Socky::Policy::PORT,
Socky::Policy::Server )
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment