TeamSpeak 3 PHP Framework  1.1.12
TeamSpeak3_Transport_TCP Class Reference

Class for connecting to a remote server through TCP. More...

Inheritance diagram for TeamSpeak3_Transport_TCP:
TeamSpeak3_Transport_Abstract

List of all members.

Public Member Functions

 connect ()
 Connects to a remote server.
 disconnect ()
 Disconnects from a remote server.
 read ($length=4096)
 Reads data from the stream.
 readLine ($token="\n")
 Reads a single line of data from the stream.
 send ($data)
 Writes data to the stream.
 sendLine ($data, $separator="\n")
 Writes a line of data to the stream.
 __sleep ()
 Commit pending data.
 __wakeup ()
 Reconnects to the remote server.
 getStream ()
 Returns the underlying stream resource.
 getConfig ($key=null, $default=null)
 Returns the configuration variables in this adapter.
 setAdapter (TeamSpeak3_Adapter_Abstract $adapter)
 Sets the TeamSpeak3_Adapter_Abstract object using this transport.
 getAdapter ()
 Returns the TeamSpeak3_Adapter_Abstract object using this transport.
 getAdapterType ()
 Returns the adapter type.
 getMetaData ()
 Returns header/meta data from stream pointer.
 isConnected ()
 Returns TRUE if the transport is connected.

Protected Member Functions

 waitForReadyRead ($time=0)
 Blocks a stream until data is available for reading if the stream is connected in non-blocking mode.

Protected Attributes

 $config = null
 $stream = null
 $adapter = null

Detailed Description

Class for connecting to a remote server through TCP.

Definition at line 32 of file TCP.php.


Member Function Documentation

Connects to a remote server.

Exceptions:
TeamSpeak3_Transport_Exception
Returns:
void

Reimplemented from TeamSpeak3_Transport_Abstract.

Definition at line 40 of file TCP.php.

Referenced by read(), readLine(), and send().

  {
    if($this->stream !== null) return;

    $host = strval($this->config["host"]);
    $port = strval($this->config["port"]);

    $address = "tcp://" . $host . ":" . $port;
    $timeout = intval($this->config["timeout"]);

    $this->stream = @stream_socket_client($address, $errno, $errstr, $timeout);

    if($this->stream === FALSE)
    {
      throw new TeamSpeak3_Transport_Exception(utf8_encode($errstr), $errno);
    }

    @stream_set_timeout($this->stream, $timeout);
    @stream_set_blocking($this->stream, $this->config["blocking"] ? 1 : 0);
  }

Disconnects from a remote server.

Returns:
void

Reimplemented from TeamSpeak3_Transport_Abstract.

Definition at line 66 of file TCP.php.

References TeamSpeak3_Transport_Abstract\getAdapterType(), and TeamSpeak3_Helper_Signal\getInstance().

  {
    if($this->stream === null) return;

    $this->stream = null;

    TeamSpeak3_Helper_Signal::getInstance()->emit(strtolower($this->getAdapterType()) . "Disconnected");
  }
TeamSpeak3_Transport_TCP::read ( length = 4096)

Reads data from the stream.

Parameters:
integer$length
Exceptions:
TeamSpeak3_Transport_Exception
Returns:
TeamSpeak3_Helper_String

Reimplemented from TeamSpeak3_Transport_Abstract.

Definition at line 82 of file TCP.php.

References connect(), TeamSpeak3_Transport_Abstract\getAdapterType(), TeamSpeak3_Helper_Signal\getInstance(), and TeamSpeak3_Transport_Abstract\waitForReadyRead().

  {
    $this->connect();
    $this->waitForReadyRead();

    $data = @stream_get_contents($this->stream, $length);

    TeamSpeak3_Helper_Signal::getInstance()->emit(strtolower($this->getAdapterType()) . "DataRead", $data);

    if($data === FALSE)
    {
      throw new TeamSpeak3_Transport_Exception("connection to server '" . $this->config["host"] . ":" . $this->config["port"] . "' lost");
    }

    return new TeamSpeak3_Helper_String($data);
  }

Reads a single line of data from the stream.

Parameters:
string$token
Exceptions:
TeamSpeak3_Transport_Exception
Returns:
TeamSpeak3_Helper_String

Definition at line 106 of file TCP.php.

References connect(), TeamSpeak3_Helper_String\factory(), TeamSpeak3_Transport_Abstract\getAdapterType(), TeamSpeak3_Helper_Signal\getInstance(), and TeamSpeak3_Transport_Abstract\waitForReadyRead().

  {
    $this->connect();

    $line = TeamSpeak3_Helper_String::factory("");

    while(!$line->endsWith($token))
    {
      $this->waitForReadyRead();

      $data = @fgets($this->stream, 4096);

      TeamSpeak3_Helper_Signal::getInstance()->emit(strtolower($this->getAdapterType()) . "DataRead", $data);

      if($data === FALSE)
      {
        if($line->count())
        {
          $line->append($token);
        }
        else
        {
          throw new TeamSpeak3_Transport_Exception("connection to server '" . $this->config["host"] . ":" . $this->config["port"] . "' lost");
        }
      }
      else
      {
        $line->append($data);
      }
    }

    return $line->trim();
  }

