TeamSpeak 3 PHP Framework
Modern use-at-will framework that provides individual components to manage TeamSpeak 3 Server instances
TeamSpeak3_Transport_TCP Class Reference

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

+ Inheritance diagram for TeamSpeak3_Transport_TCP:

Public Member Functions

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

Additional Inherited Members

- Protected Member Functions inherited from TeamSpeak3_Transport_Abstract
 waitForReadyRead ($time=0)
 Blocks a stream until data is available for reading if the stream is connected in non-blocking mode. More...
 

Detailed Description

Class for connecting to a remote server through TCP.

Member Function Documentation

◆ connect()

connect ( )

Connects to a remote server.

Exceptions
TeamSpeak3_Transport_Exception
Returns
void
38  {
39  if($this->stream !== null) return;
40 
41  $host = strval($this->config["host"]);
42  $port = strval($this->config["port"]);
43 
44  $address = "tcp://" . (strstr($host, ":") !== FALSE ? "[" . $host . "]" : $host) . ":" . $port;
45  $timeout = (int) $this->config["timeout"];
46 
47  $this->stream = @stream_socket_client($address, $errno, $errstr, $timeout);
48 
49  if($this->stream === FALSE)
50  {
51  throw new TeamSpeak3_Transport_Exception(TeamSpeak3_Helper_String::factory($errstr)->toUtf8()->toString(), $errno);
52  }
53 
54  @stream_set_timeout($this->stream, $timeout);
55  @stream_set_blocking($this->stream, $this->config["blocking"] ? 1 : 0);
56  }
Enhanced exception class for TeamSpeak3_Transport_Abstract objects.
Definition: Exception.php:29
static factory($string)
Returns a TeamSpeak3_Helper_String object for thegiven string.
Definition: String.php:60

◆ disconnect()

disconnect ( )

Disconnects from a remote server.

Returns
void
64  {
65  if($this->stream === null) return;
66 
67  $this->stream = null;
68 
69  TeamSpeak3_Helper_Signal::getInstance()->emit(strtolower($this->getAdapterType()) . "Disconnected");
70  }
static getInstance()
Returns a singleton instance of TeamSpeak3_Helper_Signal.
Definition: Signal.php:201
getAdapterType()
Returns the adapter type.
Definition: Abstract.php:204

◆ read()

read (   $length = 4096)

Reads data from the stream.

Parameters
integer$length
Exceptions
TeamSpeak3_Transport_Exception
Returns
TeamSpeak3_Helper_String
80  {
81  $this->connect();
82  $this->waitForReadyRead();
83 
84  $data = @stream_get_contents($this->stream, $length);
85 
86  TeamSpeak3_Helper_Signal::getInstance()->emit(strtolower($this->getAdapterType()) . "DataRead", $data);
87 
88  if($data === FALSE)
89  {
90  throw new TeamSpeak3_Transport_Exception("connection to server '" . $this->config["host"] . ":" . $this->config["port"] . "' lost");
91  }
92 
93  return new TeamSpeak3_Helper_String($data);
94  }
waitForReadyRead($time=0)
Blocks a stream until data is available for reading if the stream is connected in non-blocking mode...
Definition: Abstract.php:249
Helper class for string handling.
Definition: String.php:29
Enhanced exception class for TeamSpeak3_Transport_Abstract objects.
Definition: Exception.php:29
static getInstance()
Returns a singleton instance of TeamSpeak3_Helper_Signal.
Definition: Signal.php:201
getAdapterType()
Returns the adapter type.
Definition: Abstract.php:204
connect()
Connects to a remote server.
Definition: TCP.php:37

◆ readLine()

readLine (   $token = "\n")

Reads a single line of data from the stream.

Parameters
string$token
Exceptions
TeamSpeak3_Transport_Exception
Returns
TeamSpeak3_Helper_String
104  {
105  $this->connect();
106 
108 
109  while(!$line->endsWith($token))
110  {
111  $this->waitForReadyRead();
112 
113  $data = @fgets($this->stream, 4096);
114 
115  TeamSpeak3_Helper_Signal::getInstance()->emit(strtolower($this->getAdapterType()) . "DataRead", $data);
116 
117  if($data === FALSE)
118  {
119  if($line->count())
120  {
121  $line->append($token);
122  }
123  else
124  {
125  throw new TeamSpeak3_Transport_Exception("connection to server '" . $this->config["host"] . ":" . $this->config["port"] . "' lost");
126  }
127  }
128  else
129  {
130  $line->append($data);
131  }
132  }
133 
134  return $line->trim();
135  }
waitForReadyRead($time=0)
Blocks a stream until data is available for reading if the stream is connected in non-blocking mode...
Definition: Abstract.php:249
Enhanced exception class for TeamSpeak3_Transport_Abstract objects.
Definition: Exception.php:29
static getInstance()
Returns a singleton instance of TeamSpeak3_Helper_Signal.
Definition: Signal.php:201
getAdapterType()
Returns the adapter type.
Definition: Abstract.php:204
static factory($string)
Returns a TeamSpeak3_Helper_String object for thegiven string.
Definition: String.php:60
connect()
Connects to a remote server.
Definition: TCP.php:37

◆ send()

send (   $data)

Writes data to the stream.

Parameters
string$data
Returns
void
144  {
145  $this->connect();
146 
147  @stream_socket_sendto($this->stream, $data);
148 
149  TeamSpeak3_Helper_Signal::getInstance()->emit(strtolower($this->getAdapterType()) . "DataSend", $data);
150  }
static getInstance()
Returns a singleton instance of TeamSpeak3_Helper_Signal.
Definition: Signal.php:201
getAdapterType()
Returns the adapter type.
Definition: Abstract.php:204
connect()
Connects to a remote server.
Definition: TCP.php:37

◆ sendLine()

sendLine (   $data,
  $separator = "\n" 
)

Writes a line of data to the stream.

Parameters
string$data
string$separator
Returns
void
160  {
161  $size = strlen($data);
162  $pack = 4096;
163 
164  for($seek = 0 ;$seek < $size;)
165  {
166  $rest = $size-$seek;
167  $pack = $rest < $pack ? $rest : $pack;
168  $buff = substr($data, $seek, $pack);
169  $seek = $seek+$pack;
170 
171  if($seek >= $size) $buff .= $separator;
172 
173  $this->send($buff);
174  }
175  }
send($data)
Writes data to the stream.
Definition: TCP.php:143

The documentation for this class was generated from the following file: