Skip to content

Commit

Permalink
Pre-release: updated copyrighted content
Browse files Browse the repository at this point in the history
  • Loading branch information
cclilshy committed Nov 13, 2024
1 parent 329d506 commit c45e3dd
Show file tree
Hide file tree
Showing 71 changed files with 1,807 additions and 2,914 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
"php": ">=8.1",
"ext-sockets": "*",
"ext-openssl": "*",
"revolt/event-loop": "^1.0",
"psr/http-message": "*"
"psr/http-message": "*",
"revolt/event-loop": "^1.0"
},
"require-dev": {
"ext-posix": "*",
Expand Down
4 changes: 2 additions & 2 deletions example/file.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php declare(strict_types=1);

use Co\IO;
use Ripple\File\Exception\FileException;
use Ripple\File\File;

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

try {
echo IO::File()->getContents(__FILE__), \PHP_EOL;
echo File::getContents(__FILE__), \PHP_EOL;
} catch (FileException $e) {
echo $e->getMessage(), \PHP_EOL;
exit(1);
Expand Down
4 changes: 2 additions & 2 deletions example/socket-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

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

use Co\IO;
use Ripple\Socket;
use Ripple\Stream\Exception\ConnectionException;
use Ripple\Utils\Output;

use function Co\wait;

try {
$connection = IO::Socket()->connect('tcp://127.0.0.1:1080');
$connection = Socket::connect('tcp://127.0.0.1:1080');

# Enable SSL
// $connection->enableSSL();
Expand Down
9 changes: 4 additions & 5 deletions example/socket-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

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

use Co\IO;
use Ripple\Socket\SocketStream;
use Ripple\Socket;

use function Co\wait;

$onMessage = static function (string $data, SocketStream $stream) {
$onMessage = static function (string $data, Socket $stream) {
$stream->write("Received: $data");
};

$listenClient = static function (SocketStream $stream) use ($onMessage) {
$listenClient = static function (Socket $stream) use ($onMessage) {
$stream->setBlocking(false);
$stream->onReadable(static function () use ($stream, $onMessage) {
$data = $stream->read(1024);
Expand All @@ -23,7 +22,7 @@
});
};

$server = IO::Socket()->server('tcp://127.0.0.1:9080');
$server = Socket::server('tcp://127.0.0.1:9080');
$server->setBlocking(false);
$server->setOption(\SOL_SOCKET, \SO_KEEPALIVE, 1);
$server->onReadable(fn () => $listenClient($server->accept()));
Expand Down
6 changes: 3 additions & 3 deletions example/socket-tunnel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

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

use Ripple\Socket\Tunnel\Socks5;
use Ripple\Stream\Exception\ConnectionException;
use Ripple\Tunnel\Socks5;
use Ripple\Utils\Output;

use function Co\wait;
Expand All @@ -15,7 +15,7 @@
'port' => 443
]);

$googleStream = $googleSocks5->getSocketStream();
$googleStream = $googleSocks5->getSocket();
$googleStream->enableSSL();
$googleStream->write("GET / HTTP/1.1\r\nHost: www.google.com\r\nConnection: close\r\n\r\n");
$googleStream->onReadable(function () use ($googleStream) {
Expand All @@ -42,7 +42,7 @@
]
);

$connection = $google->getSocketStream();
$connection = $google->getSocket();
$connection->enableSSL();
$connection->write("GET / HTTP/1.1\r\nHost: www.google.com\r\nConnection: close\r\n\r\n");
$connection->onReadable(function () use ($connection) {
Expand Down
118 changes: 48 additions & 70 deletions src/Channel.php → src/Channel/Channel.php
Original file line number Diff line number Diff line change
@@ -1,44 +1,24 @@
<?php declare(strict_types=1);
/*
* Copyright (c) 2023-2024.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* 特此免费授予任何获得本软件及相关文档文件(“软件”)副本的人,不受限制地处理
* 本软件,包括但不限于使用、复制、修改、合并、出版、发行、再许可和/或销售
* 软件副本的权利,并允许向其提供本软件的人做出上述行为,但须符合以下条件:
/**
* Copyright © 2024 cclilshy
* Email: [email protected]
*
* 上述版权声明和本许可声明应包含在本软件的所有副本或主要部分中。
* This software is licensed under the MIT License.
* For full license details, please visit: https://opensource.org/licenses/MIT
*
* 本软件按“原样”提供,不提供任何形式的保证,无论是明示或暗示的,
* 包括但不限于适销性、特定目的的适用性和非侵权性的保证。在任何情况下,
* 无论是合同诉讼、侵权行为还是其他方面,作者或版权持有人均不对
* 由于软件或软件的使用或其他交易而引起的任何索赔、损害或其他责任承担责任。
* By using this software, you agree to the terms of the license.
* Contributions, suggestions, and feedback are always welcome!
*/

namespace Ripple;
namespace Ripple\Channel;

use Exception;
use Ripple\Channel\Exception\ChannelException;
use Ripple\File\Lock\Lock;
use Ripple\File\Lock;
use Ripple\Kernel;
use Ripple\Stream;
use Ripple\Utils\Path;
use Ripple\Utils\Serialization\Zx7e;
use Ripple\Utils\Utils;
use Throwable;

use function chr;
Expand Down Expand Up @@ -88,11 +68,9 @@ class Channel
* @param string $name
* @param bool $owner
*/
public function __construct(
protected readonly string $name,
protected bool $owner = false
) {
$this->path = Utils::tempPath($this->name, 'channel');
public function __construct(protected readonly string $name, protected bool $owner = false)
{
$this->path = Path::temp($this->name, 'channel');
$this->readLock = \Co\lock("{$this->name}.read");
$this->writeLock = \Co\lock("{$this->name}.write");

Expand Down Expand Up @@ -120,6 +98,35 @@ public function __construct(
});
}

/**
* @return void
*/
protected function openStream(): void
{
$this->stream = new Stream(fopen($this->path, 'r+'));
$this->stream->setBlocking(false);
$this->zx7e = new Zx7e();
}

/*** @return void */
public function close(): void
{
if ($this->closed) {
return;
}

$this->stream->close();
$this->readLock->close();
$this->writeLock->close();

if ($this->owner) {
file_exists($this->path) && unlink($this->path);
}

$this->closed = true;
cancelForked($this->forkHandlerID);
}

/**
* @param string $name
*
Expand Down Expand Up @@ -153,16 +160,6 @@ public function send(mixed $data): bool
return true;
}

/**
* @return void
*/
protected function openStream(): void
{
$this->stream = new Stream(fopen($this->path, 'r+'));
$this->stream->setBlocking(false);
$this->zx7e = new Zx7e();
}

/**
* @param bool $blocking
*
Expand All @@ -177,8 +174,8 @@ public function receive(bool $blocking = true): mixed
while (1) {
try {
$blocking && $this->stream->waitForReadable();
} catch (Throwable $e) {
throw new ChannelException($e->getMessage());
} catch (Throwable $exception) {
throw new ChannelException($exception->getMessage());
}

if ($this->readLock->lock(blocking: false)) {
Expand Down Expand Up @@ -209,8 +206,8 @@ public function receive(bool $blocking = true): mixed
}

return unserialize($data);
} catch (Exception $e) {
throw new ChannelException($e->getMessage());
} catch (Exception $exception) {
throw new ChannelException($exception->getMessage());
} finally {
$this->readLock->unlock();
}
Expand All @@ -228,25 +225,6 @@ public function getPath(): string
return $this->path;
}

/*** @return void */
public function close(): void
{
if ($this->closed) {
return;
}

$this->stream->close();
$this->readLock->close();
$this->writeLock->close();

if ($this->owner) {
file_exists($this->path) && unlink($this->path);
}

$this->closed = true;
cancelForked($this->forkHandlerID);
}

public function __destruct()
{
$this->close();
Expand Down
36 changes: 7 additions & 29 deletions src/Channel/Exception/ChannelException.php
Original file line number Diff line number Diff line change
@@ -1,35 +1,13 @@
<?php declare(strict_types=1);
/*
* Copyright (c) 2023-2024.
/**
* Copyright © 2024 cclilshy
* Email: [email protected]
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* This software is licensed under the MIT License.
* For full license details, please visit: https://opensource.org/licenses/MIT
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* 特此免费授予任何获得本软件及相关文档文件(“软件”)副本的人,不受限制地处理
* 本软件,包括但不限于使用、复制、修改、合并、出版、发行、再许可和/或销售
* 软件副本的权利,并允许向其提供本软件的人做出上述行为,但须符合以下条件:
*
* 上述版权声明和本许可声明应包含在本软件的所有副本或主要部分中。
*
* 本软件按“原样”提供,不提供任何形式的保证,无论是明示或暗示的,
* 包括但不限于适销性、特定目的的适用性和非侵权性的保证。在任何情况下,
* 无论是合同诉讼、侵权行为还是其他方面,作者或版权持有人均不对
* 由于软件或软件的使用或其他交易而引起的任何索赔、损害或其他责任承担责任。
* By using this software, you agree to the terms of the license.
* Contributions, suggestions, and feedback are always welcome!
*/

namespace Ripple\Channel\Exception;
Expand Down
Empty file removed src/Channel/README.md
Empty file.
Loading

0 comments on commit c45e3dd

Please sign in to comment.