| 1 | package Javonet::Core::Protocol::CommandDeserializer; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use lib 'lib'; | ||
| 5 | use aliased 'Javonet::Sdk::Core::PerlCommand' => 'PerlCommand'; | ||
| 6 | use aliased 'Javonet::Sdk::Core::Type' => 'Type', qw(get_type); | ||
| 7 | use aliased 'Javonet::Core::Protocol::TypeDeserializer' => 'TypeDeserializer'; | ||
| 8 | |||
| 9 | use Exporter qw(import); | ||
| 10 | our @EXPORT = qw(deserialize); | ||
| 11 | |||
| 12 | # Static deserialize method | ||
| 13 | sub deserialize { | ||
| 14 | my ($class, $byte_array_ref) = @_; | ||
| 15 | my @buffer = ref $byte_array_ref eq 'ARRAY' ? @$byte_array_ref : @$byte_array_ref; | ||
| 16 | my $position = 11; # Start position after header | ||
| 17 | |||
| 18 | my $command = PerlCommand->new( | ||
| 19 | runtime => $buffer[0], | ||
| 20 | command_type => $buffer[10], | ||
| 21 | payload => [] | ||
| 22 | ); | ||
| 23 | |||
| 24 | # Use position reference to simulate ref parameter | ||
| 25 | my $position_ref = \$position; | ||
| 26 | |||
| 27 | while ($position < @buffer) { | ||
| 28 | $command = $command->AddArgToPayload(ReadObject(\@buffer, $position_ref)); | ||
| 29 | } | ||
| 30 | |||
| 31 | return $command; | ||
| 32 | } | ||
| 33 | |||
| 34 | # ReadObject | ||
| 35 | sub ReadObject { | ||
| 36 | my ($buffer_ref, $position_ref) = @_; | ||
| 37 | my @buffer = @$buffer_ref; | ||
| 38 | my $position = $$position_ref; | ||
| 39 | |||
| 40 | my $type = $buffer[$position]; | ||
| 41 | |||
| 42 | if ($type == Type->get_type('Command')) { | ||
| 43 | return ReadCommand($buffer_ref, $position_ref); | ||
| 44 | } | ||
| 45 | elsif ($type == Type->get_type('JavonetString')) { | ||
| 46 | return ReadString($buffer_ref, $position_ref); | ||
| 47 | } | ||
| 48 | elsif ($type == Type->get_type('JavonetInteger')) { | ||
| 49 | return ReadInt($buffer_ref, $position_ref); | ||
| 50 | } | ||
| 51 | elsif ($type == Type->get_type('JavonetBool')) { | ||
| 52 | return ReadBool($buffer_ref, $position_ref); | ||
| 53 | } | ||
| 54 | elsif ($type == Type->get_type('JavonetFloat')) { | ||
| 55 | return ReadFloat($buffer_ref, $position_ref); | ||
| 56 | } | ||
| 57 | elsif ($type == Type->get_type('JavonetByte')) { | ||
| 58 | return ReadByte($buffer_ref, $position_ref); | ||
| 59 | } | ||
| 60 | elsif ($type == Type->get_type('JavonetChar')) { | ||
| 61 | return ReadChar($buffer_ref, $position_ref); | ||
| 62 | } | ||
| 63 | elsif ($type == Type->get_type('JavonetLongLong')) { | ||
| 64 | return ReadLong($buffer_ref, $position_ref); | ||
| 65 | } | ||
| 66 | elsif ($type == Type->get_type('JavonetDouble')) { | ||
| 67 | return ReadDouble($buffer_ref, $position_ref); | ||
| 68 | } | ||
| 69 | elsif ($type == Type->get_type('JavonetUnsignedLongLong')) { | ||
| 70 | return ReadULong($buffer_ref, $position_ref); | ||
| 71 | } | ||
| 72 | elsif ($type == Type->get_type('JavonetUnsignedInteger')) { | ||
| 73 | return ReadUInt($buffer_ref, $position_ref); | ||
| 74 | } | ||
| 75 | elsif ($type == Type->get_type('JavonetNull')) { | ||
| 76 | return ReadNull($buffer_ref, $position_ref); | ||
| 77 | } | ||
| 78 | elsif ($type == Type->get_type('JavonetUndefined')) { | ||
| 79 | return ReadUndefined($buffer_ref, $position_ref); | ||
| 80 | } | ||
| 81 | else { | ||
| 82 | die "NotImplementedException: Type not supported: $type"; | ||
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | # ReadCommand | ||
| 87 | sub ReadCommand { | ||
| 88 | my ($buffer_ref, $position_ref) = @_; | ||
| 89 | my @buffer = @$buffer_ref; | ||
| 90 | my $p = $$position_ref; | ||
| 91 | |||
| 92 | # Read numberOfElements from bytes p+1 to p+4 | ||
| 93 | my @int_buffer = @buffer[$p + 1 .. $p + 4]; | ||
| 94 | my $numberOfElements = TypeDeserializer->deserializeInt(\@int_buffer); | ||
| 95 | |||
| 96 | my $runtime = $buffer[$p + 5]; | ||
| 97 | my $commandType = $buffer[$p + 6]; | ||
| 98 | |||
| 99 | $$position_ref += 7; | ||
| 100 | |||
| 101 | # Read all payload elements at once | ||
| 102 | my @payload = (); | ||
| 103 | for (my $i = 0; $i < $numberOfElements; $i++) { | ||
| 104 | push @payload, ReadObject($buffer_ref, $position_ref); | ||
| 105 | } | ||
| 106 | |||
| 107 | return PerlCommand->new( | ||
| 108 | runtime => $runtime, | ||
| 109 | command_type => $commandType, | ||
| 110 | payload => \@payload | ||
| 111 | ); | ||
| 112 | } | ||
| 113 | |||
| 114 | # ReadString | ||
| 115 | sub ReadString { | ||
| 116 | my ($buffer_ref, $position_ref) = @_; | ||
| 117 | my @buffer = @$buffer_ref; | ||
| 118 | my $p = $$position_ref; | ||
| 119 | |||
| 120 | my $mode = $buffer[$p + 1]; | ||
| 121 | my @int_buffer = @buffer[$p + 2 .. $p + 5]; | ||
| 122 | my $size = TypeDeserializer->deserializeInt(\@int_buffer); | ||
| 123 | |||
| 124 | $$position_ref += 6; | ||
| 125 | $p = $$position_ref; | ||
| 126 | $$position_ref += $size; | ||
| 127 | |||
| 128 | my @string_buffer = @buffer[$p .. $p + $size - 1]; | ||
| 129 | return TypeDeserializer->deserializeString($mode, \@string_buffer); | ||
| 130 | } | ||
| 131 | |||
| 132 | # ReadInt | ||
| 133 | sub ReadInt { | ||
| 134 | my ($buffer_ref, $position_ref) = @_; | ||
| 135 | my @buffer = @$buffer_ref; | ||
| 136 | my $size = 4; | ||
| 137 | |||
| 138 | $$position_ref += 2; | ||
| 139 | my $p = $$position_ref; | ||
| 140 | $$position_ref += $size; | ||
| 141 | |||
| 142 | my @int_buffer = @buffer[$p .. $p + $size - 1]; | ||
| 143 | return TypeDeserializer->deserializeInt(\@int_buffer); | ||
| 144 | } | ||
| 145 | |||
| 146 | # ReadBool | ||
| 147 | sub ReadBool { | ||
| 148 | my ($buffer_ref, $position_ref) = @_; | ||
| 149 | my @buffer = @$buffer_ref; | ||
| 150 | my $size = 1; | ||
| 151 | |||
| 152 | $$position_ref += 2; | ||
| 153 | my $p = $$position_ref; | ||
| 154 | $$position_ref += $size; | ||
| 155 | |||
| 156 | return TypeDeserializer->deserializeBool($buffer[$p]); | ||
| 157 | } | ||
| 158 | |||
| 159 | # ReadFloat | ||
| 160 | sub ReadFloat { | ||
| 161 | my ($buffer_ref, $position_ref) = @_; | ||
| 162 | my @buffer = @$buffer_ref; | ||
| 163 | my $size = 4; | ||
| 164 | |||
| 165 | $$position_ref += 2; | ||
| 166 | my $p = $$position_ref; | ||
| 167 | $$position_ref += $size; | ||
| 168 | |||
| 169 | my @float_buffer = @buffer[$p .. $p + $size - 1]; | ||
| 170 | return TypeDeserializer->deserializeFloat(\@float_buffer); | ||
| 171 | } | ||
| 172 | |||
| 173 | # ReadByte | ||
| 174 | sub ReadByte { | ||
| 175 | my ($buffer_ref, $position_ref) = @_; | ||
| 176 | my @buffer = @$buffer_ref; | ||
| 177 | my $size = 1; | ||
| 178 | |||
| 179 | $$position_ref += 2; | ||
| 180 | my $p = $$position_ref; | ||
| 181 | $$position_ref += $size; | ||
| 182 | |||
| 183 | return TypeDeserializer->deserializeByte($buffer[$p]); | ||
| 184 | } | ||
| 185 | |||
| 186 | # ReadChar | ||
| 187 | sub ReadChar { | ||
| 188 | my ($buffer_ref, $position_ref) = @_; | ||
| 189 | my @buffer = @$buffer_ref; | ||
| 190 | my $size = 1; | ||
| 191 | |||
| 192 | $$position_ref += 2; | ||
| 193 | my $p = $$position_ref; | ||
| 194 | $$position_ref += $size; | ||
| 195 | |||
| 196 | return TypeDeserializer->deserializeChar($buffer[$p]); | ||
| 197 | } | ||
| 198 | |||
| 199 | # ReadLong | ||
| 200 | sub ReadLong { | ||
| 201 | my ($buffer_ref, $position_ref) = @_; | ||
| 202 | my @buffer = @$buffer_ref; | ||
| 203 | my $size = 8; | ||
| 204 | |||
| 205 | $$position_ref += 2; | ||
| 206 | my $p = $$position_ref; | ||
| 207 | $$position_ref += $size; | ||
| 208 | |||
| 209 | my @longlong_buffer = @buffer[$p .. $p + $size - 1]; | ||
| 210 | return TypeDeserializer->deserializeLongLong(\@longlong_buffer); | ||
| 211 | } | ||
| 212 | |||
| 213 | # ReadDouble | ||
| 214 | sub ReadDouble { | ||
| 215 | my ($buffer_ref, $position_ref) = @_; | ||
| 216 | my @buffer = @$buffer_ref; | ||
| 217 | my $size = 8; | ||
| 218 | |||
| 219 | $$position_ref += 2; | ||
| 220 | my $p = $$position_ref; | ||
| 221 | $$position_ref += $size; | ||
| 222 | |||
| 223 | my @double_buffer = @buffer[$p .. $p + $size - 1]; | ||
| 224 | return TypeDeserializer->deserializeDouble(\@double_buffer); | ||
| 225 | } | ||
| 226 | |||
| 227 | # ReadULong | ||
| 228 | sub ReadULong { | ||
| 229 | my ($buffer_ref, $position_ref) = @_; | ||
| 230 | my @buffer = @$buffer_ref; | ||
| 231 | my $size = 8; | ||
| 232 | |||
| 233 | $$position_ref += 2; | ||
| 234 | my $p = $$position_ref; | ||
| 235 | $$position_ref += $size; | ||
| 236 | |||
| 237 | my @ulonglong_buffer = @buffer[$p .. $p + $size - 1]; | ||
| 238 | return TypeDeserializer->deserializeULongLong(\@ulonglong_buffer); | ||
| 239 | } | ||
| 240 | |||
| 241 | # ReadUInt | ||
| 242 | sub ReadUInt { | ||
| 243 | my ($buffer_ref, $position_ref) = @_; | ||
| 244 | my @buffer = @$buffer_ref; | ||
| 245 | my $size = 4; | ||
| 246 | |||
| 247 | $$position_ref += 2; | ||
| 248 | my $p = $$position_ref; | ||
| 249 | $$position_ref += $size; | ||
| 250 | |||
| 251 | my @uint_buffer = @buffer[$p .. $p + $size - 1]; | ||
| 252 | return TypeDeserializer->deserializeUInt(\@uint_buffer); | ||
| 253 | } | ||
| 254 | |||
| 255 | # ReadNull | ||
| 256 | sub ReadNull { | ||
| 257 | my ($buffer_ref, $position_ref) = @_; | ||
| 258 | my @buffer = @$buffer_ref; | ||
| 259 | my $size = 1; | ||
| 260 | |||
| 261 | $$position_ref += 2; | ||
| 262 | my $p = $$position_ref; | ||
| 263 | $$position_ref += $size; | ||
| 264 | |||
| 265 | # Note: deserializeUndef doesn't use the buffer, but we match C# structure | ||
| 266 | return TypeDeserializer->deserializeUndef(); | ||
| 267 | } | ||
| 268 | |||
| 269 | # ReadUndefined | ||
| 270 | sub ReadUndefined { | ||
| 271 | my ($buffer_ref, $position_ref) = @_; | ||
| 272 | my @buffer = @$buffer_ref; | ||
| 273 | my $size = 1; | ||
| 274 | |||
| 275 | $$position_ref += 2; | ||
| 276 | my $p = $$position_ref; | ||
| 277 | $$position_ref += $size; | ||
| 278 | |||
| 279 | return TypeDeserializer->deserializeUndefined(); | ||
| 280 | } | ||
| 281 | |||
| 282 | 1; |