Skip to content

Commit

Permalink
fix tcp package over 65535
Browse files Browse the repository at this point in the history
  • Loading branch information
xingwenge committed Jul 23, 2019
1 parent 5fd7aa5 commit 51baf89
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 35 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ try {
$conn->disConnect();
} catch (\Exception $e) {
echo $e->getMessage(), PHP_EOL;
}
````

![运行效果图](assets/effect.gif)
Expand Down
5 changes: 0 additions & 5 deletions init.php

This file was deleted.

5 changes: 4 additions & 1 deletion src/sample/socket.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@

require_once __DIR__. '/../../vendor/autoload.php';

ini_set('display_errors', 'On');
error_reporting(E_ALL);

try {
$conn = new CanalConnector();
$conn->connect("127.0.0.1", 11111, 10, 1800, 1800);
$conn->checkValid();
$conn->subscribe("example", ".*\\..*");

while (true) {
$message = $conn->get(10);
$message = $conn->get(100);
$entries = $message->getEntries();
if ($entries) {
foreach ($entries as $entry) {
Expand Down
3 changes: 2 additions & 1 deletion src/socket/CanalConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ public function checkValid($username="", $password="")
private function readNextPacket()
{
$data = $this->socket->read($this->packetLen);
return $this->socket->read(unpack("N", $data)[1]);
$dataLen = unpack("N", $data)[1];
return $this->socket->read($dataLen);
}

/**
Expand Down
29 changes: 1 addition & 28 deletions src/socket/TcpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public function close()
* @return string Binary data
* @throws \Exception
*/
public function readAll($len)
public function read($len)
{
if ($this->sendTimeoutSet_) {
stream_set_timeout($this->handle_, 0, $this->recvTimeout * 1000000);
Expand Down Expand Up @@ -241,33 +241,6 @@ public function readAll($len)
}
}

/**
* Read from the socket
*
* @param int $len
* How many bytes
* @return string Binary data
* @throws \Exception
*
*/
public function read($len)
{
if ($this->sendTimeoutSet_) {
stream_set_timeout($this->handle_, 0, $this->recvTimeout * 1000000);
$this->sendTimeoutSet_ = false;
}
$data = fread($this->handle_, $len);
if ($data === false || $data === '') {
$md = stream_get_meta_data($this->handle_);
if ($md['timed_out']) {
throw new \Exception('TSocket: timed out reading ' . $len . ' bytes from ' . $this->host_ . ':' . $this->port_);
} else {
throw new \Exception('TSocket: Could not read ' . $len . ' bytes from ' . $this->host_ . ':' . $this->port_);
}
}
return $data;
}

/**
* @param string|false $buf Binary data
* @throws \Exception
Expand Down

0 comments on commit 51baf89

Please sign in to comment.