added new socket manager, errorhandling and testing is not finished yet

This commit is contained in:
kremsy
2015-06-21 20:43:18 +02:00
parent eaf8819a57
commit fc5a3e04b6
32 changed files with 2627 additions and 0 deletions

View File

@ -0,0 +1,42 @@
<?php
namespace React\Socket;
use React\Stream\Stream;
class Connection extends Stream implements ConnectionInterface
{
public function handleData($stream)
{
// Socket is raw, not using fread as it's interceptable by filters
// See issues #192, #209, and #240
$data = stream_socket_recvfrom($stream, $this->bufferSize);
if ('' !== $data && false !== $data) {
$this->emit('data', array($data, $this));
}
if ('' === $data || false === $data || !is_resource($stream) || feof($stream)) {
$this->end();
}
}
public function handleClose()
{
if (is_resource($this->stream)) {
// http://chat.stackoverflow.com/transcript/message/7727858#7727858
stream_socket_shutdown($this->stream, STREAM_SHUT_RDWR);
stream_set_blocking($this->stream, false);
fclose($this->stream);
}
}
public function getRemoteAddress()
{
return $this->parseAddress(stream_socket_get_name($this->stream, true));
}
private function parseAddress($address)
{
return trim(substr($address, 0, strrpos($address, ':')), '[]');
}
}