THRIFT-2813 multiple haxe library fixes/improvements
Client: Haxe
Patch: Jens Geyer

This closes #260
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 5751a2c..7b235d0 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -85,6 +85,7 @@
 	cocoa \
 	d \
 	delphi \
+	haxe \
 	javame \
 	js \
 	ocaml \
diff --git a/lib/haxe/haxelib.json b/lib/haxe/haxelib.json
new file mode 100644
index 0000000..a3bde74
--- /dev/null
+++ b/lib/haxe/haxelib.json
@@ -0,0 +1,11 @@
+{
+	"name": "thrift",
+	"url" : "http://thrift.apache.org",
+	"license": "Apache",
+	"tags": ["thrift", "rpc", "serialization", "cross", "framework"],
+	"description": "Haxe bindings for the Apache Thrift RPC and serialization framework",
+	"version": "0.9.2-beta.1",
+	"releasenote": "First release, based on Apache Thrift 0.9.2. For details see THRIFT-2644.",
+	"contributors": ["JensG"],
+	"dependencies": {}
+}
diff --git a/lib/haxe/src/org/apache/thrift/server/TServer.hx b/lib/haxe/src/org/apache/thrift/server/TServer.hx
index e689b32..56eee0a 100644
--- a/lib/haxe/src/org/apache/thrift/server/TServer.hx
+++ b/lib/haxe/src/org/apache/thrift/server/TServer.hx
@@ -38,7 +38,7 @@
 
     // Log delegation
     private var _logDelegate : Dynamic->Void  = null;
-    public var logDelegate(default,set) : Dynamic->Void;
+    public var logDelegate(get,set) : Dynamic->Void;
 
     public function new( processor : TProcessor,
                          serverTransport : TServerTransport,
@@ -87,6 +87,11 @@
     }
 
 
+    private function get_logDelegate() : Dynamic->Void {
+        return _logDelegate;
+    }
+
+
     private function DefaultLogDelegate(value : Dynamic) : Void  {
         trace( value);
     }
diff --git a/lib/haxe/src/org/apache/thrift/server/TSimpleServer.hx b/lib/haxe/src/org/apache/thrift/server/TSimpleServer.hx
index cb7cbd6..f3408e2 100644
--- a/lib/haxe/src/org/apache/thrift/server/TSimpleServer.hx
+++ b/lib/haxe/src/org/apache/thrift/server/TSimpleServer.hx
@@ -33,15 +33,14 @@
                          serverTransport : TServerTransport,
                          transportFactory : TTransportFactory = null,
                          protocolFactory : TProtocolFactory = null,
-                         logDelegate : Dynamic->Void = null) {
+                         logger : Dynamic->Void = null) {
       super( processor, serverTransport,
              transportFactory, transportFactory,
              protocolFactory, protocolFactory,
-             logDelegate);
+             logger);
     }
 
 
-
     public override function Serve() : Void
     {
         try
@@ -102,12 +101,15 @@
             }
             catch( ttx : TTransportException)
             {
-                  // Usually a client disconnect, expected
+                // Usually a client disconnect, expected
+            }
+            catch( pex : TProtocolException)
+            {
+                logDelegate(pex); // Unexpected
             }
             catch( e : Dynamic)
             {
-                // Unexpected
-                  logDelegate(e);
+                logDelegate(e); // Unexpected
             }
 
             // Fire deleteContext server event after client disconnects
diff --git a/lib/haxe/src/org/apache/thrift/transport/TFramedTransport.hx b/lib/haxe/src/org/apache/thrift/transport/TFramedTransport.hx
index 5f0168a..cef82ef 100644
--- a/lib/haxe/src/org/apache/thrift/transport/TFramedTransport.hx
+++ b/lib/haxe/src/org/apache/thrift/transport/TFramedTransport.hx
@@ -21,6 +21,7 @@
 
 import org.apache.thrift.transport.*;
 
+import haxe.io.Eof;
 import haxe.io.Bytes;
 import haxe.io.BytesBuffer;
 import haxe.io.BytesOutput;
