<?php
/******************************************************************
 *
 *
 *
 *
 *
 *
 *
 *******************************************************************/
set_time_limit(0);
// @ini_set('implicit_flush', 1);
// ob_implicit_flush(1);
// @ob_end_clean();
function socketConnectToServerEver($_socketIP,$_socketPORT)
{		
	$_socketID = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);

	if($_socketID<0){
		echo "TcpConnectErr";
		// echo mb_convert_encoding(socket_strerror($_socketID),'UTF-8','GB2312');
		return;
	}
	
	// $result = socket_connect($_socketID,$_socketIP,intval($_socketPORT));
	// echo $result;
	//echo '2:'.$_socket;
	if(socket_connect($_socketID,$_socketIP,intval($_socketPORT))){
		$data = "require blueriver_api 2.14.0.0\r\n";
		socket_write($_socketID,$data, strlen($data));
		socket_read($_socketID,1024);
		$data = "Get All device\r\n";
		socket_write($_socketID,$data, strlen($data));
		$_data=socket_read($_socketID,1024);
		$_jsonarr = json_decode($_data,true);

		while (strstr($_data,'PROCESSING')) {
			$_data = '';
			$data = "request ".$_jsonarr['request_id']."\r\n";
			socket_write($_socketID,$data, strlen($data));
			while (!strstr(($_data .= socket_read($_socketID,1024)), "\r\n")) {
				
			}
		}
		echo $_data;
		//unsleep(0);
		//socket_set_block($GLOBALS['socket']);
		WriteToTxt('0','123','listen',$_socketIP,$_socketPORT);
		socket_close($_socketID);
	}
	else{
		echo "TcpConnectErr";
		// echo mb_convert_encoding(socket_strerror($_socketID),'UTF-8','GB2312');
		return;
	}
	
		
}

function socketConnectToServer($_socketIP,$_socketPORT)
{		
	//$_socketData = "111"."\r\n";	
	//$socket
	$GLOBALS['socket'] = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);

	if($GLOBALS['socket']<0){
		echo mb_convert_encoding(socket_strerror($GLOBALS['socket']),'UTF-8','GB2312');
		return;
	}
	
	$result = socket_connect($GLOBALS['socket'],$_socketIP,intval($_socketPORT));
	//echo '2:'.$_socket;
	if($result<0){
		echo mb_convert_encoding(socket_strerror($GLOBALS['socket']),'UTF-8','GB2312');
		return;
	}
}

function phpSocketDataSend($_socketData)
{		
	//require 'json.php'
	$socketData = "require blueriver_api 2.11.0.0\r\n";
	socket_write($GLOBALS['socket'],$socketData, strlen($socketData));
	socket_read($GLOBALS['socket'],1024);
	//sleep(1);
	$_socketData = str_replace('127.0.0.1', get_ipv4(), $_socketData);
	$_socketData = $_socketData."\r\n";
	if($num = socket_write($GLOBALS['socket'],$_socketData, strlen($_socketData))){
		$_data=socket_read($GLOBALS['socket'],1024);
			
		while (!strstr($_data, "\r\n")) {
			$_data.=socket_read($GLOBALS['socket'],1024);
	    }
	    $_json = json_decode($_data,true);
			
		if ($_json['status']=='PROCESSING') {	
			//sleep(1);
			$_data = 'request '.$_json['request_id']."\r\n";
			if($num = socket_write($GLOBALS['socket'],$_data, strlen($_data))){
				
				$_data=socket_read($GLOBALS['socket'],1024);	
				while (!strstr($_data, "\r\n")) {
					$_data.=socket_read($GLOBALS['socket'],1024);
			    }				
			}		
			
		}

		echo $_data;
		
				
		// }else{
		// 	echo mb_convert_encoding(socket_strerror($GLOBALS['socket']),'UTF-8','GB2312');
		// 	echo $out;
		// }		
		
	}else{
		echo mb_convert_encoding(socket_strerror($GLOBALS['socket']),'UTF-8','GB2312');
		echo $num;
	}
	
	socket_close($GLOBALS['socket']);

}
// 获取IPv4地址
function get_ipv4(){
	if (isset($_ENV['HOSTNAME'])) {
		$MachineName = $_ENV['HOSTNAME'];
	}
	else if (isset($_ENV['COMPUTERNAME'])) {
		$MachineName = $_ENV['COMPUTERNAME'];
	}
	else{
		$MachineName = '';
	}
	return gethostbyname($MachineName);
}
function ConnectiSession()
{
	if(($out = socket_read($socket, 8888)))
		echo $out;
}
function WriteToTxt($num,$out,$type,$_socketIP,$_socketPORT){
	$fp = fopen("../socket_id.txt", 'w+b');
	if (!$fp) {
		echo 'cuowu';
		return;
	}
	fwrite($fp,$num.'::'.$out.'::'.$type.'::'.$_socketIP.'::'.$_socketPORT);
	fclose($fp);
}

function SocketConnect($_socketIP,$_socketPORT)
{	
	// set_time_limit(0);

	$fp = fsockopen("tcp://".$_socketIP,intval($_socketPORT),$errno,$errstr);

	if (!$fp) {
		echo $errstr($errno);
	}	
	else{
		stream_set_blocking($fp);
		$data = "require blueriver_api 2.12.0.0\r\n";
		if(!fwrite($fp,$data,strlen($data))){
			echo "exit";
		}
		fread($fp,1024);

	}
}
function SocketConnectSend($_data)
{
	$_dataTemp='123';
	//sleep(1);
	echo $_dataReturn;
	// $out = socket_read($GLOBALS['socket'],1024);
	// echo $GLOBALS['socket'];	
}

?>