line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
|
|
|
|
# Copyright (C) 2011, 2014 Rocky Bernstein <rocky@cpan.org> |
3
|
|
|
|
|
|
|
# Subsidiary routines used to "pack" and "unpack" TCP messages. |
4
|
15
|
|
|
15
|
|
17541
|
use strict; use warnings; no warnings 'redefine'; |
|
15
|
|
|
15
|
|
38
|
|
|
15
|
|
|
15
|
|
427
|
|
|
15
|
|
|
|
|
75
|
|
|
15
|
|
|
|
|
37
|
|
|
15
|
|
|
|
|
433
|
|
|
15
|
|
|
|
|
74
|
|
|
15
|
|
|
|
|
38
|
|
|
15
|
|
|
|
|
735
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
package Devel::Trepan::IO::TCPPack; |
7
|
15
|
|
|
15
|
|
1238
|
use POSIX qw(ceil log10); |
|
15
|
|
|
|
|
13308
|
|
|
15
|
|
|
|
|
142
|
|
8
|
15
|
|
|
15
|
|
4472
|
use Exporter; |
|
15
|
|
|
|
|
36
|
|
|
15
|
|
|
|
|
1029
|
|
9
|
|
|
|
|
|
|
our (@ISA, @EXPORT); |
10
|
|
|
|
|
|
|
@ISA = qw(Exporter); |
11
|
|
|
|
|
|
|
@EXPORT = qw(TCP_MAX_PACKET LOG_MAX_MSG pack_msg unpack_msg); |
12
|
|
|
|
|
|
|
|
13
|
15
|
|
|
15
|
|
94
|
use constant TCP_MAX_PACKET => 8192; |
|
15
|
|
|
|
|
33
|
|
|
15
|
|
|
|
|
1321
|
|
14
|
15
|
|
|
15
|
|
100
|
use constant LOG_MAX_MSG => ceil(log10(TCP_MAX_PACKET)); |
|
15
|
|
|
|
|
40
|
|
|
15
|
|
|
|
|
4769
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub pack_msg($) |
17
|
|
|
|
|
|
|
{ |
18
|
1
|
|
|
1
|
0
|
126
|
my $msg = shift; |
19
|
|
|
|
|
|
|
# A funny way of writing: '%04d' |
20
|
1
|
|
|
|
|
3
|
my $fmt = sprintf '%%0%dd' , LOG_MAX_MSG; |
21
|
1
|
|
|
|
|
6
|
return sprintf($fmt, length($msg)) . $msg; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub unpack_msg($) |
25
|
|
|
|
|
|
|
{ |
26
|
1
|
|
|
1
|
0
|
2
|
my $buf = shift; |
27
|
1
|
50
|
|
|
|
3
|
unless ($buf) { |
28
|
0
|
|
|
|
|
0
|
die "Protocol error - no text" |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
1
|
|
|
|
|
4
|
my ($pkg, $filename, $line) = caller; |
32
|
1
|
|
|
|
|
4
|
my $strnum = substr($buf, 0, LOG_MAX_MSG); |
33
|
1
|
50
|
|
|
|
6
|
unless ($strnum =~ /^\d+$/) { |
34
|
0
|
|
|
|
|
0
|
print STDERR "Protocol error - no length; got '$buf'\n"; |
35
|
0
|
|
|
|
|
0
|
return ($buf, '#'); |
36
|
|
|
|
|
|
|
} |
37
|
1
|
|
|
|
|
3
|
my $length = int($strnum); |
38
|
1
|
|
|
|
|
2
|
my $data = substr($buf, LOG_MAX_MSG, $length); |
39
|
1
|
50
|
|
|
|
5
|
if (length($buf) > LOG_MAX_MSG + $length) { |
40
|
0
|
|
|
|
|
0
|
$buf = substr($buf, LOG_MAX_MSG + $length); |
41
|
|
|
|
|
|
|
} else { |
42
|
1
|
|
|
|
|
2
|
$buf = ''; |
43
|
|
|
|
|
|
|
} |
44
|
1
|
|
|
|
|
4
|
return ($buf, $data); |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
# Demo |
48
|
|
|
|
|
|
|
unless (caller) { |
49
|
|
|
|
|
|
|
my $buf = "Hi there!"; |
50
|
|
|
|
|
|
|
my $msg; |
51
|
|
|
|
|
|
|
($buf, $msg) = unpack_msg(pack_msg($buf)); |
52
|
|
|
|
|
|
|
print "$msg\n"; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
1; |