Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9c7cb8f

Browse files
committed
Add --connections-limit option
1 parent c0997bc commit 9c7cb8f

2 files changed

Lines changed: 14 additions & 8 deletions

File tree

bin/stdinho

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ if (function_exists('pcntl_signal')) {
2222
}
2323

2424
$application = new Application(APP_NAME, PrettyVersions::getVersion('ostrolucky/'.APP_NAME));
25+
$application->setAutoExit(false);
2526
$application->add($command = new Command(APP_NAME));
2627
$application
2728
->setDefaultCommand($command->getName(), true)

src/Command.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ protected function configure(): void
4444
'Custom HTTP headers to append to response (in JSON format). Eg. --http-headers=\'["Content-encoding: gzip"]\'',
4545
'[]'
4646
)
47+
->addOption(
48+
'connections-limit',
49+
null,
50+
InputOption::VALUE_REQUIRED,
51+
'Determines after how many client connections should program shut down',
52+
INF
53+
)
4754
->setDescription('Turn any STDIN/STDOUT into HTTP server')
4855
;
4956
}
@@ -81,26 +88,24 @@ protected function initialize(InputInterface $input, OutputInterface $output): v
8188
*/
8289
protected function execute(InputInterface $input, OutputInterface $output): int
8390
{
84-
$addressPort = $input->getArgument('addressPort');
85-
$filePath = $input->getOption('file');
86-
8791
$logger = new ConsoleLogger($firstSection = $output->section());
8892

8993
$bufferer = $this->hasStdin ?
90-
new PipeBufferer($logger, STDIN, $filePath, $output->section()) :
91-
new ResolvedBufferer($filePath)
94+
new PipeBufferer($logger, STDIN, $input->getOption('file'), $output->section()) :
95+
new ResolvedBufferer($input->getOption('file'))
9296
;
9397

9498
$bufferHandler = asyncCoroutine($bufferer);
9599
$clientHandler = asyncCoroutine(new Responder($logger, $bufferer, $output, $this->customHttpHeaders));
96100

97-
Loop::run(function () use ($addressPort, $clientHandler, $logger, $firstSection, $bufferHandler) {
101+
Loop::run(function () use ($input, $clientHandler, $logger, $firstSection, $bufferHandler) {
98102
$bufferHandler();
99-
$server = listen($addressPort);
103+
$server = listen($input->getArgument('addressPort'));
100104
$firstSection->writeln(
101105
"<info>Connection opened at http://{$server->getAddress()}\nPress CTRL+C to exit.</info>\n"
102106
);
103-
while ($socket = yield $server->accept()) {
107+
$connectionsLimit = $input->getOption('connections-limit');
108+
while ($connectionsLimit-- && ($socket = yield $server->accept())) {
104109
$clientHandler($socket);
105110
}
106111
});

0 commit comments

Comments
 (0)