" /> Shaykh Al-Flash Amir Memon on the Flash Platform: May 2007 Archives

Main | June 2007 »

May 30, 2007

Flash Player QE Socket Server

I took the java "AquaServer" that was originally written by somebody in the flash community (can't remember who, i know it was a flash superstar, a couple names come to mind but i'm not sure). Well anyway, it's almost completely rewritten.

So, what benefit is it to you? The Flash Player's security model can often be very complicated because of its unique place and function in a user's computing experience. Providing this socket server will serve as a fully functional example of how to interact with the Flash Player's Socket class ( flash.net.Socket ) with respect to using low-level bytes, its security model ( socket policy files, etc ). In fact this is the server side software that the flash player team (my team) uses at Adobe to test the functionality in the first place.

Since we have no reason to crunch any data on the server-side, we simply accept data from the client, and echo it back. In the real world, i could imagine that there would be some server-side crunching going on, so in my code look for where i'm doing the echo'ing (in an "else" clause in the AquaClientThread's run() method) and start from there.

Oh, and also, for the record: This does not count as official Adobe software, its just something i made that we use internally that I thought others can benefit from, don't be surprised if you find significant bugs. Also, for all we know, the Socket security model (or something) could change which would cause this server to break -- so if you use it, be prepared to have to make changes to it if necessary.

I've copy/pasted our internal documentation below:








Flash Player QE Socket Server - FlashPlatformWiki













Flash Player QE Socket Server



From FlashPlatformWiki




Contents






Requirements


  • Windows XP / 2003 Server
    • Should work on Windows 2000 or any other windows with .NET runtime 1.1

  • .NET runtime 1.1
    • .NET runtime 2.0 or any other version WILL NOT work, the .NET Runtime is NOT backwards compatible, which majorly sucks, i know. Each version is treated like it's own product. You can have multiple versions of .NET Runtime installed and it won't be a problem (i've tried it and bloggers also testify to it).

  • JRE 1.6
    • I've ran it on JRE 1.5, however it hasn't been tested with any serious thoroughness
    • Should also work on JRE 1.4, i set the config in eclipse that seems to indicate so.


Installation


  • Make sure you have the .NET Framework Runtime 1.1 installed (installer link above)
  • Make sure you have JRE 6 installed (installer link above)
  • Unpack the .zip file and double-click the .msi installer, here's the .zip file: Download file
  • IMPORTANT: When asked for the Installation Folder, leave it as C:\AquaServers\ , I have some hard-coded strings that depend on this -- not the best decision to hard-code strings, but my time was limited.
  • IMPORTANT: Change the setting to "Everyone" on the same screen!!!! ... It was a well-known bug in the version of visual studio that I was using for there not be a way to adjust the default setting, I could upgrade, but then it might require a higher .NET Runtime version.
  • If the CLASSPATH environmental doesn't have a . in it, such as "blahblahblah;." ... put it in, and if that doesn't work, also put in the AquaServers directory path, for example: "blahblahblah;C:\AquaServers;."

Adding/removing ports to listen on


  • In C:\AquaServers, find the file port_list.txt, open it up in notepad, add or remove port numbers separated by end-lines. You will have to Stop and Start the service in order for this new setting to take effect.

Editing the xml policy that's served on each port


  • Let's say that you configured the server to listen on port 2007, go to this URL: http://localhost:2007/sockAdmin.html (or just refer to the host name or ip address instead of 'localhost' if you want to do this remotely). ONLY USE IE6 OR IE7. Firefox deals with HTTP POST a little differently, and I didn't care to make it cross-browser compatible cuz it's only internal NFR software.
  • Alternatively, you could go to C:\AquaServers\2007\ and edit the .txt file in there.

What it serves


  • If it sees a <load-policy-request/> it serves the policy xml specified in that port's sockAdmin.html
  • If it sees an HTTP GET for sockAdmin.html, it'll serve that file with the current policy XML, it also listens for the HTTP POST to update the xml policy
  • If none of the above are sent, it echoes the data back. It echoes whenever there is a pause in the data being sent then waits for more data or a socket close from the client side.
    • NOTE: All builds prior to 5/14/2007, would receive the data, turn it into a standard java.lang.String, then pump out a byte array and echo it back. This process was breaking non-english data, this is now fixed in the latest version. It now takes the byte string and echoes it back without doing any conversions to or from java.lang.String .


How it's put together


  • There are three parts to it:
    • The installer that installs the C# service wrapper as a service, and puts all the neccessary files in the C:\AquaServers directory
    • The C# wrapper service that runs as a windows service everytime the machine is booted up. This wrapper starts an instance of the java sockert application as a process per port listed in C:\AquaServers\port_list.txt
    • The core java socket server application


Troubleshooting


The best way to learn now to troubleshoot this socket server is to just learn what's running and how to manually kill all its associated processes


  • Hitting the "stop" button in the Services control panel thing should kill all processes, but just in case it doesn't, here's how you can do it manually. On the processes tab in task manager you will see the following, which you could manually kill if needed:
    • A process called "WindowsService1.exe", this is the C# wrapper, and what a horrible name for a process, but that's okay, it'll do.
    • multiple java.exe instances. Each of these is listening on each port listed in C:\AquaServers\port_list.txt
      • These are command-line processes which you can't see unless you check "Allow service to interact with desktop" on the "Log On" tab of the service's properties window.
        • you can type "quit" in the commandline of the java process to end it "nicely"




How to run on Mac or Linux


Since the core application is written in java, it's basically cross-platform, you should be able to run it with little (or even no) modification to the source. Here's how you could go about doing this:


  • Now that you've got it installed on your windows machine, copy the entire C:\AquaServer\ direcotry on your Mac/Linux Machine
  • install JRE 1.6 on that machine
  • try running it with this command if you want to listen on port 2007:

java AquaServer 2007

  • If this kicked out an error, you might have to modify the source (C:\AquaServers\AquaServerSource\AquaServer.java) and recompile. I'm guessing line 99 might cause problems. (line 99 as of this writing, i'm referring to the code referring to the creation of files in a particular directory, the backslashes might be disliked on mac or linux).
  • If you modify the source, you should run the tester on it as well, which is located here: C:\AquaServers\AquaServerTester
    • The tester isn't ready to run out-of-the-box in a way that you could say "all tests passed, we're good to go." Instead, you should tweak around some stuff such as the constants near the top of the file ( MAX_REQUESTS and NUM_BYTES_TO_SEND ).


Version Check


  • In "Add/Remove Programs," click on "Flash Player Socket Server", then click on "Click here for support information"
  • The version is simply the major version plus build date if you ignore the dots, so 2.07.0314 means version 2 and that the build date is 03/14/2007... keeping versioning simple (as long as there is only one released build per day).
  • Also, you'll notice that the .zip file has the same version plus a letter, it'll say _v20070314a.zip. This accounts for the possibility of there being up-to 26 builds in one day (which should be plenty)... however this letter won't show up in "Add/Remove programs"

Revision history


  • in version 20070522a:
    • the decision to add the zero byte at the end was based on how much data was read versus how much should be echoed. I was using the sum of in.getBytesAvailable() calls to determine how much data was read, where i should have just been summing the int in.read() calls. I'm doing that now, bug fixed.


  • in version 20070514a:
    • the code is re-arranged, it was living in the default package, now living in fpqe.socketServer
    • the server would cast all bytes to String, then cast to byte again, and echo that. Now it just echoes the bytes. This change was neccessary in order to fix non-english media. When type casting to/from String, non-English chars would break, not bueno. now should be fixed.


Uninstallation


  • Stop the service and in "Add/Remove Programs," click "Flash Player Socket Server" and click remove