as3-msgpack is a implementation of MessagePack specification for Actionscript3 language (Flash, Flex and AIR).
3 |
Getting started: http://loteixeira.github.io/lib/2013/08/19/as3-msgpack/
4 | Download the lastest release: https://github.com/loteixeira/as3-msgpack/archive/v1.0.1.zip
5 | See online documentation: http://disturbedcoder.com/files/as3-msgpack/
6 | Check the wishlist: https://github.com/loteixeira/as3-msgpack/blob/master/WISHLIST.md
7 |
8 | ## about message pack format
9 |
MessagePack is an efficient binary serialization format. It lets you exchange data among multiple languages like JSON but it's faster and smaller. For example, small integers (like flags or error code) are encoded into a single byte, and typical short strings only require an extra byte in addition to the strings themselves.
10 |
Check the website: http://msgpack.org
11 |
12 | ## supported types
13 |
Message pack specification was built to work mostly with primitive types. You shouldn't expect a serialization library, because msgpack is similiar to JSON - but is based on binary data instead of text.
14 | The types available are: signed/unsigned integer, single/double precision floating point, nil (null), boolean, array, associative array (map) and raw buffer.
15 | These types are mapped to Actionscript3 as the following:
16 | * signed integer -> int
17 | * unsigned integer -> uint
18 | * single/double precision floating point -> Number
19 | * nil -> null
20 | * boolean -> Boolean
21 | * array -> Array
22 | * associative array -> Object
23 | * raw buffer -> String or ByteArray
24 |
25 | ## changes
26 | ### 1.0.0 to 1.0.1
27 | * Using make tool to compile the binaries instead of batch files.
28 | * Improving README.md
29 | * Creating msgpack.org.md (as3-msgpack API document for msgpack.org)
30 |
31 | ### 0.4.1 to 1.0.0
32 | * The access interface has been totally modified. Now isn't possible to use singletons, to read/write message pack data you'll need create an object.
33 | * The names of classes and files were updated. The class MessagePack became MsgPack and the library MessagePack.swc became msgpack.swc.
34 | * Stream reading is available. If you read data from a buffer (IDataInput) and the object is not complete, the library stores the current status and then wait for more bytes.
35 |
36 | ## folders
37 | * bin: binaries (library and test applications);
38 | * lib: libraries used by test applications;
39 | * doc: documentation generated by asdoc;
40 | * src_lib: source code of the library;
41 | * src_test: source code of test applications (including a server and a client which exchange streaming messagepack packets through sockets);
42 |
43 | ## examples
44 | ### basic usage
45 |
The usage of MsgPack class is very simple. You need create an object and call read and write methods.
You may read streaming data making successive calls to msgpack.read method. Each MsgPack object can handle one stream at time.
62 | If all bytes are not available, the method returns incomplete (a special object).
63 | ```actionscript
64 | package
65 | {
66 | import flash.events.*;
67 | import flash.net.*;
68 |
69 | import org.msgpack.*;
70 |
71 | public class Streamer
72 | {
73 | private var msgpack:MsgPack;
74 | private var socket:Socket;
75 |
76 | public function Streamer()
77 | {
78 | msgpack = new MsgPack();
79 |
80 | // flash sockets implements the interfaces IDataInput and IDataOutput.
81 | // thus, these objects may be directly connected to as3-msgpack.
82 | socket = new Socket();
83 | socket.addEventListener(ProgressEvent.SOCKET_DATA, socketData);
84 | // connecting to our hypothetical server
85 | socket.connect("localhost", 5555);
86 | }
87 |
88 | public function send(data:*):void
89 | {
90 | // we'll write the encoded object straight into the socket (since it implements IDataOutput interface).
91 | msgpack.write(data, socket);
92 | socket.flush();
93 | }
94 |
95 | private function socketData(e:ProgressEvent):void
96 | {
97 | // until the data is ready, this call returns incomplete.
98 | var data:* = msgpack.read(socket);
99 |
100 | // is the object complete?
101 | if (data != incomplete)
102 | {
103 | trace("OBJECT READY:");
104 | trace(data);
105 | }
106 | }
107 | }
108 | }
109 | ```
110 |
111 | ### using flags
112 |
Currently there are two flags which you may use to initialize a MsgPack object:
113 | * MsgPackFlags.READ_RAW_AS_BYTE_ARRAY: message pack raw data is read as byte array instead of string;
114 | * MsgPackFlags.ACCEPT_LITTLE_ENDIAN: MsgPack objects will work with little endian buffers (message pack specification defines big endian as default).
115 |
116 | ```actionscript
117 | var msg:MsgPack;
118 |
119 | // use logical operator OR to set the flags.
120 | msgpack = new MsgPack(MsgPackFlags.READ_RAW_AS_BYTE_ARRAY | MsgPackFlags.ACCEPT_LITTLE_ENDIAN);
121 | ```
122 |
--------------------------------------------------------------------------------
/WISHLIST.md:
--------------------------------------------------------------------------------
1 | Things to create/fix:
2 |
3 | * NumberWorker always encodes values as double: before assembling the value, NumberWorker must check if the value fits a float and use it whenever is possible. ([see the code here](/src_lib/org/msgpack/NumberWorker.as#L36))
4 | * Support for the new version of MessagePack spec. (https://github.com/msgpack/msgpack/blob/master/spec.md)
5 | * Support for MessagePack-RPC. (https://github.com/msgpack-rpc/msgpack-rpc/blob/master/spec.md)
6 | * Avoid the issue of Number instances storing integer values (https://github.com/loteixeira/as3-msgpack/issues/4)
7 | * Create a prototype using C++ (or should we try to port the existing msgpack for C++?)
--------------------------------------------------------------------------------
/bin/as3_client/as3_client-app.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | as3-client
5 |
6 | 1.0
7 |
8 | as3_client
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 | as3_client
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 | as3_client.swf
26 | standard
27 | false
28 | true
29 | false
30 | portrait
31 | auto
32 | true
33 | true
34 | true
35 |
36 |
37 |
38 |
39 | false
40 |
41 | false
42 |
43 |
44 |
45 |
--------------------------------------------------------------------------------
/bin/as3_client/as3_client.air:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/loteixeira/as3-msgpack/cdbd4e4f71e45b460a88555d1b0f06890a24b53d/bin/as3_client/as3_client.air
--------------------------------------------------------------------------------
/bin/as3_client/as3_client.swf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/loteixeira/as3-msgpack/cdbd4e4f71e45b460a88555d1b0f06890a24b53d/bin/as3_client/as3_client.swf
--------------------------------------------------------------------------------
/bin/as3_server/as3_server-app.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | as3-server
5 |
6 | 1.0
7 |
8 | as3_server
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 | as3_server
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 | as3_server.swf
34 | standard
35 | false
36 | true
37 | false
38 | portrait
39 | auto
40 | true
41 | true
42 | true
43 |
44 |
45 |
46 |
47 | false
48 |
49 | false
50 |
51 |
52 |
53 |
54 |
55 |
--------------------------------------------------------------------------------
/bin/as3_server/as3_server.air:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/loteixeira/as3-msgpack/cdbd4e4f71e45b460a88555d1b0f06890a24b53d/bin/as3_server/as3_server.air
--------------------------------------------------------------------------------
/bin/as3_server/as3_server.swf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/loteixeira/as3-msgpack/cdbd4e4f71e45b460a88555d1b0f06890a24b53d/bin/as3_server/as3_server.swf
--------------------------------------------------------------------------------
/bin/lib_test.swf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/loteixeira/as3-msgpack/cdbd4e4f71e45b460a88555d1b0f06890a24b53d/bin/lib_test.swf
--------------------------------------------------------------------------------
/bin/msgpack.swc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/loteixeira/as3-msgpack/cdbd4e4f71e45b460a88555d1b0f06890a24b53d/bin/msgpack.swc
--------------------------------------------------------------------------------
/bin/stream_test.swf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/loteixeira/as3-msgpack/cdbd4e4f71e45b460a88555d1b0f06890a24b53d/bin/stream_test.swf
--------------------------------------------------------------------------------
/doc/AC_OETags.js:
--------------------------------------------------------------------------------
1 | ////////////////////////////////////////////////////////////////////////////////
2 | //
3 | // ADOBE SYSTEMS INCORPORATED
4 | // Copyright 2008 Adobe Systems Incorporated
5 | // All Rights Reserved.
6 | //
7 | // NOTICE: Adobe permits you to use, modify, and distribute this file
8 | // in accordance with the terms of the license agreement accompanying it.
9 | //
10 | ////////////////////////////////////////////////////////////////////////////////
11 |
12 |
13 | //v1.0
14 | function AC_AddExtension(src, ext)
15 | {
16 | if (src.indexOf('?') != -1)
17 | return src.replace(/\?/, ext+'?');
18 | else
19 | return src + ext;
20 | }
21 |
22 | function AC_Generateobj(objAttrs, params, embedAttrs)
23 | {
24 | var str = '';
34 |
35 | document.write(str);
36 | }
37 |
38 | function AC_FL_RunContent(){
39 | var ret =
40 | AC_GetArgs
41 | ( arguments, ".swf", "movie", "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
42 | , "application/x-shockwave-flash"
43 | );
44 | AC_Generateobj(ret.objAttrs, ret.params, ret.embedAttrs);
45 | }
46 |
47 | function AC_GetArgs(args, ext, srcParamName, classid, mimeType){
48 | var ret = new Object();
49 | ret.embedAttrs = new Object();
50 | ret.params = new Object();
51 | ret.objAttrs = new Object();
52 | for (var i=0; i < args.length; i=i+2){
53 | var currArg = args[i].toLowerCase();
54 |
55 | switch (currArg){
56 | case "classid":
57 | break;
58 | case "pluginspage":
59 | ret.embedAttrs[args[i]] = args[i+1];
60 | break;
61 | case "src":
62 | case "movie":
63 | args[i+1] = AC_AddExtension(args[i+1], ext);
64 | ret.embedAttrs["src"] = args[i+1];
65 | ret.params[srcParamName] = args[i+1];
66 | break;
67 | case "onafterupdate":
68 | case "onbeforeupdate":
69 | case "onblur":
70 | case "oncellchange":
71 | case "onclick":
72 | case "ondblClick":
73 | case "ondrag":
74 | case "ondragend":
75 | case "ondragenter":
76 | case "ondragleave":
77 | case "ondragover":
78 | case "ondrop":
79 | case "onfinish":
80 | case "onfocus":
81 | case "onhelp":
82 | case "onmousedown":
83 | case "onmouseup":
84 | case "onmouseover":
85 | case "onmousemove":
86 | case "onmouseout":
87 | case "onkeypress":
88 | case "onkeydown":
89 | case "onkeyup":
90 | case "onload":
91 | case "onlosecapture":
92 | case "onpropertychange":
93 | case "onreadystatechange":
94 | case "onrowsdelete":
95 | case "onrowenter":
96 | case "onrowexit":
97 | case "onrowsinserted":
98 | case "onstart":
99 | case "onscroll":
100 | case "onbeforeeditfocus":
101 | case "onactivate":
102 | case "onbeforedeactivate":
103 | case "ondeactivate":
104 | case "type":
105 | case "codebase":
106 | ret.objAttrs[args[i]] = args[i+1];
107 | break;
108 | case "width":
109 | case "height":
110 | case "align":
111 | case "vspace":
112 | case "hspace":
113 | case "class":
114 | case "title":
115 | case "accesskey":
116 | case "name":
117 | case "id":
118 | case "tabindex":
119 | ret.embedAttrs[args[i]] = ret.objAttrs[args[i]] = args[i+1];
120 | break;
121 | default:
122 | ret.embedAttrs[args[i]] = ret.params[args[i]] = args[i+1];
123 | }
124 | }
125 | ret.objAttrs["classid"] = classid;
126 | if (mimeType) ret.embedAttrs["type"] = mimeType;
127 | return ret;
128 | }
129 |
130 |
--------------------------------------------------------------------------------
/doc/all-classes.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | All Classes - API Documentation
5 |
6 |
7 |
8 |
9 |
10 |
11 |
Documentation for classes includes syntax, usage information, and code samples for methods, properties, and event handlers and listeners for those APIs that belong to a specific class in ActionScript. The classes are listed alphabetically. If you are not sure to which class a certain method or property belongs, you can look it up in the Index.
--------------------------------------------------------------------------------
/doc/override.css:
--------------------------------------------------------------------------------
1 | /*
2 | ////////////////////////////////////////////////////////////////////////////////
3 | //
4 | // ADOBE SYSTEMS INCORPORATED
5 | // Copyright 2008 Adobe Systems Incorporated
6 | // All Rights Reserved.
7 | //
8 | // NOTICE: Adobe permits you to use, modify, and distribute this file
9 | // in accordance with the terms of the license agreement accompanying it.
10 | //
11 | ////////////////////////////////////////////////////////////////////////////////
12 | */
--------------------------------------------------------------------------------
/doc/package-frame.html:
--------------------------------------------------------------------------------
1 |
2 |
3 | as3-msgpack1.0.1
4 |
5 |
6 |
7 |
8 |
9 |
10 |
Frame Alert
11 |
This document is designed to be viewed using the frames feature. If you see this message, you are using a non-frame-capable web client.
12 |
13 | Link toNon-frame version.
14 |
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/doc/package-list.html:
--------------------------------------------------------------------------------
1 | Package List - API Documentation
MessagePack for Actionscript3 (Flash, Flex and AIR).
2 |
as3-msgpack was designed to work with the interfaces IDataInput and IDataOutput, thus the API might be easily connected with the native classes that handle binary data (such as ByteArray, Socket, FileStream and URLStream).
3 | Moreover, as3-msgpack is capable of decoding data from binary streams.
4 | Get started: http://loteixeira.github.io/lib/2013/08/19/as3-msgpack/
For downloads, source code and further information, check the project repository: https://github.com/loteixeira/as3-msgpack.
22 |
--------------------------------------------------------------------------------
/src_lib/org/msgpack/ArrayWorker.as:
--------------------------------------------------------------------------------
1 | //
2 | // as3-msgpack (MessagePack for Actionscript3)
3 | // Copyright (C) 2013 Lucas Teixeira (Disturbed Coder)
4 | //
5 | //
6 | // Licensed under the Apache License, Version 2.0 (the "License");
7 | // you may not use this file except in compliance with the License.
8 | // You may obtain a copy of the License at
9 | //
10 | // http://www.apache.org/licenses/LICENSE-2.0
11 | //
12 | // Unless required by applicable law or agreed to in writing, software
13 | // distributed under the License is distributed on an "AS IS" BASIS,
14 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | // See the License for the specific language governing permissions and
16 | // limitations under the License.
17 | //
18 | package org.msgpack
19 | {
20 | import flash.utils.*;
21 |
22 | internal class ArrayWorker extends Worker
23 | {
24 | public static function checkType(byte:int):Boolean
25 | {
26 | return (byte & 0xf0) == 0x90 || byte == 0xdc || byte == 0xdd;
27 | }
28 |
29 | private var array:Array;
30 | private var workers:Array;
31 | private var count:int;
32 |
33 | public function ArrayWorker(factory:Factory, byte:int = -1)
34 | {
35 | super(factory, byte);
36 | array = [];
37 | workers = [];
38 | count = -1;
39 | }
40 |
41 | override public function assembly(data:*, destination:IDataOutput):void
42 | {
43 | var l:uint = data.length;
44 |
45 | if (l < 16)
46 | {
47 | // fix array
48 | destination.writeByte(0x90 | l);
49 | }
50 | else if (l < 65536)
51 | {
52 | // array 16
53 | destination.writeByte(0xdc);
54 | destination.writeShort(l);
55 | }
56 | else
57 | {
58 | // array 32
59 | destination.writeByte(0xdd);
60 | destination.writeUnsignedInt(l);
61 | }
62 |
63 | // write elements
64 | for (var i:uint = 0; i < l; i++)
65 | {
66 | var worker:Worker = factory.getWorkerByType(data[i]);
67 | worker.assembly(data[i], destination);
68 | }
69 | }
70 |
71 | override public function disassembly(source:IDataInput):*
72 | {
73 | if (count == -1)
74 | {
75 | if ((byte & 0xf0) == 0x90)
76 | count = byte & 0x0f
77 | else if (byte == 0xdc && source.bytesAvailable >= 2)
78 | count = source.readUnsignedShort();
79 | else if (byte == 0xdd && source.bytesAvailable >= 4)
80 | count = source.readUnsignedInt();
81 | }
82 |
83 | if (array.length < count)
84 | {
85 | var first:uint = array.length;
86 |
87 | for (var i:uint = first; i < count; i++)
88 | {
89 | if (!workers[i])
90 | {
91 | if (source.bytesAvailable == 0)
92 | break;
93 |
94 | workers.push(factory.getWorkerByByte(source));
95 | }
96 |
97 | var obj:* = workers[i].disassembly(source);
98 |
99 | if (obj != incomplete)
100 | {
101 | array.push(obj);
102 | continue;
103 | }
104 |
105 | break;
106 | }
107 | }
108 |
109 | if (array.length == count)
110 | return array;
111 |
112 | return incomplete;
113 | }
114 | }
115 | }
--------------------------------------------------------------------------------
/src_lib/org/msgpack/BooleanWorker.as:
--------------------------------------------------------------------------------
1 | //
2 | // as3-msgpack (MessagePack for Actionscript3)
3 | // Copyright (C) 2013 Lucas Teixeira (Disturbed Coder)
4 | //
5 | //
6 | // Licensed under the Apache License, Version 2.0 (the "License");
7 | // you may not use this file except in compliance with the License.
8 | // You may obtain a copy of the License at
9 | //
10 | // http://www.apache.org/licenses/LICENSE-2.0
11 | //
12 | // Unless required by applicable law or agreed to in writing, software
13 | // distributed under the License is distributed on an "AS IS" BASIS,
14 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | // See the License for the specific language governing permissions and
16 | // limitations under the License.
17 | //
18 | package org.msgpack
19 | {
20 | import flash.utils.*;
21 |
22 | internal class BooleanWorker extends Worker
23 | {
24 | public static function checkType(byte:int):Boolean
25 | {
26 | return byte == 0xc3 || byte == 0xc2;
27 | }
28 |
29 | public function BooleanWorker(factory:Factory, byte:int = -1)
30 | {
31 | super(factory, byte);
32 | }
33 |
34 | override public function assembly(data:*, destination:IDataOutput):void
35 | {
36 | destination.writeByte(data ? 0xc3 : 0xc2);
37 | }
38 |
39 | override public function disassembly(source:IDataInput):*
40 | {
41 | return byte == 0xc3;
42 | }
43 | }
44 | }
--------------------------------------------------------------------------------
/src_lib/org/msgpack/Factory.as:
--------------------------------------------------------------------------------
1 | //
2 | // as3-msgpack (MessagePack for Actionscript3)
3 | // Copyright (C) 2013 Lucas Teixeira (Disturbed Coder)
4 | //
5 | //
6 | // Licensed under the Apache License, Version 2.0 (the "License");
7 | // you may not use this file except in compliance with the License.
8 | // You may obtain a copy of the License at
9 | //
10 | // http://www.apache.org/licenses/LICENSE-2.0
11 | //
12 | // Unless required by applicable law or agreed to in writing, software
13 | // distributed under the License is distributed on an "AS IS" BASIS,
14 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | // See the License for the specific language governing permissions and
16 | // limitations under the License.
17 | //
18 | package org.msgpack
19 | {
20 | import flash.utils.*;
21 |
22 | /**
23 | * The factory class is responsible for managing the workers which will encode/decode data. Each MsgPack instance has it own factory.
24 | * You shouldn't instantiate this class using operator new. Instances are created internally by MsgPack objects.
25 | * @see MsgPack
26 | */
27 | public class Factory
28 | {
29 | private var flags:uint;
30 | private var workers:Object;
31 | private var root:Worker;
32 |
33 | /**
34 | * @private
35 | */
36 | public function Factory(flags:uint)
37 | {
38 | this.flags = flags;
39 | workers = {};
40 | }
41 |
42 | /**
43 | * Assign workerClass to the specified classes.
44 | * Note: all parameters must be of type Class.
45 | * @param workerClass The worker class.
46 | * @param ...args List of classes to assign the worker.
47 | * @see Worker
48 | * @throws org.msgpack.MsgPackError Thrown when you try to assign the worker to ordinary objects, not classes.
49 | */
50 | public function assign(workerClass:Class, ...args):void
51 | {
52 | for (var i:uint = 0; i < args.length; i++)
53 | {
54 | if (args[i] != null && !(args[i] is Class))
55 | throw new MsgPackError("Workers must be assigned to classes not regular objects");
56 |
57 | var typeName:String = getQualifiedClassName(args[i]);
58 | workers[typeName] = workerClass;
59 | }
60 | }
61 |
62 | /**
63 | * Remove the worker from the class which was assigned. If the worker was assigned to several classes, you must call this method for each one.
64 | * @param type The class type which the worker was assigned to.
65 | * @see Worker
66 | */
67 | public function unassign(type:Class):void
68 | {
69 | var typeName:String = getQualifiedClassName(type);
70 | workers[typeName] = undefined;
71 | }
72 |
73 | /**
74 | * Return the worker assigned to the class of data. For example, if data is the value 1.5 Number class is used.
75 | * @param data Data type used to find the related worker.
76 | * @return Return the related worker.
77 | * @throws org.msgpack.MsgPackError Thrown when no worker is assigned to the class of data.
78 | */
79 | public function getWorkerByType(data:*):Worker
80 | {
81 | var typeName:String = data == null ? "null" : getQualifiedClassName(data);
82 |
83 | if (!workers[typeName])
84 | throw new MsgPackError("Worker for type '" + typeName + "' not found");
85 |
86 | return new workers[typeName](this);
87 | }
88 |
89 | /**
90 | * Return the worker which is capable of decoding the next byte of the input stream.
91 | * @param source Input stream.
92 | * @return Return the related worker.
93 | * @throws org.msgpack.MsgPackError Thrown when no worker is capable of decode the next byte of the input stream.
94 | */
95 | public function getWorkerByByte(source:IDataInput):Worker
96 | {
97 | var byte:int = source.readByte() & 0xff;
98 |
99 | for each (var workerClass:Class in workers)
100 | {
101 | if (!workerClass["checkType"](byte))
102 | continue;
103 |
104 | return new workerClass(this, byte);
105 | }
106 |
107 | throw new MsgPackError("Worker for signature 0x" + byte.toString(16) + " not found");
108 | }
109 |
110 | /**
111 | * Check if the flag is true.
112 | * @param f Flag value.
113 | * @return True or flase.
114 | * @see MsgPackFlags#ACCEPT_LITTLE_ENDIAN
115 | * @see MsgPackFlags#READ_RAW_AS_BYTE_ARRAY
116 | */
117 | public function checkFlag(f:uint):Boolean
118 | {
119 | return (f & flags) != 0;
120 | }
121 | }
122 | }
--------------------------------------------------------------------------------
/src_lib/org/msgpack/IntegerWorker.as:
--------------------------------------------------------------------------------
1 | //
2 | // as3-msgpack (MessagePack for Actionscript3)
3 | // Copyright (C) 2013 Lucas Teixeira (Disturbed Coder)
4 | //
5 | //
6 | // Licensed under the Apache License, Version 2.0 (the "License");
7 | // you may not use this file except in compliance with the License.
8 | // You may obtain a copy of the License at
9 | //
10 | // http://www.apache.org/licenses/LICENSE-2.0
11 | //
12 | // Unless required by applicable law or agreed to in writing, software
13 | // distributed under the License is distributed on an "AS IS" BASIS,
14 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | // See the License for the specific language governing permissions and
16 | // limitations under the License.
17 | //
18 | package org.msgpack
19 | {
20 | import flash.utils.*;
21 |
22 | internal class IntegerWorker extends Worker
23 | {
24 | public static function checkType(byte:int):Boolean
25 | {
26 | return (byte & 0x80) == 0 || (byte & 0xe0) == 0xe0 || byte == 0xcc || byte == 0xcd ||
27 | byte == 0xce || byte == 0xcf || byte == 0xd0 || byte == 0xd1 ||
28 | byte == 0xd2 || byte == 0xd3;
29 | }
30 |
31 | public function IntegerWorker(factory:Factory, byte:int = -1)
32 | {
33 | super(factory, byte);
34 | }
35 |
36 | override public function assembly(data:*, destination:IDataOutput):void
37 | {
38 | if (data < -(1 << 5))
39 | {
40 | if (data < -(1 << 15))
41 | {
42 | // signed 32
43 | destination.writeByte(0xd2);
44 | destination.writeInt(data);
45 | }
46 | else if (data < -(1 << 7))
47 | {
48 | // signed 16
49 | destination.writeByte(0xd1);
50 | destination.writeShort(data);
51 | }
52 | else
53 | {
54 | // signed 8
55 | destination.writeByte(0xd0);
56 | destination.writeByte(data);
57 | }
58 | }
59 | else if (data < (1 << 7))
60 | {
61 | // fixnum
62 | destination.writeByte(data);
63 | }
64 | else
65 | {
66 | if (data < (1 << 8))
67 | {
68 | // unsigned 8
69 | destination.writeByte(0xcc);
70 | destination.writeByte(data);
71 | }
72 | else if (data < (1 << 16))
73 | {
74 | // unsigned 16
75 | destination.writeByte(0xcd);
76 | destination.writeShort(data);
77 | }
78 | else
79 | {
80 | // unsigned 32
81 | destination.writeByte(0xce);
82 | destination.writeUnsignedInt(data);
83 | }
84 | }
85 | }
86 |
87 | override public function disassembly(source:IDataInput):*
88 | {
89 | var i:uint;
90 | var data:*;
91 |
92 | if ((byte & 0x80) == 0)
93 | {
94 | // positive fixnum
95 | return byte;
96 | }
97 | else if ((byte & 0xe0) == 0xe0)
98 | {
99 | // negative fixnum
100 | return byte - 0xff - 1;
101 | }
102 | else if (byte == 0xcc && source.bytesAvailable >= 1)
103 | {
104 | // unsigned byte
105 | return source.readUnsignedByte();
106 | }
107 | else if (byte == 0xcd && source.bytesAvailable >= 2)
108 | {
109 | // unsigned short
110 | return source.readUnsignedShort();
111 | }
112 | else if (byte == 0xce && source.bytesAvailable >= 4)
113 | {
114 | // unsigned int
115 | return source.readUnsignedInt();
116 | }
117 | else if (byte == 0xcf && source.bytesAvailable >= 8)
118 | {
119 | // TODO: can't read 64 bits unsigned integers
120 | for (i = 0; i < 8; i++)
121 | source.readByte();
122 |
123 | return NaN;
124 | }
125 | else if (byte == 0xd0 && source.bytesAvailable >= 1)
126 | {
127 | // signed byte
128 | return source.readByte();
129 | }
130 | else if (byte == 0xd1 && source.bytesAvailable >= 2)
131 | {
132 | // signed short
133 | return source.readShort();
134 | }
135 | else if (byte == 0xd2 && source.bytesAvailable >= 4)
136 | {
137 | // signed int
138 | return source.readInt();
139 | }
140 | else if (byte == 0xd3 && source.bytesAvailable >= 8)
141 | {
142 | // TODO: can't read 64 bits integers
143 | for (i = 0; i < 8; i++)
144 | source.readByte();
145 |
146 | return NaN;
147 | }
148 |
149 | return incomplete;
150 | }
151 | }
152 | }
--------------------------------------------------------------------------------
/src_lib/org/msgpack/MapWorker.as:
--------------------------------------------------------------------------------
1 | //
2 | // as3-msgpack (MessagePack for Actionscript3)
3 | // Copyright (C) 2013 Lucas Teixeira (Disturbed Coder)
4 | //
5 | //
6 | // Licensed under the Apache License, Version 2.0 (the "License");
7 | // you may not use this file except in compliance with the License.
8 | // You may obtain a copy of the License at
9 | //
10 | // http://www.apache.org/licenses/LICENSE-2.0
11 | //
12 | // Unless required by applicable law or agreed to in writing, software
13 | // distributed under the License is distributed on an "AS IS" BASIS,
14 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | // See the License for the specific language governing permissions and
16 | // limitations under the License.
17 | //
18 | package org.msgpack
19 | {
20 | import flash.utils.*;
21 |
22 | internal class MapWorker extends Worker
23 | {
24 | public static function checkType(byte:int):Boolean
25 | {
26 | return (byte & 0xf0) == 0x80 || byte == 0xde || byte == 0xdf;
27 | }
28 |
29 | private var count:int;
30 | private var ready:int;
31 | private var map:Object;
32 | private var keyWorker:Worker;
33 | private var valWorker:Worker;
34 | private var key:*;
35 | private var val:*;
36 |
37 | public function MapWorker(factory:Factory, byte:int = -1)
38 | {
39 | super(factory, byte);
40 | count = -1;
41 | ready = 0;
42 | map = {};
43 | key = incomplete;
44 | val = incomplete;
45 | }
46 |
47 | override public function assembly(data:*, destination:IDataOutput):void
48 | {
49 | var elements:Array = [];
50 |
51 | for (var key:String in data)
52 | elements.push(key);
53 |
54 | var l:uint = elements.length;
55 |
56 | if (l < 16)
57 | {
58 | // fix map
59 | destination.writeByte(0x80 | l);
60 | }
61 | else if (l < 65536)
62 | {
63 | // map 16
64 | destination.writeByte(0xde);
65 | destination.writeShort(l);
66 | }
67 | else
68 | {
69 | // map 32
70 | destination.writeByte(0xdf);
71 | destination.writeUnsignedInt(l);
72 | }
73 |
74 | for (var i:uint = 0; i < l; i++)
75 | {
76 | var elemKey:String = elements[i];
77 |
78 | var keyWorker:Worker = factory.getWorkerByType(elemKey);
79 | keyWorker.assembly(elemKey, destination);
80 |
81 | var valWorker:Worker = factory.getWorkerByType(data[elemKey]);
82 | valWorker.assembly(data[elemKey], destination);
83 | }
84 | }
85 |
86 | override public function disassembly(source:IDataInput):*
87 | {
88 | if (count == -1)
89 | {
90 | if ((byte & 0xf0) == 0x80)
91 | count = byte & 0x0f;
92 | else if (byte == 0xde && source.bytesAvailable >= 2)
93 | count = source.readUnsignedShort();
94 | else if (byte == 0xdf && source.bytesAvailable >= 4)
95 | count = source.readUnsignedInt();
96 | }
97 |
98 | if (ready < count)
99 | {
100 | var first:uint = ready;
101 |
102 | for (var i:uint = first; i < count; i++)
103 | {
104 | if (key == incomplete)
105 | {
106 | if (!keyWorker)
107 | {
108 | if (source.bytesAvailable == 0)
109 | break;
110 |
111 | keyWorker = factory.getWorkerByByte(source);
112 | }
113 |
114 | key = keyWorker.disassembly(source);
115 | }
116 |
117 | if (key != incomplete && val == incomplete)
118 | {
119 | if (!valWorker)
120 | {
121 | if (source.bytesAvailable == 0)
122 | break;
123 |
124 | valWorker = factory.getWorkerByByte(source);
125 | }
126 |
127 | val = valWorker.disassembly(source);
128 | }
129 |
130 | if (key != incomplete && val != incomplete)
131 | {
132 | map[key.toString()] = val;
133 | keyWorker = undefined;
134 | valWorker = undefined;
135 | key = incomplete;
136 | val = incomplete;
137 | ready++;
138 | continue;
139 | }
140 |
141 | break;
142 | }
143 | }
144 |
145 | if (ready == count)
146 | return map;
147 |
148 | return incomplete;
149 | }
150 | }
151 | }
--------------------------------------------------------------------------------
/src_lib/org/msgpack/MsgPack.as:
--------------------------------------------------------------------------------
1 | //
2 | // as3-msgpack (MessagePack for Actionscript3)
3 | // Copyright (C) 2013 Lucas Teixeira (Disturbed Coder)
4 | //
5 | //
6 | // Licensed under the Apache License, Version 2.0 (the "License");
7 | // you may not use this file except in compliance with the License.
8 | // You may obtain a copy of the License at
9 | //
10 | // http://www.apache.org/licenses/LICENSE-2.0
11 | //
12 | // Unless required by applicable law or agreed to in writing, software
13 | // distributed under the License is distributed on an "AS IS" BASIS,
14 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | // See the License for the specific language governing permissions and
16 | // limitations under the License.
17 | //
18 | package org.msgpack
19 | {
20 | import flash.utils.*;
21 |
22 | /**
23 | * MessagePack class. Use objects of this class to read and write message pack data.
24 | * Each MsgPack instance has a Factory instance.
25 | * @see Factory
26 | */
27 | public class MsgPack
28 | {
29 | //
30 | // static attributes
31 | //
32 | /**
33 | * Major version value.
34 | */
35 | public static const MAJOR:uint = 1;
36 | /**
37 | * Minor version value.
38 | */
39 | public static const MINOR:uint = 0;
40 | /**
41 | * Revision version value;
42 | */
43 | public static const REVISION:uint = 1;
44 |
45 | /**
46 | * Get full version as string.
47 | * @return Full version string.
48 | */
49 | public static function get VERSION():String
50 | {
51 | return MAJOR + "." + MINOR + "." + REVISION;
52 | }
53 |
54 | //
55 | // private attributes
56 | //
57 | private var _factory:Factory;
58 | private var root:Worker;
59 |
60 |
61 | //
62 | // constructor
63 | //
64 | /**
65 | * Create a new instance of MsgPack capable of reading/writing data.
66 | * You can decode streaming data using the method read.
67 | * The standard workers are:
68 | *
NullWorker: null
69 | *
BooleanWorker: Boolean
70 | *
IntegerWorker: int and uint
71 | *
NumberWorker: Number
72 | *
ArrayWorker: Array
73 | *
RawWorker: ByteArray or String
74 | *
MapWorker: Object
75 | * @param flags Set of flags capable of customizing the runtime behavior of this object.
76 | * @see #read()
77 | * @see #write()
78 | * @see Worker
79 | * @see MsgPackFlags#READ_RAW_AS_BYTE_ARRAY
80 | * @see MsgPackFlags#ACCEPT_LITTLE_ENDIAN
81 | * @see Factory#checkFlag()
82 | */
83 | public function MsgPack(flags:uint = 0)
84 | {
85 | _factory = new Factory(flags);
86 | _factory.assign(NullWorker, null);
87 | _factory.assign(BooleanWorker, Boolean);
88 | _factory.assign(IntegerWorker, int, uint);
89 | _factory.assign(NumberWorker, Number);
90 | _factory.assign(ArrayWorker, Array);
91 | _factory.assign(RawWorker, ByteArray, String);
92 | _factory.assign(MapWorker, Object);
93 | }
94 |
95 | //
96 | // getters and setters
97 | //
98 | /**
99 | * Get the factory associated to this object.
100 | * @return Factory instance used by this instance.
101 | * @see Worker
102 | */
103 | public function get factory():Factory
104 | {
105 | return _factory;
106 | }
107 |
108 | //
109 | // public interface
110 | //
111 | /**
112 | * Write an object in output buffer.
113 | * @param data Object to be encoded
114 | * @param output Any object that implements IDataOutput interface (ByteArray, Socket, URLStream, etc).
115 | * @return Return output whether it isn't null. Otherwise return a new ByteArray.
116 | * @see Worker#assembly()
117 | */
118 | public function write(data:*, output:IDataOutput = null):*
119 | {
120 | var worker:Worker = _factory.getWorkerByType(data);
121 |
122 | if (!output)
123 | output = new ByteArray();
124 |
125 | checkBigEndian(output);
126 |
127 | worker.assembly(data, output);
128 | return output;
129 | }
130 |
131 | /**
132 | * Read an object from input buffer. This method supports streaming.
133 | * If the object cannot be completely decoded (not all bytes available in input), incomplete object is returned.
134 | * However, the internal state (the part that was already decoded) is saved. Thus, you can read from a stream if you make successive calls to this method.
135 | * If all bytes are available, the decoded object is returned.
136 | * @param input Any object that implements IDataInput interface (ByteArray, Socket, URLStream, etc).
137 | * @return Return the decoded object if all bytes were available in the input stream, otherwise returns incomplete object.
138 | * @see org.msgpack#incomplete
139 | * @see Worker#disassembly()
140 | */
141 | public function read(input:IDataInput):*
142 | {
143 | checkBigEndian(input);
144 |
145 | if (!root)
146 | {
147 | if (input.bytesAvailable == 0)
148 | return incomplete;
149 |
150 | root = _factory.getWorkerByByte(input);
151 | }
152 |
153 | var obj:* = root.disassembly(input);
154 |
155 | if (obj != incomplete)
156 | root = undefined;
157 |
158 | return obj;
159 | }
160 |
161 | private function checkBigEndian(dataStream:*):void
162 | {
163 | if (dataStream.endian == "littleEndian" && !_factory.checkFlag(MsgPackFlags.ACCEPT_LITTLE_ENDIAN))
164 | throw new MsgPackError("Object uses little endian but MessagePack was designed for big endian. To avoid this error use the flag ACCEPT_LITTLE_ENDIAN.");
165 | }
166 | }
167 | }
--------------------------------------------------------------------------------
/src_lib/org/msgpack/MsgPackError.as:
--------------------------------------------------------------------------------
1 | //
2 | // as3-msgpack (MessagePack for Actionscript3)
3 | // Copyright (C) 2013 Lucas Teixeira (Disturbed Coder)
4 | //
5 | //
6 | // Licensed under the Apache License, Version 2.0 (the "License");
7 | // you may not use this file except in compliance with the License.
8 | // You may obtain a copy of the License at
9 | //
10 | // http://www.apache.org/licenses/LICENSE-2.0
11 | //
12 | // Unless required by applicable law or agreed to in writing, software
13 | // distributed under the License is distributed on an "AS IS" BASIS,
14 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | // See the License for the specific language governing permissions and
16 | // limitations under the License.
17 | //
18 | package org.msgpack
19 | {
20 | /**
21 | * Message Pack error class.
22 | */
23 | public class MsgPackError extends Error
24 | {
25 | public function MsgPackError(message:String)
26 | {
27 | super(message);
28 | }
29 | }
30 | }
--------------------------------------------------------------------------------
/src_lib/org/msgpack/MsgPackFlags.as:
--------------------------------------------------------------------------------
1 | //
2 | // as3-msgpack (MessagePack for Actionscript3)
3 | // Copyright (C) 2013 Lucas Teixeira (Disturbed Coder)
4 | //
5 | //
6 | // Licensed under the Apache License, Version 2.0 (the "License");
7 | // you may not use this file except in compliance with the License.
8 | // You may obtain a copy of the License at
9 | //
10 | // http://www.apache.org/licenses/LICENSE-2.0
11 | //
12 | // Unless required by applicable law or agreed to in writing, software
13 | // distributed under the License is distributed on an "AS IS" BASIS,
14 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | // See the License for the specific language governing permissions and
16 | // limitations under the License.
17 | //
18 | package org.msgpack
19 | {
20 | public class MsgPackFlags
21 | {
22 | /**
23 | * Flag which indicates that raw buffers must be decoded as a ByteArray instead of a String.
24 | * @see Factory#checkFlag()
25 | */
26 | public static const READ_RAW_AS_BYTE_ARRAY:uint = 0x01;
27 | /**
28 | * Flag which indicates that little endian buffers must be accepted (MessagePack specification works only with big endian).
29 | * @see Factory#checkFlag()
30 | */
31 | public static const ACCEPT_LITTLE_ENDIAN:uint = 0x02;
32 | }
33 | }
--------------------------------------------------------------------------------
/src_lib/org/msgpack/NullWorker.as:
--------------------------------------------------------------------------------
1 | //
2 | // as3-msgpack (MessagePack for Actionscript3)
3 | // Copyright (C) 2013 Lucas Teixeira (Disturbed Coder)
4 | //
5 | //
6 | // Licensed under the Apache License, Version 2.0 (the "License");
7 | // you may not use this file except in compliance with the License.
8 | // You may obtain a copy of the License at
9 | //
10 | // http://www.apache.org/licenses/LICENSE-2.0
11 | //
12 | // Unless required by applicable law or agreed to in writing, software
13 | // distributed under the License is distributed on an "AS IS" BASIS,
14 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | // See the License for the specific language governing permissions and
16 | // limitations under the License.
17 | //
18 | package org.msgpack
19 | {
20 | import flash.utils.*;
21 |
22 | internal class NullWorker extends Worker
23 | {
24 | public static function checkType(byte:int):Boolean
25 | {
26 | return byte == 0xc0;
27 | }
28 |
29 | public function NullWorker(factory:Factory, byte:int = -1)
30 | {
31 | super(factory, byte);
32 | }
33 |
34 | override public function assembly(data:*, destination:IDataOutput):void
35 | {
36 | destination.writeByte(0xc0);
37 | }
38 |
39 | override public function disassembly(source:IDataInput):*
40 | {
41 | return null;
42 | }
43 | }
44 | }
--------------------------------------------------------------------------------
/src_lib/org/msgpack/NumberWorker.as:
--------------------------------------------------------------------------------
1 | //
2 | // as3-msgpack (MessagePack for Actionscript3)
3 | // Copyright (C) 2013 Lucas Teixeira (Disturbed Coder)
4 | //
5 | //
6 | // Licensed under the Apache License, Version 2.0 (the "License");
7 | // you may not use this file except in compliance with the License.
8 | // You may obtain a copy of the License at
9 | //
10 | // http://www.apache.org/licenses/LICENSE-2.0
11 | //
12 | // Unless required by applicable law or agreed to in writing, software
13 | // distributed under the License is distributed on an "AS IS" BASIS,
14 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | // See the License for the specific language governing permissions and
16 | // limitations under the License.
17 | //
18 | package org.msgpack
19 | {
20 | import flash.utils.*;
21 |
22 | internal class NumberWorker extends Worker
23 | {
24 | public static function checkType(byte:int):Boolean
25 | {
26 | return byte == 0xca || byte == 0xcb;
27 | }
28 |
29 | public function NumberWorker(factory:Factory, byte:int = -1)
30 | {
31 | super(factory, byte);
32 | }
33 |
34 | override public function assembly(data:*, destination:IDataOutput):void
35 | {
36 | destination.writeByte(0xcb);
37 | destination.writeDouble(data);
38 | }
39 |
40 | override public function disassembly(source:IDataInput):*
41 | {
42 | var data:Number;
43 |
44 | if (byte == 0xca && source.bytesAvailable >= 4)
45 | return source.readFloat();
46 | else if (byte == 0xcb && source.bytesAvailable >= 8)
47 | return source.readDouble();
48 |
49 | return incomplete;
50 | }
51 | }
52 | }
--------------------------------------------------------------------------------
/src_lib/org/msgpack/RawWorker.as:
--------------------------------------------------------------------------------
1 | //
2 | // as3-msgpack (MessagePack for Actionscript3)
3 | // Copyright (C) 2013 Lucas Teixeira (Disturbed Coder)
4 | //
5 | // Contribution:
6 | // * 2012.10.22 - ccrossley (https://github.com/ccrossley)
7 | // * 2013.01.22 - sparkle (https://github.com/sparkle)
8 | //
9 | //
10 | // Licensed under the Apache License, Version 2.0 (the "License");
11 | // you may not use this file except in compliance with the License.
12 | // You may obtain a copy of the License at
13 | //
14 | // http://www.apache.org/licenses/LICENSE-2.0
15 | //
16 | // Unless required by applicable law or agreed to in writing, software
17 | // distributed under the License is distributed on an "AS IS" BASIS,
18 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 | // See the License for the specific language governing permissions and
20 | // limitations under the License.
21 | //
22 | package org.msgpack
23 | {
24 | import flash.utils.*;
25 |
26 | internal class RawWorker extends Worker
27 | {
28 | private var count:int;
29 |
30 | public static function checkType(byte:int):Boolean
31 | {
32 | return (byte & 0xe0) == 0xa0 || byte == 0xda || byte == 0xdb;
33 | }
34 |
35 | public function RawWorker(factory:Factory, byte:int = -1)
36 | {
37 | super(factory, byte);
38 | count = -1;
39 | }
40 |
41 | override public function assembly(data:*, destination:IDataOutput):void
42 | {
43 | var bytes:ByteArray;
44 |
45 | if (data is ByteArray)
46 | {
47 | bytes = data;
48 | }
49 | else
50 | {
51 | bytes = new ByteArray();
52 | bytes.writeUTFBytes(data.toString());
53 | }
54 |
55 | if (bytes.length < 32)
56 | {
57 | // fix raw
58 | destination.writeByte(0xa0 | bytes.length);
59 | }
60 | else if (bytes.length < 65536)
61 | {
62 | // raw 16
63 | destination.writeByte(0xda);
64 | destination.writeShort(bytes.length);
65 | }
66 | else
67 | {
68 | // raw 32
69 | destination.writeByte(0xdb);
70 | destination.writeInt(bytes.length);
71 | }
72 |
73 | destination.writeBytes(bytes);
74 | }
75 |
76 | override public function disassembly(source:IDataInput):*
77 | {
78 | if (count == -1)
79 | {
80 | if ((byte & 0xe0) == 0xa0)
81 | count = byte & 0x1f;
82 | else if (byte == 0xda && source.bytesAvailable >= 2)
83 | count = source.readUnsignedShort();
84 | else if (byte == 0xdb && source.bytesAvailable >= 4)
85 | count = source.readUnsignedInt();
86 | }
87 |
88 | if (source.bytesAvailable >= count)
89 | {
90 | var data:ByteArray = new ByteArray();
91 |
92 | // we need to check whether the byte array is empty to avoid EOFError
93 | // thanks to ccrossley
94 | if (count > 0)
95 | source.readBytes(data, 0, count);
96 |
97 | // using flags this worker may return RAW as String (not only as ByteArray like previous version)
98 | // thanks to sparkle
99 | return factory.checkFlag(MsgPackFlags.READ_RAW_AS_BYTE_ARRAY) ? data : data.toString();
100 | }
101 |
102 | return incomplete;
103 | }
104 | }
105 | }
--------------------------------------------------------------------------------
/src_lib/org/msgpack/Worker.as:
--------------------------------------------------------------------------------
1 | //
2 | // as3-msgpack (MessagePack for Actionscript3)
3 | // Copyright (C) 2013 Lucas Teixeira (Disturbed Coder)
4 | //
5 | //
6 | // Licensed under the Apache License, Version 2.0 (the "License");
7 | // you may not use this file except in compliance with the License.
8 | // You may obtain a copy of the License at
9 | //
10 | // http://www.apache.org/licenses/LICENSE-2.0
11 | //
12 | // Unless required by applicable law or agreed to in writing, software
13 | // distributed under the License is distributed on an "AS IS" BASIS,
14 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | // See the License for the specific language governing permissions and
16 | // limitations under the License.
17 | //
18 | package org.msgpack
19 | {
20 | import flash.utils.*;
21 |
22 | /**
23 | * Worker base class. Workers are used in factories where they are assigned to encode/decode message pack data of a type. Each type of data uses a own worker.
24 | * If you want to create a custom worker (for a custom type) you need to create a class which extends this class.
25 | * @see Factory
26 | */
27 | public class Worker
28 | {
29 | /**
30 | * Static method which checks whether this worker is capable of decoding the data type of this byte.
31 | * Children classes must rewrite this static method.
32 | * @param byte Signature byte of a message pack object.
33 | * @return Must return true if this worker is capable of decoding the following data.
34 | */
35 | public static function checkType(byte:int):Boolean
36 | {
37 | return false;
38 | }
39 |
40 | /**
41 | * The instance of the parent factory
42 | */
43 | protected var factory:Factory;
44 | /**
45 | * The signature byte of the following data. If this worker was created to encode an object, the value of this property is always -1.
46 | */
47 | protected var byte:int;
48 |
49 | /**
50 | * Construct a new instance of this worker. Workers are created anytime that the parent factory needs to encode or decode the data type handled by this worker.
51 | * @param factory Parent factory
52 | * @param byte Signature byte
53 | */
54 | public function Worker(factory:Factory, byte:int = -1)
55 | {
56 | this.factory = factory;
57 | this.byte = byte;
58 | }
59 |
60 | /**
61 | * The instance of the parent factory.
62 | * @return Return the instance of the parent factory.
63 | */
64 | public function getFactory():Factory
65 | {
66 | return factory;
67 | }
68 |
69 | /**
70 | * The signature byte of the following data. If this worker was created to encode an object, the value of this property is always -1.
71 | * @return Return the signature byte.
72 | */
73 | public function getByte():int
74 | {
75 | return byte;
76 | }
77 |
78 | /**
79 | * Encode data into destination stream.
80 | * @param data Object to be encoded.
81 | * @param destination Object which implements IDataOutput.
82 | * @see MsgPack#write()
83 | */
84 | public function assembly(data:*, destination:IDataOutput):void
85 | {
86 | }
87 |
88 | /**
89 | * Decode an object from source stream. If not all bytes of the object are available, this method must return incomplete,
90 | * and the content which was already decoded must be saved. Thus, you can read stream data making consecutive calls to this method.
91 | * @param source Object which implements IDataInput.
92 | * @return The decoded object
93 | * @see org.msgpack#incomplete
94 | * @see MsgPack#read()
95 | */
96 | public function disassembly(source:IDataInput):*
97 | {
98 | return incomplete;
99 | }
100 | }
101 | }
--------------------------------------------------------------------------------
/src_lib/org/msgpack/incomplete.as:
--------------------------------------------------------------------------------
1 | //
2 | // as3-msgpack (MessagePack for Actionscript3)
3 | // Copyright (C) 2013 Lucas Teixeira (Disturbed Coder)
4 | //
5 | //
6 | // Licensed under the Apache License, Version 2.0 (the "License");
7 | // you may not use this file except in compliance with the License.
8 | // You may obtain a copy of the License at
9 | //
10 | // http://www.apache.org/licenses/LICENSE-2.0
11 | //
12 | // Unless required by applicable law or agreed to in writing, software
13 | // distributed under the License is distributed on an "AS IS" BASIS,
14 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | // See the License for the specific language governing permissions and
16 | // limitations under the License.
17 | //
18 | package org.msgpack
19 | {
20 | /**
21 | * Instance that represents an message pack object not completelly decoded.
22 | * @see MsgPack#read()
23 | * @see Worker#disassembly()
24 | */
25 | public const incomplete:Object =
26 | {
27 | toString: function():String { return "incomplete" }
28 | };
29 | }
--------------------------------------------------------------------------------
/src_test/DateWorker.as:
--------------------------------------------------------------------------------
1 | //
2 | // as3-msgpack (MessagePack for Actionscript3)
3 | // Copyright (C) 2013 Lucas Teixeira (Disturbed Coder)
4 | //
5 | //
6 | // Licensed under the Apache License, Version 2.0 (the "License");
7 | // you may not use this file except in compliance with the License.
8 | // You may obtain a copy of the License at
9 | //
10 | // http://www.apache.org/licenses/LICENSE-2.0
11 | //
12 | // Unless required by applicable law or agreed to in writing, software
13 | // distributed under the License is distributed on an "AS IS" BASIS,
14 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | // See the License for the specific language governing permissions and
16 | // limitations under the License.
17 | //
18 | package
19 | {
20 | import flash.utils.*;
21 |
22 | import org.msgpack.*;
23 |
24 | public class DateWorker extends Worker
25 | {
26 | private var worker:Worker;
27 |
28 | public function DateWorker(factory:Factory, byte:int = -1)
29 | {
30 | super(factory, byte);
31 | }
32 |
33 | override public function assembly(data:*, destination:IDataOutput):void
34 | {
35 | var value:Number = data.time;
36 |
37 | if (!worker)
38 | worker = factory.getWorkerByType(value);
39 |
40 | worker.assembly(value, destination);
41 | }
42 | }
43 | }
--------------------------------------------------------------------------------
/src_test/LibTest.as:
--------------------------------------------------------------------------------
1 | //
2 | // as3-msgpack (MessagePack for Actionscript3)
3 | // Copyright (C) 2013 Lucas Teixeira (Disturbed Coder)
4 | //
5 | //
6 | // Licensed under the Apache License, Version 2.0 (the "License");
7 | // you may not use this file except in compliance with the License.
8 | // You may obtain a copy of the License at
9 | //
10 | // http://www.apache.org/licenses/LICENSE-2.0
11 | //
12 | // Unless required by applicable law or agreed to in writing, software
13 | // distributed under the License is distributed on an "AS IS" BASIS,
14 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | // See the License for the specific language governing permissions and
16 | // limitations under the License.
17 | //
18 | package
19 | {
20 | import br.dcoder.console.*;
21 |
22 | import flash.display.*;
23 | import flash.events.*;
24 | import flash.geom.*;
25 | import flash.utils.*;
26 |
27 | import org.msgpack.*;
28 |
29 | [SWF(width="800", height="600", backgroundColor="#FFFFFF", frameRate="30")]
30 | public class LibTest extends Sprite
31 | {
32 | private var startTime:uint;
33 |
34 | public function LibTest()
35 | {
36 | // create console
37 | Console.create(this);
38 | Console.instance.draggable = false;
39 | Console.instance.resizable = false;
40 |
41 | // wait to be added on the stage
42 | addEventListener(Event.ADDED_TO_STAGE, addedToStage);
43 | }
44 |
45 | private function addedToStage(e:Event):void
46 | {
47 | // configure stage
48 | removeEventListener(Event.ADDED_TO_STAGE, addedToStage);
49 | stage.align = StageAlign.TOP_LEFT;
50 | stage.scaleMode = StageScaleMode.NO_SCALE;
51 | stage.addEventListener(Event.RESIZE, resize);
52 |
53 | // set resize
54 | resize(null);
55 |
56 | // start the test!
57 | start();
58 | }
59 |
60 | private function resize(e:Event):void
61 | {
62 | Console.instance.area = new Rectangle(0, 0, stage.stageWidth, stage.stageHeight);
63 | }
64 |
65 | private function start():void
66 | {
67 | cpln("starting as3-msgpack lib test (version " + MsgPack.VERSION + ")");
68 | cpln("");
69 |
70 | var msgpack:MsgPack = new MsgPack(MsgPackFlags.READ_RAW_AS_BYTE_ARRAY);
71 |
72 | // null
73 | test(msgpack, null);
74 |
75 | // true
76 | test(msgpack, true);
77 |
78 | // false
79 | test(msgpack, false);
80 |
81 | // Number
82 | test(msgpack, 666.12345);
83 |
84 | // int
85 | test(msgpack, 10);
86 | test(msgpack, 1000);
87 | test(msgpack, 100000);
88 | test(msgpack, -10);
89 | test(msgpack, -1000);
90 | test(msgpack, -100000);
91 |
92 | // ByteArray
93 | var tmp:ByteArray = new ByteArray();
94 | tmp.writeUTFBytes("aéç㪺°¹²³£¢¬");
95 | test(msgpack, tmp);
96 |
97 | // String
98 | test(msgpack, "MsgPack for AS3");
99 |
100 | // Array
101 | test(msgpack, [1, 2, 3, "message pack"]);
102 |
103 | // Testing empty string (bug fixed in version 0.4.1 - thanks to ccrossley)
104 | test(msgpack, ["lucas", "", "teixeira"]);
105 |
106 | // Object
107 | test(msgpack, {name: "Lucas", age: 27, man: true});
108 |
109 | // custom type test
110 | // here we create a handler to encode Date class as a number (miliseconds)
111 | customTypeTest();
112 | }
113 |
114 | private function test(msgpack:MsgPack, data:*):void
115 | {
116 | // print type info
117 | var name:String = getQualifiedClassName(data);
118 | cpln("testing '" + data + "' (" + name + "):");
119 |
120 | // encode data and print buffer length
121 | var bytes:ByteArray = msgpack.write(data);
122 | bytes.position = 0;
123 | cpln("encoded length = " + bytes.length);
124 |
125 | // decode data and print the result object
126 | var result:* = msgpack.read(bytes);
127 | cpln("decoded value = " + result);
128 | cpln(getQualifiedClassName(result));
129 |
130 | // if is a object, let's iterate through the elements
131 | if (name == "Object")
132 | printObject(result);
133 |
134 | cpln("");
135 | }
136 |
137 | private function printObject(obj:Object):void
138 | {
139 | for (var i:String in obj)
140 | cpln(i + " = " + obj[i]);
141 | }
142 |
143 | private function customTypeTest():void
144 | {
145 | cpln("testing custom type");
146 |
147 | var msgpack:MsgPack = new MsgPack();
148 | msgpack.factory.assign(DateWorker, Date);
149 |
150 | var date:Date = new Date();
151 |
152 | // encode date
153 | cpln("enconding date: " + date);
154 | var bytes:ByteArray = msgpack.write(date);
155 | bytes.position = 0;
156 | cpln("encoded length = " + bytes.length);
157 |
158 | // decode date
159 | var miliseconds:Number = msgpack.read(bytes);
160 | cpln("decoded value = " + miliseconds + " (" + new Date(miliseconds) + ")");
161 |
162 | cpln("");
163 | }
164 | }
165 | }
--------------------------------------------------------------------------------
/src_test/StreamTest.as:
--------------------------------------------------------------------------------
1 | //
2 | // as3-msgpack (MessagePack for Actionscript3)
3 | // Copyright (C) 2013 Lucas Teixeira (Disturbed Coder)
4 | //
5 | //
6 | // Licensed under the Apache License, Version 2.0 (the "License");
7 | // you may not use this file except in compliance with the License.
8 | // You may obtain a copy of the License at
9 | //
10 | // http://www.apache.org/licenses/LICENSE-2.0
11 | //
12 | // Unless required by applicable law or agreed to in writing, software
13 | // distributed under the License is distributed on an "AS IS" BASIS,
14 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | // See the License for the specific language governing permissions and
16 | // limitations under the License.
17 | //
18 | package
19 | {
20 | import br.dcoder.console.*;
21 |
22 | import flash.display.*;
23 | import flash.events.*;
24 | import flash.geom.*;
25 | import flash.utils.*;
26 |
27 | import org.msgpack.*;
28 |
29 | [SWF(width="800", height="600", backgroundColor="#FFFFFF", frameRate="30")]
30 | public class StreamTest extends Sprite
31 | {
32 | private var startTime:uint;
33 |
34 | public function StreamTest()
35 | {
36 | // create console
37 | Console.create(this);
38 | Console.instance.draggable = false;
39 | Console.instance.resizable = false;
40 |
41 | // wait to be added on the stage
42 | addEventListener(Event.ADDED_TO_STAGE, addedToStage);
43 | }
44 |
45 | private function addedToStage(e:Event):void
46 | {
47 | // configure stage
48 | removeEventListener(Event.ADDED_TO_STAGE, addedToStage);
49 | stage.align = StageAlign.TOP_LEFT;
50 | stage.scaleMode = StageScaleMode.NO_SCALE;
51 | stage.addEventListener(Event.RESIZE, resize);
52 |
53 | // set resize
54 | resize(null);
55 |
56 | // start the test!
57 | start();
58 | }
59 |
60 | private function resize(e:Event):void
61 | {
62 | Console.instance.area = new Rectangle(0, 0, stage.stageWidth, stage.stageHeight);
63 | }
64 |
65 | private function start():void
66 | {
67 | cpln("starting as3-msgpack stream test (version " + MsgPack.VERSION + ")");
68 | cpln("");
69 |
70 | var msgpack:MsgPack = new MsgPack();
71 | streamTest(msgpack);
72 | }
73 |
74 | private function streamTest(msgpack:MsgPack):void
75 | {
76 | startTime = getTimer();
77 | cpln("testing stream reading");
78 |
79 | var data:Object =
80 | {
81 | title: "My Title",
82 | body: "My Body",
83 | isMsgPackCool: true,
84 | theNumber: 42,
85 | planets: ["Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"]
86 | };
87 |
88 | var bytes:ByteArray = msgpack.write(data);
89 | bytes.position = 0;
90 | cpln("assembling object, length = " + bytes.length);
91 |
92 | var streamBytes:ByteArray = new ByteArray();
93 | setTimeout(writeStream, 10, bytes, streamBytes, 0, msgpack);
94 | setTimeout(readStream, 10, msgpack, streamBytes);
95 | }
96 |
97 | private function writeStream(bytes:ByteArray, streamBytes:ByteArray, counter:int, msgpack:MsgPack):void
98 | {
99 | streamBytes.length = counter + 1;
100 | streamBytes[counter] = bytes[counter];
101 |
102 | if (counter < bytes.length - 1)
103 | setTimeout(writeStream, 10, bytes, streamBytes, counter + 1, msgpack);
104 | else
105 | cpln(streamBytes.length + " bytes written");
106 | }
107 |
108 | private function readStream(msgpack:MsgPack, streamBytes:ByteArray):void
109 | {
110 | var obj:* = incomplete;
111 | obj = msgpack.read(streamBytes);
112 |
113 | if (obj == incomplete)
114 | {
115 | setTimeout(readStream, 10, msgpack, streamBytes);
116 | }
117 | else
118 | {
119 | cpln("done in " + (getTimer() - startTime) + "ms");
120 | printObject(obj);
121 | }
122 | }
123 |
124 | private function printObject(obj:Object):void
125 | {
126 | for (var i:String in obj)
127 | cpln(i + " = " + obj[i]);
128 | }
129 | }
130 | }
--------------------------------------------------------------------------------
/src_test/as3_client/AS3Client.as:
--------------------------------------------------------------------------------
1 | //
2 | // as3-msgpack (MessagePack for Actionscript3)
3 | // Copyright (C) 2013 Lucas Teixeira (Disturbed Coder)
4 | //
5 | //
6 | // Licensed under the Apache License, Version 2.0 (the "License");
7 | // you may not use this file except in compliance with the License.
8 | // You may obtain a copy of the License at
9 | //
10 | // http://www.apache.org/licenses/LICENSE-2.0
11 | //
12 | // Unless required by applicable law or agreed to in writing, software
13 | // distributed under the License is distributed on an "AS IS" BASIS,
14 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | // See the License for the specific language governing permissions and
16 | // limitations under the License.
17 | //
18 | package
19 | {
20 | import br.dcoder.console.*;
21 |
22 | import com.adobe.serialization.json.*;
23 |
24 | import flash.display.*;
25 | import flash.events.*;
26 | import flash.geom.*;
27 | import flash.net.*;
28 |
29 | import org.msgpack.*;
30 |
31 | public class AS3Client extends Sprite
32 | {
33 | private var msgpack:MsgPack;
34 | private var socket:Socket;
35 |
36 | public function AS3Client()
37 | {
38 | Console.create(this);
39 | Console.instance.caption = "as3-msgpack socket client"
40 | Console.instance.draggable = false;
41 | Console.instance.resizable = false;
42 | Console.instance.getEventDispatcher().addEventListener(ConsoleEvent.INPUT, consoleInput);
43 |
44 | msgpack = new MsgPack();
45 |
46 | addEventListener(Event.ADDED_TO_STAGE, addedToStage);
47 | }
48 |
49 | private function addedToStage(e:Event):void
50 | {
51 | removeEventListener(Event.ADDED_TO_STAGE, addedToStage);
52 | stage.addEventListener(Event.RESIZE, resize);
53 | stage.align = StageAlign.TOP_LEFT;
54 | stage.scaleMode = StageScaleMode.NO_SCALE;
55 |
56 | resize(null);
57 | start();
58 | }
59 |
60 | private function resize(e:Event):void
61 | {
62 | Console.instance.area = new Rectangle(0, 0, stage.stageWidth, stage.stageHeight);
63 | }
64 |
65 | private function start():void
66 | {
67 | cpln("starting as3-msgpack socket client");
68 |
69 | socket = new Socket();
70 | socket.addEventListener(Event.CLOSE, socketClose);
71 | socket.addEventListener(Event.CONNECT, socketConnect);
72 | socket.addEventListener(ProgressEvent.SOCKET_DATA, socketData);
73 | socket.connect("localhost", 5555);
74 | }
75 |
76 | private function socketClose(e:Event):void
77 | {
78 | cpln("connection closed");
79 | }
80 |
81 | private function socketConnect(e:Event):void
82 | {
83 | cpln("connected to the server");
84 | cpln("type a JSON object to send to the server...");
85 | cpln("");
86 | }
87 |
88 | private function socketData(e:ProgressEvent):void
89 | {
90 | cpln("read incoming data");
91 | var data:* = msgpack.read(socket);
92 |
93 | if (data != incomplete)
94 | {
95 | cpln("packet decoded:");
96 | cpln(JSON.encode(data));
97 | cpln("");
98 | }
99 | }
100 |
101 | private function consoleInput(e:ConsoleEvent):void
102 | {
103 | if (!socket.connected)
104 | {
105 | cpln("ERROR: not connected to the server");
106 | return;
107 | }
108 |
109 | try
110 | {
111 | cpln("encoding packet");
112 | var data:Object = JSON.decode(e.text);
113 | msgpack.write(data, socket);
114 | socket.flush();
115 | }
116 | catch (e:Error)
117 | {
118 | cpln("ERROR: can't parse json");
119 | cpln(e);
120 | }
121 | }
122 | }
123 | }
--------------------------------------------------------------------------------
/src_test/as3_client/as3_client.fla:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/loteixeira/as3-msgpack/cdbd4e4f71e45b460a88555d1b0f06890a24b53d/src_test/as3_client/as3_client.fla
--------------------------------------------------------------------------------
/src_test/as3_common/air_certificate.p12:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/loteixeira/as3-msgpack/cdbd4e4f71e45b460a88555d1b0f06890a24b53d/src_test/as3_common/air_certificate.p12
--------------------------------------------------------------------------------
/src_test/as3_common/com/adobe/serialization/json/JSON.as:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (c) 2008, Adobe Systems Incorporated
3 | All rights reserved.
4 |
5 | Redistribution and use in source and binary forms, with or without
6 | modification, are permitted provided that the following conditions are
7 | met:
8 |
9 | * Redistributions of source code must retain the above copyright notice,
10 | this list of conditions and the following disclaimer.
11 |
12 | * Redistributions in binary form must reproduce the above copyright
13 | notice, this list of conditions and the following disclaimer in the
14 | documentation and/or other materials provided with the distribution.
15 |
16 | * Neither the name of Adobe Systems Incorporated nor the names of its
17 | contributors may be used to endorse or promote products derived from
18 | this software without specific prior written permission.
19 |
20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21 | IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 | PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 | */
32 |
33 | package com.adobe.serialization.json
34 | {
35 |
36 | /**
37 | * This class provides encoding and decoding of the JSON format.
38 | *
39 | * Example usage:
40 | *
41 | * // create a JSON string from an internal object
42 | * JSON.encode( myObject );
43 | *
44 | * // read a JSON string into an internal object
45 | * var myObject:Object = JSON.decode( jsonString );
46 | *
47 | */
48 | public class JSON
49 | {
50 | /**
51 | * Encodes a object into a JSON string.
52 | *
53 | * @param o The object to create a JSON string for
54 | * @return the JSON string representing o
55 | * @langversion ActionScript 3.0
56 | * @playerversion Flash 9.0
57 | * @tiptext
58 | */
59 | public static function encode( o:Object ):String
60 | {
61 | return new JSONEncoder( o ).getString();
62 | }
63 |
64 | /**
65 | * Decodes a JSON string into a native object.
66 | *
67 | * @param s The JSON string representing the object
68 | * @param strict Flag indicating if the decoder should strictly adhere
69 | * to the JSON standard or not. The default of true
70 | * throws errors if the format does not match the JSON syntax exactly.
71 | * Pass false to allow for non-properly-formatted JSON
72 | * strings to be decoded with more leniancy.
73 | * @return A native object as specified by s
74 | * @throw JSONParseError
75 | * @langversion ActionScript 3.0
76 | * @playerversion Flash 9.0
77 | * @tiptext
78 | */
79 | public static function decode( s:String, strict:Boolean = true ):*
80 | {
81 | return new JSONDecoder( s, strict ).getValue();
82 | }
83 |
84 | }
85 |
86 | }
--------------------------------------------------------------------------------
/src_test/as3_common/com/adobe/serialization/json/JSONParseError.as:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (c) 2008, Adobe Systems Incorporated
3 | All rights reserved.
4 |
5 | Redistribution and use in source and binary forms, with or without
6 | modification, are permitted provided that the following conditions are
7 | met:
8 |
9 | * Redistributions of source code must retain the above copyright notice,
10 | this list of conditions and the following disclaimer.
11 |
12 | * Redistributions in binary form must reproduce the above copyright
13 | notice, this list of conditions and the following disclaimer in the
14 | documentation and/or other materials provided with the distribution.
15 |
16 | * Neither the name of Adobe Systems Incorporated nor the names of its
17 | contributors may be used to endorse or promote products derived from
18 | this software without specific prior written permission.
19 |
20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21 | IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 | PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 | */
32 |
33 | package com.adobe.serialization.json {
34 |
35 | /**
36 | *
37 | *
38 | */
39 | public class JSONParseError extends Error {
40 |
41 | /** The location in the string where the error occurred */
42 | private var _location:int;
43 |
44 | /** The string in which the parse error occurred */
45 | private var _text:String;
46 |
47 | /**
48 | * Constructs a new JSONParseError.
49 | *
50 | * @param message The error message that occured during parsing
51 | * @langversion ActionScript 3.0
52 | * @playerversion Flash 9.0
53 | * @tiptext
54 | */
55 | public function JSONParseError( message:String = "", location:int = 0, text:String = "") {
56 | super( message );
57 | name = "JSONParseError";
58 | _location = location;
59 | _text = text;
60 | }
61 |
62 | /**
63 | * Provides read-only access to the location variable.
64 | *
65 | * @return The location in the string where the error occurred
66 | * @langversion ActionScript 3.0
67 | * @playerversion Flash 9.0
68 | * @tiptext
69 | */
70 | public function get location():int {
71 | return _location;
72 | }
73 |
74 | /**
75 | * Provides read-only access to the text variable.
76 | *
77 | * @return The string in which the error occurred
78 | * @langversion ActionScript 3.0
79 | * @playerversion Flash 9.0
80 | * @tiptext
81 | */
82 | public function get text():String {
83 | return _text;
84 | }
85 | }
86 |
87 | }
--------------------------------------------------------------------------------
/src_test/as3_common/com/adobe/serialization/json/JSONToken.as:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (c) 2008, Adobe Systems Incorporated
3 | All rights reserved.
4 |
5 | Redistribution and use in source and binary forms, with or without
6 | modification, are permitted provided that the following conditions are
7 | met:
8 |
9 | * Redistributions of source code must retain the above copyright notice,
10 | this list of conditions and the following disclaimer.
11 |
12 | * Redistributions in binary form must reproduce the above copyright
13 | notice, this list of conditions and the following disclaimer in the
14 | documentation and/or other materials provided with the distribution.
15 |
16 | * Neither the name of Adobe Systems Incorporated nor the names of its
17 | contributors may be used to endorse or promote products derived from
18 | this software without specific prior written permission.
19 |
20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21 | IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 | PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 | */
32 |
33 | package com.adobe.serialization.json {
34 |
35 | public class JSONToken {
36 |
37 | private var _type:int;
38 | private var _value:Object;
39 |
40 | /**
41 | * Creates a new JSONToken with a specific token type and value.
42 | *
43 | * @param type The JSONTokenType of the token
44 | * @param value The value of the token
45 | * @langversion ActionScript 3.0
46 | * @playerversion Flash 9.0
47 | * @tiptext
48 | */
49 | public function JSONToken( type:int = -1 /* JSONTokenType.UNKNOWN */, value:Object = null ) {
50 | _type = type;
51 | _value = value;
52 | }
53 |
54 | /**
55 | * Returns the type of the token.
56 | *
57 | * @see com.adobe.serialization.json.JSONTokenType
58 | * @langversion ActionScript 3.0
59 | * @playerversion Flash 9.0
60 | * @tiptext
61 | */
62 | public function get type():int {
63 | return _type;
64 | }
65 |
66 | /**
67 | * Sets the type of the token.
68 | *
69 | * @see com.adobe.serialization.json.JSONTokenType
70 | * @langversion ActionScript 3.0
71 | * @playerversion Flash 9.0
72 | * @tiptext
73 | */
74 | public function set type( value:int ):void {
75 | _type = value;
76 | }
77 |
78 | /**
79 | * Gets the value of the token
80 | *
81 | * @see com.adobe.serialization.json.JSONTokenType
82 | * @langversion ActionScript 3.0
83 | * @playerversion Flash 9.0
84 | * @tiptext
85 | */
86 | public function get value():Object {
87 | return _value;
88 | }
89 |
90 | /**
91 | * Sets the value of the token
92 | *
93 | * @see com.adobe.serialization.json.JSONTokenType
94 | * @langversion ActionScript 3.0
95 | * @playerversion Flash 9.0
96 | * @tiptext
97 | */
98 | public function set value ( v:Object ):void {
99 | _value = v;
100 | }
101 |
102 | }
103 |
104 | }
--------------------------------------------------------------------------------
/src_test/as3_common/com/adobe/serialization/json/JSONTokenType.as:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (c) 2008, Adobe Systems Incorporated
3 | All rights reserved.
4 |
5 | Redistribution and use in source and binary forms, with or without
6 | modification, are permitted provided that the following conditions are
7 | met:
8 |
9 | * Redistributions of source code must retain the above copyright notice,
10 | this list of conditions and the following disclaimer.
11 |
12 | * Redistributions in binary form must reproduce the above copyright
13 | notice, this list of conditions and the following disclaimer in the
14 | documentation and/or other materials provided with the distribution.
15 |
16 | * Neither the name of Adobe Systems Incorporated nor the names of its
17 | contributors may be used to endorse or promote products derived from
18 | this software without specific prior written permission.
19 |
20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21 | IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 | PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 | */
32 |
33 | package com.adobe.serialization.json {
34 |
35 | /**
36 | * Class containing constant values for the different types
37 | * of tokens in a JSON encoded string.
38 | */
39 | public class JSONTokenType {
40 |
41 | public static const UNKNOWN:int = -1;
42 |
43 | public static const COMMA:int = 0;
44 |
45 | public static const LEFT_BRACE:int = 1;
46 |
47 | public static const RIGHT_BRACE:int = 2;
48 |
49 | public static const LEFT_BRACKET:int = 3;
50 |
51 | public static const RIGHT_BRACKET:int = 4;
52 |
53 | public static const COLON:int = 6;
54 |
55 | public static const TRUE:int = 7;
56 |
57 | public static const FALSE:int = 8;
58 |
59 | public static const NULL:int = 9;
60 |
61 | public static const STRING:int = 10;
62 |
63 | public static const NUMBER:int = 11;
64 |
65 | public static const NAN:int = 12;
66 |
67 | }
68 |
69 | }
--------------------------------------------------------------------------------
/src_test/as3_server/AS3Server.as:
--------------------------------------------------------------------------------
1 | //
2 | // as3-msgpack (MessagePack for Actionscript3)
3 | // Copyright (C) 2013 Lucas Teixeira (Disturbed Coder)
4 | //
5 | //
6 | // Licensed under the Apache License, Version 2.0 (the "License");
7 | // you may not use this file except in compliance with the License.
8 | // You may obtain a copy of the License at
9 | //
10 | // http://www.apache.org/licenses/LICENSE-2.0
11 | //
12 | // Unless required by applicable law or agreed to in writing, software
13 | // distributed under the License is distributed on an "AS IS" BASIS,
14 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | // See the License for the specific language governing permissions and
16 | // limitations under the License.
17 | //
18 | package
19 | {
20 | import br.dcoder.console.*;
21 |
22 | import com.adobe.serialization.json.*;
23 |
24 | import flash.display.*;
25 | import flash.events.*;
26 | import flash.geom.*;
27 | import flash.net.*;
28 |
29 | import org.msgpack.*;
30 |
31 | public class AS3Server extends Sprite
32 | {
33 | private var msgpack:MsgPack;
34 | private var serverSocket:ServerSocket;
35 | private var clientSocket:Socket;
36 |
37 | public function AS3Server()
38 | {
39 | Console.create(this);
40 | Console.instance.caption = "as3-msgpack socket server"
41 | Console.instance.draggable = false;
42 | Console.instance.resizable = false;
43 | Console.instance.getEventDispatcher().addEventListener(ConsoleEvent.INPUT, consoleInput);
44 |
45 | msgpack = new MsgPack();
46 |
47 | addEventListener(Event.ADDED_TO_STAGE, addedToStage);
48 | }
49 |
50 | private function addedToStage(e:Event):void
51 | {
52 | removeEventListener(Event.ADDED_TO_STAGE, addedToStage);
53 | stage.addEventListener(Event.RESIZE, resize);
54 | stage.align = StageAlign.TOP_LEFT;
55 | stage.scaleMode = StageScaleMode.NO_SCALE;
56 |
57 | resize(null);
58 | start();
59 | }
60 |
61 | private function resize(e:Event):void
62 | {
63 | Console.instance.area = new Rectangle(0, 0, stage.stageWidth, stage.stageHeight);
64 | }
65 |
66 | private function start():void
67 | {
68 | cpln("starting as3-msgpack socket server");
69 |
70 | serverSocket = new ServerSocket();
71 | serverSocket.addEventListener(Event.CLOSE, serverSocketClose);
72 | serverSocket.addEventListener(ServerSocketConnectEvent.CONNECT, serverSocketConnect);
73 |
74 | var port:int = 5555;
75 | cpln("binding server to port " + port);
76 | serverSocket.bind(port);
77 |
78 | cpln("listening for incoming connections...");
79 | serverSocket.listen();
80 |
81 | cpln("");
82 | }
83 |
84 | private function serverSocketClose(e:Event):void
85 | {
86 | cpln("connection closed");
87 | cpln("");
88 | clientSocket = undefined;
89 | }
90 |
91 | private function serverSocketConnect(e:ServerSocketConnectEvent):void
92 | {
93 | cpln("client connected");
94 | cpln("type a JSON object to send to the client...");
95 | cpln("");
96 | clientSocket = e.socket;
97 | clientSocket.addEventListener(ProgressEvent.SOCKET_DATA, socketData);
98 | }
99 |
100 | private function socketData(e:ProgressEvent):void
101 | {
102 | cpln("read incoming data");
103 | var data:* = msgpack.read(clientSocket);
104 |
105 | if (data != incomplete)
106 | {
107 | cpln("packet decoded:");
108 | cpln(JSON.encode(data));
109 | cpln("");
110 | }
111 | }
112 |
113 | private function consoleInput(e:ConsoleEvent):void
114 | {
115 | if (!clientSocket)
116 | {
117 | cpln("ERROR: no client connect");
118 | return;
119 | }
120 |
121 | try
122 | {
123 | cpln("writing packet");
124 | var data:Object = JSON.decode(e.text);
125 | msgpack.write(data, clientSocket);
126 | clientSocket.flush();
127 | }
128 | catch (e:Error)
129 | {
130 | cpln("ERROR: can't parse json");
131 | cpln(e);
132 | }
133 | }
134 | }
135 | }
--------------------------------------------------------------------------------
/src_test/as3_server/as3_server.fla:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/loteixeira/as3-msgpack/cdbd4e4f71e45b460a88555d1b0f06890a24b53d/src_test/as3_server/as3_server.fla
--------------------------------------------------------------------------------