cheesegrits did you tested len>=0x200000?
I think
print $sock chr(($len >> 8) & 0xFF);
print $sock chr(($len >> 8) & 0xFF);
print $sock chr(($len >> 8) & 0xFF);
…
will print one value. Maybe you mean
print $sock chr(($len >> 24) & 0xFF);
print $sock chr(($len >> 16) & 0xFF);
print $sock chr(($len >> 8) & 0xFF);
yeah?
Then I read wiki.mikrotik.com/wiki/API:
len >= 0x10000000
0xF0 and len as four bytes
so your code is
elsif ($len < 0x10000000)
{
$len |= 0xE0000000;
0xE0000000 != 0xF000000000
and better elsif ($len < 0x10000000) → else
The formula is seems simple. My code:
$bytes=$len < 0x80? 1 : $len < 0x4000? 2 : $len < 0x200000? 3 : $len < 0x10000000? 4 : 5;
$len|=(0x0F >> (5-$bytes)) << ($bytes*8-$bytes+1);
while ($bytes--)
{
$str.=chr($len & 0xFF);
$len>>=8;
}
$str - rezult