├── .gitignore ├── .gitmodules ├── Assets ├── AddressBook.sproto ├── Main.cs ├── foobar.sproto ├── main.unity ├── main.unity.meta ├── protocol.sproto └── test │ ├── Benchmark.cs │ ├── Client.cs │ ├── Test.cs │ ├── TestAll.cs │ ├── TestRpc.cs │ ├── TestUnit.cs │ └── Util.cs ├── ProjectSettings ├── AudioManager.asset ├── DynamicsManager.asset ├── EditorBuildSettings.asset ├── EditorSettings.asset ├── GraphicsSettings.asset ├── InputManager.asset ├── NavMeshAreas.asset ├── NavMeshLayers.asset ├── NetworkManager.asset ├── Physics2DSettings.asset ├── ProjectSettings.asset ├── QualitySettings.asset ├── TagManager.asset └── TimeManager.asset ├── README.md └── benchmark /.gitignore: -------------------------------------------------------------------------------- 1 | /Temp 2 | /Library 3 | *.csproj 4 | *.sln 5 | *.DS_Store 6 | *.meta 7 | *.userprefs 8 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "Assets/sproto-cs"] 2 | path = Assets/sproto-cs 3 | url = https://github.com/jintiao/sproto-cs 4 | -------------------------------------------------------------------------------- /Assets/AddressBook.sproto: -------------------------------------------------------------------------------- 1 | .Person { 2 | name 0 : string 3 | id 1 : integer 4 | email 2 : string 5 | 6 | .PhoneNumber { 7 | number 0 : string 8 | type 1 : integer 9 | } 10 | 11 | phone 3 : *PhoneNumber 12 | } 13 | 14 | .AddressBook { 15 | person 0 : *Person(id) 16 | others 1 : *Person 17 | } -------------------------------------------------------------------------------- /Assets/Main.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | using System.IO; 4 | using System; 5 | 6 | public class Main : MonoBehaviour { 7 | private Client mClient = null; 8 | private string mText = ""; 9 | 10 | void Start () { 11 | new TestUnit ().Run (); 12 | new Test ().Run (); 13 | new TestAll ().Run (); 14 | new TestRpc ().Run (); 15 | } 16 | 17 | void OnGUI () { 18 | if (GUI.Button (new Rect (10, 10, 100, 100), "Benchmark")){ 19 | new Benchmark().Run (); 20 | } 21 | 22 | if (mClient != null) { 23 | 24 | mText = GUI.TextField (new Rect(120, 150, 200, 40), mText, 25); 25 | if (GUI.Button (new Rect (330, 150, 80, 40), "Get")){ 26 | mClient.SendGet (mText); 27 | mText = ""; 28 | } 29 | 30 | GUI.enabled = false; 31 | } 32 | 33 | if (GUI.Button (new Rect (10, 150, 100, 100), "Client")){ 34 | mClient = new Client(); 35 | mClient.Run (); 36 | } 37 | 38 | GUI.enabled = true; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Assets/foobar.sproto: -------------------------------------------------------------------------------- 1 | .foobar { 2 | .nest { 3 | a 1 : string 4 | b 3 : boolean 5 | c 5 : integer 6 | } 7 | a 0 : string 8 | b 1 : integer 9 | c 2 : boolean 10 | d 3 : nest 11 | 12 | e 4 : *string 13 | f 5 : *integer 14 | g 6 : *boolean 15 | h 7 : *foobar 16 | } -------------------------------------------------------------------------------- /Assets/main.unity: -------------------------------------------------------------------------------- 1 | %YAML 1.1 2 | %TAG !u! tag:unity3d.com,2011: 3 | --- !u!29 &1 4 | SceneSettings: 5 | m_ObjectHideFlags: 0 6 | m_PVSData: 7 | m_PVSObjectsArray: [] 8 | m_PVSPortalsArray: [] 9 | m_OcclusionBakeSettings: 10 | smallestOccluder: 5 11 | smallestHole: .25 12 | backfaceThreshold: 100 13 | --- !u!104 &2 14 | RenderSettings: 15 | m_Fog: 0 16 | m_FogColor: {r: .5, g: .5, b: .5, a: 1} 17 | m_FogMode: 3 18 | m_FogDensity: .00999999978 19 | m_LinearFogStart: 0 20 | m_LinearFogEnd: 300 21 | m_AmbientLight: {r: .200000003, g: .200000003, b: .200000003, a: 1} 22 | m_SkyboxMaterial: {fileID: 0} 23 | m_HaloStrength: .5 24 | m_FlareStrength: 1 25 | m_FlareFadeSpeed: 3 26 | m_HaloTexture: {fileID: 0} 27 | m_SpotCookie: {fileID: 0} 28 | m_ObjectHideFlags: 0 29 | --- !u!127 &3 30 | LevelGameManager: 31 | m_ObjectHideFlags: 0 32 | --- !u!157 &4 33 | LightmapSettings: 34 | m_ObjectHideFlags: 0 35 | m_LightProbes: {fileID: 0} 36 | m_Lightmaps: [] 37 | m_LightmapsMode: 1 38 | m_BakedColorSpace: 0 39 | m_UseDualLightmapsInForward: 0 40 | m_LightmapEditorSettings: 41 | m_Resolution: 50 42 | m_LastUsedResolution: 0 43 | m_TextureWidth: 1024 44 | m_TextureHeight: 1024 45 | m_BounceBoost: 1 46 | m_BounceIntensity: 1 47 | m_SkyLightColor: {r: .860000014, g: .930000007, b: 1, a: 1} 48 | m_SkyLightIntensity: 0 49 | m_Quality: 0 50 | m_Bounces: 1 51 | m_FinalGatherRays: 1000 52 | m_FinalGatherContrastThreshold: .0500000007 53 | m_FinalGatherGradientThreshold: 0 54 | m_FinalGatherInterpolationPoints: 15 55 | m_AOAmount: 0 56 | m_AOMaxDistance: .100000001 57 | m_AOContrast: 1 58 | m_LODSurfaceMappingDistance: 1 59 | m_Padding: 0 60 | m_TextureCompression: 0 61 | m_LockAtlas: 0 62 | --- !u!196 &5 63 | NavMeshSettings: 64 | m_ObjectHideFlags: 0 65 | m_BuildSettings: 66 | agentRadius: .5 67 | agentHeight: 2 68 | agentSlope: 45 69 | agentClimb: .400000006 70 | ledgeDropHeight: 0 71 | maxJumpAcrossDistance: 0 72 | accuratePlacement: 0 73 | minRegionArea: 2 74 | widthInaccuracy: 16.666666 75 | heightInaccuracy: 10 76 | m_NavMesh: {fileID: 0} 77 | --- !u!1 &1691594113 78 | GameObject: 79 | m_ObjectHideFlags: 0 80 | m_PrefabParentObject: {fileID: 0} 81 | m_PrefabInternal: {fileID: 0} 82 | serializedVersion: 4 83 | m_Component: 84 | - 4: {fileID: 1691594118} 85 | - 20: {fileID: 1691594117} 86 | - 92: {fileID: 1691594116} 87 | - 124: {fileID: 1691594115} 88 | - 81: {fileID: 1691594114} 89 | - 114: {fileID: 1691594119} 90 | m_Layer: 0 91 | m_Name: Main Camera 92 | m_TagString: MainCamera 93 | m_Icon: {fileID: 0} 94 | m_NavMeshLayer: 0 95 | m_StaticEditorFlags: 0 96 | m_IsActive: 1 97 | --- !u!81 &1691594114 98 | AudioListener: 99 | m_ObjectHideFlags: 0 100 | m_PrefabParentObject: {fileID: 0} 101 | m_PrefabInternal: {fileID: 0} 102 | m_GameObject: {fileID: 1691594113} 103 | m_Enabled: 1 104 | --- !u!124 &1691594115 105 | Behaviour: 106 | m_ObjectHideFlags: 0 107 | m_PrefabParentObject: {fileID: 0} 108 | m_PrefabInternal: {fileID: 0} 109 | m_GameObject: {fileID: 1691594113} 110 | m_Enabled: 1 111 | --- !u!92 &1691594116 112 | Behaviour: 113 | m_ObjectHideFlags: 0 114 | m_PrefabParentObject: {fileID: 0} 115 | m_PrefabInternal: {fileID: 0} 116 | m_GameObject: {fileID: 1691594113} 117 | m_Enabled: 1 118 | --- !u!20 &1691594117 119 | Camera: 120 | m_ObjectHideFlags: 0 121 | m_PrefabParentObject: {fileID: 0} 122 | m_PrefabInternal: {fileID: 0} 123 | m_GameObject: {fileID: 1691594113} 124 | m_Enabled: 1 125 | serializedVersion: 2 126 | m_ClearFlags: 1 127 | m_BackGroundColor: {r: .192156866, g: .301960796, b: .474509805, a: .0196078438} 128 | m_NormalizedViewPortRect: 129 | serializedVersion: 2 130 | x: 0 131 | y: 0 132 | width: 1 133 | height: 1 134 | near clip plane: .300000012 135 | far clip plane: 1000 136 | field of view: 60 137 | orthographic: 1 138 | orthographic size: 5 139 | m_Depth: -1 140 | m_CullingMask: 141 | serializedVersion: 2 142 | m_Bits: 4294967295 143 | m_RenderingPath: -1 144 | m_TargetTexture: {fileID: 0} 145 | m_TargetDisplay: 0 146 | m_HDR: 0 147 | m_OcclusionCulling: 1 148 | m_StereoConvergence: 10 149 | m_StereoSeparation: .0219999999 150 | --- !u!4 &1691594118 151 | Transform: 152 | m_ObjectHideFlags: 0 153 | m_PrefabParentObject: {fileID: 0} 154 | m_PrefabInternal: {fileID: 0} 155 | m_GameObject: {fileID: 1691594113} 156 | m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} 157 | m_LocalPosition: {x: 0, y: 0, z: -10} 158 | m_LocalScale: {x: 1, y: 1, z: 1} 159 | m_Children: [] 160 | m_Father: {fileID: 0} 161 | m_RootOrder: 0 162 | --- !u!114 &1691594119 163 | MonoBehaviour: 164 | m_ObjectHideFlags: 0 165 | m_PrefabParentObject: {fileID: 0} 166 | m_PrefabInternal: {fileID: 0} 167 | m_GameObject: {fileID: 1691594113} 168 | m_Enabled: 1 169 | m_EditorHideFlags: 0 170 | m_Script: {fileID: 11500000, guid: f8d80c0448068414e9f4cf2423c2412c, type: 3} 171 | m_Name: 172 | m_EditorClassIdentifier: 173 | -------------------------------------------------------------------------------- /Assets/main.unity.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: ea6f87e24116040f8ab3344879c40a5c 3 | DefaultImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Assets/protocol.sproto: -------------------------------------------------------------------------------- 1 | .package { 2 | type 0 : integer 3 | session 1 : integer 4 | } 5 | 6 | foobar 1 { 7 | request { 8 | what 0 : string 9 | } 10 | response { 11 | ok 0 : boolean 12 | } 13 | } 14 | 15 | foo 2 { 16 | response { 17 | ok 0 : boolean 18 | } 19 | } 20 | 21 | bar 3 {} 22 | 23 | blackhole 4 { 24 | request {} 25 | } -------------------------------------------------------------------------------- /Assets/test/Benchmark.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.IO; 3 | using System; 4 | 5 | public class Benchmark { 6 | private SpObject obj; 7 | SpStream encode_stream = new SpStream (1024); 8 | SpStream pack_stream = new SpStream (1024); 9 | SpStream unpack_stream = new SpStream (1024); 10 | const int BENCHUMARK_RUN_TIMES = 100000; 11 | SpTypeManager manager; 12 | 13 | public Benchmark () { 14 | manager = LoadProto (); 15 | 16 | obj = CreateObject (); 17 | manager.Codec.Encode ("AddressBook", obj, encode_stream); 18 | encode_stream.Position = 0; 19 | SpPacker.Pack (encode_stream, pack_stream); 20 | pack_stream.Position = 0; 21 | SpPacker.Unpack (pack_stream, unpack_stream); 22 | } 23 | 24 | public void Run () { 25 | double encode = Encode (); 26 | double pack = Pack (); 27 | double encodeAndPack = EncodeAndPack (); 28 | double unpack = Unpack (); 29 | double decode = Decode (); 30 | double unpackAndDecode = UnpackAndDecode (); 31 | 32 | Util.Log ("Encode:\t" + encode +" s\n" + 33 | "Pack:\t" + pack +" s\n" + 34 | "EncodeAndPack:\t" + encodeAndPack +" s\n" + 35 | "Unpack:\t" + unpack +" s\n" + 36 | "Decode:\t" + decode +" s\n" + 37 | "UnpackAndDecode:\t" + unpackAndDecode +" s\n" 38 | ); 39 | } 40 | 41 | public double Encode () { 42 | double begin = GetMs (); 43 | 44 | for (int i = 0; i < BENCHUMARK_RUN_TIMES; i++) { 45 | encode_stream.Position = 0; 46 | pack_stream.Position = 0; 47 | unpack_stream.Position = 0; 48 | 49 | manager.Codec.Encode ("AddressBook", obj, encode_stream); 50 | //SpPacker.Pack (encode_stream, pack_stream); 51 | //SpPacker.Unpack (pack_stream, unpack_stream); 52 | unpack_stream.Position = 0; 53 | //manager.Codec.Decode ("AddressBook", unpack_stream); 54 | } 55 | 56 | double end = GetMs (); 57 | return (end - begin)/1000; 58 | } 59 | 60 | public double Pack () { 61 | double begin = GetMs (); 62 | 63 | for (int i = 0; i < BENCHUMARK_RUN_TIMES; i++) { 64 | encode_stream.Position = 0; 65 | pack_stream.Position = 0; 66 | unpack_stream.Position = 0; 67 | 68 | //manager.Codec.Encode ("AddressBook", obj, encode_stream); 69 | SpPacker.Pack (encode_stream, pack_stream); 70 | //SpPacker.Unpack (pack_stream, unpack_stream); 71 | unpack_stream.Position = 0; 72 | //manager.Codec.Decode ("AddressBook", unpack_stream); 73 | } 74 | 75 | double end = GetMs (); 76 | return (end - begin)/1000; 77 | } 78 | 79 | public double EncodeAndPack () { 80 | double begin = GetMs (); 81 | 82 | for (int i = 0; i < BENCHUMARK_RUN_TIMES; i++) { 83 | encode_stream.Position = 0; 84 | pack_stream.Position = 0; 85 | unpack_stream.Position = 0; 86 | 87 | manager.Codec.Encode ("AddressBook", obj, encode_stream); 88 | SpPacker.Pack (encode_stream, pack_stream); 89 | //SpPacker.Unpack (pack_stream, unpack_stream); 90 | unpack_stream.Position = 0; 91 | //manager.Codec.Decode ("AddressBook", unpack_stream); 92 | } 93 | 94 | double end = GetMs (); 95 | return (end - begin)/1000; 96 | } 97 | 98 | public double Unpack () { 99 | double begin = GetMs (); 100 | 101 | for (int i = 0; i < BENCHUMARK_RUN_TIMES; i++) { 102 | encode_stream.Position = 0; 103 | pack_stream.Position = 0; 104 | unpack_stream.Position = 0; 105 | 106 | //manager.Codec.Encode ("AddressBook", obj, encode_stream); 107 | //SpPacker.Pack (encode_stream, pack_stream); 108 | SpPacker.Unpack (pack_stream, unpack_stream); 109 | unpack_stream.Position = 0; 110 | //manager.Codec.Decode ("AddressBook", unpack_stream); 111 | } 112 | 113 | double end = GetMs (); 114 | return (end - begin)/1000; 115 | } 116 | 117 | public double Decode () { 118 | double begin = GetMs (); 119 | 120 | for (int i = 0; i < BENCHUMARK_RUN_TIMES; i++) { 121 | encode_stream.Position = 0; 122 | pack_stream.Position = 0; 123 | unpack_stream.Position = 0; 124 | 125 | //manager.Codec.Encode ("AddressBook", obj, encode_stream); 126 | //SpPacker.Pack (encode_stream, pack_stream); 127 | //SpPacker.Unpack (pack_stream, unpack_stream); 128 | unpack_stream.Position = 0; 129 | manager.Codec.Decode ("AddressBook", unpack_stream); 130 | } 131 | 132 | double end = GetMs (); 133 | return (end - begin)/1000; 134 | } 135 | 136 | public double UnpackAndDecode () { 137 | double begin = GetMs (); 138 | 139 | for (int i = 0; i < BENCHUMARK_RUN_TIMES; i++) { 140 | encode_stream.Position = 0; 141 | pack_stream.Position = 0; 142 | unpack_stream.Position = 0; 143 | 144 | //manager.Codec.Encode ("AddressBook", obj, encode_stream); 145 | //SpPacker.Pack (encode_stream, pack_stream); 146 | SpPacker.Unpack (pack_stream, unpack_stream); 147 | unpack_stream.Position = 0; 148 | manager.Codec.Decode ("AddressBook", unpack_stream); 149 | } 150 | 151 | double end = GetMs (); 152 | return (end - begin)/1000; 153 | } 154 | 155 | private SpObject CreateObject () { 156 | SpObject obj = new SpObject (); 157 | 158 | SpObject person = new SpObject (); 159 | 160 | { 161 | SpObject p = new SpObject (); 162 | p.Insert ("name", "Alice"); 163 | p.Insert ("id", 10000); 164 | 165 | SpObject phone = new SpObject (); 166 | { 167 | SpObject p1 = new SpObject (); 168 | p1.Insert ("number", "123456789"); 169 | p1.Insert ("type", 1); 170 | phone.Append (p1); 171 | } 172 | { 173 | SpObject p1 = new SpObject (); 174 | p1.Insert ("number", "87654321"); 175 | p1.Insert ("type", 2); 176 | phone.Append (p1); 177 | } 178 | p.Insert ("phone", phone); 179 | 180 | person.Append (p); 181 | } 182 | { 183 | SpObject p = new SpObject (); 184 | p.Insert ("name", "Bob"); 185 | p.Insert ("id", 20000); 186 | 187 | SpObject phone = new SpObject (); 188 | { 189 | SpObject p1 = new SpObject (); 190 | p1.Insert ("number", "01234567890"); 191 | p1.Insert ("type", 3); 192 | phone.Append (p1); 193 | } 194 | p.Insert ("phone", phone); 195 | 196 | person.Append (p); 197 | } 198 | 199 | obj.Insert ("person", person); 200 | 201 | return obj; 202 | } 203 | 204 | private SpTypeManager LoadProto () { 205 | SpTypeManager tm = null; 206 | string path = Util.GetFullPath ("AddressBook.sproto"); 207 | using (FileStream stream = new FileStream (path, FileMode.Open)) { 208 | tm = SpTypeManager.Import (stream); 209 | } 210 | return tm; 211 | } 212 | 213 | double GetMs() { 214 | TimeSpan ts = DateTime.Now - new DateTime(1960, 1, 1); 215 | return ts.TotalMilliseconds; 216 | } 217 | } 218 | -------------------------------------------------------------------------------- /Assets/test/Client.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.IO; 3 | using System.Net; 4 | using System.Net.Sockets; 5 | using System; 6 | 7 | public class Client { 8 | private string c2s = @" 9 | .package { 10 | type 0 : integer 11 | session 1 : integer 12 | } 13 | 14 | handshake 1 { 15 | response { 16 | msg 0 : string 17 | } 18 | } 19 | 20 | get 2 { 21 | request { 22 | what 0 : string 23 | } 24 | response { 25 | result 0 : string 26 | } 27 | } 28 | 29 | set 3 { 30 | request { 31 | what 0 : string 32 | value 1 : string 33 | } 34 | }"; 35 | 36 | private string s2c = @" 37 | .package { 38 | type 0 : integer 39 | session 1 : integer 40 | } 41 | 42 | heartbeat 1 {} 43 | "; 44 | 45 | private Socket mSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 46 | SpStream mSendStream = new SpStream (); 47 | private byte[] mRecvBuffer = new byte[1024]; 48 | private int mRecvOffset = 0; 49 | private int mSession = 0; 50 | private SpRpc mRpc; 51 | 52 | public void Run () { 53 | IPAddress ip = IPAddress.Parse ("127.0.0.1"); 54 | mSocket.Connect(new IPEndPoint(ip, 8888)); 55 | 56 | mRpc = SpRpc.Create (s2c, "package"); 57 | mRpc.Attach (c2s); 58 | 59 | Send ("handshake", null); 60 | Send ("set", new SpObject (SpObject.ArgType.Table, 61 | "what", "hello", 62 | "value", "world")); 63 | 64 | mSocket.BeginReceive (mRecvBuffer, 0, mRecvBuffer.Length, SocketFlags.None, new AsyncCallback(ReadCallback), this); 65 | } 66 | 67 | public void Recv (IAsyncResult ar) { 68 | int ret = mSocket.EndReceive (ar); 69 | 70 | if (ret > 0) { 71 | mRecvOffset += ret; 72 | 73 | int read = 0; 74 | while (mRecvOffset > read) { 75 | int size = (mRecvBuffer[read + 1] | (mRecvBuffer[read + 0] << 8)); 76 | 77 | read += 2; 78 | if (mRecvOffset >= size + read) { 79 | SpStream stream = new SpStream (mRecvBuffer, read, size, size); 80 | SpRpcResult result = mRpc.Dispatch (stream); 81 | switch (result.Op) { 82 | case SpRpcOp.Request: 83 | Util.Log ("Recv Request : " + result.Protocol.Name + ", session : " + result.Session); 84 | if (result.Arg != null) 85 | Util.DumpObject (result.Arg); 86 | break; 87 | case SpRpcOp.Response: 88 | Util.Log ("Recv Response : " + result.Protocol.Name + ", session : " + result.Session); 89 | if (result.Arg != null) 90 | Util.DumpObject (result.Arg); 91 | break; 92 | } 93 | 94 | read += size; 95 | } 96 | } 97 | Util.Assert (mRecvOffset == read); 98 | mRecvOffset = 0; 99 | } 100 | 101 | mSocket.BeginReceive (mRecvBuffer, 0, mRecvBuffer.Length, SocketFlags.None, new System.AsyncCallback(ReadCallback), this); 102 | } 103 | 104 | public static void ReadCallback(IAsyncResult ar) { 105 | Client client = (Client)ar.AsyncState; 106 | client.Recv (ar); 107 | } 108 | 109 | private void Send (string proto, SpObject args) { 110 | mSendStream.Reset (); 111 | mSession++; 112 | 113 | Util.Log ("Send Request : " + proto + ", session : " + mSession); 114 | if (args != null) 115 | Util.DumpObject (args); 116 | 117 | mSendStream.Write ((short)0); 118 | mRpc.Request (proto, args, mSession, mSendStream); 119 | int len = mSendStream.Length - 2; 120 | mSendStream.Buffer[0] = (byte)((len >> 8) & 0xff); 121 | mSendStream.Buffer[1] = (byte)(len & 0xff); 122 | mSocket.Send (mSendStream.Buffer, mSendStream.Length, SocketFlags.None); 123 | } 124 | 125 | public void SendGet (string str) { 126 | Send ("get", new SpObject (SpObject.ArgType.Table, "what", str)); 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /Assets/test/Test.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.IO; 3 | 4 | public class Test { 5 | public void Run () { 6 | SpTypeManager manager = LoadProto (); 7 | 8 | SpObject obj = CreateObject (); 9 | CheckObj (obj); 10 | 11 | Util.Log ("Encode"); 12 | SpStream encode_stream = new SpStream (); 13 | manager.Codec.Encode ("AddressBook", obj, encode_stream); 14 | 15 | encode_stream.Position = 0; 16 | Util.DumpStream (encode_stream); 17 | 18 | Util.Log ("Decode"); 19 | encode_stream.Position = 0; 20 | SpObject newObj = manager.Codec.Decode ("AddressBook", encode_stream); 21 | CheckObj (newObj); 22 | 23 | Util.Log ("Pack"); 24 | encode_stream.Position = 0; 25 | SpStream pack_stream = new SpStream (); 26 | SpPacker.Pack (encode_stream, pack_stream); 27 | 28 | pack_stream.Position = 0; 29 | Util.DumpStream (pack_stream); 30 | 31 | Util.Log ("Unpack"); 32 | pack_stream.Position = 0; 33 | SpStream unpack_stream = new SpStream (); 34 | SpPacker.Unpack (pack_stream, unpack_stream); 35 | 36 | unpack_stream.Position = 0; 37 | Util.DumpStream (unpack_stream); 38 | 39 | Util.Log ("Decode"); 40 | unpack_stream.Position = 0; 41 | newObj = manager.Codec.Decode ("AddressBook", unpack_stream); 42 | CheckObj (newObj); 43 | } 44 | 45 | private SpObject CreateObject () { 46 | SpObject obj = new SpObject (); 47 | 48 | SpObject person = new SpObject (); 49 | 50 | { 51 | SpObject p = new SpObject (); 52 | p.Insert ("name", "Alice"); 53 | p.Insert ("id", 10000); 54 | 55 | SpObject phone = new SpObject (); 56 | { 57 | SpObject p1 = new SpObject (); 58 | p1.Insert ("number", "123456789"); 59 | p1.Insert ("type", 1); 60 | phone.Append (p1); 61 | } 62 | { 63 | SpObject p1 = new SpObject (); 64 | p1.Insert ("number", "87654321"); 65 | p1.Insert ("type", 2); 66 | phone.Append (p1); 67 | } 68 | p.Insert ("phone", phone); 69 | 70 | person.Insert (p["id"].AsInt (), p); 71 | } 72 | { 73 | SpObject p = new SpObject (); 74 | p.Insert ("name", "Bob"); 75 | p.Insert ("id", 20000); 76 | 77 | SpObject phone = new SpObject (); 78 | { 79 | SpObject p1 = new SpObject (); 80 | p1.Insert ("number", "01234567890"); 81 | p1.Insert ("type", 3); 82 | phone.Append (p1); 83 | } 84 | p.Insert ("phone", phone); 85 | 86 | person.Insert (p["id"].AsInt (), p); 87 | } 88 | 89 | obj.Insert ("person", person); 90 | 91 | return obj; 92 | } 93 | 94 | private SpTypeManager LoadProto () { 95 | SpTypeManager tm = null; 96 | string path = Util.GetFullPath ("AddressBook.sproto"); 97 | using (FileStream stream = new FileStream (path, FileMode.Open)) { 98 | tm = SpTypeManager.Import (stream); 99 | } 100 | return tm; 101 | } 102 | 103 | private void CheckObj (SpObject obj) { 104 | Util.DumpObject (obj); 105 | Util.Assert (obj["person"][10000]["id"].AsInt () == 10000); 106 | Util.Assert (obj["person"][10000]["name"].AsString ().Equals ("Alice")); 107 | Util.Assert (obj["person"][10000]["phone"][0]["type"].AsInt () == 1); 108 | Util.Assert (obj["person"][10000]["phone"][0]["number"].AsString ().Equals ("123456789")); 109 | Util.Assert (obj["person"][10000]["phone"][1]["type"].AsInt () == 2); 110 | Util.Assert (obj["person"][10000]["phone"][1]["number"].AsString ().Equals ("87654321")); 111 | Util.Assert (obj["person"][20000]["id"].AsInt () == 20000); 112 | Util.Assert (obj["person"][20000]["name"].AsString ().Equals ("Bob")); 113 | Util.Assert (obj["person"][20000]["phone"][0]["type"].AsInt () == 3); 114 | Util.Assert (obj["person"][20000]["phone"][0]["number"].AsString ().Equals ("01234567890")); 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /Assets/test/TestAll.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.IO; 6 | 7 | public class TestAll { 8 | public void Run () { 9 | SpTypeManager manager = LoadProto (); 10 | 11 | // different ways to create SpObject 12 | Util.Log ("Object Create Check"); 13 | CheckObj (CreateObject ()); 14 | CheckObj (CreateObject2 ()); 15 | SpObject obj = CreateObject3 (); 16 | CheckObj (obj); 17 | 18 | Util.Log ("Encode"); 19 | SpStream small_stream = new SpStream (32); 20 | bool success = manager.Codec.Encode ("foobar", obj, small_stream); 21 | Util.Assert (success == false); 22 | Util.Log ("encode failed! require size : " + small_stream.Position); 23 | small_stream.Position = 0; 24 | Util.DumpStream (small_stream); 25 | 26 | SpStream encode_stream = manager.Codec.Encode ("foobar", obj); 27 | encode_stream.Position = 0; 28 | Util.DumpStream (encode_stream); 29 | 30 | Util.Log ("Decode "); 31 | encode_stream.Position = 0; 32 | SpObject ooo = manager.Codec.Decode ("foobar", encode_stream); 33 | CheckObj (ooo); 34 | 35 | Util.Log ("Pack"); 36 | encode_stream.Position = 0; 37 | small_stream.Position = 0; 38 | success = SpPacker.Pack (encode_stream, small_stream); 39 | Util.Assert (success == false); 40 | Util.Log ("pack failed! require size : " + small_stream.Position); 41 | small_stream.Position = 0; 42 | Util.DumpStream (small_stream); 43 | 44 | SpStream pack_stream = SpPacker.Pack (encode_stream); 45 | pack_stream.Position = 0; 46 | Util.DumpStream (pack_stream); 47 | 48 | Util.Log ("Unpack"); 49 | pack_stream.Position = 0; 50 | small_stream.Position = 0; 51 | success = SpPacker.Unpack (pack_stream, small_stream); 52 | Util.Assert (success == false); 53 | Util.Log ("unpack failed! require size : " + small_stream.Position); 54 | small_stream.Position = 0; 55 | Util.DumpStream (small_stream); 56 | 57 | pack_stream.Position = 0; 58 | SpStream decode_stream = SpPacker.Unpack (pack_stream); 59 | decode_stream.Position = 0; 60 | Util.DumpStream (decode_stream); 61 | 62 | Util.Log ("Decode "); 63 | decode_stream.Position = 0; 64 | SpObject newObj = manager.Codec.Decode ("foobar", decode_stream); 65 | CheckObj (newObj); 66 | } 67 | 68 | private SpTypeManager LoadProto () { 69 | SpTypeManager tm = null; 70 | string path = Util.GetFullPath ("foobar.sproto"); 71 | using (FileStream stream = new FileStream (path, FileMode.Open)) { 72 | tm = SpTypeManager.Import (stream); 73 | } 74 | return tm; 75 | } 76 | 77 | private SpObject CreateObject () { 78 | SpObject obj = new SpObject (); 79 | 80 | obj.Insert ("a", new SpObject ("hello")); 81 | obj.Insert ("b", new SpObject (1000000)); 82 | obj.Insert ("c", new SpObject (true)); 83 | 84 | SpObject d = new SpObject (); 85 | d.Insert ("a", "world"); 86 | d.Insert ("c", -1); 87 | obj.Insert ("d", d); 88 | 89 | SpObject e = new SpObject (); 90 | e.Append ("ABC"); 91 | e.Append ("def"); 92 | obj.Insert ("e", e); 93 | 94 | SpObject f = new SpObject (); 95 | f.Append (-3); 96 | f.Append (-2); 97 | f.Append (-1); 98 | f.Append (0); 99 | f.Append (1); 100 | f.Append (2); 101 | obj.Insert ("f", f); 102 | 103 | SpObject g = new SpObject (); 104 | g.Append (true); 105 | g.Append (false); 106 | g.Append (true); 107 | obj.Insert ("g", g); 108 | 109 | SpObject h = new SpObject (); 110 | { 111 | SpObject t = new SpObject (); 112 | t.Insert ("b", 100); 113 | h.Append (t); 114 | } 115 | { 116 | SpObject t = new SpObject (); 117 | t.Insert ("b", -100); 118 | t.Insert ("c", false); 119 | h.Append (t); 120 | } 121 | { 122 | SpObject t = new SpObject (); 123 | t.Insert ("b", 0); 124 | 125 | SpObject he = new SpObject (); 126 | he.Append ("test"); 127 | t.Insert ("e", he); 128 | h.Append (t); 129 | } 130 | obj.Insert ("h", h); 131 | 132 | return obj; 133 | } 134 | 135 | private SpObject CreateObject2 () { 136 | SpObject obj = new SpObject (); 137 | 138 | obj.Insert ("a", "hello"); 139 | obj.Insert ("b", 1000000); 140 | obj.Insert ("c", true); 141 | 142 | SpObject d = new SpObject (); 143 | d.Insert ("a", "world"); 144 | d.Insert ("c", -1); 145 | obj.Insert ("d", d); 146 | 147 | obj.Insert ("e", new SpObject (SpObject.ArgType.Array, "ABC", "def")); 148 | obj.Insert ("f", new SpObject (SpObject.ArgType.Array, -3, -2, -1, 0, 1, 2)); 149 | obj.Insert ("g", new SpObject (SpObject.ArgType.Array, true, false, true)); 150 | 151 | SpObject h = new SpObject (); 152 | { 153 | SpObject t = new SpObject (); 154 | t.Insert ("b", 100); 155 | h.Append (t); 156 | } 157 | { 158 | SpObject t = new SpObject (); 159 | t.Insert ("b", -100); 160 | t.Insert ("c", false); 161 | h.Append (t); 162 | } 163 | { 164 | SpObject t = new SpObject (); 165 | t.Insert ("b", 0); 166 | 167 | SpObject he = new SpObject (); 168 | he.Append ("test"); 169 | t.Insert ("e", he); 170 | h.Append (t); 171 | } 172 | obj.Insert ("h", h); 173 | 174 | return obj; 175 | } 176 | 177 | private SpObject CreateObject3 () { 178 | SpObject obj = new SpObject (SpObject.ArgType.Table, 179 | "a", "hello", 180 | "b", 1000000, 181 | "c", true, 182 | "d", new SpObject (SpObject.ArgType.Table, 183 | "a", "world", 184 | "c", -1), 185 | "e", new SpObject (SpObject.ArgType.Array, "ABC", "def"), 186 | "f", new SpObject (SpObject.ArgType.Array, -3, -2, -1, 0, 1, 2), 187 | "g", new SpObject (SpObject.ArgType.Array, true, false, true), 188 | "h", new SpObject (SpObject.ArgType.Array, 189 | new SpObject (SpObject.ArgType.Table, "b", 100), 190 | new SpObject (SpObject.ArgType.Table, "b", -100, "c", false), 191 | new SpObject (SpObject.ArgType.Table, "b", 0, "e", new SpObject (SpObject.ArgType.Array, "test"))) 192 | ); 193 | 194 | return obj; 195 | } 196 | 197 | private void CheckObj (SpObject obj) { 198 | Util.DumpObject (obj); 199 | Util.Assert (obj["a"].AsString ().Equals ("hello")); 200 | Util.Assert (obj["b"].AsInt () == 1000000); 201 | Util.Assert (obj["c"].AsBoolean () == true); 202 | Util.Assert (obj["d"]["a"].AsString ().Equals ("world")); 203 | Util.Assert (obj["d"]["c"].AsInt () == -1); 204 | Util.Assert (obj["e"][0].AsString ().Equals ("ABC")); 205 | Util.Assert (obj["e"][1].AsString ().Equals ("def")); 206 | Util.Assert (obj["f"][0].AsInt () == -3); 207 | Util.Assert (obj["f"][1].AsInt () == -2); 208 | Util.Assert (obj["f"][2].AsInt () == -1); 209 | Util.Assert (obj["f"][3].AsInt () == 0); 210 | Util.Assert (obj["f"][4].AsInt () == 1); 211 | Util.Assert (obj["f"][5].AsInt () == 2); 212 | Util.Assert (obj["g"][0].AsBoolean () == true); 213 | Util.Assert (obj["g"][1].AsBoolean () == false); 214 | Util.Assert (obj["g"][2].AsBoolean () == true); 215 | Util.Assert (obj["h"][0]["b"].AsInt () == 100); 216 | Util.Assert (obj["h"][1]["b"].AsInt () == -100); 217 | Util.Assert (obj["h"][1]["c"].AsBoolean () == false); 218 | Util.Assert (obj["h"][2]["b"].AsInt () == 0); 219 | Util.Assert (obj["h"][2]["e"][0].AsString ().Equals ("test")); 220 | } 221 | } 222 | 223 | -------------------------------------------------------------------------------- /Assets/test/TestRpc.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.IO; 3 | 4 | public class TestRpc { 5 | private const string server_proto = @" 6 | .package { 7 | type 0 : integer 8 | session 1 : integer 9 | } 10 | 11 | foobar 1 { 12 | request { 13 | what 0 : string 14 | } 15 | response { 16 | ok 0 : boolean 17 | } 18 | } 19 | 20 | foo 2 { 21 | response { 22 | ok 0 : boolean 23 | } 24 | } 25 | 26 | bar 3 {} 27 | 28 | blackhole 4 { 29 | request {} 30 | } 31 | "; 32 | 33 | const string client_proto = @" 34 | .package { 35 | type 0 : integer 36 | session 1 : integer 37 | } 38 | "; 39 | 40 | private SpRpc server; 41 | private SpRpc client; 42 | 43 | public void Run () { 44 | SpTypeManager server_tm = SpTypeManager.Import (server_proto); 45 | 46 | server = SpRpc.Create (server_tm, "package"); 47 | 48 | client = SpRpc.Create (client_proto, "package"); 49 | client.Attach (server_tm); 50 | 51 | TestFoobar (); 52 | TestFoo (); 53 | TestBar (); 54 | TestBlackhole (); 55 | } 56 | 57 | private void TestFoobar () { 58 | Util.Log ("client request foobar"); 59 | 60 | SpObject foobar_request = new SpObject (); 61 | foobar_request.Insert ("what", "foo"); 62 | SpStream req = client.Request ("foobar", foobar_request, 1); 63 | 64 | Util.Assert (req.Length == 11); 65 | Util.Log ("request foobar size = " + req.Length); 66 | 67 | req.Position = 0; 68 | SpRpcResult dispatch_result = server.Dispatch (req); 69 | Util.Assert (dispatch_result.Arg["what"].AsString ().Equals ("foo")); 70 | Util.DumpObject (dispatch_result.Arg); 71 | 72 | Util.Log ("server response"); 73 | 74 | SpObject foobar_response = new SpObject (); 75 | foobar_response.Insert ("ok", true); 76 | SpStream resp = server.Response (dispatch_result.Session, foobar_response); 77 | 78 | Util.Assert (resp.Length == 7); 79 | Util.Log ("response package size = " + resp.Length); 80 | 81 | Util.Log ("client dispatch"); 82 | 83 | resp.Position = 0; 84 | dispatch_result = client.Dispatch (resp); 85 | Util.Assert (dispatch_result.Arg["ok"].AsBoolean () == true); 86 | Util.DumpObject (dispatch_result.Arg); 87 | } 88 | 89 | private void TestFoo () { 90 | SpStream req = client.Request ("foo", null, 2); 91 | 92 | Util.Assert (req.Length == 4); 93 | Util.Log ("request foo size = " + req.Length); 94 | 95 | req.Position = 0; 96 | SpRpcResult dispatch_result = server.Dispatch (req); 97 | 98 | SpObject foobar_response = new SpObject (); 99 | foobar_response.Insert ("ok", false); 100 | SpStream resp = server.Response (dispatch_result.Session, foobar_response); 101 | 102 | Util.Assert (resp.Length == 7); 103 | Util.Log ("response package size = " + resp.Length); 104 | 105 | Util.Log ("client dispatch"); 106 | 107 | resp.Position = 0; 108 | dispatch_result = client.Dispatch (resp); 109 | Util.Assert (dispatch_result.Arg["ok"].AsBoolean () == false); 110 | Util.DumpObject (dispatch_result.Arg); 111 | } 112 | 113 | private void TestBar () { 114 | SpStream req = client.Request ("bar"); 115 | 116 | Util.Assert (req.Length == 3); 117 | Util.Log ("request bar size = " + req.Length); 118 | 119 | req.Position = 0; 120 | server.Dispatch (req); 121 | } 122 | 123 | private void TestBlackhole () { 124 | SpStream req = client.Request ("blackhole"); 125 | Util.Assert (req.Length == 3); 126 | Util.Log ("request blackhole size = " + req.Length); 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /Assets/test/TestUnit.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.IO; 3 | 4 | public class TestUnit { 5 | 6 | SpTypeManager manager; 7 | 8 | private void TestStr (string s) { 9 | SpObject obj = new SpObject (SpObject.ArgType.Table, "a", s); 10 | 11 | Util.Log ("------------------TestStr----------------------------"); 12 | Util.Log (s); 13 | 14 | Util.Log ("Encode"); 15 | SpStream encode_stream = new SpStream (2); 16 | bool ret = manager.Codec.Encode ("ss", obj, encode_stream); 17 | Util.Assert (ret == false); 18 | encode_stream = new SpStream (encode_stream.Position); 19 | ret = manager.Codec.Encode ("ss", obj, encode_stream); 20 | Util.Assert (ret == true); 21 | encode_stream.Position = 0; 22 | Util.DumpStream (encode_stream); 23 | 24 | Util.Log ("Pack"); 25 | encode_stream.Position = 0; 26 | SpStream pack_stream = new SpStream (); 27 | SpPacker.Pack (encode_stream, pack_stream); 28 | pack_stream.Position = 0; 29 | Util.DumpStream (pack_stream); 30 | 31 | Util.Log ("Unpack"); 32 | pack_stream.Position = 0; 33 | SpStream unpack_stream = new SpStream (); 34 | SpPacker.Unpack (pack_stream, unpack_stream); 35 | unpack_stream.Position = 0; 36 | Util.DumpStream (unpack_stream); 37 | 38 | Util.Log ("Decode"); 39 | unpack_stream.Position = 0; 40 | SpObject dobj = manager.Codec.Decode ("ss", unpack_stream); 41 | string ds = dobj["a"].AsString (); 42 | Util.Log (ds); 43 | Util.Assert (s == ds); 44 | } 45 | 46 | private void TestNest () { 47 | SpObject clist = new SpObject (SpObject.ArgType.Table, 48 | "character", 49 | new SpObject (SpObject.ArgType.Array, 50 | new SpObject (SpObject.ArgType.Table, 51 | "id", 52 | 1, 53 | "attribute", 54 | new SpObject (SpObject.ArgType.Table, 55 | "level", 56 | 1, 57 | "templateId", 58 | 1001, 59 | "ability", 60 | new SpObject (SpObject.ArgType.Table, 61 | "hp", 2530, 62 | "att", 2310 63 | ) 64 | ) 65 | ), 66 | new SpObject (SpObject.ArgType.Table, 67 | "id", 68 | 2, 69 | "attribute", 70 | new SpObject (SpObject.ArgType.Table, 71 | "level", 72 | 1, 73 | "templateId", 74 | 1002, 75 | "ability", 76 | new SpObject (SpObject.ArgType.Table, 77 | "hp", 1320, 78 | "att", 2090 79 | ) 80 | ) 81 | ) 82 | ) 83 | ); 84 | 85 | Util.Log ("------------------TEST CHARACTER----------------------------"); 86 | Util.DumpObject (clist); 87 | 88 | Util.Log ("Encode"); 89 | SpStream encode_stream = new SpStream (); 90 | manager.Codec.Encode ("clist", clist, encode_stream); 91 | encode_stream.Position = 0; 92 | Util.DumpStream (encode_stream); 93 | 94 | Util.Log ("Pack"); 95 | encode_stream.Position = 0; 96 | SpStream pack_stream = new SpStream (); 97 | SpPacker.Pack (encode_stream, pack_stream); 98 | pack_stream.Position = 0; 99 | Util.DumpStream (pack_stream); 100 | 101 | Util.Log ("Unpack"); 102 | pack_stream.Position = 0; 103 | SpStream unpack_stream = new SpStream (); 104 | SpPacker.Unpack (pack_stream, unpack_stream); 105 | unpack_stream.Position = 0; 106 | Util.DumpStream (unpack_stream); 107 | 108 | Util.Log ("Decode"); 109 | unpack_stream.Position = 0; 110 | SpObject dobj = manager.Codec.Decode ("clist", unpack_stream); 111 | Util.DumpObject (dobj); 112 | } 113 | 114 | public void Run () { 115 | 116 | string proto = @" 117 | .ss { 118 | a 0 : string 119 | } 120 | 121 | .ability { 122 | hp 0 : integer 123 | att 1 : integer 124 | } 125 | 126 | .attribute { 127 | templateId 0 : integer 128 | level 1 : integer 129 | ability 2 : ability 130 | } 131 | 132 | .character { 133 | id 0 : integer 134 | attribute 1 : attribute 135 | } 136 | 137 | .clist { 138 | character 0 : *character(id) 139 | } 140 | 141 | "; 142 | manager = SpTypeManager.Import (proto); 143 | 144 | 145 | 146 | TestStr ("12345678");/* 147 | TestNest (); 148 | 149 | TestStr (""); 150 | TestStr ("123"); 151 | TestStr ("123456"); 152 | TestStr ("12345678"); 153 | TestStr ("12345678123"); 154 | TestStr ("12345678123456"); 155 | TestStr ("1234567812345678"); 156 | TestStr ("12345678123456781234567812345678"); 157 | TestStr ("123456781234567812345678123456781"); 158 | TestStr ("123456781234567812345678123456781234567"); */ 159 | } 160 | 161 | private void CheckObj (SpObject obj) { 162 | Util.DumpObject (obj); 163 | Util.Assert (obj["person"][10000]["id"].AsInt () == 10000); 164 | Util.Assert (obj["person"][10000]["name"].AsString ().Equals ("Alice")); 165 | Util.Assert (obj["person"][10000]["phone"][0]["type"].AsInt () == 1); 166 | Util.Assert (obj["person"][10000]["phone"][0]["number"].AsString ().Equals ("123456789")); 167 | Util.Assert (obj["person"][10000]["phone"][1]["type"].AsInt () == 2); 168 | Util.Assert (obj["person"][10000]["phone"][1]["number"].AsString ().Equals ("87654321")); 169 | Util.Assert (obj["person"][20000]["id"].AsInt () == 20000); 170 | Util.Assert (obj["person"][20000]["name"].AsString ().Equals ("Bob")); 171 | Util.Assert (obj["person"][20000]["phone"][0]["type"].AsInt () == 3); 172 | Util.Assert (obj["person"][20000]["phone"][0]["number"].AsString ().Equals ("01234567890")); 173 | } 174 | } 175 | -------------------------------------------------------------------------------- /Assets/test/Util.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.IO; 3 | using System; 4 | using UnityEngine; 5 | 6 | public class Util { 7 | public static void DumpStream (SpStream stream) { 8 | string str = ""; 9 | 10 | byte[] buf = new byte[16]; 11 | int count; 12 | 13 | while ((count = stream.Read (buf, 0, buf.Length)) > 0) { 14 | str += DumpLine (buf, count); 15 | } 16 | 17 | Log (str); 18 | } 19 | 20 | private static string DumpLine (byte[] buf, int count) { 21 | string str = ""; 22 | 23 | for (int i = 0; i < count; i++) { 24 | str += ((i < count) ? String.Format ("{0:X2}", buf[i]) : " "); 25 | str += ((i > 0) && (i < count - 1) && ((i + 1) % 8 == 0) ? " " : " "); 26 | } 27 | str += "\n"; 28 | 29 | return str; 30 | } 31 | 32 | public static void DumpObject (SpObject obj) { 33 | Log (DumpObject (obj, 0)); 34 | } 35 | 36 | private static string DumpObject (SpObject obj, int tab) { 37 | string str = ""; 38 | 39 | if (obj != null) { 40 | if (obj.IsTable ()) { 41 | str = GetTab (tab) + "