Thread Socket Server - binary Daten - Big endian ???
(18 answers)
Opened by gtrdriver at 2012-09-16 12:29
Hier mal ein Beispiel, wie der Protokolldecoder und -encoder aussehen könnte:
Code (perl): (dl
)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 use 5.012; use warnings; use IO::Handle; use constant { PKF_PACKET => '(SCC)>', LEN_PACKET => 4, ENC_BIN => 0, ENC_AT => 1, TYP_ATOK => 0x01, TYP_ATERR => 0x04, PKF_AT => 'S>', LEN_AT => 2, TYP_ASYNC => 0x02, PKF_ASYNC => '(QSS)>', LEN_ASYNC => 12, TYP_ACK => 0x03, PKF_ACK => 'S>', LEN_ACK => 2, DECODER => 0, ENCODER => 1, PKF_HEARTBEAT => 'CCCCCC', LEN_HEARTBEAT => 6 }; my %messages; sub read_packet { my ($fh) = @_; if ($fh->read(my $_, LEN_PACKET) == LEN_PACKET) { my ($transaction, $encoding, $type) = unpack PKF_PACKET; given ($type) { when ([TYP_ATOK, TYP_ATERR]) { if ($fh->read(my $_, LEN_AT) == LEN_AT) { my ($length) = unpack PKF_AT; my $body; unless ($fh->read($body, $length) == $length) { die 'failed to read AT message body'; } return { type => $type, transaction => $transaction, body => $body } } else { die 'failed to read AT message header'; } } when (TYP_ASYNC) { if ($fh->read(my $_, LEN_ASYNC) == LEN_ASYNC) { my ($modem, $message, $length) = unpack PKF_ASYNC; my $body; unless ($fh->read($body, $length) == $length) { die 'failed to read asynchronous message body'; } my $decoder; unless (defined($decoder = $messages{$message}->[DECODER])) { die "no decode delegate for message $message"; } return { type => $type, transaction => $transaction, modem => $modem, message => $message, body => $decoder->($body) }; } else { die 'failed to read asynchronous message header'; } } when (TYP_ACK) { if ($fh->read(my $_, LEN_ACK) == LEN_ACK) { my ($status) = unpack PKF_ACK; return { type => $type, transaction => $transaction, status => $status }; } else { die 'failed to read acknowledgement'; } } default { die "unknown message type $_"; } } } else { die 'failed to read packet header'; } } sub write_packet { my ($fh, $packet) = @_; my $encoding; my $type = $packet->{type}; given ($type) { when ([TYP_ATOK, TYP_ATERR]) { $encoding = ENC_AT; } default { $encoding = ENC_BIN; } } my $data = pack(PKF_PACKET, $packet->{transaction}, $encoding, $type); given ($type) { when ([TYP_ATOK, TYP_ATERR]) { $data .= pack(PKF_AT, length($packet->{body})); $data .= $packet->{body}; } when (TYP_ASYNC) { my $message = $packet->{message}; my $encoder; unless (defined($encoder = $messages{$message}->[ENCODER])) { die "no encode delegate for message $message"; } my $body = $encoder->($packet->{body}); $data .= pack(PKF_ASYNC, $packet->{modem}, $packet->{message}, length($body)); $data .= $body; } when (TYP_ACK) { $data .= pack(PKF_ACK, $packet->{status}); } default { die "unknown message type $_"; } } $fh->write($data, length($data)); } $messages{0xAB}->[DECODER] = sub { [unpack PKF_HEARTBEAT, $_[0]]; }; $messages{0xAB}->[ENCODER] = sub { pack PKF_HEARTBEAT, @{$_[0]}; }; Vorsicht, der Code ist nicht gründlich getestet, schließlich habe ich keine passende Hardware oder Emulationsumgebung zur Hand. When C++ is your hammer, every problem looks like your thumb.
|