Optimised our sending and receiving of items

This commit is contained in:
2023-07-17 16:36:53 +10:00
parent a8f76aec31
commit c1ec4eff36
14 changed files with 634 additions and 572 deletions

View File

@@ -6,44 +6,80 @@ use Carbon\Carbon;
use App\Classes\FTN\Packet;
class Mail extends Item
final class Mail extends Send
{
private Packet $file;
/** @var int Our internal position counter */
private int $readpos;
/**
* @throws \Exception
*/
public function __construct(Packet $mail,int $action)
public function __construct(Packet $mail,int $type)
{
switch ($action) {
case self::I_SEND:
$this->file = $mail;
parent::__construct();
break;
default:
throw new \Exception('Unknown action: '.$action);
}
$this->action = $action;
$this->f = $mail;
$this->ftype = ((($type&0xff)<<8)|self::IS_PKT);
$this->readpos = 0;
}
public function __get($key) {
switch ($key) {
case 'file': return $this->file;
case 'messages': return $this->file->messages;
case 'dbids':
return $this->f->messages->pluck('dbid');
case 'name':
return sprintf('%08x',timew($this->youngest()));
case 'nameas':
return sprintf('%s.pkt',$this->name);
case 'mtime':
return $this->youngest()->timestamp;
case 'size':
return strlen($this->f);
case 'type':
return ($this->ftype&0xff00)>>8;
default:
return parent::__get($key);
}
}
public function read(int $start,int $length): string
public function close(bool $successful): void
{
return substr((string)$this->file,$start,$length);
if ($successful)
$this->complete = TRUE;
}
public function feof(): bool
{
return ($this->readpos === $this->size);
}
public function open(string $compress=''): bool
{
return TRUE;
}
public function read(int $length): string
{
$result = substr((string)$this->f,$this->readpos,$length);
$this->readpos += strlen($result);
return $result;
}
public function seek(int $pos): bool
{
$this->readpos = ($pos < $this->size) ? $pos : $this->size;
return TRUE;
}
public function youngest(): Carbon
{
return $this->file->messages->pluck('date')->sort()->last();
return $this->f->messages->pluck('date')->sort()->last();
}
}