This commit is contained in:
2020-10-10 11:05:57 +08:00
parent 3d62ec2c72
commit b8bdd7cc87
+19 -43
View File
@@ -15,17 +15,16 @@
namespace Kafka; namespace Kafka;
/** /**
+------------------------------------------------------------------------------ * +------------------------------------------------------------------------------
* Kafka broker socket * Kafka broker socket
+------------------------------------------------------------------------------ * +------------------------------------------------------------------------------
* *
* @package * @package
* @version $_SWANBR_VERSION_$ * @version $_SWANBR_VERSION_$
* @copyright Copyleft * @copyright Copyleft
* @author $_SWANBR_AUTHOR_$ * @author $_SWANBR_AUTHOR_$
+------------------------------------------------------------------------------ * +------------------------------------------------------------------------------
*/ */
class Socket class Socket
{ {
// {{{ consts // {{{ consts
@@ -169,7 +168,7 @@ class Socket
/** /**
* @param float $sendTimeoutSec * @param float $sendTimeoutSec
*/ */
public function setSendTimeoutSec($sendTimeoutSec) public function setSendTimeoutSec(float $sendTimeoutSec)
{ {
$this->sendTimeoutSec = $sendTimeoutSec; $this->sendTimeoutSec = $sendTimeoutSec;
} }
@@ -177,7 +176,7 @@ class Socket
/** /**
* @param float $sendTimeoutUsec * @param float $sendTimeoutUsec
*/ */
public function setSendTimeoutUsec($sendTimeoutUsec) public function setSendTimeoutUsec(float $sendTimeoutUsec)
{ {
$this->sendTimeoutUsec = $sendTimeoutUsec; $this->sendTimeoutUsec = $sendTimeoutUsec;
} }
@@ -185,7 +184,7 @@ class Socket
/** /**
* @param float $recvTimeoutSec * @param float $recvTimeoutSec
*/ */
public function setRecvTimeoutSec($recvTimeoutSec) public function setRecvTimeoutSec(float $recvTimeoutSec)
{ {
$this->recvTimeoutSec = $recvTimeoutSec; $this->recvTimeoutSec = $recvTimeoutSec;
} }
@@ -193,7 +192,7 @@ class Socket
/** /**
* @param float $recvTimeoutUsec * @param float $recvTimeoutUsec
*/ */
public function setRecvTimeoutUsec($recvTimeoutUsec) public function setRecvTimeoutUsec(float $recvTimeoutUsec)
{ {
$this->recvTimeoutUsec = $recvTimeoutUsec; $this->recvTimeoutUsec = $recvTimeoutUsec;
} }
@@ -201,13 +200,11 @@ class Socket
/** /**
* @param int $number * @param int $number
*/ */
public function setMaxWriteAttempts($number) public function setMaxWriteAttempts(int $number)
{ {
$this->maxWriteAttempts = $number; $this->maxWriteAttempts = $number;
} }
// }}}
// {{{ public function connect()
/** /**
* Connects the socket * Connects the socket
@@ -239,7 +236,7 @@ class Socket
if ($this->stream == false) { if ($this->stream == false) {
$error = 'Could not connect to ' $error = 'Could not connect to '
. $this->host . ':' . $this->port . $this->host . ':' . $this->port
. ' ('.$errstr.' ['.$errno.'])'; . ' (' . $errstr . ' [' . $errno . '])';
throw new \Kafka\Exception($error); throw new \Kafka\Exception($error);
} }
@@ -248,25 +245,24 @@ class Socket
$this->readWatcher = \Amp\onReadable($this->stream, function () { $this->readWatcher = \Amp\onReadable($this->stream, function () {
do { do {
if(!$this->isSocketDead($this->stream)){ if (!$this->isSocketDead()) {
$newData = @fread($this->stream, self::READ_MAX_LEN); $newData = @fread($this->stream, self::READ_MAX_LEN);
}else{ } else {
$this->reconnect(); $this->reconnect();
return; return;
} }
if ($newData) { if ($newData) {
$this->read($newData); $this->read($newData);
$newData = true;
} }
} while ($newData); } while ($newData);
}); });
$this->writeWatcher = \Amp\onWritable($this->stream, function () { $this->writeWatcher = \Amp\onWritable($this->stream, function () {
$this->write(); $this->write();
}, array('enable' => false)); // <-- let's initialize the watcher as "disabled" }, array('enable' => false));
} }
// }}}
// {{{ public function reconnect()
/** /**
* reconnect the socket * reconnect the socket
@@ -280,8 +276,6 @@ class Socket
$this->connect(); $this->connect();
} }
// }}}
// {{{ public function getSocket()
/** /**
* get the socket * get the socket
@@ -294,14 +288,14 @@ class Socket
return $this->stream; return $this->stream;
} }
// }}}
// {{{ public function SetOnReadable()
public $onReadable; public $onReadable;
/** /**
* set on readable callback function * set on readable callback function
* *
* @access public * @access public
* @param \Closure $read
* @return void * @return void
*/ */
public function SetOnReadable(\Closure $read) public function SetOnReadable(\Closure $read)
@@ -309,8 +303,6 @@ class Socket
$this->onReadable = $read; $this->onReadable = $read;
} }
// }}}
// {{{ public function close()
/** /**
* close the socket * close the socket
@@ -341,8 +333,6 @@ class Socket
return is_resource($this->stream); return is_resource($this->stream);
} }
// }}}
// {{{ public function read()
/** /**
* Read from the socket at most $len bytes. * Read from the socket at most $len bytes.
@@ -350,17 +340,13 @@ class Socket
* This method will not wait for all the requested data, it will return as * This method will not wait for all the requested data, it will return as
* soon as any data is received. * soon as any data is received.
* *
* @param integer $len Maximum number of bytes to read. * @param $data
* @param boolean $verifyExactLength Throw an exception if the number of read bytes is less than $len
*
* @return string Binary data
* @throws \Kafka\Exception\SocketEOF
*/ */
public function read($data) public function read($data)
{ {
$this->readBuffer .= $data; $this->readBuffer .= $data;
do { do {
if ($this->readNeedLength == 0) { // response start if ($this->readNeedLength == 0) {
if (strlen($this->readBuffer) < 4) { if (strlen($this->readBuffer) < 4) {
return; return;
} }
@@ -380,16 +366,11 @@ class Socket
} while (strlen($this->readBuffer)); } while (strlen($this->readBuffer));
} }
// }}}
// {{{ public function write()
/** /**
* Write to the socket. * Write to the socket.
* *
* @param string $buf The data to write * @param null $data
*
* @return integer
* @throws \Kafka\Exception\SocketEOF
*/ */
public function write($data = null) public function write($data = null)
{ {
@@ -403,14 +384,12 @@ class Socket
\Amp\disable($this->writeWatcher); \Amp\disable($this->writeWatcher);
} elseif ($bytesWritten >= 0) { } elseif ($bytesWritten >= 0) {
\Amp\enable($this->writeWatcher); \Amp\enable($this->writeWatcher);
} elseif ($this->isSocketDead($this->stream)) { } elseif ($this->isSocketDead()) {
$this->reconnect(); $this->reconnect();
} }
$this->writeBuffer = substr($this->writeBuffer, $bytesWritten); $this->writeBuffer = substr($this->writeBuffer, $bytesWritten);
} }
// }}}
// {{{ protected function isSocketDead()
/** /**
* check the stream is close * check the stream is close
@@ -421,7 +400,4 @@ class Socket
{ {
return !is_resource($this->stream) || @feof($this->stream); return !is_resource($this->stream) || @feof($this->stream);
} }
// }}}
// }}}
} }