<?php 
 | 
namespace Socket; 
 | 
#´´½¨ÈÕÆÚ£º2012-5-16 ÏÂÎç1:26:01 
 | 
#×÷Õߣºzhouliang 
 | 
  
 | 
const nSingleRecvMaxLen=1000; #×î´ó½ÓÊÕ³¤¶È  
 | 
  
 | 
class Socket 
 | 
{ 
 | 
    private $m_socket=null; 
 | 
    private $m_bConnect=false; 
 | 
    function __construct() 
 | 
    { 
 | 
    } 
 | 
     
 | 
    function __destruct() 
 | 
    { 
 | 
        socket_close( $this->m_socket ); 
 | 
    } 
 | 
     
 | 
    #½øÐгõʼ»¯,³É¹¦·µ»Øtrue£¬·ñÔò·µ»Øfalse 
 | 
    public function Init() 
 | 
    { 
 | 
        $this->m_socket = socket_create( AF_INET, SOCK_STREAM, getprotobyname("tcp") ); 
 | 
         
 | 
        if( !$this->m_socket ) 
 | 
        { 
 | 
            \Logging\LogInfo( '´´½¨socketʧ°Ü£¡' ); 
 | 
            return false; 
 | 
        } 
 | 
        else 
 | 
        { 
 | 
            socket_set_option( $this->m_socket,SOL_SOCKET,SO_RCVTIMEO,array("sec"=>30, "usec"=>0 ) ); 
 | 
            \Logging\LogInfo( '´´½¨socket³É¹¦£¡' ); 
 | 
            return true; 
 | 
        } 
 | 
    } 
 | 
     
 | 
    #socketÁ¬½Ó²Ù×÷£¬³É¹¦Ôò·µ»Øtrue,ʧ°Ü·µ»Øfalse 
 | 
    public function Connect( $strIP, $strPort ) 
 | 
    { 
 | 
        if( !isset( $this->m_socket ) ) 
 | 
        { 
 | 
            \Logging\LogError( 'socket Î´´´½¨£¡' ); 
 | 
            return false; 
 | 
        } 
 | 
         
 | 
        if( $this->m_bConnect ) 
 | 
        { 
 | 
            \Logging\LogInfo( 'socketÒѾÁ¬½Ó' ); 
 | 
            return true; 
 | 
        } 
 | 
         
 | 
        if( !socket_connect( $this->m_socket, $strIP, $strPort ) ) 
 | 
        { 
 | 
            \Logging\LogError( "Á¬½Ó·þÎñÆ÷ʧ°Ü IP:$strIP Port:$strPort, ´íÎóÔÒò:" 
 | 
                    .socket_last_error( $this->m_socket ) ); 
 | 
            $this->m_bConnect=false; 
 | 
            return false; 
 | 
        } 
 | 
        else 
 | 
        { 
 | 
            \Logging\LogInfo( "socket Á¬½Ó³É¹¦, IP:$strIP Port:$strPort" ); 
 | 
            $this->m_bConnect=true; 
 | 
            return true; 
 | 
        } 
 | 
         
 | 
        $this->m_bConnect=true; 
 | 
    } 
 | 
     
 | 
    #socket·¢ËͲÙ×÷£¬³É¹¦·µ»Øtrue,ʧ°Ü·µ»Øfalse 
 | 
    public function Write( &$buffer ) 
 | 
    { 
 | 
        if( !$this->IsConnected() ) 
 | 
        { 
 | 
            \Logging\LogError( "socket»¹Î´Á¬½Ó²»ÄܽøÐÐд²Ù×÷£¡" ); 
 | 
            return false; 
 | 
        } 
 | 
        if( false===socket_write( $this->m_socket, $buffer ) ) 
 | 
        { 
 | 
         
 | 
            \Logging\LogError( "·¢ËÍÊý¾Ýʧ°Ü!´íÎóÂë:".socket_last_error( $this->m_socket ) ); 
 | 
            return false; 
 | 
        } 
 | 
        else 
 | 
        { 
 | 
            \Logging\LogInfo( '·¢ËÍÊý¾Ý³É¹¦!' ); 
 | 
            return true; 
 | 
        } 
 | 
    } 
 | 
     
 | 
    #socket½ÓÊÕ²Ù×÷£¬³É¹¦·µ»Øtrue,ʧ°Ü·µ»Øfalse 
 | 
    public function Read( &$buffer ) 
 | 
    { 
 | 
        if( !$this->IsConnected() ) 
 | 
        { 
 | 
            \Logging\LogError( 'socket»¹Î´´´½¨£¬²»ÄܽøÐжÁ²Ù×÷£¡' ); 
 | 
            return false; 
 | 
        } 
 | 
         
 | 
        $buffer=socket_read( $this->m_socket, nSingleRecvMaxLen ); 
 | 
        if( !$buffer ) 
 | 
        { 
 | 
            $strLastError=socket_last_error( $this->m_socket ); 
 | 
            \Logging\LogError( '½ÓÊÕÊý¾Ýʧ°Ü£¡´íÎóÂë:'.$strLastError ); 
 | 
            return false; 
 | 
        } 
 | 
         
 | 
        return true; 
 | 
    } 
 | 
     
 | 
     
 | 
    #ÊÇ·ñÁ¬½Óµ½Ô¶¶Ë 
 | 
    public function IsConnected() 
 | 
    { 
 | 
        return $this->m_bConnect; 
 | 
    } 
 | 
} 
 | 
?> 
 |