@@ -73,31 +74,41 @@
     }
 
     public override function read(buf : BytesBuffer, off : Int, len : Int) : Int {
-        var data = Bytes.alloc(len);
+        try {
+            var data = Bytes.alloc(len);
 
-        if (readBuffer_ != null) {
-            var got : Int = readBuffer_.readBytes(data, off, len);
-            if (got > 0) {
-                buf.addBytes(data,0,got);
-                return got;
+            if ((readBuffer_ != null) && (readBuffer_.position < readBuffer_.length)) {
+                var got : Int = readBuffer_.readBytes(data, off, len);
+                if (got > 0) {
+                    buf.addBytes(data,0,got);
+                    return got;
+                };
             };
-        };
 
-        // Read another frame of data
-        readFrame();
+            // Read another frame of data
+            readFrame();
 
-        var got = readBuffer_.readBytes(data, off, len);
-        buf.addBytes(data,0,got);
-        return got;
+            var got = readBuffer_.readBytes(data, off, len);
+            buf.addBytes(data,0,got);
+            return got;
+        }
+        catch (eof : Eof) {
+            throw new TTransportException(TTransportException.END_OF_FILE, 'Can\'t read $len bytes!');
+        }
     }
 
 
     function readFrameSize() : Int {
-        var buffer = new BytesBuffer();
-        var len = transport_.readAll( buffer, 0, 4);
-        var inp = new BytesInput( buffer.getBytes(), 0, 4);
-        inp.bigEndian = true;
-        return inp.readInt32();
+        try {
+            var buffer = new BytesBuffer();
+            var len = transport_.readAll( buffer, 0, 4);
+            var inp = new BytesInput( buffer.getBytes(), 0, 4);
+            inp.bigEndian = true;
+            return inp.readInt32();
+        }
+        catch(eof : Eof) {
+            throw new TTransportException(TTransportException.END_OF_FILE, 'Can\'t read 4 bytes!');
+        }
     }
 
 
@@ -111,10 +122,15 @@
             throw new TTransportException(TTransportException.UNKNOWN, 'Frame size ($size) larger than max length ($maxLength_)!');
         };
 
-        var buffer = new BytesBuffer();
-        size = transport_.readAll( buffer, 0, size);
-        readBuffer_ = new BytesInput( buffer.getBytes(), 0, size);
-        readBuffer_.bigEndian = true;
+        try {
+            var buffer = new BytesBuffer();
+            size = transport_.readAll( buffer, 0, size);
+            readBuffer_ = new BytesInput( buffer.getBytes(), 0, size);
+            readBuffer_.bigEndian = true;
+        }
+        catch(eof : Eof) {
+            throw new TTransportException(TTransportException.END_OF_FILE, 'Can\'t read $size bytes!');
+        }
     }
 
     public override function write(buf : Bytes, off : Int, len : Int) : Void {
@@ -135,7 +151,7 @@
 
         writeFrameSize(len);
         transport_.write(buf, 0, len);
-        transport_.flush();
+        transport_.flush(callback);
     }
 }
 
diff --git a/lib/haxe/src/org/apache/thrift/transport/TServerSocket.hx b/lib/haxe/src/org/apache/thrift/transport/TServerSocket.hx
index f38b584..586a8b6 100644
--- a/lib/haxe/src/org/apache/thrift/transport/TServerSocket.hx
+++ b/lib/haxe/src/org/apache/thrift/transport/TServerSocket.hx
@@ -43,13 +43,13 @@
     private var _port : Int = 0;
 
     // Timeout for client sockets from accept
-    private var _clientTimeout : Int = 0;
+    private var _clientTimeout : Float = 0;
 
     // Whether or not to wrap new TSocket connections in buffers
     private var _useBufferedSockets : Bool = false;
 
 
-    public function new( port : Int, clientTimeout : Int = 0, useBufferedSockets : Bool = false)
+    public function new( port : Int, clientTimeout : Float = 0, useBufferedSockets : Bool = false)
     {
         _port = port;
         _clientTimeout = clientTimeout;
diff --git a/lib/haxe/src/org/apache/thrift/transport/TSocket.hx b/lib/haxe/src/org/apache/thrift/transport/TSocket.hx
index 9d5e2dc..b6f2119 100644
--- a/lib/haxe/src/org/apache/thrift/transport/TSocket.hx
+++ b/lib/haxe/src/org/apache/thrift/transport/TSocket.hx
@@ -83,6 +83,7 @@
         #else
         this.host = new Host(host);
         #end
+
         this.port = port;
     }
 
@@ -143,7 +144,7 @@
 
             #else
 
-            socket.waitForRead();
+            //socket.waitForRead();  -  no, this ignores timeout and blocks infinitely
             if(readCount < off) {
                 input.read(off-readCount);
                 readCount = off;
@@ -208,7 +209,6 @@
         }
         var bytes = outbuf.buffer;
 
