Made testserver communicate to parent process with JSON

This is so that if the testserver needs to communicate anything more than
the port in the future (e.g., xmpp port for the test sync server), it
can do so in a flexible manner.

BUG=53934
TEST=manually

Committed: http://src.chromium.org/viewvc/chrome?view=rev&revision=66879

Committed: http://src.chromium.org/viewvc/chrome?view=rev&revision=67018

Committed: http://src.chromium.org/viewvc/chrome?view=rev&revision=67386

Committed: http://src.chromium.org/viewvc/chrome?view=rev&revision=67398

Review URL: http://codereview.chromium.org/5196001

git-svn-id: http://src.chromium.org/svn/trunk/src/net/tools/testserver@67481 4ff67af0-8c30-449e-8e8b-ad334ec8d88c
diff --git a/testserver.py b/testserver.py
index e3e1176..7ce8123 100755
--- a/testserver.py
+++ b/testserver.py
@@ -49,6 +49,7 @@
 SERVER_FTP = 1
 SERVER_SYNC = 2
 
+# Using debug() seems to cause hangs on XP: see http://crbug.com/64515 .
 debug_output = sys.stderr
 def debug(str):
   debug_output.write(str + "\n")
@@ -1326,15 +1327,19 @@
       'port': listen_port
     }
     server_data_json = simplejson.dumps(server_data)
+    server_data_len = len(server_data_json)
+    print 'sending server_data: %s (%d bytes)' % (
+      server_data_json, server_data_len)
     if sys.platform == 'win32':
       fd = msvcrt.open_osfhandle(options.startup_pipe, 0)
     else:
       fd = options.startup_pipe
     startup_pipe = os.fdopen(fd, "w")
-    # Write the listening port as a 2 byte value. This is _not_ using
-    # network byte ordering since the other end of the pipe is on the same
-    # machine.
-    startup_pipe.write(struct.pack('@H', listen_port))
+    # First write the data length as an unsigned 4-byte value.  This
+    # is _not_ using network byte ordering since the other end of the
+    # pipe is on the same machine.
+    startup_pipe.write(struct.pack('=L', server_data_len))
+    startup_pipe.write(server_data_json)
     startup_pipe.close()
 
   try: