TeamSpeak 3 PHP Framework  1.1.12
libraries/TeamSpeak3/Transport/TCP.php
Go to the documentation of this file.
00001 <?php
00002 
00003 /**
00004  * @file
00005  * TeamSpeak 3 PHP Framework
00006  *
00007  * $Id: TCP.php 2/18/2012 12:42:46 scp@orilla $
00008  *
00009  * This program is free software: you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation, either version 3 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00021  *
00022  * @package   TeamSpeak3
00023  * @version   1.1.12
00024  * @author    Sven 'ScP' Paulsen
00025  * @copyright Copyright (c) 2010 by Planet TeamSpeak. All rights reserved.
00026  */
00027 
00028 /**
00029  * @class TeamSpeak3_Transport_TCP
00030  * @brief Class for connecting to a remote server through TCP.
00031  */
00032 class TeamSpeak3_Transport_TCP extends TeamSpeak3_Transport_Abstract
00033 {
00034   /**
00035    * Connects to a remote server.
00036    *
00037    * @throws TeamSpeak3_Transport_Exception
00038    * @return void
00039    */
00040   public function connect()
00041   {
00042     if($this->stream !== null) return;
00043 
00044     $host = strval($this->config["host"]);
00045     $port = strval($this->config["port"]);
00046 
00047     $address = "tcp://" . $host . ":" . $port;
00048     $timeout = intval($this->config["timeout"]);
00049 
00050     $this->stream = @stream_socket_client($address, $errno, $errstr, $timeout);
00051 
00052     if($this->stream === FALSE)
00053     {
00054       throw new TeamSpeak3_Transport_Exception(utf8_encode($errstr), $errno);
00055     }
00056 
00057     @stream_set_timeout($this->stream, $timeout);
00058     @stream_set_blocking($this->stream, $this->config["blocking"] ? 1 : 0);
00059   }
00060 
00061   /**
00062    * Disconnects from a remote server.
00063    *
00064    * @return void
00065    */
00066   public function disconnect()
00067   {
00068     if($this->stream === null) return;
00069 
00070     $this->stream = null;
00071 
00072     TeamSpeak3_Helper_Signal::getInstance()->emit(strtolower($this->getAdapterType()) . "Disconnected");
00073   }
00074 
00075   /**
00076    * Reads data from the stream.
00077    *
00078    * @param  integer $length
00079    * @throws TeamSpeak3_Transport_Exception
00080    * @return TeamSpeak3_Helper_String
00081    */
00082   public function read($length = 4096)
00083   {
00084     $this->connect();
00085     $this->waitForReadyRead();
00086 
00087     $data = @stream_get_contents($this->stream, $length);
00088 
00089     TeamSpeak3_Helper_Signal::getInstance()->emit(strtolower($this->getAdapterType()) . "DataRead", $data);
00090 
00091     if($data === FALSE)
00092     {
00093       throw new TeamSpeak3_Transport_Exception("connection to server '" . $this->config["host"] . ":" . $this->config["port"] . "' lost");
00094     }
00095 
00096     return new TeamSpeak3_Helper_String($data);
00097   }
00098 
00099   /**
00100    * Reads a single line of data from the stream.
00101    *
00102    * @param  string $token
00103    * @throws TeamSpeak3_Transport_Exception
00104    * @return TeamSpeak3_Helper_String
00105    */
00106   public function readLine($token = "\n")
00107   {
00108     $this->connect();
00109 
00110     $line = TeamSpeak3_Helper_String::factory("");
00111 
00112     while(!$line->endsWith($token))
00113     {
00114       $this->waitForReadyRead();
00115 
00116       $data = @fgets($this->stream, 4096);
00117 
00118       TeamSpeak3_Helper_Signal::getInstance()->emit(strtolower($this->getAdapterType()) . "DataRead", $data);
00119 
00120       if($data === FALSE)
00121       {
00122         if($line->count())
00123         {
00124           $line->append($token);
00125         }
00126         else
00127         {
00128           throw new TeamSpeak3_Transport_Exception("connection to server '" . $this->config["host"] . ":" . $this->config["port"] . "' lost");
00129         }
00130       }
00131       else
00132       {
00133         $line->append($data);
00134       }
00135     }
00136 
00137     return $line->trim();
00138   }
00139 
00140   /**
00141    * Writes data to the stream.
00142    *
00143    * @param  string $data
00144    * @return void
00145    */
00146   public function send($data)
00147   {
00148     $this->connect();
00149 
00150     @stream_socket_sendto($this->stream, $data);
00151 
00152     TeamSpeak3_Helper_Signal::getInstance()->emit(strtolower($this->getAdapterType()) . "DataSend", $data);
00153   }
00154 
00155   /**
00156    * Writes a line of data to the stream.
00157    *
00158    * @param  string $data
00159    * @param  string $separator
00160    * @return void
00161    */
00162   public function sendLine($data, $separator = "\n")
00163   {
00164     $size = strlen($data);
00165     $pack = 4096;
00166 
00167     for($seek = 0 ;$seek < $size;)
00168     {
00169       $rest = $size-$seek;
00170       $pack = $rest < $pack ? $rest : $pack;
00171       $buff = substr($data, $seek, $pack);
00172       $seek = $seek+$pack;
00173 
00174       if($seek >= $size) $buff .= $separator;
00175 
00176       $this->send($buff);
00177     }
00178   }
00179 }
 All Classes Files Functions Variables