-
         #else
 
         var bytes = obuffer.getBytes();
@@ -222,11 +222,13 @@
         ioCallback = callback;
         try {
             readCount = 0;
+
             #if js
             output.send( bytes);
             #else
             output.writeBytes( bytes, 0, bytes.length);
             #end
+
             if(ioCallback != null) {
                 ioCallback(null);  // success call
             }
@@ -260,15 +262,14 @@
         }
 
         #elseif flash
-
         var socket = new Socket();
         socket.connect(host, port);
 
         #else
-
         var socket = new Socket();
         socket.setBlocking(true);
         socket.setFastSend(true);
+        socket.setTimeout(5.0);
         socket.connect(host, port);
 
         #end
@@ -286,10 +287,12 @@
 
         #if (flash || js)
         output = socket;
-          input = socket;
+        input = socket;
+
         #else
-          output = socket.output;
-          input = socket.input;
+        output = socket.output;
+        input = socket.input;
+
         #end
     }
 
diff --git a/lib/haxe/test/HaxeTests.hxproj b/lib/haxe/test/HaxeTests.hxproj
new file mode 100644
index 0000000..4e8929b
--- /dev/null
+++ b/lib/haxe/test/HaxeTests.hxproj
@@ -0,0 +1,67 @@
+<?xml version="1.0" encoding="utf-8"?>
+<project version="2">
+  <!-- Output SWF options -->
+  <output>
+    <movie outputType="Application" />
+    <movie input="" />
+    <movie path="bin/HaxeTests" />
+    <movie fps="30" />
+    <movie width="800" />
+    <movie height="600" />
+    <movie version="1" />
+    <movie minorVersion="0" />
+    <movie platform="C++" />
+    <movie background="#FFFFFF" />
+  </output>
+  <!-- Other classes to be compiled into your SWF -->
+  <classpaths>
+    <class path="src" />
+    <class path="gen-haxe" />
+    <class path="../src" />
+  </classpaths>
+  <!-- Build options -->
+  <build>
+    <option directives="" />
+    <option flashStrict="False" />
+    <option noInlineOnDebug="False" />
+    <option mainClass="Main" />
+    <option enabledebug="False" />
+    <option additional="" />
+  </build>
+  <!-- haxelib libraries -->
+  <haxelib>
+    <!-- example: <library name="..." /> -->
+  </haxelib>
+  <!-- Class files to compile (other referenced classes will automatically be included) -->
+  <compileTargets>
+    <!-- example: <compile path="..." /> -->
+  </compileTargets>
+  <!-- Paths to exclude from the Project Explorer tree -->
+  <hiddenPaths>
+    <hidden path="obj" />
+    <hidden path="cpp.hxml" />
+    <hidden path="csharp.hxml" />
+    <hidden path="flash.hxml" />
+    <hidden path="java.hxml" />
+    <hidden path="javascript.hxml" />
+    <hidden path="make_all.bat" />
+    <hidden path="make_all.sh" />
+    <hidden path="Makefile.am" />
+    <hidden path="neko.hxml" />
+    <hidden path="php.hxml" />
+    <hidden path="project.hide" />
+    <hidden path="python.hxml" />
+  </hiddenPaths>
+  <!-- Executed before build -->
+  <preBuildCommand>thrift -r -gen haxe  ../../../test/ThriftTest.thrift</preBuildCommand>
+  <!-- Executed after build -->
+  <postBuildCommand alwaysRun="False" />
+  <!-- Other project options -->
+  <options>
+    <option showHiddenPaths="False" />
+    <option testMovie="Unknown" />
+    <option testMovieCommand="" />
+  </options>
+  <!-- Plugin storage -->
+  <storage />
+</project>
\ No newline at end of file
diff --git a/lib/haxe/test/Makefile.am b/lib/haxe/test/Makefile.am
index 5e92f98..357436c 100644
--- a/lib/haxe/test/Makefile.am
+++ b/lib/haxe/test/Makefile.am
@@ -48,3 +48,17 @@
 check: $(BIN_CPP)
 	$(BIN_CPP)
 
+EXTRA_DIST = \
+             src \
+             cpp.hxml \
+             csharp.hxml \
+             flash.hxml \
+             java.hxml \
+             javascript.hxml \
+             neko.hxml \
+             php.hxml \
+             python.hxml \
+             project.hide \
+             HaxeTests.hxproj \
+             make_all.bat \
+             make_all.sh