Browse Source

[php] fix Socket.setTimeout (#7623)

Looks good to me.
I hope we will get better sys tests some day :)
AlexHaxe 6 years ago
parent
commit
c3f1d3871a
5 changed files with 24 additions and 5 deletions
  1. 3 0
      std/php/Const.hx
  2. 1 1
      std/php/Global.hx
  3. 5 2
      std/php/_std/sys/net/Socket.hx
  4. 9 1
      std/php/net/Socket.hx
  5. 6 1
      std/sys/Http.hx

+ 3 - 0
std/php/Const.hx

@@ -168,6 +168,9 @@ extern class Const {
 	static var AF_INET6 : Int;
 	static var AF_INET6 : Int;
 	static var AF_UNIX : Int;
 	static var AF_UNIX : Int;
 	static var SOL_TCP : Int;
 	static var SOL_TCP : Int;
+	static var SOL_SOCKET : Int;
+	static var SO_RCVTIMEO : Int;
+	static var SO_SNDTIMEO : Int;
 	static var TCP_NODELAY : Int;
 	static var TCP_NODELAY : Int;
 	static var PHP_BINARY_READ : Int;
 	static var PHP_BINARY_READ : Int;
 	/**
 	/**

+ 1 - 1
std/php/Global.hx

@@ -1132,7 +1132,7 @@ extern class Global {
 	/**
 	/**
 		@see http://php.net/manual/en/function.socket-set-option.php
 		@see http://php.net/manual/en/function.socket-set-option.php
 	**/
 	**/
-	static function socket_set_option( stream:Resource, level:Int, option:Int, val:Bool ) : Bool;
+	static function socket_set_option( stream:Resource, level:Int, option:Int, val:Any ) : Bool;
 
 
 	/**
 	/**
 		@see http://php.net/manual/en/function.socket-select.php
 		@see http://php.net/manual/en/function.socket-select.php

+ 5 - 2
std/php/_std/sys/net/Socket.hx

@@ -123,8 +123,11 @@ class Socket {
 	public function setTimeout( timeout : Float ) : Void {
 	public function setTimeout( timeout : Float ) : Void {
 		var s = Std.int(timeout);
 		var s = Std.int(timeout);
 		var ms = Std.int((timeout - s) * 1000000);
 		var ms = Std.int((timeout - s) * 1000000);
-		var r = stream_set_timeout(__s, s, ms);
-		checkError(r, 0, 'Unable to set timeout');
+		var timeOut:NativeStructArray<{sec:Int, usec:Int}> = {sec: s, usec: ms};
+		var r = socket_set_option(__s, SOL_SOCKET, SO_RCVTIMEO, timeOut);
+		checkError(r, 0, 'Unable to set receive timeout');
+		r = socket_set_option(__s, SOL_SOCKET, SO_SNDTIMEO, timeOut);
+		checkError(r, 0, 'Unable to set send timeout');
 	}
 	}
 
 
 	public function setBlocking( b : Bool ) : Void {
 	public function setBlocking( b : Bool ) : Void {

+ 9 - 1
std/php/net/Socket.hx

@@ -124,13 +124,21 @@ class Socket extends sys.net.Socket {
 		checkError(r, 0, 'Unable to retrieve the host name');
 		checkError(r, 0, 'Unable to retrieve the host name');
 		return hpOfString(r);
 		return hpOfString(r);
 	}
 	}
+
+	override public function setTimeout( timeout : Float ) : Void {
+		var s = Std.int(timeout);
+		var ms = Std.int((timeout - s) * 1000000);
+		var r = stream_set_timeout(__s, s, ms);
+		checkError(r, 0, 'Unable to set timeout');
+	}
+
 	private static function getType(isUdp : Bool) : Int {
 	private static function getType(isUdp : Bool) : Int {
 		return isUdp ? SOCK_DGRAM : SOCK_STREAM;
 		return isUdp ? SOCK_DGRAM : SOCK_STREAM;
 	}
 	}
 
 
 	private static function getProtocol(protocol : String) : Int {
 	private static function getProtocol(protocol : String) : Int {
 		return getprotobyname(protocol);
 		return getprotobyname(protocol);
- 	}
+	}
 
 
 	public static function select(read : Array<Socket>, write : Array<Socket>, others : Array<Socket>, ?timeout : Float) : { read: Array<Socket>,write: Array<Socket>,others: Array<Socket> }
 	public static function select(read : Array<Socket>, write : Array<Socket>, others : Array<Socket>, ?timeout : Float) : { read: Array<Socket>,write: Array<Socket>,others: Array<Socket> }
 	{
 	{

+ 6 - 1
std/sys/Http.hx

@@ -97,8 +97,13 @@ class Http extends haxe.http.HttpBase {
 				#else
 				#else
 				throw "Https is only supported with -lib hxssl";
 				throw "Https is only supported with -lib hxssl";
 				#end
 				#end
-			} else
+			} else {
+				#if php
+				sock = new php.net.Socket();
+				#else
 				sock = new Socket();
 				sock = new Socket();
+				#end
+			}
 		}
 		}
 		var host = url_regexp.matched(2);
 		var host = url_regexp.matched(2);
 		var portString = url_regexp.matched(3);
 		var portString = url_regexp.matched(3);