diff options
author | Jens Geyer <jensg@apache.org> | 2016-03-15 23:04:27 +0200 |
---|---|---|
committer | Jens Geyer <jensg@apache.org> | 2016-03-16 09:59:47 +0200 |
commit | 1d20a370d25b7154104289bb337ab4375edf19b9 (patch) | |
tree | dea68765309e7119b83a0bd59dc153e1dd6a941a /lib/haxe/src | |
parent | aadcf34cbf643b5eff1c771047a05a4c77be9d9e (diff) | |
download | thrift-1d20a370d25b7154104289bb337ab4375edf19b9.tar.gz |
THRIFT-3742 haxe php cli support
Client: Haxe
Patch: Oleksii Prudkyi + minor changes from Jens Geyer
This closes #950
Diffstat (limited to 'lib/haxe/src')
7 files changed, 143 insertions, 106 deletions
diff --git a/lib/haxe/src/org/apache/thrift/helper/ZigZag.hx b/lib/haxe/src/org/apache/thrift/helper/ZigZag.hx index 862060d8d..6fb2e7b9e 100644 --- a/lib/haxe/src/org/apache/thrift/helper/ZigZag.hx +++ b/lib/haxe/src/org/apache/thrift/helper/ZigZag.hx @@ -20,6 +20,7 @@ package org.apache.thrift.helper; import haxe.Int64; +import haxe.Int32; class ZigZag { @@ -28,7 +29,15 @@ class ZigZag { * represented compactly as a varint. */ public static function FromInt( n : Int) : UInt { + #if php + + return cast(cast(cast(n,Int32) << 1,Int32) ^ cast(cast(n,Int32) >> 31,Int32),UInt); + + #else + return cast(n << 1,UInt) ^ cast(n >> 31,UInt); + + #end } @@ -36,7 +45,22 @@ class ZigZag { * Convert from zigzag int to int. */ public static function ToInt( n : UInt) : Int { + #if php + + var convertedLong = ToLong(n); + //if overflow return high + if( convertedLong.high != convertedLong.low >> 31 ) { + return convertedLong.high; + } else { + return convertedLong.low; + } + + + #else + return (0x7FFFFFFF & cast(n >> 1,Int)) ^ (-cast(n & 1,Int)); + + #end } @@ -125,4 +149,5 @@ class ZigZag { } #end } -
\ No newline at end of file + +
\ No newline at end of file diff --git a/lib/haxe/src/org/apache/thrift/protocol/TProtocol.hx b/lib/haxe/src/org/apache/thrift/protocol/TProtocol.hx index 22e88e44b..b7f3842d0 100644 --- a/lib/haxe/src/org/apache/thrift/protocol/TProtocol.hx +++ b/lib/haxe/src/org/apache/thrift/protocol/TProtocol.hx @@ -79,7 +79,7 @@ interface TProtocol { function readString() : String; function readBinary() : Bytes; - // recursion tracking - function IncrementRecursionDepth() : Void; - function DecrementRecursionDepth() : Void; + // recursion tracking + function IncrementRecursionDepth() : Void; + function DecrementRecursionDepth() : Void; } diff --git a/lib/haxe/src/org/apache/thrift/protocol/TProtocolDecorator.hx b/lib/haxe/src/org/apache/thrift/protocol/TProtocolDecorator.hx index 22c5dd250..769e93cc5 100644 --- a/lib/haxe/src/org/apache/thrift/protocol/TProtocolDecorator.hx +++ b/lib/haxe/src/org/apache/thrift/protocol/TProtocolDecorator.hx @@ -216,11 +216,11 @@ class TProtocolDecorator implements TProtocol return wrapped.readBinary(); } - public function IncrementRecursionDepth() : Void { + public function IncrementRecursionDepth() : Void { return wrapped.IncrementRecursionDepth(); - } + } - public function DecrementRecursionDepth() : Void { + public function DecrementRecursionDepth() : Void { return wrapped.DecrementRecursionDepth(); - } + } } diff --git a/lib/haxe/src/org/apache/thrift/protocol/TProtocolUtil.hx b/lib/haxe/src/org/apache/thrift/protocol/TProtocolUtil.hx index 71ed4ba36..1ec59d2cf 100644 --- a/lib/haxe/src/org/apache/thrift/protocol/TProtocolUtil.hx +++ b/lib/haxe/src/org/apache/thrift/protocol/TProtocolUtil.hx @@ -35,76 +35,76 @@ class TProtocolUtil { * @param type the next value will be intepreted as this TType value. */ public static function skip(prot:TProtocol, type : Int) : Void { - prot.IncrementRecursionDepth(); - try - { - switch (type) { - case TType.BOOL: - prot.readBool(); - - case TType.BYTE: - prot.readByte(); - - case TType.I16: - prot.readI16(); - - case TType.I32: - prot.readI32(); - - case TType.I64: - prot.readI64(); - - case TType.DOUBLE: - prot.readDouble(); - - case TType.STRING: - prot.readBinary(); - - case TType.STRUCT: - prot.readStructBegin(); - while (true) { - var field:TField = prot.readFieldBegin(); - if (field.type == TType.STOP) { - break; - } - skip(prot, field.type); - prot.readFieldEnd(); - } - prot.readStructEnd(); - - case TType.MAP: - var map:TMap = prot.readMapBegin(); - for (i in 0 ... map.size) { - skip(prot, map.keyType); - skip(prot, map.valueType); - } - prot.readMapEnd(); - - case TType.SET: - var set:TSet = prot.readSetBegin(); - for (j in 0 ... set.size) { - skip(prot, set.elemType); - } - prot.readSetEnd(); - - case TType.LIST: - var list:TList = prot.readListBegin(); - for (k in 0 ... list.size) { - skip(prot, list.elemType); - } - prot.readListEnd(); - - default: - trace("Unknown field type ",type," in skipMaxDepth()"); - } - - prot.DecrementRecursionDepth(); - } - catch(e:Dynamic) - { - prot.DecrementRecursionDepth(); - throw e; - } + prot.IncrementRecursionDepth(); + try + { + switch (type) { + case TType.BOOL: + prot.readBool(); + + case TType.BYTE: + prot.readByte(); + + case TType.I16: + prot.readI16(); + + case TType.I32: + prot.readI32(); + + case TType.I64: + prot.readI64(); + + case TType.DOUBLE: + prot.readDouble(); + + case TType.STRING: + prot.readBinary(); + + case TType.STRUCT: + prot.readStructBegin(); + while (true) { + var field:TField = prot.readFieldBegin(); + if (field.type == TType.STOP) { + break; + } + skip(prot, field.type); + prot.readFieldEnd(); + } + prot.readStructEnd(); + + case TType.MAP: + var map:TMap = prot.readMapBegin(); + for (i in 0 ... map.size) { + skip(prot, map.keyType); + skip(prot, map.valueType); + } + prot.readMapEnd(); + + case TType.SET: + var set:TSet = prot.readSetBegin(); + for (j in 0 ... set.size) { + skip(prot, set.elemType); + } + prot.readSetEnd(); + + case TType.LIST: + var list:TList = prot.readListBegin(); + for (k in 0 ... list.size) { + skip(prot, list.elemType); + } + prot.readListEnd(); + + default: + trace("Unknown field type ",type," in skipMaxDepth()"); + } + + prot.DecrementRecursionDepth(); + } + catch(e:Dynamic) + { + prot.DecrementRecursionDepth(); + throw e; + } } } diff --git a/lib/haxe/src/org/apache/thrift/protocol/TRecursionTracker.hx b/lib/haxe/src/org/apache/thrift/protocol/TRecursionTracker.hx index b882cf21f..cf0211b39 100644 --- a/lib/haxe/src/org/apache/thrift/protocol/TRecursionTracker.hx +++ b/lib/haxe/src/org/apache/thrift/protocol/TRecursionTracker.hx @@ -24,25 +24,25 @@ import org.apache.thrift.*; class TRecursionTracker { - // default - private static inline var DEFAULT_RECURSION_DEPTH : Int = 64; + // default + private static inline var DEFAULT_RECURSION_DEPTH : Int = 64; - // limit and actual value - public var recursionLimit : Int = DEFAULT_RECURSION_DEPTH; + // limit and actual value + public var recursionLimit : Int = DEFAULT_RECURSION_DEPTH; private var recursionDepth : Int = 0; - public function IncrementRecursionDepth() : Void - { - if (recursionDepth < recursionLimit) - ++recursionDepth; - else - throw new TProtocolException(TProtocolException.DEPTH_LIMIT, "Depth limit exceeded"); - } - - public function DecrementRecursionDepth() : Void - { - --recursionDepth; - } + public function IncrementRecursionDepth() : Void + { + if (recursionDepth < recursionLimit) + ++recursionDepth; + else + throw new TProtocolException(TProtocolException.DEPTH_LIMIT, "Depth limit exceeded"); + } + + public function DecrementRecursionDepth() : Void + { + --recursionDepth; + } } diff --git a/lib/haxe/src/org/apache/thrift/transport/TServerSocket.hx b/lib/haxe/src/org/apache/thrift/transport/TServerSocket.hx index 586a8b6bf..4badb2a1f 100644 --- a/lib/haxe/src/org/apache/thrift/transport/TServerSocket.hx +++ b/lib/haxe/src/org/apache/thrift/transport/TServerSocket.hx @@ -39,26 +39,22 @@ class TServerSocket extends TServerTransport { // Underlying server with socket private var _socket : Socket= null; - // Port to listen on - private var _port : Int = 0; - // Timeout for client sockets from accept - private var _clientTimeout : Float = 0; + private var _clientTimeout : Float = 5; // Whether or not to wrap new TSocket connections in buffers private var _useBufferedSockets : Bool = false; - public function new( port : Int, clientTimeout : Float = 0, useBufferedSockets : Bool = false) + public function new(?address : String = 'localhost', port : Int, clientTimeout : Float = 5, useBufferedSockets : Bool = false) { - _port = port; _clientTimeout = clientTimeout; _useBufferedSockets = useBufferedSockets; try { _socket = new Socket(); - _socket.bind( new Host('localhost'), port); + _socket.bind( new Host(address), port); } catch (e : Dynamic) { @@ -74,7 +70,9 @@ class TServerSocket extends TServerTransport { if (_socket != null) { try { + #if !php _socket.listen(1); + #end } catch (e : Dynamic) { @@ -94,7 +92,7 @@ class TServerSocket extends TServerTransport { { var accepted = _socket.accept(); var result = TSocket.fromSocket(accepted); - accepted.setTimeout( _clientTimeout); + result.setTimeout( _clientTimeout); if( _useBufferedSockets) { diff --git a/lib/haxe/src/org/apache/thrift/transport/TSocket.hx b/lib/haxe/src/org/apache/thrift/transport/TSocket.hx index 8aa060865..7941ab9fe 100644 --- a/lib/haxe/src/org/apache/thrift/transport/TSocket.hx +++ b/lib/haxe/src/org/apache/thrift/transport/TSocket.hx @@ -73,7 +73,7 @@ class TSocket extends TTransport { private var output : Output = null; #end - private static inline var DEFAULT_TIMEOUT = 5.0; + private var timeout : Float = 30; private var obuffer : BytesOutput = new BytesOutput(); private var ioCallback : TException->Void = null; @@ -92,7 +92,8 @@ class TSocket extends TTransport { #if ! (flash || js) // used by TSocketServer public static function fromSocket( socket : Socket) : TSocket { - var result = new TSocket("",0); + var socketHost = socket.host(); + var result = new TSocket(socketHost.host.toString(), socketHost.port); result.assignSocket(socket); return result; } @@ -240,7 +241,7 @@ class TSocket extends TTransport { } catch (e : TException) { - trace('TException $e'); + trace('TException $e, message : ${e.errorMsg}'); if(ioCallback != null) { ioCallback(e); } @@ -270,11 +271,17 @@ class TSocket extends TTransport { var socket = new Socket(); socket.connect(host, port); + #elseif php + var socket = new Socket(); + socket.connect(host, port); + socket.setBlocking(true); + socket.setTimeout(timeout); + #else var socket = new Socket(); socket.setBlocking(true); socket.setFastSend(true); - socket.setTimeout( DEFAULT_TIMEOUT); + socket.setTimeout(timeout); socket.connect(host, port); #end @@ -301,4 +308,11 @@ class TSocket extends TTransport { #end } + public function setTimeout( timeout : Float ) : Void { + if(isOpen()) { + socket.setTimeout(timeout); + } + this.timeout = timeout; + } + } |