diff --git a/app/Classes/Protocol/Binkd.php b/app/Classes/Protocol/Binkd.php index d535ef0..e57f878 100644 --- a/app/Classes/Protocol/Binkd.php +++ b/app/Classes/Protocol/Binkd.php @@ -276,7 +276,8 @@ final class Binkd extends BaseProtocol */ private function binkp_recv(): int { - Log::debug(sprintf('%s: + Start',__METHOD__)); + if ($this->DEBUG) + Log::debug(sprintf('%s: + Start',__METHOD__)); $buf = ''; $blksz = $this->rx_size == -1 ? BinkpMessage::BLK_HDR_SIZE : $this->rx_size; @@ -288,7 +289,8 @@ final class Binkd extends BaseProtocol } catch (SocketException $e) { if ($e->getCode() == 11) { // @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; } @@ -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_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) goto ZeroLen; @@ -332,7 +335,8 @@ final class Binkd extends BaseProtocol /* Next block */ } else { 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) { $this->mib++; @@ -346,7 +350,8 @@ final class Binkd extends BaseProtocol 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)); @@ -440,7 +445,9 @@ final class Binkd extends BaseProtocol $rc = 1; } - Log::debug(sprintf('%s: = End [%d]',__METHOD__,$rc)); + if ($this->DEBUG) + Log::debug(sprintf('%s: = End [%d]',__METHOD__,$rc)); + return $rc; } @@ -449,12 +456,14 @@ final class Binkd extends BaseProtocol */ 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 */ $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 */ 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; } diff --git a/app/Classes/Protocol/EMSI.php b/app/Classes/Protocol/EMSI.php index af24ee0..b542991 100644 --- a/app/Classes/Protocol/EMSI.php +++ b/app/Classes/Protocol/EMSI.php @@ -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,']'))); /* Baud rate */ - $this->client->setSpeed($this->emsi_dat_parse($str,$x,']')); + $this->client->speed = $this->emsi_dat_parse($str,$x,']'); /* Flags */ Log::notice(sprintf('%s: - Remote Flags [%s]',__METHOD__,$this->emsi_dat_parse($str,$x,']'))); diff --git a/app/Classes/Sock/SocketClient.php b/app/Classes/Sock/SocketClient.php index 4966ef8..2ba8d14 100644 --- a/app/Classes/Sock/SocketClient.php +++ b/app/Classes/Sock/SocketClient.php @@ -188,6 +188,7 @@ final class SocketClient { if ($this->DEBUG) Log::debug(sprintf('%s: = End [%d]',__METHOD__,$rc)); + return $rc; } @@ -298,6 +299,10 @@ final class SocketClient { if ($rc === FALSE) 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; } @@ -319,7 +324,8 @@ final class SocketClient { if ($this->hasData($timeout) > 0) { try { 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; } @@ -376,17 +382,6 @@ final class SocketClient { 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 * @@ -404,18 +399,10 @@ final class SocketClient { if ($rc === FALSE) 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 our speed in bps - * - * @return int - * @todo change to __get() - */ - public function speed(): int - { - return $this->speed; + return $rc; } public function timer_expired(int $timer): int diff --git a/app/Console/Commands/StartServer.php b/app/Console/Commands/StartServer.php new file mode 100644 index 0000000..e5b3134 --- /dev/null +++ b/app/Console/Commands/StartServer.php @@ -0,0 +1,102 @@ + [ + '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__)); + } +}