Created server:start, turned down BINKP debugging, some Socket tweaks

This commit is contained in:
Deon George 2021-06-16 22:26:08 +10:00
parent 292040cef7
commit f9310566a0
4 changed files with 133 additions and 33 deletions

View File

@ -276,7 +276,8 @@ final class Binkd extends BaseProtocol
*/ */
private function binkp_recv(): int private function binkp_recv(): int
{ {
Log::debug(sprintf('%s: + Start',__METHOD__)); if ($this->DEBUG)
Log::debug(sprintf('%s: + Start',__METHOD__));
$buf = ''; $buf = '';
$blksz = $this->rx_size == -1 ? BinkpMessage::BLK_HDR_SIZE : $this->rx_size; $blksz = $this->rx_size == -1 ? BinkpMessage::BLK_HDR_SIZE : $this->rx_size;
@ -288,7 +289,8 @@ final class Binkd extends BaseProtocol
} catch (SocketException $e) { } catch (SocketException $e) {
if ($e->getCode() == 11) { if ($e->getCode() == 11) {
// @todo We maybe should count these and abort if there are too many? // @todo We maybe should count these and abort if there are too many?
Log::debug(sprintf('%s: - Socket EAGAIN',__METHOD__)); if ($this->DEBUG)
Log::debug(sprintf('%s: - Socket EAGAIN',__METHOD__));
return 1; return 1;
} }
@ -322,7 +324,8 @@ final class Binkd extends BaseProtocol
$this->rx_size = ((ord(substr($buf,0,1))&0x7f) << 8 )+ord(substr($buf,1,1)); $this->rx_size = ((ord(substr($buf,0,1))&0x7f) << 8 )+ord(substr($buf,1,1));
$this->rx_ptr = 0; $this->rx_ptr = 0;
Log::debug(sprintf('%s: - HEADER, is_msg [%d]',__METHOD__,$this->is_msg)); if ($this->DEBUG)
Log::debug(sprintf('%s: - HEADER, is_msg [%d]',__METHOD__,$this->is_msg));
if ($this->rx_size == 0) if ($this->rx_size == 0)
goto ZeroLen; goto ZeroLen;
@ -332,7 +335,8 @@ final class Binkd extends BaseProtocol
/* Next block */ /* Next block */
} else { } else {
ZeroLen: ZeroLen:
Log::debug(sprintf('%s: - NEXT BLOCK, is_msg [%d]',__METHOD__,$this->is_msg)); if ($this->DEBUG)
Log::debug(sprintf('%s: - NEXT BLOCK, is_msg [%d]',__METHOD__,$this->is_msg));
if ($this->is_msg) { if ($this->is_msg) {
$this->mib++; $this->mib++;
@ -346,7 +350,8 @@ final class Binkd extends BaseProtocol
return 1; return 1;
} }
Log::debug(sprintf('%s: - BUFFER [%d]',__METHOD__,strlen($buf))); if ($this->DEBUG)
Log::debug(sprintf('%s: - BUFFER [%d]',__METHOD__,strlen($buf)));
$rc = ord(substr($buf,0,1)); $rc = ord(substr($buf,0,1));
@ -440,7 +445,9 @@ final class Binkd extends BaseProtocol
$rc = 1; $rc = 1;
} }
Log::debug(sprintf('%s: = End [%d]',__METHOD__,$rc)); if ($this->DEBUG)
Log::debug(sprintf('%s: = End [%d]',__METHOD__,$rc));
return $rc; return $rc;
} }
@ -449,12 +456,14 @@ final class Binkd extends BaseProtocol
*/ */
private function binkp_send(): int private function binkp_send(): int
{ {
Log::debug(sprintf('%s: + Start - tx_left [%d]',__METHOD__,$this->tx_left)); if ($this->DEBUG)
Log::debug(sprintf('%s: + Start - tx_left [%d]',__METHOD__,$this->tx_left));
if ($this->tx_left == 0 ) { /* tx buffer is empty */ if ($this->tx_left == 0 ) { /* tx buffer is empty */
$this->tx_ptr = $this->tx_left = 0; $this->tx_ptr = $this->tx_left = 0;
Log::debug(sprintf('%s: - Msgs [%d]',__METHOD__,$this->mqueue->count())); if ($this->DEBUG)
Log::debug(sprintf('%s: - Msgs [%d]',__METHOD__,$this->mqueue->count()));
if ($this->mqueue->count()) { /* there are unsent messages */ if ($this->mqueue->count()) { /* there are unsent messages */
while ($msg = $this->mqueue->shift()) { while ($msg = $this->mqueue->shift()) {
@ -522,7 +531,9 @@ final class Binkd extends BaseProtocol
} }
} }
Log::debug(sprintf('%s: = End [1]',__METHOD__)); if ($this->DEBUG)
Log::debug(sprintf('%s: = End [1]',__METHOD__));
return 1; return 1;
} }

View File

@ -418,7 +418,7 @@ final class EMSI extends BaseProtocol implements CRCInterface,ZmodemInterface
Log::notice(sprintf('%s: - Remote Phone Number [%s]',__METHOD__,$this->emsi_dat_parse($str,$x,']'))); Log::notice(sprintf('%s: - Remote Phone Number [%s]',__METHOD__,$this->emsi_dat_parse($str,$x,']')));
/* Baud rate */ /* Baud rate */
$this->client->setSpeed($this->emsi_dat_parse($str,$x,']')); $this->client->speed = $this->emsi_dat_parse($str,$x,']');
/* Flags */ /* Flags */
Log::notice(sprintf('%s: - Remote Flags [%s]',__METHOD__,$this->emsi_dat_parse($str,$x,']'))); Log::notice(sprintf('%s: - Remote Flags [%s]',__METHOD__,$this->emsi_dat_parse($str,$x,']')));

View File

@ -188,6 +188,7 @@ final class SocketClient {
if ($this->DEBUG) if ($this->DEBUG)
Log::debug(sprintf('%s: = End [%d]',__METHOD__,$rc)); Log::debug(sprintf('%s: = End [%d]',__METHOD__,$rc));
return $rc; return $rc;
} }
@ -298,6 +299,10 @@ final class SocketClient {
if ($rc === FALSE) if ($rc === FALSE)
throw new SocketException($x=socket_last_error($this->connection),socket_strerror($x)); throw new SocketException($x=socket_last_error($this->connection),socket_strerror($x));
// If our buffer is null, see if we have any out of band data.
if (($rc == 0) && is_nulL($buf) && ($this->hasData(0) > 0))
socket_recv($this->connection,$buf, $len,MSG_OOB);
return is_null($buf) ? '' : $buf; return is_null($buf) ? '' : $buf;
} }
@ -319,7 +324,8 @@ final class SocketClient {
if ($this->hasData($timeout) > 0) { if ($this->hasData($timeout) > 0) {
try { try {
if (! strlen($this->rx_buf = $this->read(0,self::RX_BUF_SIZE))) { if (! strlen($this->rx_buf = $this->read(0,self::RX_BUF_SIZE))) {
Log::debug(sprintf('%s: - Nothing read',__METHOD__)); if ($this->DEBUG)
Log::debug(sprintf('%s: - Nothing read',__METHOD__));
return self::TTY_TIMEOUT; return self::TTY_TIMEOUT;
} }
@ -376,17 +382,6 @@ final class SocketClient {
return socket_write($this->connection,$message,$length); return socket_write($this->connection,$message,$length);
} }
/**
* Set our speed
*
* @param int $value
* @todo change to __set()
*/
public function setSpeed(int $value): void
{
$this->speed = $value;
}
/** /**
* Wait for data on a socket * Wait for data on a socket
* *
@ -404,18 +399,10 @@ final class SocketClient {
if ($rc === FALSE) if ($rc === FALSE)
throw new \Exception('Socket Error: '.socket_strerror(socket_last_error())); throw new \Exception('Socket Error: '.socket_strerror(socket_last_error()));
return $rc; if ($this->DEBUG)
} Log::debug(sprintf('Socket select returned [%d] (%d)',$rc,$timeout),['read'=>$read,'write'=>$write,'except'=>$except]);
/** return $rc;
* Return our speed in bps
*
* @return int
* @todo change to __get()
*/
public function speed(): int
{
return $this->speed;
} }
public function timer_expired(int $timer): int public function timer_expired(int $timer): int

View File

@ -0,0 +1,102 @@
<?php
namespace App\Console\Commands;
use App\Classes\Protocol\Binkd;
use App\Classes\Protocol\EMSI;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use App\Classes\Sock\SocketException;
use App\Classes\Sock\SocketServer;
use App\Classes\Protocol\EMSI as EMSIClass;
class StartServer extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'server:start';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Start Server';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
// @todo This should be a config item.
$start = [
'emsi' => [
'address'=>'0.0.0.0',
'port'=>60179,
'class'=>new EMSI,
],
'binkp' => [
'address'=>'0.0.0.0',
'port'=>24554,
'class'=>new Binkd,
],
];
$children = collect();
Log::debug(sprintf('%s:+ Starting Servers...',__METHOD__));
foreach ($start as $item => $config) {
Log::debug(sprintf('%s: - Starting: [%s]',__METHOD__,$item));
$pid = pcntl_fork();
if ($pid == -1)
die('could not fork');
// We are the child
if (! $pid) {
Log::info(sprintf('%s: - Started: [%s]',__METHOD__,$item));
$server = new SocketServer($config['port'],$config['address']);
$server->setConnectionHandler([$config['class'],'onConnect']);
try {
$server->listen();
} catch (SocketException $e) {
if ($e->getMessage() == 'Can\'t accept connections: "Success"')
Log::debug('Server Terminated');
else
Log::emergency('Uncaught Message: '.$e->getMessage());
}
Log::info(sprintf('%s: - Finished: [%s]',__METHOD__,$item));
// Child finished we need to get out of this loop.
exit;
}
$children->put($pid,$item);
}
// Wait for children to exit
while ($children->count()) {
// Wait for children to finish
$exited = pcntl_wait($status);
Log::info(sprintf('%s: - Exited: [%s]',__METHOD__,$children->pull($exited)));
}
// Done
Log::debug(sprintf('%s:= Finished.',__METHOD__));
}
}