Simplify packet processing. Re-enable pkt processing tests.
This commit is contained in:
@@ -22,8 +22,8 @@ class Packet extends FTNBase implements \Iterator, \Countable
|
||||
{
|
||||
private const LOGKEY = 'PKT';
|
||||
|
||||
private const BLOCKSIZE = 1024;
|
||||
protected const PACKED_MSG_LEAD = "\02\00";
|
||||
protected const PACKED_END = "\00\00";
|
||||
|
||||
public const regex = '([[:xdigit:]]{4})(?:-(\d{4,10}))?-(.+)';
|
||||
|
||||
@@ -172,7 +172,7 @@ class Packet extends FTNBase implements \Iterator, \Countable
|
||||
/**
|
||||
* Process a packet file
|
||||
*
|
||||
* @param mixed $f
|
||||
* @param mixed $f File handler returning packet data
|
||||
* @param string $name
|
||||
* @param int $size
|
||||
* @param Domain|null $domain
|
||||
@@ -213,18 +213,18 @@ class Packet extends FTNBase implements \Iterator, \Countable
|
||||
$o->name = $name;
|
||||
|
||||
$x = fread($f,2);
|
||||
if (strlen($x) === 2) {
|
||||
// End of Packet?
|
||||
if ($x === "\00\00")
|
||||
return $o;
|
||||
|
||||
// End of Packet?
|
||||
if ((strlen($x) === 2) && ($x === "\00\00"))
|
||||
return $o;
|
||||
|
||||
// Messages start with self::PACKED_MSG_LEAD
|
||||
if ((strlen($x) === 2) && ($x !== self::PACKED_MSG_LEAD))
|
||||
throw new InvalidPacketException('Not a valid packet: '.bin2hex($x));
|
||||
// Messages start with self::PACKED_MSG_LEAD
|
||||
elseif ($x !== self::PACKED_MSG_LEAD)
|
||||
throw new InvalidPacketException('Not a valid packet: '.bin2hex($x));
|
||||
|
||||
// No message attached
|
||||
else if (! strlen($x))
|
||||
throw new InvalidPacketException('No message in packet: '.bin2hex($x));
|
||||
} else
|
||||
throw new InvalidPacketException('Not a valid packet, not EOP or SOM'.bin2hex($x));
|
||||
|
||||
// Work out the packet zone
|
||||
if ($o->fz && ($o->fd || $domain)) {
|
||||
@@ -250,98 +250,36 @@ class Packet extends FTNBase implements \Iterator, \Countable
|
||||
->singleOrFail();
|
||||
}
|
||||
|
||||
$buf_ptr = 0; // Pointer to the end of the current message
|
||||
$message = ''; // Current message we are building
|
||||
$readbuf = ''; // What we are reading to determine the message contents
|
||||
$last = ''; // If during buffer reads, the end of the last buffer had our NULL end of message marker
|
||||
$msgbuf = '';
|
||||
$leader = Message::HEADER_LEN+strlen(self::PACKED_MSG_LEAD);
|
||||
|
||||
// We loop through reading from the buffer, to find our end of message tag
|
||||
while ($buf_ptr || (! feof($f) && ($readbuf=fread($f,self::BLOCKSIZE)))) {
|
||||
if (! $buf_ptr)
|
||||
$read_ptr = ftell($f);
|
||||
while ((! feof($f) && ($readbuf=fread($f,$leader)))) {
|
||||
$read_ptr = ftell($f);
|
||||
$msgbuf .= $readbuf;
|
||||
|
||||
// Packed messages are Message::HEADER_LEN, prefixed with self::PACKED_MSG_LEAD
|
||||
// If we havent got our header yet
|
||||
if (strlen($message) < (Message::HEADER_LEN+strlen(self::PACKED_MSG_LEAD))) {
|
||||
$addchars = (Message::HEADER_LEN+strlen(self::PACKED_MSG_LEAD))-strlen($message);
|
||||
$message .= substr($readbuf,$buf_ptr,$addchars);
|
||||
$buf_ptr += $addchars;
|
||||
// See if we have our EOM/EOP marker
|
||||
if ((($end=strpos($msgbuf,"\x00".self::PACKED_MSG_LEAD,$leader)) !== FALSE)
|
||||
|| (($end=strpos($msgbuf,"\x00".self::PACKED_END,$leader)) !== FALSE))
|
||||
{
|
||||
// Parse our message
|
||||
$o->parseMessage(substr($msgbuf,0,$end));
|
||||
|
||||
// If our buffer wasnt big enough, and thus $addchars didnt have enough chars to add.
|
||||
if ($buf_ptr >= strlen($readbuf)) {
|
||||
$buf_ptr = 0;
|
||||
continue;
|
||||
}
|
||||
$msgbuf = substr($msgbuf,$end+3);
|
||||
continue;
|
||||
|
||||
// If we have more to read
|
||||
} elseif ($read_ptr < $size) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Take 2 chars from the buffer and check if we have our end packet signature
|
||||
// $last is set, because we detected a NULL, so we'll add two more chars and see if we have our EOM
|
||||
// signature. Some of those chars may belong to the next message, if $last has more than 1 NULL.
|
||||
if ($last && ($buf_ptr === 0)) {
|
||||
$last .= substr($readbuf,0,2);
|
||||
|
||||
if (($end=strpos($last,"\x00".self::PACKED_MSG_LEAD,$buf_ptr)) !== FALSE) {
|
||||
$o->parseMessage(substr($message,0,$end-2));
|
||||
$last = '';
|
||||
$message = '';
|
||||
$buf_ptr = 1+$end;
|
||||
|
||||
// Loop to rebuild our header for the next message
|
||||
continue;
|
||||
}
|
||||
|
||||
// We didnt have an EOM marker
|
||||
$last = '';
|
||||
}
|
||||
|
||||
// See if our EOM marker is in the read buffer
|
||||
if (($end=strpos($readbuf,"\x00".self::PACKED_MSG_LEAD,$buf_ptr)) === FALSE) {
|
||||
// Just in case our packet break is at the end of the buffer
|
||||
$last = substr($readbuf,-2);
|
||||
|
||||
// We have an EOM or EOP marker here, so loop around to get the next read
|
||||
if (str_contains($last,"\x00") && ($size < $read_ptr)) {
|
||||
$message .= substr($readbuf,$buf_ptr);
|
||||
$buf_ptr = 0;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
// No EOM marker
|
||||
$last = '';
|
||||
|
||||
// See if we have an EOP marker
|
||||
$end = strpos($readbuf,"\x00\x00\x00",$buf_ptr);
|
||||
}
|
||||
|
||||
// See if we have found the end of the packet, if not read more.
|
||||
if ($end === FALSE) {
|
||||
if ($read_ptr < $size) {
|
||||
$message .= substr($readbuf,$buf_ptr);
|
||||
$buf_ptr = 0;
|
||||
|
||||
continue;
|
||||
|
||||
// No more to read, so the packet is bad
|
||||
} else
|
||||
throw new InvalidPacketException(sprintf('Cannot determine END of message/packet: %s|%s',get_class($o),hex_dump($message)));
|
||||
|
||||
} else {
|
||||
$message .= substr($readbuf,$buf_ptr,$end-$buf_ptr);
|
||||
$buf_ptr = $end+3;
|
||||
|
||||
if ($buf_ptr >= strlen($readbuf))
|
||||
$buf_ptr = 0;
|
||||
}
|
||||
|
||||
// Look for the next message
|
||||
$o->parseMessage($message);
|
||||
$message = '';
|
||||
// If we get here
|
||||
throw new InvalidPacketException(sprintf('Cannot determine END of message/packet: %s|%s',get_class($o),hex_dump($message)));;
|
||||
}
|
||||
|
||||
// If our message is still set, then we have an unprocessed message
|
||||
if ($message)
|
||||
$o->parseMessage($message);
|
||||
if ($msgbuf)
|
||||
throw new InvalidPacketException(sprintf('Unprocessed data in packet: %s|%s',get_class($o),hex_dump($msgbuf)));
|
||||
|
||||
return $o;
|
||||
}
|
||||
@@ -546,4 +484,9 @@ class Packet extends FTNBase implements \Iterator, \Countable
|
||||
|
||||
$this->messages->push($msg);
|
||||
}
|
||||
|
||||
public function pluck(string $key): Collection
|
||||
{
|
||||
return $this->messages->pluck($key);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user