phpsocks5/Workerman/Protocols/Text.php

71 lines
1.6 KiB
PHP
Raw Normal View History

2016-09-20 21:27:41 +08:00
<?php
/**
* This file is part of workerman.
*
* Licensed under The MIT License
* For full copyright and license information, please see the MIT-LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author walkor<walkor@workerman.net>
* @copyright walkor<walkor@workerman.net>
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
2015-04-04 21:46:31 +08:00
namespace Workerman\Protocols;
2016-09-20 21:27:41 +08:00
use Workerman\Connection\TcpConnection;
2015-04-04 21:46:31 +08:00
/**
2016-09-20 21:27:41 +08:00
* Text Protocol.
2015-04-04 21:46:31 +08:00
*/
class Text
{
/**
2016-09-20 21:27:41 +08:00
* Check the integrity of the package.
*
* @param string $buffer
* @param TcpConnection $connection
* @return int
2015-04-04 21:46:31 +08:00
*/
2016-09-20 21:27:41 +08:00
public static function input($buffer, TcpConnection $connection)
2015-04-04 21:46:31 +08:00
{
2016-09-20 21:27:41 +08:00
// Judge whether the package length exceeds the limit.
if (strlen($buffer) >= TcpConnection::$maxPackageSize) {
2015-04-04 21:46:31 +08:00
$connection->close();
return 0;
}
2016-09-20 21:27:41 +08:00
// Find the position of "\n".
2015-04-04 21:46:31 +08:00
$pos = strpos($buffer, "\n");
2016-09-20 21:27:41 +08:00
// No "\n", packet length is unknown, continue to wait for the data so return 0.
if ($pos === false) {
2015-04-04 21:46:31 +08:00
return 0;
}
2016-09-20 21:27:41 +08:00
// Return the current package length.
return $pos + 1;
2015-04-04 21:46:31 +08:00
}
2016-09-20 21:27:41 +08:00
2015-04-04 21:46:31 +08:00
/**
2016-09-20 21:27:41 +08:00
* Encode.
*
2015-04-04 21:46:31 +08:00
* @param string $buffer
* @return string
*/
public static function encode($buffer)
{
2016-09-20 21:27:41 +08:00
// Add "\n"
return $buffer . "\n";
2015-04-04 21:46:31 +08:00
}
2016-09-20 21:27:41 +08:00
2015-04-04 21:46:31 +08:00
/**
2016-09-20 21:27:41 +08:00
* Decode.
*
2015-04-04 21:46:31 +08:00
* @param string $buffer
* @return string
*/
public static function decode($buffer)
{
2016-09-20 21:27:41 +08:00
// Remove "\n"
2015-04-04 21:46:31 +08:00
return trim($buffer);
}
}