Writes data to the stream.

Parameters:
string$data
Returns:
void

Reimplemented from TeamSpeak3_Transport_Abstract.

Definition at line 146 of file TCP.php.

References connect(), TeamSpeak3_Transport_Abstract\getAdapterType(), and TeamSpeak3_Helper_Signal\getInstance().

Referenced by sendLine().

  {
    $this->connect();

    @stream_socket_sendto($this->stream, $data);

    TeamSpeak3_Helper_Signal::getInstance()->emit(strtolower($this->getAdapterType()) . "DataSend", $data);
  }
TeamSpeak3_Transport_TCP::sendLine ( data,
separator = "\n" 
)

Writes a line of data to the stream.

Parameters:
string$data
string$separator
Returns:
void

Definition at line 162 of file TCP.php.

References send().

  {
    $size = strlen($data);
    $pack = 4096;

    for($seek = 0 ;$seek < $size;)
    {
      $rest = $size-$seek;
      $pack = $rest < $pack ? $rest : $pack;
      $buff = substr($data, $seek, $pack);
      $seek = $seek+$pack;

      if($seek >= $size) $buff .= $separator;

      $this->send($buff);
    }
  }

Commit pending data.

Returns:
array

Definition at line 92 of file Abstract.php.

  {
    return array("config");
  }

Reconnects to the remote server.

Returns:
void

Definition at line 102 of file Abstract.php.

References TeamSpeak3_Transport_Abstract\connect().

  {
    $this->connect();
  }

Returns the underlying stream resource.

Returns:
resource

Definition at line 159 of file Abstract.php.

  {
    return $this->stream;
  }
TeamSpeak3_Transport_Abstract::getConfig ( key = null,
default = null 
) [inherited]

Returns the configuration variables in this adapter.

Parameters:
string$key
mixed$default
Returns:
array

Definition at line 171 of file Abstract.php.

  {
    if($key !== null)
    {
      return array_key_exists($key, $this->config) ? $this->config[$key] : $default;
    }

    return $this->config;
  }

Sets the TeamSpeak3_Adapter_Abstract object using this transport.

Parameters:
TeamSpeak3_Adapter_Abstract$adapter
Returns:
void

Definition at line 187 of file Abstract.php.

  {
    $this->adapter = $adapter;
  }

Returns the TeamSpeak3_Adapter_Abstract object using this transport.

Returns:
TeamSpeak3_Adapter_Abstract

Definition at line 197 of file Abstract.php.

Referenced by TeamSpeak3_Transport_Abstract\waitForReadyRead().

  {
    return $this->adapter;
  }

Returns the adapter type.

Returns:
string

Definition at line 207 of file Abstract.php.

References TeamSpeak3_Helper_String\factory().

Referenced by disconnect(), TeamSpeak3_Transport_UDP\disconnect(), read(), TeamSpeak3_Transport_UDP\read(), readLine(), TeamSpeak3_Transport_UDP\send(), send(), and TeamSpeak3_Transport_Abstract\waitForReadyRead().

  {
    if($this->adapter instanceof TeamSpeak3_Adapter_Abstract)
    {
      $string = TeamSpeak3_Helper_String::factory(get_class($this->adapter));

      return $string->substr($string->findLast("_"))->replace(array("_", " "), "")->toString();
    }

    return "Unknown";
  }

Returns header/meta data from stream pointer.

Exceptions:
TeamSpeak3_Transport_Exception
Returns:
array

Definition at line 225 of file Abstract.php.

  {
    if($this->stream === null)
    {
      throw new TeamSpeak3_Transport_Exception("unable to retrieve header/meta data from stream pointer");
    }

    return stream_get_meta_data($this->stream);
  }

Returns TRUE if the transport is connected.

Returns:
boolean

Definition at line 240 of file Abstract.php.

Referenced by TeamSpeak3_Adapter_ServerQuery\__destruct(), and TeamSpeak3_Transport_Abstract\waitForReadyRead().

  {
    return (is_resource($this->stream)) ? TRUE : FALSE;
  }
TeamSpeak3_Transport_Abstract::waitForReadyRead ( time = 0) [protected, inherited]

Blocks a stream until data is available for reading if the stream is connected in non-blocking mode.

Parameters:
integer$time
Returns:
void

Definition at line 252 of file Abstract.php.

References TeamSpeak3_Transport_Abstract\getAdapter(), TeamSpeak3_Transport_Abstract\getAdapterType(), TeamSpeak3_Helper_Signal\getInstance(), and TeamSpeak3_Transport_Abstract\isConnected().

Referenced by read(), TeamSpeak3_Transport_UDP\read(), and readLine().

  {
    if(!$this->isConnected() || $this->config["blocking"]) return;

    do {
      $read = array($this->stream);
      $null = null;

      if($time)
      {
        TeamSpeak3_Helper_Signal::getInstance()->emit(strtolower($this->getAdapterType()) . "WaitTimeout", $time, $this->getAdapter());
      }

      $time = $time+$this->config["timeout"];
    } while(@stream_select($read, $null, $null, $this->config["timeout"]) == 0);
  }

The documentation for this class was generated from the following file:
 All Classes Files Functions Variables