34 const EOL_WIN =
"\r\n";
37 const EOL_OLD_MAC =
"\r";
40 const EOL_UNIX =
"\n";
43 const EOL_NEW_MAC =
"\n";
87 $this->setSocket($socket);
99 $this->socket = $socket;
100 $this->incomingData =
"";
101 $this->rcvQueue = array();
102 $this->sndQueue = array();
115 return $this->socket;
138 if (!in_array($eol, $eols)) {
139 throw new \Erebot\InvalidValueException(
'Invalid EOL mode');
141 if ($eol === self::EOL_ANY) {
142 $eol = array(
"\r\n",
"\r",
"\n");
178 foreach ($this->eol as $eol) {
179 $pos = strpos($this->incomingData, $eol);
180 if ($pos !==
false) {
184 if ($pos ===
false) {
189 $line = \Erebot\Utils::toUTF8(substr($this->incomingData, 0, $pos));
190 $this->incomingData = substr($this->incomingData, $pos + $len);
191 $this->rcvQueue[] = $line;
193 $logger = \Plop\Plop::getInstance();
196 array(
'line' => addcslashes($line,
"\000..\037"))
218 if ($this->socket === null) {
222 if (feof($this->socket)) {
226 $received = fread($this->socket, 4096);
227 if ($received ===
false) {
230 $this->incomingData .= $received;
233 $metadata = stream_get_meta_data($this->socket);
234 if ($metadata[
'stream_type'] ==
'tcp_socket/ssl' && !feof($this->socket)) {
235 $blocking = (int) $metadata[
'blocked'];
236 stream_set_blocking($this->socket, 0);
237 $received = fread($this->socket, 4096);
238 stream_set_blocking($this->socket, $blocking);
240 if ($received !==
false) {
241 $this->incomingData .= $received;
246 while ($this->getLine()) {
264 if (count($this->rcvQueue)) {
265 return array_shift($this->rcvQueue);
281 if ($this->socket === null) {
282 throw new \Erebot\IllegalActionException(
'Uninitialized socket');
285 if (strcspn($line,
"\r\n") != strlen($line)) {
286 throw new \Erebot\InvalidValueException(
287 'Line contains forbidden characters'
290 $this->sndQueue[] = $line;
308 if (!count($this->sndQueue)) {
312 $line = array_shift($this->sndQueue);
313 $logger = \Plop\Plop::getInstance();
317 $eol = $this->eol[count($this->eol) - 1];
319 $len = strlen($line);
320 for ($written = 0; $written < $len; $written += $fwrite) {
321 $fwrite = @fwrite($this->socket, substr($line, $written));
322 if ($fwrite ===
false) {
326 $line = substr($line, 0, -strlen($eol));
329 array(
'line' => addcslashes($line,
"\000..\037"))
346 return count($this->rcvQueue);
361 return count($this->sndQueue);
$rcvQueue
A FIFO queue for incoming messages.
$sndQueue
A FIFO queue for outgoing messages.
$eol
Line-ending mode in use.
A class that provides a line-by-line reader.
$socket
The underlying socket, represented as a stream.
__construct($eol, $socket=null)
$incomingData
A raw buffer for incoming data.