phrity / net-stream
实现PSR-7 Stream和PSR-17 StreamFactory的Socket流类
2.1.0
2024-09-14 12:03 UTC
Requires
- php: ^8.0
- phrity/util-errorhandler: ^1.1
- psr/http-factory: ^1.0
- psr/http-message: ^1.1 | ^2.0
Requires (Dev)
- php-coveralls/php-coveralls: ^2.0
- phpunit/phpunit: ^9.0 | ^10.0 | ^11.0
- phrity/net-uri: ^2.0
- squizlabs/php_codesniffer: ^3.5
This package is auto-updated.
Last update: 2024-09-14 12:05:45 UTC
README
介绍
提供PSR-7 StreamInterface和PSR-17 StreamFactoryInterface实现的包,同时也添加了流和套接字相关功能。它适用于使用套接字连接的场景。
安装
使用 Composer 安装;
composer require phrity/net-stream
Stream类
Phrity\Net\Stream
类完全兼容PSR-7 StreamInterface,实现指定方法但不添加额外功能。可在需要PSR-7 StreamInterface兼容性的任何地方使用。
class Stream implements StreamInterface { // Constructor public function __construct(resource $stream); // Must be a resource of type stream // PSR-7 methods public function __toString(): string; public function close(): void; public function detach(): resource|null; public function getSize(): int|null; public function tell(): int; public function eof(): bool; public function isSeekable(): bool; public function seek(int $offset, int $whence = SEEK_SET): void; public function rewind(): void; public function isWritable(): bool; public function write(string $string): int; public function isReadable(): bool; public function read(int $length): string; public function getContents(): string; public function getMetadata(string|null $key = null): mixed; // Additional methods public function getResource(): resource; }
SocketStream类
Phrity\Net\SocketStream
类扩展了Phrity\Net\Stream
,并添加了适用于套接字流的额外方法。
class SocketStream extends Stream { // Methods public function isConnected(): bool; // If stream is connected to remote public function getRemoteName(): string|null; // Returns remote name public function getLocalName(): string|null; // Returns local name public function getResourceType(): string; // Get resource type public function isBlocking(): bool|null; // If stream is blocking or not public function setBlocking(bool $enable): bool; // Change blocking mode public function setTimeout(int $seconds, int $microseconds = 0): bool; // Set timeout public function readLine(int $length): string|null; // Read a line from stream, up to $length bytes public function closeRead(): void; // Closes the stream for further reading public function closeWrite(): void; // Closes the stream for further writing }
SocketClient类
Phrity\Net\SocketClient
类使远程套接字成为客户端。
class SocketClient { // Constructor public function __construct(UriInterface $uri); // Methods public function setPersistent(bool $persistent): self; // If client should use persisten connection public function setTimeout(int|null $timeout): self; // Set timeout public function setContext(array|null $options = null, array|null $params = null): self; // Set stream context public function connect(): SocketStream; // Connect to remote }
SocketServer类
Phrity\Net\SocketServer
类在本地套接字上启用服务器。
class SocketServer extends Stream { // Constructor public function __construct(UriInterface $uri); // Methods public function accept(int|null $timeout = null): SocketStream|null; // Accept connection on socket server public function getTransports(): array; // Get available transports public function setContext(array|null $options = null, array|null $params = null): self; // Set stream context public function isBlocking(): bool|null; // If stream is blocking or not public function setBlocking(bool $enable): bool; // Change blocking mode }
StreamCollection类
Phrity\Net\StreamCollection
类用于处理零个到多个连接。
class StreamCollection implements Countable, Iterator { // Constructor public function __construct(); // Collectors and selectors public function attach(Stream $attach, string|null $key = null): string; // Attach stream to collection public function detach(Stream|string $detach): bool; // Detach stream from collection public function getReadable(): self; // Get collection of readable streams public function getWritable(): self; // Get collection of writable streams public function waitRead(int $seconds = 60): self; // Wait for and get collection of streams with data to read // Countable interface implementation public function count(): int; // Iterator interface implementation public function current(): Stream; public function key(): string; public function next(): void; public function rewind(): void; public function valid(): bool; }
StreamFactory类
Phrity\Net\StreamFactory
类完全兼容PSR-17 StreamFactoryInterface,实现指定方法和一些额外功能。可在需要PSR-17 StreamFactoryInterface兼容性的任何地方使用。
class StreamFactory implements StreamFactoryInterface { // Constructor public function __construct(); // PSR-17 methods public function createStream(string $content = ''): Stream; public function createStreamFromFile(string $filename, string $mode = 'r'): Stream; public function createStreamFromResource(resource $resource): Stream; // Must be a resource of type stream // Additional methods public function createSocketStreamFromResource($resource): SocketStream; // Create a socket stream public function createSocketClient(UriInterface $uri): SocketClient; / Create socket client public function createSocketServer(UriInterface $uri): SocketServer; // Create a socket server public function createStreamCollection(): StreamCollection; // Create a stream collection }
StreamException类
当发生流相关错误时,会抛出Phrity\Net\StreamException
。
class StreamException extends RuntimeException { // Constructor public function __construct(int $code, array $data = [], Throwable|null $previous = null) }