📜 ⬆️ ⬇️

Binary protocol ProtoBuf / AMF and AS3

Not so long ago (a year ago), google suggested that everyone use the Protocol Buffers language.

I read, looked - funny. Great prospects for use.
there are ports on AS3, I tested this: http://code.google.com/p/protobuf-actionscript3/

I want to use as a replacement for AMF.
In a test example, I simulate the transfer of data on a binary socket.
Immediately, there were problems: In the form in which it has been done now, it is normally possible to work only according to the request / response scheme, i.e. when you know which class should come back.
When working with a "nameless" socket in AMF, you can find out what class you came for.
Let's try to implement it in ProtoBuf:
All the source code of the test example in the attachment, here I focus on only my finishing touch)

so the main class: Main.as (read the comments)
AS3 code:
')
  1. package
  2. {
  3. import com.google.protobuf.Message;
  4. import com.google.protobuf.Person;
  5. import com.google.protobuf.PhoneNumber;
  6. import flash.display.Sprite;
  7. import flash.events.Event;
  8. import flash.net.getClassByAlias;
  9. import flash.net.registerClassAlias;
  10. import flash.utils.ByteArray;
  11. import flash.utils.getDefinitionByName;
  12. import flash.utils.getQualifiedClassName;
  13. / **
  14. * Test class for working with protoBuf - the source code for the parser class has been changed
  15. * @author k0t0vich
  16. * /
  17. public class Main extends Sprite
  18. {
  19. public function Main (): void
  20. {
  21. if (stage) init ();
  22. else addEventListener (Event.ADDED_TO_STAGE, init);
  23. }
  24. private function init (e: Event = null): void
  25. {
  26. removeEventListener (Event.ADDED_TO_STAGE, init);
  27. // entry point
  28. // Register Aliases For Classes
  29. registerClassAlias ​​("py.Person", Person);
  30. registerClassAlias ​​("py.Number", PhoneNumber);
  31. // Create and encrypt a class .--------------------------
  32. // write
  33. var person: Person = new Person ();
  34. person.alias = "py.Person";
  35. person.setName ("John Doe");
  36. person.setId (1234);
  37. person.setEmail ("jdoe@example.com");
  38. var number: PhoneNumber = new PhoneNumber ();
  39. var number2: PhoneNumber = new PhoneNumber ();
  40. number.setNumber ("555-6789");
  41. person.addPhone (number);
  42. number2.setNumber ("123123123-12312");
  43. person.addPhone (number2);
  44. trace ("person:" + person);
  45. // Write it out to a IDataOutput
  46. var bytes: ByteArray = new ByteArray ();
  47. person.writeToDataOutput (bytes);
  48. trace ("writed bytes:" + bytes);
  49. // ------------------------------------------------ -------
  50. // simulate the arrival of data from the socket
  51. onData (bytes);
  52. }
  53. // Data from
  54. private function onData (bytes: ByteArray): void
  55. {
  56. bytes.position = 0;
  57. // read
  58. var message: Message = new Message ();
  59. // do not read the entire message, but only the name of the alias
  60. var alias: String = message.readClassAliasFromDataOutput (bytes);
  61. var classRef: Class = getClassByAlias ​​(alias) as Class;
  62. var item: * = new classRef ();
  63. bytes.position = 0;
  64. item.readFromDataOutput (bytes);
  65. // execute a specific class method
  66. item.changeModel ();
  67. }
  68. }
  69. }

Then, in the onData (Main.as) handler, we first read the name of the alias, create a class using this alias and in it already, read the rest of the data

AS3 code:


  1. private function onData (bytes: ByteArray): void
  2. {
  3. bytes.position = 0;
  4. // read
  5. var message: Message = new Message ();
  6. // do not read the entire message, but only the name of the alias
  7. var alias: String = message.readClassAliasFromDataOutput (bytes);
  8. var classRef: Class = getClassByAlias ​​(alias) as Class;
  9. var item: * = new classRef ();
  10. bytes.position = 0;
  11. item.readFromDataOutput (bytes);
  12. // execute a specific class method
  13. item.changeModel ();
  14. }

Source: https://habr.com/ru/post/62020/


All Articles