├── .gitignore ├── .gitmodules ├── 3rd ├── p │ ├── lua-p.c │ └── p.lua └── pbc │ ├── Android.mk │ ├── Makefile │ ├── README.md │ ├── binding │ └── lua │ │ ├── Makefile │ │ ├── README.md │ │ ├── parser.lua │ │ ├── pbc-lua.c │ │ ├── protobuf.lua │ │ ├── test.lua │ │ ├── test2.lua │ │ └── testparser.lua │ ├── build │ ├── addressbook │ ├── addressbook.pb │ ├── decode │ ├── descriptor.pb │ ├── float │ ├── float.pb │ ├── map │ ├── o │ │ ├── alloc.d │ │ ├── array.d │ │ ├── bootstrap.d │ │ ├── context.d │ │ ├── decode.d │ │ ├── map.d │ │ ├── pattern.d │ │ ├── proto.d │ │ ├── register.d │ │ ├── rmessage.d │ │ ├── stringpool.d │ │ ├── varint.d │ │ └── wmessage.d │ ├── pattern │ ├── pbc │ ├── test │ └── test.pb │ ├── license.txt │ ├── pbc.h │ ├── pbc.sln │ ├── pbc.vcxproj │ ├── pbc.vcxproj.filters │ ├── pbc.xcodeproj │ ├── project.pbxproj │ ├── project.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcuserdata │ │ │ ├── dev.xcuserdatad │ │ │ └── UserInterfaceState.xcuserstate │ │ │ └── juren.xcuserdatad │ │ │ └── UserInterfaceState.xcuserstate │ └── xcuserdata │ │ ├── dev.xcuserdatad │ │ └── xcschemes │ │ │ ├── pbc.xcscheme │ │ │ └── xcschememanagement.plist │ │ └── juren.xcuserdatad │ │ └── xcschemes │ │ ├── pbc.xcscheme │ │ └── xcschememanagement.plist │ ├── pbc │ └── pbc-Prefix.pch │ ├── src │ ├── alloc.c │ ├── alloc.h │ ├── array.c │ ├── array.h │ ├── bootstrap.c │ ├── bootstrap.h │ ├── context.c │ ├── context.h │ ├── decode.c │ ├── descriptor.pbc.h │ ├── map.c │ ├── map.h │ ├── pattern.c │ ├── pattern.h │ ├── proto.c │ ├── proto.h │ ├── register.c │ ├── rmessage.c │ ├── stringpool.c │ ├── stringpool.h │ ├── varint.c │ ├── varint.h │ └── wmessage.c │ ├── test │ ├── addressbook.c │ ├── addressbook.proto │ ├── array.c │ ├── decode.c │ ├── descriptor.proto │ ├── float.c │ ├── float.proto │ ├── map.c │ ├── pattern.c │ ├── pbc.c │ ├── test.c │ ├── test.proto │ └── varint.c │ └── tool │ └── dump.c ├── LICENSE ├── README.md ├── clean.sh ├── configs ├── center.cfg ├── log.cfg └── simpledb.cfg ├── res └── addressbook.proto ├── src ├── center │ ├── agent.lua │ ├── center.lua │ ├── class │ │ └── login.lua │ ├── main.lua │ ├── preload.lua │ └── watchdog.lua ├── copy │ └── main.lua ├── log │ ├── globallog.lua │ ├── main_log.lua │ └── simplemonitor.lua ├── scene │ └── main.lua └── simpledb │ ├── agent.lua │ ├── main.lua │ ├── preload.lua │ ├── simpledb.lua │ └── watchdog.lua ├── start.sh └── tests ├── ASClient ├── .actionScriptProperties ├── .project ├── .settings │ └── org.eclipse.core.resources.prefs ├── bin-debug │ ├── ClientTest.html │ ├── ClientTest.swf │ ├── assets │ │ ├── crossdomain.xml │ │ └── policy.xml │ ├── history │ │ ├── history.css │ │ ├── history.js │ │ └── historyFrame.html │ ├── playerProductInstall.swf │ ├── srcview │ │ ├── ClientTest.zip │ │ ├── SourceIndex.xml │ │ ├── SourceStyles.css │ │ ├── SourceTree.html │ │ ├── SourceTree.swf │ │ ├── index.html │ │ ├── playerProductInstall.swf │ │ ├── source │ │ │ ├── ClientTest.as.html │ │ │ ├── assets │ │ │ │ └── crossdomain.xml.txt │ │ │ ├── socket │ │ │ │ └── ClientSocket.as.html │ │ │ └── view │ │ │ │ └── TestView.as.html │ │ └── swfobject.js │ └── swfobject.js ├── bin-release │ ├── ClientTest.swf │ ├── assets │ │ ├── crossdomain.xml │ │ └── policy.xml │ ├── crossdomain.xml │ └── srcview │ │ ├── ClientTest.zip │ │ ├── SourceIndex.xml │ │ ├── SourceStyles.css │ │ ├── SourceTree.html │ │ ├── SourceTree.swf │ │ ├── index.html │ │ ├── playerProductInstall.swf │ │ ├── source │ │ ├── ClientTest.as.html │ │ ├── assets │ │ │ └── crossdomain.xml.txt │ │ ├── socket │ │ │ └── ClientSocket.as.html │ │ └── view │ │ │ └── TestView.as.html │ │ └── swfobject.js └── src │ ├── ClientTest.as │ ├── assets │ └── crossdomain.xml │ ├── socket │ └── ClientSocket.as │ └── view │ └── TestView.as ├── c.lua ├── client ├── client.c └── client.lua /.gitignore: -------------------------------------------------------------------------------- 1 | # Object files 2 | *.o 3 | *.ko 4 | *.obj 5 | *.elf 6 | 7 | # Libraries 8 | *.lib 9 | *.a 10 | 11 | # Shared objects (inc. Windows DLLs) 12 | *.dll 13 | *.so 14 | *.so.* 15 | *.dylib 16 | 17 | # Executables 18 | *.exe 19 | *.out 20 | *.app 21 | *.i*86 22 | *.x86_64 23 | *.hex 24 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "skynet"] 2 | path = skynet 3 | url = https://github.com/cloudwu/skynet.git 4 | -------------------------------------------------------------------------------- /3rd/p/lua-p.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/forthxu/zmkp/f96bbf20263c9828629948a5e01373736f35602a/3rd/p/lua-p.c -------------------------------------------------------------------------------- /3rd/p/p.lua: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env lua 2 | p=require("p.core") 3 | -- data = p.add(15,25) 4 | -- print(data.key); 5 | -- for i,v in ipairs(data.sub_array) do 6 | -- print(i,v) 7 | -- end 8 | 9 | msg="徐" 10 | x = p.unpack(p.pack(65535,4294967295,msg)) 11 | print(x.v,x.p,x.msg) -------------------------------------------------------------------------------- /3rd/pbc/Android.mk: -------------------------------------------------------------------------------- 1 | LOCAL_PATH := $(call my-dir) 2 | 3 | include $(CLEAR_VARS) 4 | 5 | LOCAL_MODULE := pbc 6 | 7 | LOCAL_MODULE_FILENAME := libpbc 8 | 9 | LOCAL_SRC_FILES := \ 10 | src/alloc.c \ 11 | src/array.c \ 12 | src/bootstrap.c \ 13 | src/context.c \ 14 | src/decode.c \ 15 | src/map.c \ 16 | src/pattern.c \ 17 | src/proto.c \ 18 | src/register.c \ 19 | src/rmessage.c \ 20 | src/stringpool.c \ 21 | src/varint.c \ 22 | src/wmessage.c \ 23 | 24 | 25 | 26 | LOCAL_C_INCLUDES+= src\ 27 | 28 | 29 | include $(BUILD_STATIC_LIBRARY) 30 | -------------------------------------------------------------------------------- /3rd/pbc/Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | CFLAGS = -O2 -fPIC 3 | AR = ar rc 4 | 5 | BUILD = build 6 | 7 | .PHONY : all lib clean tool 8 | 9 | LIBSRCS = context.c varint.c array.c pattern.c register.c proto.c map.c alloc.c rmessage.c wmessage.c bootstrap.c stringpool.c decode.c 10 | LIBNAME = libpbc.a 11 | 12 | TESTSRCS = addressbook.c pattern.c pbc.c float.c map.c test.c decode.c 13 | PROTOSRCS = addressbook.proto descriptor.proto float.proto test.proto 14 | 15 | BUILD_O = $(BUILD)/o 16 | 17 | all : lib test 18 | 19 | lib : $(LIBNAME) 20 | 21 | clean : 22 | rm -rf $(BUILD) 23 | 24 | $(BUILD) : $(BUILD_O) 25 | 26 | $(BUILD_O) : 27 | mkdir -p $@ 28 | 29 | TOOL := $(BUILD)/dump 30 | 31 | tool : $(TOOL) 32 | 33 | $(TOOL) : | $(BUILD) 34 | $(TOOL) : $(LIBNAME) 35 | $(TOOL) : tool/dump.c 36 | cd $(BUILD) && $(CC) $(CFLAGS) -I.. -L. -o dump ../$< -lpbc 37 | 38 | LIB_O := 39 | 40 | define BUILD_temp 41 | TAR := $(BUILD_O)/$(notdir $(basename $(1))) 42 | LIB_O := $(LIB_O) $$(TAR).o 43 | $$(TAR).o : | $(BUILD_O) 44 | -include $$(TAR).d 45 | $$(TAR).o : src/$(1) 46 | $(CC) $(CFLAGS) -c -Isrc -I. -o $$@ -MMD $$< 47 | endef 48 | 49 | $(foreach s,$(LIBSRCS),$(eval $(call BUILD_temp,$(s)))) 50 | 51 | $(LIBNAME) : $(LIB_O) 52 | cd $(BUILD) && $(AR) $(LIBNAME) $(addprefix ../,$^) 53 | 54 | TEST := 55 | 56 | define TEST_temp 57 | TAR := $(BUILD)/$(notdir $(basename $(1))) 58 | TEST := $(TEST) $$(TAR) 59 | $$(TAR) : | $(BUILD) 60 | $$(TAR) : $(LIBNAME) 61 | $$(TAR) : test/$(1) 62 | cd $(BUILD) && $(CC) $(CFLAGS) -I.. -L. -o $$(notdir $$@) ../$$< -lpbc 63 | endef 64 | 65 | $(foreach s,$(TESTSRCS),$(eval $(call TEST_temp,$(s)))) 66 | 67 | test : $(TEST) proto 68 | 69 | PROTO := 70 | 71 | define PROTO_temp 72 | TAR := $(BUILD)/$(notdir $(basename $(1))) 73 | PROTO := $(PROTO) $$(TAR).pb 74 | $$(TAR).pb : | $(BUILD) 75 | $$(TAR).pb : test/$(1) 76 | protoc -o$$@ $$< 77 | endef 78 | 79 | $(foreach s,$(PROTOSRCS),$(eval $(call PROTO_temp,$(s)))) 80 | 81 | proto : $(PROTO) 82 | 83 | .PHONY : all lib test proto clean 84 | 85 | -------------------------------------------------------------------------------- /3rd/pbc/README.md: -------------------------------------------------------------------------------- 1 | ## PBC 2 | 3 | PBC is a google protocol buffers library for C without code generation. 4 | 5 | ## Quick Example 6 | 7 | package tutorial; 8 | 9 | message Person { 10 | required string name = 1; 11 | required int32 id = 2; // Unique ID number for this person. 12 | optional string email = 3; 13 | 14 | enum PhoneType { 15 | MOBILE = 0; 16 | HOME = 1; 17 | WORK = 2; 18 | } 19 | 20 | message PhoneNumber { 21 | required string number = 1; 22 | optional PhoneType type = 2 [default = HOME]; 23 | } 24 | 25 | repeated PhoneNumber phone = 4; 26 | } 27 | 28 | ```C 29 | struct pbc_rmessage * m = pbc_rmessage_new(env, "tutorial.Person", slice); 30 | printf("name = %s\n", pbc_rmessage_string(m , "name" , 0 , NULL)); 31 | printf("id = %d\n", pbc_rmessage_integer(m , "id" , 0 , NULL)); 32 | printf("email = %s\n", pbc_rmessage_string(m , "email" , 0 , NULL)); 33 | 34 | int phone_n = pbc_rmessage_size(m, "phone"); 35 | int i; 36 | 37 | for (i=0;i 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. -------------------------------------------------------------------------------- /3rd/pbc/pbc.h: -------------------------------------------------------------------------------- 1 | #ifndef PROTOBUF_C_H 2 | #define PROTOBUF_C_H 3 | 4 | #include 5 | #include 6 | 7 | #define PBC_ARRAY_CAP 64 8 | 9 | #define PBC_NOEXIST -1 10 | #define PBC_INT 1 11 | #define PBC_REAL 2 12 | #define PBC_BOOL 3 13 | #define PBC_ENUM 4 14 | #define PBC_STRING 5 15 | #define PBC_MESSAGE 6 16 | #define PBC_FIXED64 7 17 | #define PBC_FIXED32 8 18 | #define PBC_BYTES 9 19 | #define PBC_INT64 10 20 | #define PBC_UINT 11 21 | #define PBC_UNKNOWN 12 22 | #define PBC_REPEATED 128 23 | 24 | typedef struct _pbc_array { char _data[PBC_ARRAY_CAP]; } pbc_array[1]; 25 | 26 | struct pbc_slice { 27 | void *buffer; 28 | int len; 29 | }; 30 | 31 | struct pbc_pattern; 32 | struct pbc_env; 33 | struct pbc_rmessage; 34 | struct pbc_wmessage; 35 | 36 | struct pbc_env * pbc_new(void); 37 | void pbc_delete(struct pbc_env *); 38 | int pbc_register(struct pbc_env *, struct pbc_slice * slice); 39 | int pbc_type(struct pbc_env *, const char * type_name , const char * key , const char ** type); 40 | const char * pbc_error(struct pbc_env *); 41 | 42 | // callback api 43 | union pbc_value { 44 | struct { 45 | uint32_t low; 46 | uint32_t hi; 47 | } i; 48 | double f; 49 | struct pbc_slice s; 50 | struct { 51 | int id; 52 | const char * name; 53 | } e; 54 | }; 55 | 56 | typedef void (*pbc_decoder)(void *ud, int type, const char * type_name, union pbc_value *v, int id, const char *key); 57 | int pbc_decode(struct pbc_env * env, const char * type_name , struct pbc_slice * slice, pbc_decoder f, void *ud); 58 | 59 | // message api 60 | 61 | struct pbc_rmessage * pbc_rmessage_new(struct pbc_env * env, const char * type_name , struct pbc_slice * slice); 62 | void pbc_rmessage_delete(struct pbc_rmessage *); 63 | 64 | uint32_t pbc_rmessage_integer(struct pbc_rmessage * , const char *key , int index, uint32_t *hi); 65 | double pbc_rmessage_real(struct pbc_rmessage * , const char *key , int index); 66 | const char * pbc_rmessage_string(struct pbc_rmessage * , const char *key , int index, int *sz); 67 | struct pbc_rmessage * pbc_rmessage_message(struct pbc_rmessage *, const char *key, int index); 68 | int pbc_rmessage_size(struct pbc_rmessage *, const char *key); 69 | int pbc_rmessage_next(struct pbc_rmessage *, const char **key); 70 | 71 | struct pbc_wmessage * pbc_wmessage_new(struct pbc_env * env, const char *type_name); 72 | void pbc_wmessage_delete(struct pbc_wmessage *); 73 | 74 | // for negative integer, pass -1 to hi 75 | int pbc_wmessage_integer(struct pbc_wmessage *, const char *key, uint32_t low, uint32_t hi); 76 | int pbc_wmessage_real(struct pbc_wmessage *, const char *key, double v); 77 | int pbc_wmessage_string(struct pbc_wmessage *, const char *key, const char * v, int len); 78 | struct pbc_wmessage * pbc_wmessage_message(struct pbc_wmessage *, const char *key); 79 | void * pbc_wmessage_buffer(struct pbc_wmessage *, struct pbc_slice * slice); 80 | 81 | // array api 82 | 83 | int pbc_array_size(pbc_array); 84 | uint32_t pbc_array_integer(pbc_array array, int index, uint32_t *hi); 85 | double pbc_array_real(pbc_array array, int index); 86 | struct pbc_slice * pbc_array_slice(pbc_array array, int index); 87 | 88 | void pbc_array_push_integer(pbc_array array, uint32_t low, uint32_t hi); 89 | void pbc_array_push_slice(pbc_array array, struct pbc_slice *); 90 | void pbc_array_push_real(pbc_array array, double v); 91 | 92 | struct pbc_pattern * pbc_pattern_new(struct pbc_env * , const char * message, const char *format, ...); 93 | void pbc_pattern_delete(struct pbc_pattern *); 94 | 95 | // return unused bytes , -1 for error 96 | int pbc_pattern_pack(struct pbc_pattern *, void *input, struct pbc_slice * s); 97 | 98 | // <0 for error 99 | int pbc_pattern_unpack(struct pbc_pattern *, struct pbc_slice * s , void * output); 100 | 101 | void pbc_pattern_set_default(struct pbc_pattern * , void *data); 102 | void pbc_pattern_close_arrays(struct pbc_pattern *, void *data); 103 | 104 | #endif 105 | -------------------------------------------------------------------------------- /3rd/pbc/pbc.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2012 4 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "pbc", "pbc.vcxproj", "{82356F33-956B-4931-9977-BD7994B1C761}" 5 | EndProject 6 | Global 7 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 8 | Debug|Win32 = Debug|Win32 9 | Release|Win32 = Release|Win32 10 | EndGlobalSection 11 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 12 | {82356F33-956B-4931-9977-BD7994B1C761}.Debug|Win32.ActiveCfg = Debug|Win32 13 | {82356F33-956B-4931-9977-BD7994B1C761}.Debug|Win32.Build.0 = Debug|Win32 14 | {82356F33-956B-4931-9977-BD7994B1C761}.Release|Win32.ActiveCfg = Release|Win32 15 | {82356F33-956B-4931-9977-BD7994B1C761}.Release|Win32.Build.0 = Release|Win32 16 | EndGlobalSection 17 | GlobalSection(SolutionProperties) = preSolution 18 | HideSolutionNode = FALSE 19 | EndGlobalSection 20 | EndGlobal 21 | -------------------------------------------------------------------------------- /3rd/pbc/pbc.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | {82356F33-956B-4931-9977-BD7994B1C761} 43 | Win32Proj 44 | ConsoleApplication1 45 | 46 | 47 | 48 | StaticLibrary 49 | true 50 | v110_xp 51 | Unicode 52 | 53 | 54 | StaticLibrary 55 | false 56 | v110_xp 57 | true 58 | Unicode 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | Level3 74 | Disabled 75 | WIN32;_LIB;_DEBUG;%(PreprocessorDefinitions) 76 | .;.\pbc;.\pbc\src 77 | true 78 | CompileAsCpp 79 | 4146;4273;4244;4018 80 | 81 | 82 | Windows 83 | true 84 | 85 | 86 | 87 | 88 | 89 | Level3 90 | MaxSpeed 91 | true 92 | true 93 | WIN32;_LIB;NDEBUG;%(PreprocessorDefinitions) 94 | .;.\pbc;.\pbc\src 95 | true 96 | CompileAsCpp 97 | 4146;4273;4244;4018 98 | 99 | 100 | Windows 101 | true 102 | true 103 | true 104 | 105 | 106 | 107 | 108 | 109 | 110 | -------------------------------------------------------------------------------- /3rd/pbc/pbc.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {aa230bd6-7da2-4cfb-af39-52310e429716} 6 | 7 | 8 | 9 | 10 | src 11 | 12 | 13 | src 14 | 15 | 16 | src 17 | 18 | 19 | src 20 | 21 | 22 | src 23 | 24 | 25 | src 26 | 27 | 28 | src 29 | 30 | 31 | src 32 | 33 | 34 | src 35 | 36 | 37 | src 38 | 39 | 40 | 41 | 42 | 43 | src 44 | 45 | 46 | src 47 | 48 | 49 | src 50 | 51 | 52 | src 53 | 54 | 55 | src 56 | 57 | 58 | src 59 | 60 | 61 | src 62 | 63 | 64 | src 65 | 66 | 67 | src 68 | 69 | 70 | src 71 | 72 | 73 | src 74 | 75 | 76 | src 77 | 78 | 79 | src 80 | 81 | 82 | -------------------------------------------------------------------------------- /3rd/pbc/pbc.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /3rd/pbc/pbc.xcodeproj/project.xcworkspace/xcuserdata/dev.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/forthxu/zmkp/f96bbf20263c9828629948a5e01373736f35602a/3rd/pbc/pbc.xcodeproj/project.xcworkspace/xcuserdata/dev.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /3rd/pbc/pbc.xcodeproj/project.xcworkspace/xcuserdata/juren.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/forthxu/zmkp/f96bbf20263c9828629948a5e01373736f35602a/3rd/pbc/pbc.xcodeproj/project.xcworkspace/xcuserdata/juren.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /3rd/pbc/pbc.xcodeproj/xcuserdata/dev.xcuserdatad/xcschemes/pbc.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 42 | 43 | 44 | 45 | 51 | 52 | 54 | 55 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /3rd/pbc/pbc.xcodeproj/xcuserdata/dev.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | pbc.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | D7EF1333179E5530002B6A46 16 | 17 | primary 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /3rd/pbc/pbc.xcodeproj/xcuserdata/juren.xcuserdatad/xcschemes/pbc.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 42 | 43 | 44 | 45 | 51 | 52 | 54 | 55 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /3rd/pbc/pbc.xcodeproj/xcuserdata/juren.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | pbc.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | D7EF1333179E5530002B6A46 16 | 17 | primary 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /3rd/pbc/pbc/pbc-Prefix.pch: -------------------------------------------------------------------------------- 1 | // 2 | // Prefix header for all source files of the 'pbc' target in the 'pbc' project 3 | // 4 | 5 | #ifdef __OBJC__ 6 | #import 7 | #import "pbc.h" 8 | #endif 9 | -------------------------------------------------------------------------------- /3rd/pbc/src/alloc.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | static int _g = 0; 5 | 6 | void * _pbcM_malloc(size_t sz) { 7 | ++ _g; 8 | return malloc(sz); 9 | } 10 | 11 | void _pbcM_free(void *p) { 12 | if (p) { 13 | -- _g; 14 | free(p); 15 | } 16 | } 17 | 18 | void* _pbcM_realloc(void *p, size_t sz) { 19 | return realloc(p,sz); 20 | } 21 | 22 | void _pbcM_memory() { 23 | printf("%d\n",_g); 24 | } 25 | 26 | struct heap_page { 27 | struct heap_page * next; 28 | }; 29 | 30 | struct heap { 31 | struct heap_page *current; 32 | int size; 33 | int used; 34 | }; 35 | 36 | struct heap * 37 | _pbcH_new(int pagesize) { 38 | int cap = 1024; 39 | while(cap < pagesize) { 40 | cap *= 2; 41 | } 42 | struct heap * h = (struct heap *)_pbcM_malloc(sizeof(struct heap)); 43 | h->current = (struct heap_page *)_pbcM_malloc(sizeof(struct heap_page) + cap); 44 | h->size = cap; 45 | h->used = 0; 46 | h->current->next = NULL; 47 | return h; 48 | } 49 | 50 | void 51 | _pbcH_delete(struct heap *h) { 52 | struct heap_page * p = h->current; 53 | struct heap_page * next = p->next; 54 | for(;;) { 55 | _pbcM_free(p); 56 | if (next == NULL) 57 | break; 58 | p = next; 59 | next = p->next; 60 | } 61 | _pbcM_free(h); 62 | } 63 | 64 | void* 65 | _pbcH_alloc(struct heap *h, int size) { 66 | size = (size + 3) & ~3; 67 | if (h->size - h->used < size) { 68 | struct heap_page * p; 69 | if (size < h->size) { 70 | p = (struct heap_page *)_pbcM_malloc(sizeof(struct heap_page) + h->size); 71 | } else { 72 | p = (struct heap_page *)_pbcM_malloc(sizeof(struct heap_page) + size); 73 | } 74 | p->next = h->current; 75 | h->current = p; 76 | h->used = size; 77 | return (p+1); 78 | } else { 79 | char * buffer = (char *)(h->current + 1); 80 | buffer += h->used; 81 | h->used += size; 82 | return buffer; 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /3rd/pbc/src/alloc.h: -------------------------------------------------------------------------------- 1 | #ifndef PROTOBUF_C_ALLOC_H 2 | #define PROTOBUF_C_ALLOC_H 3 | 4 | #include 5 | #include 6 | 7 | void * _pbcM_malloc(size_t sz); 8 | void _pbcM_free(void *p); 9 | void * _pbcM_realloc(void *p, size_t sz); 10 | void _pbcM_memory(); 11 | 12 | struct heap; 13 | 14 | struct heap * _pbcH_new(int pagesize); 15 | void _pbcH_delete(struct heap *); 16 | void* _pbcH_alloc(struct heap *, int size); 17 | 18 | #define HMALLOC(size) ((h) ? _pbcH_alloc(h, size) : _pbcM_malloc(size)) 19 | 20 | #define malloc _pbcM_malloc 21 | #define free _pbcM_free 22 | #define realloc _pbcM_realloc 23 | #define memory _pbcM_memory 24 | 25 | #ifdef _WIN32 26 | 27 | #include 28 | 29 | #endif 30 | 31 | #ifdef _MSC_VER 32 | 33 | #define alloca _alloca 34 | 35 | #endif 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /3rd/pbc/src/array.c: -------------------------------------------------------------------------------- 1 | #include "pbc.h" 2 | #include "array.h" 3 | #include "alloc.h" 4 | 5 | #include 6 | #include 7 | 8 | struct array { 9 | int number; 10 | struct heap *heap; 11 | union _pbc_var * a; 12 | }; 13 | 14 | #define INNER_FIELD ((PBC_ARRAY_CAP - sizeof(struct array)) / sizeof(pbc_var)) 15 | 16 | void 17 | _pbcA_open(pbc_array _array) { 18 | struct array * a = (struct array *)_array; 19 | a->number = 0; 20 | a->heap = NULL; 21 | a->a = (union _pbc_var *)(a+1); 22 | } 23 | 24 | void 25 | _pbcA_open_heap(pbc_array _array, struct heap *h) { 26 | struct array * a = (struct array *)_array; 27 | a->number = 0; 28 | a->heap = h; 29 | a->a = (union _pbc_var *)(a+1); 30 | } 31 | 32 | void 33 | _pbcA_close(pbc_array _array) { 34 | struct array * a = (struct array *)_array; 35 | if (a->heap == NULL && a->a != NULL && (union _pbc_var *)(a+1) != a->a) { 36 | _pbcM_free(a->a); 37 | a->a = NULL; 38 | } 39 | } 40 | 41 | void 42 | _pbcA_push(pbc_array _array, pbc_var var) { 43 | struct array * a = (struct array *)_array; 44 | if (a->number == 0) { 45 | a->a = (union _pbc_var *)(a+1); 46 | } else if (a->number >= INNER_FIELD) { 47 | if (a->number == INNER_FIELD) { 48 | int cap = 1; 49 | while (cap <= a->number + 1) 50 | cap *= 2; 51 | struct heap * h = a->heap; 52 | union _pbc_var * outer = (union _pbc_var *)HMALLOC(cap * sizeof(union _pbc_var)); 53 | memcpy(outer , a->a , INNER_FIELD * sizeof(pbc_var)); 54 | a->a = outer; 55 | } else { 56 | int size=a->number; 57 | if (((size + 1) ^ size) > size) { 58 | struct heap * h = a->heap; 59 | if (h) { 60 | void * old = a->a; 61 | a->a = (union _pbc_var *)_pbcH_alloc(h, sizeof(union _pbc_var) * (size+1) * 2); 62 | memcpy(a->a, old, sizeof(union _pbc_var) * size); 63 | } else { 64 | a->a = (union _pbc_var *)_pbcM_realloc(a->a,sizeof(union _pbc_var) * (size+1) * 2); 65 | } 66 | } 67 | } 68 | } 69 | a->a[a->number] = *var; 70 | ++ a->number; 71 | } 72 | 73 | void 74 | _pbcA_index(pbc_array _array, int idx, pbc_var var) 75 | { 76 | struct array * a = (struct array *)_array; 77 | var[0] = a->a[idx]; 78 | } 79 | 80 | void * 81 | _pbcA_index_p(pbc_array _array, int idx) 82 | { 83 | struct array * a = (struct array *)_array; 84 | return &(a->a[idx]); 85 | } 86 | 87 | int 88 | pbc_array_size(pbc_array _array) { 89 | struct array * a = (struct array *)_array; 90 | return a->number; 91 | } 92 | 93 | uint32_t 94 | pbc_array_integer(pbc_array array, int index, uint32_t *hi) { 95 | pbc_var var; 96 | _pbcA_index(array , index , var); 97 | if (hi) { 98 | *hi = var->integer.hi; 99 | } 100 | return var->integer.low; 101 | } 102 | 103 | double 104 | pbc_array_real(pbc_array array, int index) { 105 | pbc_var var; 106 | _pbcA_index(array , index , var); 107 | return var->real; 108 | } 109 | 110 | struct pbc_slice * 111 | pbc_array_slice(pbc_array _array, int index) { 112 | struct array * a = (struct array *)_array; 113 | if (index <0 || index > a->number) { 114 | return NULL; 115 | } 116 | return (struct pbc_slice *) &(a->a[index]); 117 | } 118 | 119 | void 120 | pbc_array_push_integer(pbc_array array, uint32_t low, uint32_t hi) { 121 | pbc_var var; 122 | var->integer.low = low; 123 | var->integer.hi = hi; 124 | _pbcA_push(array,var); 125 | } 126 | 127 | void 128 | pbc_array_push_slice(pbc_array array, struct pbc_slice *s) { 129 | pbc_var var; 130 | var->m = *s; 131 | _pbcA_push(array,var); 132 | } 133 | 134 | void 135 | pbc_array_push_real(pbc_array array, double v) { 136 | pbc_var var; 137 | var->real = v; 138 | _pbcA_push(array,var); 139 | } 140 | -------------------------------------------------------------------------------- /3rd/pbc/src/array.h: -------------------------------------------------------------------------------- 1 | #ifndef PROTOBUF_C_ARRAY_H 2 | #define PROTOBUF_C_ARRAY_H 3 | 4 | #include "varint.h" 5 | #include "pbc.h" 6 | #include "alloc.h" 7 | 8 | typedef union _pbc_var { 9 | struct longlong integer; 10 | double real; 11 | struct { 12 | const char * str; 13 | int len; 14 | } s; 15 | struct { 16 | int id; 17 | const char * name; 18 | } e; 19 | struct pbc_slice m; 20 | void * p[2]; 21 | } pbc_var[1]; 22 | 23 | void _pbcA_open(pbc_array); 24 | void _pbcA_open_heap(pbc_array, struct heap *h); 25 | void _pbcA_close(pbc_array); 26 | 27 | void _pbcA_push(pbc_array, pbc_var var); 28 | void _pbcA_index(pbc_array , int idx, pbc_var var); 29 | void * _pbcA_index_p(pbc_array _array, int idx); 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /3rd/pbc/src/bootstrap.c: -------------------------------------------------------------------------------- 1 | #include "pbc.h" 2 | #include "map.h" 3 | #include "context.h" 4 | #include "pattern.h" 5 | #include "proto.h" 6 | #include "alloc.h" 7 | #include "bootstrap.h" 8 | #include "stringpool.h" 9 | #include "array.h" 10 | #include "descriptor.pbc.h" 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | /* 18 | 19 | // Descriptor 20 | 21 | // google.protobuf.Descriptor.proto encoded in descriptor.pbc.h with proto pbc.file . 22 | 23 | package pbc; 24 | 25 | message field { 26 | optional string name = 1; 27 | optional int32 id = 2; 28 | optional int32 label = 3; // 0 optional 1 required 2 repeated 29 | optional int32 type = 4; // type_id 30 | optional string type_name = 5; 31 | optional int32 default_int = 6; 32 | optional string default_string = 7; 33 | optional double default_real = 8; 34 | } 35 | 36 | message file { 37 | optional string name = 1; 38 | repeated string dependency = 2; 39 | 40 | repeated string message_name = 3; 41 | repeated int32 message_size = 4; 42 | repeated field message_field = 5; 43 | 44 | repeated string enum_name = 6; 45 | repeated int32 enum_size = 7; 46 | repeated string enum_string = 8; 47 | repeated int32 enum_id = 9; 48 | } 49 | 50 | */ 51 | 52 | struct field_t { 53 | struct pbc_slice name; 54 | int32_t id; 55 | int32_t label; 56 | int32_t type; 57 | struct pbc_slice type_name; 58 | int32_t default_integer; 59 | struct pbc_slice default_string; 60 | double default_real; 61 | }; 62 | 63 | struct file_t { 64 | struct pbc_slice name; // string 65 | pbc_array dependency; // string 66 | pbc_array message_name; // string 67 | pbc_array message_size; // int32 68 | pbc_array message_field; // field_t 69 | pbc_array enum_name; // string 70 | pbc_array enum_size; // int32 71 | pbc_array enum_string; // string 72 | pbc_array enum_id; // int32 73 | }; 74 | 75 | static void 76 | set_enum_one(struct pbc_env *p, struct file_t *file, const char *name, int start, int sz) { 77 | struct map_kv *table = (struct map_kv *)malloc(sz * sizeof(struct map_kv)); 78 | int i; 79 | for (i=0;ienum_id, start+i, id); 83 | _pbcA_index(file->enum_string, start+i, string); 84 | table[i].id = (int)id->integer.low; 85 | table[i].pointer = (void *)string->s.str; 86 | } 87 | _pbcP_push_enum(p,name,table,sz); 88 | 89 | free(table); 90 | } 91 | 92 | static void 93 | set_enums(struct pbc_env *p, struct file_t *file) { 94 | int n = pbc_array_size(file->enum_size); 95 | int i; 96 | int start = 0; 97 | for (i=0;ienum_name,i,name); 100 | pbc_var var; 101 | _pbcA_index(file->enum_size,i,var); 102 | set_enum_one(p, file, name->s.str, start , (int)var->integer.low); 103 | start += var->integer.low; 104 | } 105 | } 106 | 107 | static void 108 | set_default(struct _field *f, struct field_t *input) { 109 | switch (f->type) { 110 | case PTYPE_DOUBLE: 111 | case PTYPE_FLOAT: 112 | f->default_v->real = input->default_real; 113 | break; 114 | case PTYPE_STRING: 115 | case PTYPE_ENUM: 116 | f->default_v->m = input->default_string; 117 | break; 118 | default: 119 | f->default_v->integer.low = input->default_integer; 120 | break; 121 | } 122 | } 123 | 124 | static void 125 | set_msg_one(struct pbc_pattern * FIELD_T, struct pbc_env *p, struct file_t *file, const char *name, int start, int sz , pbc_array queue) { 126 | int i; 127 | for (i=0;imessage_field, start+i, _field); 130 | struct field_t field; 131 | 132 | int ret = pbc_pattern_unpack(FIELD_T, &_field->m, &field); 133 | if (ret != 0) { 134 | continue; 135 | } 136 | struct _field f; 137 | f.id = field.id; 138 | f.name = (const char *)field.name.buffer; 139 | f.type = field.type; 140 | f.label = field.label; 141 | f.type_name.n = (const char *)field.type_name.buffer; 142 | set_default(&f, &field); 143 | 144 | _pbcP_push_message(p,name, &f , queue); 145 | 146 | // don't need to close pattern since no array 147 | } 148 | _pbcP_init_message(p, name); 149 | } 150 | 151 | static void 152 | set_msgs(struct pbc_pattern * FIELD_T, struct pbc_env *p, struct file_t *file , pbc_array queue) { 153 | int n = pbc_array_size(file->message_size); 154 | int i; 155 | int start = 0; 156 | for (i=0;imessage_name,i,name); 159 | pbc_var sz; 160 | _pbcA_index(file->message_size,i,sz); 161 | set_msg_one(FIELD_T, p, file, name->s.str, start , (int)sz->integer.low , queue); 162 | start += sz->integer.low; 163 | } 164 | } 165 | 166 | static void 167 | set_field_one(struct pbc_env *p, struct _field *f) { 168 | const char * type_name = f->type_name.n; 169 | if (f->type == PTYPE_MESSAGE) { 170 | f->type_name.m = (struct _message *)_pbcM_sp_query(p->msgs, type_name); 171 | // printf("MESSAGE: %s %p\n",type_name, f->type_name.m); 172 | } else if (f->type == PTYPE_ENUM) { 173 | f->type_name.e = (struct _enum *)_pbcM_sp_query(p->enums, type_name); 174 | // printf("ENUM: %s %p ",type_name, f->type_name.e); 175 | const char * str = f->default_v->s.str; 176 | if (str && str[0]) { 177 | int err = _pbcM_si_query(f->type_name.e->name, str , &(f->default_v->e.id)); 178 | if (err < 0) 179 | goto _default; 180 | f->default_v->e.name = (const char *)_pbcM_ip_query(f->type_name.e->id, f->default_v->e.id); 181 | // printf("[%s %d]\n",str,f->default_v->e.id); 182 | } else { 183 | _default: 184 | memcpy(f->default_v, f->type_name.e->default_v, sizeof(pbc_var)); 185 | // printf("(%s %d)\n",f->default_v->e.name,f->default_v->e.id); 186 | } 187 | } 188 | } 189 | 190 | void 191 | _pbcB_register_fields(struct pbc_env *p, pbc_array queue) { 192 | int sz = pbc_array_size(queue); 193 | int i; 194 | for (i=0;im.buffer; 198 | set_field_one(p, f); 199 | } 200 | } 201 | 202 | static void 203 | _set_string(struct _pattern_field * f) { 204 | f->ptype = PTYPE_STRING; 205 | f->ctype = CTYPE_VAR; 206 | f->defv->s.str = ""; 207 | f->defv->s.len = 0; 208 | } 209 | 210 | static void 211 | _set_int32(struct _pattern_field * f) { 212 | f->ptype = PTYPE_INT32; 213 | f->ctype = CTYPE_INT32; 214 | } 215 | 216 | static void 217 | _set_double(struct _pattern_field * f) { 218 | f->ptype = PTYPE_DOUBLE; 219 | f->ctype = CTYPE_DOUBLE; 220 | } 221 | 222 | static void 223 | _set_message_array(struct _pattern_field *f) { 224 | f->ptype = PTYPE_MESSAGE; 225 | f->ctype = CTYPE_ARRAY; 226 | } 227 | 228 | static void 229 | _set_string_array(struct _pattern_field * f) { 230 | f->ptype = PTYPE_STRING; 231 | f->ctype = CTYPE_ARRAY; 232 | } 233 | 234 | static void 235 | _set_int32_array(struct _pattern_field * f) { 236 | f->ptype = PTYPE_INT32; 237 | f->ctype = CTYPE_ARRAY; 238 | } 239 | 240 | #define SET_PATTERN(pat , idx , pat_type, field_name , type) \ 241 | pat->f[idx].id = idx+1 ; \ 242 | pat->f[idx].offset = offsetof(struct pat_type, field_name); \ 243 | _set_##type(&pat->f[idx]); 244 | 245 | #define F(idx,field_name,type) SET_PATTERN(FIELD_T, idx, field_t ,field_name, type) 246 | #define D(idx,field_name,type) SET_PATTERN(FILE_T, idx, file_t ,field_name, type) 247 | 248 | static int 249 | register_internal(struct pbc_env * p, struct pbc_slice *slice) { 250 | struct pbc_pattern * FIELD_T = _pbcP_new(p,8); 251 | F(0,name,string); 252 | F(1,id,int32); 253 | F(2,label,int32); 254 | F(3,type,int32); 255 | F(4,type_name,string); 256 | F(5,default_integer,int32); 257 | F(6,default_string,string); 258 | F(7,default_real,double); 259 | 260 | struct pbc_pattern * FILE_T = _pbcP_new(p,10); 261 | 262 | D(0,name,string); 263 | D(1,dependency,string_array); 264 | D(2,message_name,string_array); 265 | D(3,message_size,int32_array); 266 | D(4,message_field,message_array); 267 | D(5,enum_name,string_array); 268 | D(6,enum_size,int32_array); 269 | D(7,enum_string,string_array); 270 | D(8,enum_id,int32_array); 271 | 272 | int ret = 0; 273 | 274 | struct file_t file; 275 | int r = pbc_pattern_unpack(FILE_T, slice, &file); 276 | if (r != 0) { 277 | ret = 1; 278 | goto _return; 279 | } 280 | 281 | _pbcM_sp_insert(p->files , (const char *)file.name.buffer, NULL); 282 | 283 | pbc_array queue; 284 | _pbcA_open(queue); 285 | 286 | set_enums(p, &file); 287 | set_msgs(FIELD_T, p, &file, queue); 288 | _pbcB_register_fields(p, queue); 289 | 290 | _pbcA_close(queue); 291 | pbc_pattern_close_arrays(FILE_T, &file); 292 | 293 | _return: 294 | free(FIELD_T); 295 | free(FILE_T); 296 | return ret; 297 | } 298 | 299 | void 300 | _pbcB_init(struct pbc_env * p) { 301 | struct pbc_slice slice = { pbc_descriptor,sizeof(pbc_descriptor) }; 302 | register_internal(p,&slice); 303 | } 304 | -------------------------------------------------------------------------------- /3rd/pbc/src/bootstrap.h: -------------------------------------------------------------------------------- 1 | #ifndef PROTOBUF_C_BOOTSTRAP_H 2 | #define PROTOBUF_C_BOOTSTRAP_H 3 | 4 | #include "proto.h" 5 | #include "pbc.h" 6 | 7 | void _pbcB_init(struct pbc_env *); 8 | void _pbcB_register_fields(struct pbc_env *, pbc_array queue); 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /3rd/pbc/src/context.c: -------------------------------------------------------------------------------- 1 | #include "pbc.h" 2 | #include "alloc.h" 3 | #include "varint.h" 4 | #include "context.h" 5 | 6 | #include 7 | #include 8 | #include 9 | #ifndef _MSC_VER 10 | #include 11 | #endif 12 | 13 | #define INNER_ATOM ((PBC_CONTEXT_CAP - sizeof(struct context)) / sizeof(struct atom)) 14 | 15 | static char * 16 | wiretype_decode(uint8_t *buffer, int cap , struct atom *a , int start) 17 | { 18 | uint8_t temp[10]; 19 | struct longlong r; 20 | int len; 21 | if (cap >= 10) { 22 | len = _pbcV_decode(buffer, &r); 23 | if (r.hi !=0) 24 | return NULL; 25 | } else { 26 | memcpy(temp, buffer , cap); 27 | len = _pbcV_decode(temp, &r); 28 | if (len > cap || r.hi !=0) 29 | return NULL; 30 | } 31 | 32 | int wiretype = r.low & 7; 33 | a->wire_id = r.low; 34 | buffer += len; 35 | start += len; 36 | cap -=len; 37 | 38 | switch (wiretype) { 39 | case WT_VARINT : 40 | if (cap >=10) { 41 | len = _pbcV_decode(buffer, &a->v.i); 42 | } else { 43 | memcpy(temp, buffer , cap); 44 | len = _pbcV_decode(temp, &a->v.i); 45 | if (cap < len) 46 | return NULL; 47 | } 48 | return (char *)buffer+len; 49 | case WT_BIT64 : 50 | if (cap < 8) 51 | return NULL; 52 | a->v.i.low = buffer[0] | 53 | buffer[1] << 8 | 54 | buffer[2] << 16 | 55 | buffer[3] << 24; 56 | a->v.i.hi = buffer[4] | 57 | buffer[5] << 8 | 58 | buffer[6] << 16 | 59 | buffer[7] << 24; 60 | return (char *)buffer + 8; 61 | case WT_LEND : 62 | if (cap >=10) { 63 | len = _pbcV_decode(buffer, &r); 64 | } else { 65 | memcpy(temp, buffer , cap); 66 | len = _pbcV_decode(temp, &r); 67 | } 68 | if (cap < len + r.low || r.hi !=0) 69 | return NULL; 70 | a->v.s.start = start + len; 71 | a->v.s.end = start + len + r.low; 72 | return (char *)buffer + len + r.low; 73 | case WT_BIT32 : 74 | if (cap < 4) 75 | return NULL; 76 | a->v.i.low = buffer[0] | 77 | buffer[1] << 8 | 78 | buffer[2] << 16 | 79 | buffer[3] << 24; 80 | a->v.i.hi = 0; 81 | return (char *)buffer + 4; 82 | default: 83 | return NULL; 84 | } 85 | } 86 | 87 | static inline int 88 | _decode_varint(uint8_t * buffer, int size , struct atom * a) { 89 | a->wire_id = WT_VARINT; 90 | if (size < 10) { 91 | uint8_t temp[10]; 92 | memcpy(temp,buffer,size); 93 | return _pbcV_decode(temp , &(a->v.i)); 94 | } else { 95 | return _pbcV_decode(buffer , &(a->v.i)); 96 | } 97 | } 98 | 99 | static int 100 | _open_packed_varint(struct context * ctx , uint8_t * buffer, int size) { 101 | struct atom * a = (struct atom *)(ctx + 1); 102 | 103 | int i; 104 | 105 | for (i=0;ia = a; 115 | } else { 116 | int cap = 64; 117 | ctx->a = (struct atom *)malloc(cap * sizeof(struct atom)); 118 | while (size > 0) { 119 | if (i >= cap) { 120 | cap = cap + 64; 121 | ctx->a = (struct atom *)realloc(ctx->a, cap * sizeof(struct atom)); 122 | continue; 123 | } 124 | int len = _decode_varint(buffer, size, &a[i]); 125 | buffer += len; 126 | size -= len; 127 | 128 | ++i; 129 | } 130 | memcpy(ctx->a, a , sizeof(struct atom) * INNER_ATOM); 131 | } 132 | ctx->number = i; 133 | 134 | return i; 135 | } 136 | 137 | int 138 | _pbcC_open_packed(pbc_ctx _ctx, int ptype, void *buffer, int size) { 139 | struct context * ctx = (struct context *)_ctx; 140 | ctx->buffer = (char *)buffer; 141 | ctx->size = size; 142 | ctx->number = 0; 143 | ctx->a = NULL; 144 | 145 | if (buffer == NULL || size == 0) { 146 | return 0; 147 | } 148 | 149 | int bits = 0; 150 | 151 | switch (ptype) { 152 | case PTYPE_INT64: 153 | case PTYPE_UINT64: 154 | case PTYPE_INT32: 155 | case PTYPE_BOOL: 156 | case PTYPE_UINT32: 157 | case PTYPE_ENUM: 158 | case PTYPE_SINT32: 159 | case PTYPE_SINT64: 160 | return _open_packed_varint(ctx , (uint8_t *)buffer, size); 161 | case PTYPE_DOUBLE: 162 | case PTYPE_FIXED64: 163 | case PTYPE_SFIXED64: 164 | ctx->number = size / 8; 165 | bits = 64; 166 | break; 167 | case PTYPE_FLOAT: 168 | case PTYPE_FIXED32: 169 | case PTYPE_SFIXED32: 170 | ctx->number = size / 4; 171 | bits = 32; 172 | break; 173 | default: 174 | return 0; 175 | } 176 | 177 | struct atom * a = (struct atom *)(ctx + 1); 178 | 179 | if (ctx->number > INNER_ATOM) { 180 | ctx->a = (struct atom *)malloc(ctx->number * sizeof(struct atom)); 181 | a = ctx->a; 182 | } else { 183 | ctx->a = a; 184 | } 185 | 186 | int i; 187 | if (bits == 64) { 188 | uint8_t * data = (uint8_t *)buffer; 189 | for (i=0;inumber;i++) { 190 | a[i].wire_id = WT_BIT64; 191 | a[i].v.i.low = data[0] | 192 | data[1] << 8 | 193 | data[2] << 16 | 194 | data[3] << 24; 195 | a[i].v.i.hi = data[4] | 196 | data[5] << 8 | 197 | data[6] << 16 | 198 | data[7] << 24; 199 | data += 8; 200 | } 201 | } else { 202 | uint8_t * data = (uint8_t *)buffer; 203 | for (i=0;inumber;i++) { 204 | a[i].wire_id = WT_BIT32; 205 | a[i].v.i.low = data[0] | 206 | data[1] << 8 | 207 | data[2] << 16 | 208 | data[3] << 24; 209 | a[i].v.i.hi = 0; 210 | data += 4; 211 | } 212 | } 213 | 214 | return ctx->number; 215 | } 216 | 217 | int 218 | _pbcC_open(pbc_ctx _ctx , void *buffer, int size) { 219 | struct context * ctx = (struct context *)_ctx; 220 | ctx->buffer = (char *)buffer; 221 | ctx->size = size; 222 | 223 | if (buffer == NULL || size == 0) { 224 | ctx->number = 0; 225 | ctx->a = NULL; 226 | return 0; 227 | } 228 | 229 | struct atom * a = (struct atom *)(ctx + 1); 230 | 231 | int i; 232 | int start = 0; 233 | 234 | ctx->a = a; 235 | 236 | for (i=0;i 0) { 248 | int cap = 64; 249 | ctx->a = (struct atom *)malloc(cap * sizeof(struct atom)); 250 | while (size > 0) { 251 | if (i >= cap) { 252 | cap = cap + 64; 253 | ctx->a = (struct atom *)realloc(ctx->a, cap * sizeof(struct atom)); 254 | continue; 255 | } 256 | char * next = wiretype_decode((uint8_t *)buffer, size , &ctx->a[i] , start); 257 | if (next == NULL) { 258 | return -i; 259 | } 260 | start += next - (char *)buffer; 261 | size -= next - (char *)buffer; 262 | buffer = next; 263 | ++i; 264 | } 265 | memcpy(ctx->a, a , sizeof(struct atom) * INNER_ATOM); 266 | } 267 | ctx->number = i; 268 | 269 | return i; 270 | } 271 | 272 | 273 | void 274 | _pbcC_close(pbc_ctx _ctx) { 275 | struct context * ctx = (struct context *)_ctx; 276 | if (ctx->a != NULL && (struct atom *)(ctx+1) != ctx->a) { 277 | free(ctx->a); 278 | ctx->a = NULL; 279 | } 280 | } 281 | -------------------------------------------------------------------------------- /3rd/pbc/src/context.h: -------------------------------------------------------------------------------- 1 | #ifndef PROTOBUF_C_CONTEXT_H 2 | #define PROTOBUF_C_CONTEXT_H 3 | 4 | #include 5 | 6 | #include "array.h" 7 | 8 | #define PBC_CONTEXT_CAP 256 9 | 10 | // wiretype 11 | 12 | #define WT_VARINT 0 13 | #define WT_BIT64 1 14 | #define WT_LEND 2 15 | #define WT_BIT32 5 16 | 17 | #define CTYPE_INT32 1 18 | #define CTYPE_INT64 2 19 | #define CTYPE_DOUBLE 3 20 | #define CTYPE_FLOAT 4 21 | #define CTYPE_POINTER 5 22 | #define CTYPE_BOOL 6 23 | #define CTYPE_INT8 7 24 | #define CTYPE_INT16 8 25 | #define CTYPE_ARRAY 9 26 | #define CTYPE_VAR 10 27 | #define CTYPE_PACKED 11 28 | 29 | #define PTYPE_DOUBLE 1 30 | #define PTYPE_FLOAT 2 31 | #define PTYPE_INT64 3 // Not ZigZag encoded. Negative numbers take 10 bytes. Use TYPE_SINT64 if negative values are likely. 32 | #define PTYPE_UINT64 4 33 | #define PTYPE_INT32 5 // Not ZigZag encoded. Negative numbers take 10 bytes. Use TYPE_SINT32 if negative values are likely. 34 | #define PTYPE_FIXED64 6 35 | #define PTYPE_FIXED32 7 36 | #define PTYPE_BOOL 8 37 | #define PTYPE_STRING 9 38 | #define PTYPE_GROUP 10 // Tag-delimited aggregate. 39 | #define PTYPE_MESSAGE 11 // Length-delimited aggregate. 40 | #define PTYPE_BYTES 12 41 | #define PTYPE_UINT32 13 42 | #define PTYPE_ENUM 14 43 | #define PTYPE_SFIXED32 15 44 | #define PTYPE_SFIXED64 16 45 | #define PTYPE_SINT32 17 // Uses ZigZag encoding. 46 | #define PTYPE_SINT64 18 // Uses ZigZag encoding. 47 | 48 | struct slice { 49 | int start; 50 | int end; 51 | }; 52 | 53 | struct atom { 54 | int wire_id; 55 | union { 56 | struct slice s; 57 | struct longlong i; 58 | } v; 59 | }; 60 | 61 | struct context { 62 | char * buffer; 63 | int size; 64 | int number; 65 | struct atom * a; 66 | }; 67 | 68 | typedef struct _pbc_ctx { char _data[PBC_CONTEXT_CAP]; } pbc_ctx[1]; 69 | 70 | int _pbcC_open(pbc_ctx , void *buffer, int size); // <=0 failed 71 | int _pbcC_open_packed(pbc_ctx _ctx, int ptype, void *buffer, int size); 72 | void _pbcC_close(pbc_ctx); 73 | 74 | static inline double 75 | read_double(struct atom * a) { 76 | union { 77 | uint64_t i; 78 | double d; 79 | } u; 80 | u.i = (uint64_t) a->v.i.low | (uint64_t) a->v.i.hi << 32; 81 | return u.d; 82 | } 83 | 84 | static inline float 85 | read_float(struct atom * a) { 86 | union { 87 | uint32_t i; 88 | float f; 89 | } u; 90 | u.i = a->v.i.low; 91 | return u.f; 92 | } 93 | 94 | static inline void 95 | double_encode(double v , uint8_t * buffer) { 96 | union { 97 | double v; 98 | uint64_t e; 99 | } u; 100 | u.v = v; 101 | buffer[0] = (uint8_t) (u.e & 0xff); 102 | buffer[1] = (uint8_t) (u.e >> 8 & 0xff); 103 | buffer[2] = (uint8_t) (u.e >> 16 & 0xff); 104 | buffer[3] = (uint8_t) (u.e >> 24 & 0xff); 105 | buffer[4] = (uint8_t) (u.e >> 32 & 0xff); 106 | buffer[5] = (uint8_t) (u.e >> 40 & 0xff); 107 | buffer[6] = (uint8_t) (u.e >> 48 & 0xff); 108 | buffer[7] = (uint8_t) (u.e >> 56 & 0xff); 109 | } 110 | 111 | static inline void 112 | float_encode(float v , uint8_t * buffer) { 113 | union { 114 | float v; 115 | uint32_t e; 116 | } u; 117 | u.v = v; 118 | buffer[0] = (uint8_t) (u.e & 0xff); 119 | buffer[1] = (uint8_t) (u.e >> 8 & 0xff); 120 | buffer[2] = (uint8_t) (u.e >> 16 & 0xff); 121 | buffer[3] = (uint8_t) (u.e >> 24 & 0xff); 122 | } 123 | 124 | #define CHECK_LEND(a,err) if ((a->wire_id & 7) != WT_LEND) return err; 125 | 126 | #if 0 127 | /* maybe we don't need check these wire type */ 128 | #define CHECK_VARINT(a,err) if ((a->wire_id & 7) != WT_VARINT) return err; 129 | #define CHECK_BIT32(a,err) if ((a->wire_id & 7) != WT_BIT32) return err; 130 | #define CHECK_BIT64(a,err) if ((a->wire_id & 7) != WT_BIT64) return err; 131 | 132 | #else 133 | 134 | #define CHECK_VARINT(a,err) 135 | #define CHECK_BIT32(a,err) 136 | #define CHECK_BIT64(a,err) 137 | 138 | #endif 139 | 140 | #endif 141 | -------------------------------------------------------------------------------- /3rd/pbc/src/decode.c: -------------------------------------------------------------------------------- 1 | #include "pbc.h" 2 | #include "alloc.h" 3 | #include "context.h" 4 | #include "proto.h" 5 | #include "varint.h" 6 | 7 | #include 8 | 9 | static const char * TYPENAME[] = { 10 | "invalid", // 0 11 | "integer", // 1 12 | "real", // 2 13 | "boolean", // 3 14 | "enum", // 4 15 | "string", // 5 16 | "message", // 6 17 | "fixed64", // 7 18 | "fixed32", // 8 19 | "bytes", // 9 20 | "int64", // 10 21 | "uint", // 11 22 | }; 23 | 24 | static int 25 | call_unknown(pbc_decoder f, void * ud, int id, struct atom *a, uint8_t * start) { 26 | union pbc_value v; 27 | switch (a->wire_id & 7) { 28 | case WT_VARINT: 29 | v.i.low = a->v.i.low; 30 | v.i.hi = a->v.i.hi; 31 | f(ud, PBC_INT, TYPENAME[PBC_INT], &v, id , NULL); 32 | break; 33 | case WT_BIT64: 34 | v.i.low = a->v.i.low; 35 | v.i.hi = a->v.i.hi; 36 | f(ud, PBC_FIXED64, TYPENAME[PBC_FIXED64], &v, id , NULL); 37 | break; 38 | case WT_LEND: 39 | v.s.buffer = (char*)start + a->v.s.start; 40 | v.s.len = a->v.s.end - a->v.s.start; 41 | f(ud, PBC_BYTES, TYPENAME[PBC_BYTES], &v, id , NULL); 42 | break; 43 | case WT_BIT32: 44 | v.i.low = a->v.i.low; 45 | v.i.hi = 0; 46 | f(ud, PBC_FIXED32, TYPENAME[PBC_FIXED32], &v, id , NULL); 47 | break; 48 | default: 49 | return 1; 50 | } 51 | return 0; 52 | } 53 | 54 | static int 55 | call_type(pbc_decoder pd, void * ud, struct _field *f, struct atom *a, uint8_t * start) { 56 | union pbc_value v; 57 | const char * type_name = NULL; 58 | int type = _pbcP_type(f, &type_name); 59 | assert(type != 0); 60 | if (type_name == NULL) { 61 | type_name = TYPENAME[type & ~PBC_REPEATED]; 62 | } 63 | switch (f->type) { 64 | case PTYPE_DOUBLE: 65 | CHECK_BIT64(a, -1); 66 | v.f = read_double(a); 67 | break; 68 | case PTYPE_FLOAT: 69 | CHECK_BIT32(a, -1); 70 | v.f = (double) read_float(a); 71 | break; 72 | case PTYPE_ENUM: 73 | CHECK_VARINT(a, -1); 74 | v.e.id = a->v.i.low; 75 | v.e.name = (const char *)_pbcM_ip_query(f->type_name.e->id , v.e.id); 76 | break; 77 | case PTYPE_INT64: 78 | case PTYPE_UINT64: 79 | CHECK_VARINT(a, -1); 80 | v.i.low = a->v.i.low; 81 | v.i.hi = a->v.i.hi; 82 | break; 83 | case PTYPE_FIXED64: 84 | case PTYPE_SFIXED64: 85 | CHECK_BIT64(a, -1); 86 | v.i.low = a->v.i.low; 87 | v.i.hi = a->v.i.hi; 88 | break; 89 | case PTYPE_INT32: 90 | case PTYPE_UINT32: 91 | case PTYPE_BOOL: 92 | CHECK_VARINT(a, -1); 93 | v.i.low = a->v.i.low; 94 | v.i.hi = 0; 95 | break; 96 | case PTYPE_FIXED32: 97 | case PTYPE_SFIXED32: 98 | CHECK_BIT32(a, -1); 99 | v.i.low = a->v.i.low; 100 | v.i.hi = 0; 101 | break; 102 | case PTYPE_SINT32: 103 | CHECK_VARINT(a, -1); 104 | v.i.low = a->v.i.low; 105 | v.i.hi = a->v.i.hi; 106 | _pbcV_dezigzag32((struct longlong *)&(v.i)); 107 | break; 108 | case PTYPE_SINT64: 109 | CHECK_VARINT(a, -1); 110 | v.i.low = a->v.i.low; 111 | v.i.hi = a->v.i.hi; 112 | _pbcV_dezigzag64((struct longlong *)&(v.i)); 113 | break; 114 | case PTYPE_STRING: 115 | case PTYPE_BYTES: 116 | case PTYPE_MESSAGE: 117 | CHECK_LEND(a, -1); 118 | v.s.buffer = start + a->v.s.start; 119 | v.s.len = a->v.s.end - a->v.s.start; 120 | break; 121 | default: 122 | assert(0); 123 | break; 124 | } 125 | pd(ud, type, type_name, &v, f->id, f->name); 126 | return 0; 127 | } 128 | 129 | static int 130 | call_array(pbc_decoder pd, void * ud, struct _field *f, uint8_t * buffer , int size) { 131 | union pbc_value v; 132 | const char * type_name = NULL; 133 | int type = _pbcP_type(f, &type_name); 134 | assert(type != 0); 135 | if (type_name == NULL) { 136 | type_name = TYPENAME[type & ~PBC_REPEATED]; 137 | } 138 | v.i.hi = 0; 139 | int i; 140 | switch(f->type) { 141 | case PTYPE_DOUBLE: 142 | if (size % 8 != 0) { 143 | return -1; 144 | } 145 | for (i=0;iid, f->name); 160 | } 161 | return size/8; 162 | case PTYPE_FLOAT: 163 | if (size % 4 != 0) 164 | return -1; 165 | for (i=0;iid, f->name); 176 | } 177 | return size/4; 178 | case PTYPE_FIXED32: 179 | case PTYPE_SFIXED32: 180 | if (size % 4 != 0) 181 | return -1; 182 | for (i=0;iid, f->name); 188 | } 189 | return size/4; 190 | case PTYPE_FIXED64: 191 | case PTYPE_SFIXED64: 192 | if (size % 8 != 0) 193 | return -1; 194 | for (i=0;iid, f->name); 204 | } 205 | return size/8; 206 | case PTYPE_INT64: 207 | case PTYPE_UINT64: 208 | case PTYPE_INT32: 209 | case PTYPE_UINT32: 210 | case PTYPE_BOOL: { 211 | int n = 0; 212 | while (size > 0) { 213 | int len; 214 | if (size >= 10) { 215 | len = _pbcV_decode(buffer, (struct longlong *)&(v.i)); 216 | } else { 217 | uint8_t temp[10]; 218 | memcpy(temp, buffer, size); 219 | len = _pbcV_decode(buffer, (struct longlong *)&(v.i)); 220 | if (len > size) 221 | return -1; 222 | } 223 | pd(ud, type , type_name, &v, f->id, f->name); 224 | buffer += len; 225 | size -= len; 226 | ++n; 227 | } 228 | return n; 229 | } 230 | case PTYPE_ENUM: { 231 | int n = 0; 232 | while (size > 0) { 233 | int len; 234 | if (size >= 10) { 235 | len = _pbcV_decode(buffer, (struct longlong *)&(v.i)); 236 | } else { 237 | uint8_t temp[10]; 238 | memcpy(temp, buffer, size); 239 | len = _pbcV_decode(buffer, (struct longlong *)&(v.i)); 240 | if (len > size) 241 | return -1; 242 | } 243 | v.e.id = v.i.low; 244 | v.e.name = (const char *)_pbcM_ip_query(f->type_name.e->id , v.i.low); 245 | pd(ud, type , type_name, &v, f->id, f->name); 246 | buffer += len; 247 | size -= len; 248 | ++n; 249 | } 250 | return n; 251 | } 252 | case PTYPE_SINT32: { 253 | int n = 0; 254 | while (size > 0) { 255 | int len; 256 | if (size >= 10) { 257 | len = _pbcV_decode(buffer, (struct longlong *)&(v.i)); 258 | _pbcV_dezigzag32((struct longlong *)&(v.i)); 259 | } else { 260 | uint8_t temp[10]; 261 | memcpy(temp, buffer, size); 262 | len = _pbcV_decode(buffer, (struct longlong *)&(v.i)); 263 | if (len > size) 264 | return -1; 265 | _pbcV_dezigzag32((struct longlong *)&(v.i)); 266 | } 267 | pd(ud, type , type_name, &v, f->id, f->name); 268 | buffer += len; 269 | size -= len; 270 | ++n; 271 | } 272 | return n; 273 | } 274 | case PTYPE_SINT64: { 275 | int n = 0; 276 | while (size > 0) { 277 | int len; 278 | if (size >= 10) { 279 | len = _pbcV_decode(buffer, (struct longlong *)&(v.i)); 280 | _pbcV_dezigzag64((struct longlong *)&(v.i)); 281 | } else { 282 | uint8_t temp[10]; 283 | memcpy(temp, buffer, size); 284 | len = _pbcV_decode(buffer, (struct longlong *)&(v.i)); 285 | if (len > size) 286 | return -1; 287 | _pbcV_dezigzag64((struct longlong *)&(v.i)); 288 | } 289 | pd(ud, type , type_name, &v, f->id, f->name); 290 | buffer += len; 291 | size -= len; 292 | ++n; 293 | } 294 | return n; 295 | } 296 | default: 297 | return -1; 298 | } 299 | } 300 | 301 | int 302 | pbc_decode(struct pbc_env * env, const char * type_name , struct pbc_slice * slice, pbc_decoder pd, void *ud) { 303 | struct _message * msg = _pbcP_get_message(env, type_name); 304 | if (msg == NULL) { 305 | env->lasterror = "Proto not found"; 306 | return -1; 307 | } 308 | if (slice->len == 0) { 309 | return 0; 310 | } 311 | pbc_ctx _ctx; 312 | int count = _pbcC_open(_ctx,slice->buffer,slice->len); 313 | if (count <= 0) { 314 | env->lasterror = "decode context error"; 315 | _pbcC_close(_ctx); 316 | return count - 1; 317 | } 318 | struct context * ctx = (struct context *)_ctx; 319 | uint8_t * start = (uint8_t *)slice->buffer; 320 | 321 | int i; 322 | for (i=0;inumber;i++) { 323 | int id = ctx->a[i].wire_id >> 3; 324 | struct _field * f = (struct _field *)_pbcM_ip_query(msg->id , id); 325 | if (f==NULL) { 326 | int err = call_unknown(pd,ud,id,&ctx->a[i],start); 327 | if (err) { 328 | _pbcC_close(_ctx); 329 | return -i-1; 330 | } 331 | } else if (f->label == LABEL_PACKED) { 332 | struct atom * a = &ctx->a[i]; 333 | int n = call_array(pd, ud, f , start + a->v.s.start , a->v.s.end - a->v.s.start); 334 | if (n < 0) { 335 | _pbcC_close(_ctx); 336 | return -i-1; 337 | } 338 | } else { 339 | if (call_type(pd,ud,f,&ctx->a[i],start) != 0) { 340 | _pbcC_close(_ctx); 341 | return -i-1; 342 | } 343 | } 344 | } 345 | 346 | _pbcC_close(_ctx); 347 | return ctx->number; 348 | } 349 | 350 | -------------------------------------------------------------------------------- /3rd/pbc/src/map.h: -------------------------------------------------------------------------------- 1 | #ifndef PROTOBUF_C_MAP_H 2 | #define PROTOBUF_C_MAP_H 3 | 4 | #include "alloc.h" 5 | 6 | struct map_ip; 7 | struct map_si; 8 | struct map_sp; 9 | 10 | struct map_kv { 11 | int id; 12 | void *pointer; 13 | }; 14 | 15 | struct map_si * _pbcM_si_new(struct map_kv * table, int size); 16 | int _pbcM_si_query(struct map_si *map, const char *key, int *result); 17 | void _pbcM_si_delete(struct map_si *map); 18 | 19 | struct map_ip * _pbcM_ip_new(struct map_kv * table, int size); 20 | struct map_ip * _pbcM_ip_combine(struct map_ip * a, struct map_ip * b); 21 | void * _pbcM_ip_query(struct map_ip * map, int id); 22 | void _pbcM_ip_delete(struct map_ip *map); 23 | 24 | struct map_sp * _pbcM_sp_new(int max, struct heap *h); 25 | void _pbcM_sp_insert(struct map_sp *map, const char *key, void * value); 26 | void * _pbcM_sp_query(struct map_sp *map, const char *key); 27 | void ** _pbcM_sp_query_insert(struct map_sp *map, const char *key); 28 | void _pbcM_sp_delete(struct map_sp *map); 29 | void _pbcM_sp_foreach(struct map_sp *map, void (*func)(void *p)); 30 | void _pbcM_sp_foreach_ud(struct map_sp *map, void (*func)(void *p, void *ud), void *ud); 31 | void * _pbcM_sp_next(struct map_sp *map, const char ** key); 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /3rd/pbc/src/pattern.h: -------------------------------------------------------------------------------- 1 | #ifndef PROTOBUF_C_PATTERN_H 2 | #define PROTOBUF_C_PATTERN_H 3 | 4 | #include "pbc.h" 5 | #include "context.h" 6 | #include "array.h" 7 | 8 | struct _pattern_field { 9 | int id; 10 | int offset; 11 | int ptype; 12 | int ctype; 13 | int label; 14 | pbc_var defv; 15 | }; 16 | 17 | struct pbc_pattern { 18 | struct pbc_env * env; 19 | int count; 20 | struct _pattern_field f[1]; 21 | }; 22 | 23 | struct pbc_pattern * _pbcP_new(struct pbc_env * env, int n); 24 | int _pbcP_unpack_packed(uint8_t *buffer, int size, int ptype, pbc_array array); 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /3rd/pbc/src/proto.c: -------------------------------------------------------------------------------- 1 | #include "pbc.h" 2 | #include "proto.h" 3 | #include "pattern.h" 4 | #include "map.h" 5 | #include "alloc.h" 6 | #include "stringpool.h" 7 | #include "bootstrap.h" 8 | 9 | #include 10 | #include 11 | 12 | const char * 13 | pbc_error(struct pbc_env * p) { 14 | const char *err = p->lasterror; 15 | p->lasterror = ""; 16 | return err; 17 | } 18 | 19 | struct _message * 20 | _pbcP_get_message(struct pbc_env * p , const char *name) { 21 | return (struct _message *)_pbcM_sp_query(p->msgs, name); 22 | } 23 | 24 | struct pbc_env * 25 | pbc_new(void) { 26 | struct pbc_env * p = (struct pbc_env *)malloc(sizeof(*p)); 27 | p->files = _pbcM_sp_new(0 , NULL); 28 | p->enums = _pbcM_sp_new(0 , NULL); 29 | p->msgs = _pbcM_sp_new(0 , NULL); 30 | p->lasterror = ""; 31 | 32 | _pbcB_init(p); 33 | 34 | return p; 35 | } 36 | 37 | static void 38 | free_enum(void *p) { 39 | struct _enum * e = (struct _enum *)p; 40 | _pbcM_ip_delete(e->id); 41 | _pbcM_si_delete(e->name); 42 | 43 | free(p); 44 | } 45 | 46 | static void 47 | free_stringpool(void *p) { 48 | _pbcS_delete((struct _stringpool *)p); 49 | } 50 | 51 | static void 52 | free_msg(void *p) { 53 | struct _message * m = (struct _message *)p; 54 | if (m->id) 55 | _pbcM_ip_delete(m->id); 56 | free(m->def); 57 | _pbcM_sp_foreach(m->name, free); 58 | _pbcM_sp_delete(m->name); 59 | free(p); 60 | } 61 | 62 | void 63 | pbc_delete(struct pbc_env *p) { 64 | _pbcM_sp_foreach(p->enums, free_enum); 65 | _pbcM_sp_delete(p->enums); 66 | 67 | _pbcM_sp_foreach(p->msgs, free_msg); 68 | _pbcM_sp_delete(p->msgs); 69 | 70 | _pbcM_sp_foreach(p->files, free_stringpool); 71 | _pbcM_sp_delete(p->files); 72 | 73 | free(p); 74 | } 75 | 76 | struct _enum * 77 | _pbcP_push_enum(struct pbc_env * p, const char *name, struct map_kv *table, int sz) { 78 | void * check = _pbcM_sp_query(p->enums, name); 79 | if (check) 80 | return NULL; 81 | struct _enum * v = (struct _enum *)malloc(sizeof(*v)); 82 | v->key = name; 83 | v->id = _pbcM_ip_new(table,sz); 84 | v->name = _pbcM_si_new(table,sz); 85 | v->default_v->e.id = table[0].id; 86 | v->default_v->e.name = (const char *)table[0].pointer; 87 | 88 | _pbcM_sp_insert(p->enums, name , v); 89 | return v; 90 | } 91 | 92 | void 93 | _pbcP_push_message(struct pbc_env * p, const char *name, struct _field *f , pbc_array queue) { 94 | struct _message * m = (struct _message *)_pbcM_sp_query(p->msgs, name); 95 | if (m==NULL) { 96 | m = (struct _message *)malloc(sizeof(*m)); 97 | m->def = NULL; 98 | m->key = name; 99 | m->id = NULL; 100 | m->name = _pbcM_sp_new(0 , NULL); 101 | m->env = p; 102 | _pbcM_sp_insert(p->msgs, name, m); 103 | } 104 | struct _field * field = (struct _field *)malloc(sizeof(*field)); 105 | memcpy(field,f,sizeof(*f)); 106 | _pbcM_sp_insert(m->name, field->name, field); 107 | pbc_var atom; 108 | atom->m.buffer = field; 109 | if (f->type == PTYPE_MESSAGE || f->type == PTYPE_ENUM) { 110 | _pbcA_push(queue, atom); 111 | } 112 | } 113 | 114 | struct _iter { 115 | int count; 116 | struct map_kv * table; 117 | }; 118 | 119 | static void 120 | _count(void *p, void *ud) { 121 | struct _iter *iter = (struct _iter *)ud; 122 | iter->count ++; 123 | } 124 | 125 | static void 126 | _set_table(void *p, void *ud) { 127 | struct _field * field = (struct _field *)p; 128 | struct _iter *iter = (struct _iter *)ud; 129 | iter->table[iter->count].id = field->id; 130 | iter->table[iter->count].pointer = field; 131 | ++iter->count; 132 | } 133 | 134 | struct _message * 135 | _pbcP_init_message(struct pbc_env * p, const char *name) { 136 | struct _message * m = (struct _message *)_pbcM_sp_query(p->msgs, name); 137 | if (m == NULL) { 138 | m = (struct _message *)malloc(sizeof(*m)); 139 | m->def = NULL; 140 | m->key = name; 141 | m->id = NULL; 142 | m->name = _pbcM_sp_new(0 , NULL); 143 | m->env = p; 144 | _pbcM_sp_insert(p->msgs, name, m); 145 | 146 | return m; 147 | } 148 | if (m->id) { 149 | // extend message, delete old id map. 150 | _pbcM_ip_delete(m->id); 151 | } 152 | struct _iter iter = { 0, NULL }; 153 | _pbcM_sp_foreach_ud(m->name, _count, &iter); 154 | iter.table = (struct map_kv *)malloc(iter.count * sizeof(struct map_kv)); 155 | iter.count = 0; 156 | _pbcM_sp_foreach_ud(m->name, _set_table, &iter); 157 | 158 | m->id = _pbcM_ip_new(iter.table , iter.count); 159 | 160 | free(iter.table); 161 | 162 | return m; 163 | } 164 | 165 | int 166 | _pbcP_message_default(struct _message * m, const char * name, pbc_var defv) { 167 | struct _field * f= (struct _field *)_pbcM_sp_query(m->name, name); 168 | if (f==NULL) { 169 | // invalid key 170 | defv->p[0] = NULL; 171 | defv->p[1] = NULL; 172 | return -1; 173 | } 174 | *defv = *(f->default_v); 175 | return f->type; 176 | } 177 | 178 | int 179 | _pbcP_type(struct _field * field, const char ** type) { 180 | if (field == NULL) { 181 | return 0; 182 | } 183 | int ret = 0; 184 | switch (field->type) { 185 | case PTYPE_DOUBLE: 186 | case PTYPE_FLOAT: 187 | ret = PBC_REAL; 188 | break; 189 | case PTYPE_INT64: 190 | case PTYPE_SINT64: 191 | ret = PBC_INT64; 192 | break; 193 | case PTYPE_INT32: 194 | case PTYPE_SINT32: 195 | ret = PBC_INT; 196 | break; 197 | case PTYPE_UINT32: 198 | case PTYPE_UINT64: 199 | ret = PBC_UINT; 200 | break; 201 | case PTYPE_FIXED32: 202 | case PTYPE_SFIXED32: 203 | ret = PBC_FIXED32; 204 | break; 205 | case PTYPE_SFIXED64: 206 | case PTYPE_FIXED64: 207 | ret = PBC_FIXED64; 208 | break; 209 | case PTYPE_BOOL: 210 | ret = PBC_BOOL; 211 | break; 212 | case PTYPE_STRING: 213 | ret = PBC_STRING; 214 | break; 215 | case PTYPE_BYTES: 216 | ret = PBC_BYTES; 217 | break; 218 | case PTYPE_ENUM: 219 | ret = PBC_ENUM; 220 | if (type) { 221 | *type = field->type_name.e->key; 222 | } 223 | break; 224 | case PTYPE_MESSAGE: 225 | ret = PBC_MESSAGE; 226 | if (type) { 227 | *type = field->type_name.m->key; 228 | } 229 | break; 230 | default: 231 | return 0; 232 | } 233 | if (field->label == LABEL_REPEATED || 234 | field->label == LABEL_PACKED) { 235 | ret |= PBC_REPEATED; 236 | } 237 | 238 | return ret; 239 | } 240 | 241 | int 242 | pbc_type(struct pbc_env * p, const char * type_name , const char * key , const char ** type) { 243 | struct _message *m = _pbcP_get_message(p, type_name); 244 | if (m==NULL) { 245 | return 0; 246 | } 247 | if (key == NULL) { 248 | return PBC_NOEXIST; 249 | } 250 | struct _field * field = (struct _field *)_pbcM_sp_query(m->name, key); 251 | return _pbcP_type(field, type); 252 | } 253 | -------------------------------------------------------------------------------- /3rd/pbc/src/proto.h: -------------------------------------------------------------------------------- 1 | #ifndef PROTOBUFC_PROTO_H 2 | #define PROTOBUFC_PROTO_H 3 | 4 | #include "pbc.h" 5 | #include "map.h" 6 | #include "array.h" 7 | #ifndef _MSC_VER 8 | #include 9 | #endif 10 | #include 11 | 12 | struct map_ip; 13 | struct map_si; 14 | struct map_sp; 15 | struct _message; 16 | struct _enum; 17 | 18 | #define LABEL_OPTIONAL 0 19 | #define LABEL_REQUIRED 1 20 | #define LABEL_REPEATED 2 21 | #define LABEL_PACKED 3 22 | 23 | struct _field { 24 | int id; 25 | const char *name; 26 | int type; 27 | int label; 28 | pbc_var default_v; 29 | union { 30 | const char * n; 31 | struct _message * m; 32 | struct _enum * e; 33 | } type_name; 34 | }; 35 | 36 | struct _message { 37 | const char * key; 38 | struct map_ip * id; // id -> _field 39 | struct map_sp * name; // string -> _field 40 | struct pbc_rmessage * def; // default message 41 | struct pbc_env * env; 42 | }; 43 | 44 | struct _enum { 45 | const char * key; 46 | struct map_ip * id; 47 | struct map_si * name; 48 | pbc_var default_v; 49 | }; 50 | 51 | struct pbc_env { 52 | struct map_sp * files; // string -> void * 53 | struct map_sp * enums; // string -> _enum 54 | struct map_sp * msgs; // string -> _message 55 | const char * lasterror; 56 | }; 57 | 58 | struct _message * _pbcP_init_message(struct pbc_env * p, const char *name); 59 | void _pbcP_push_message(struct pbc_env * p, const char *name, struct _field *f , pbc_array queue); 60 | struct _enum * _pbcP_push_enum(struct pbc_env * p, const char *name, struct map_kv *table, int sz ); 61 | int _pbcP_message_default(struct _message * m, const char * name, pbc_var defv); 62 | struct _message * _pbcP_get_message(struct pbc_env * p, const char *name); 63 | int _pbcP_type(struct _field * field, const char **type); 64 | 65 | #endif 66 | -------------------------------------------------------------------------------- /3rd/pbc/src/register.c: -------------------------------------------------------------------------------- 1 | #include "pbc.h" 2 | #include "proto.h" 3 | #include "alloc.h" 4 | #include "map.h" 5 | #include "bootstrap.h" 6 | #include "context.h" 7 | #include "stringpool.h" 8 | 9 | #include 10 | #include 11 | 12 | #ifdef _MSC_VER 13 | #define strtoll _strtoi64 14 | #endif 15 | 16 | static const char * 17 | _concat_name(struct _stringpool *p , const char *prefix , int prefix_sz , const char *name , int name_sz, int *sz) { 18 | if (prefix_sz == 0) { 19 | if (sz) { 20 | *sz = name_sz; 21 | } 22 | return _pbcS_build(p , name, name_sz); 23 | } 24 | char * temp = (char *)alloca(name_sz + prefix_sz + 2); 25 | memcpy(temp,prefix,prefix_sz); 26 | temp[prefix_sz] = '.'; 27 | memcpy(temp+prefix_sz+1,name,name_sz); 28 | temp[name_sz + prefix_sz + 1] = '\0'; 29 | if (sz) { 30 | *sz = name_sz + prefix_sz + 1; 31 | } 32 | const char * ret = _pbcS_build(p , temp, name_sz + prefix_sz + 1); 33 | return ret; 34 | } 35 | 36 | static void 37 | _register_enum(struct pbc_env *p, struct _stringpool *pool, struct pbc_rmessage * enum_type, const char *prefix, int prefix_sz) { 38 | int field_count = pbc_rmessage_size(enum_type, "value"); 39 | struct map_kv *table = (struct map_kv *)malloc(field_count * sizeof(struct map_kv)); 40 | int i; 41 | for (i=0;itype == PTYPE_STRING || f->type == PTYPE_BYTES) { 60 | f->default_v->s.str = ""; 61 | f->default_v->s.len = 0; 62 | } else { 63 | f->default_v->integer.low = 0; 64 | f->default_v->integer.hi = 0; 65 | } 66 | return; 67 | } 68 | 69 | switch (f->type) { 70 | case PTYPE_DOUBLE: 71 | case PTYPE_FLOAT: 72 | f->default_v->real = strtod(value,NULL); 73 | break; 74 | case PTYPE_STRING: 75 | f->default_v->s.str = _pbcS_build(pool, value , sz); 76 | f->default_v->s.len = sz; 77 | break; 78 | case PTYPE_ENUM: 79 | // enum default value will be converted to f->default_v->e in bootstrap.c : set_field_one() 80 | f->default_v->s.str = value; 81 | f->default_v->s.len = sz; 82 | break; 83 | case PTYPE_BOOL: 84 | if (strcmp(value,"true") == 0) { 85 | f->default_v->integer.low = 1; 86 | } else { 87 | f->default_v->integer.low = 0; 88 | } 89 | f->default_v->integer.hi = 0; 90 | break; 91 | case PTYPE_UINT64: 92 | case PTYPE_INT64: 93 | case PTYPE_SFIXED64: 94 | case PTYPE_SINT64: { 95 | long long v = strtoll(value, NULL, 10); 96 | f->default_v->integer.low = (long) v; 97 | f->default_v->integer.hi = (long)(v >> 32); 98 | break; 99 | } 100 | case PTYPE_INT32: 101 | case PTYPE_FIXED32: 102 | case PTYPE_SFIXED32: 103 | case PTYPE_SINT32: { 104 | int low = strtol(value, NULL, 10); 105 | f->default_v->integer.low = low; 106 | if (low < 0) { 107 | f->default_v->integer.hi = -1; 108 | } else { 109 | f->default_v->integer.hi = 0; 110 | } 111 | break; 112 | } 113 | case PTYPE_UINT32: 114 | f->default_v->integer.low = strtoul(value, NULL, 10); 115 | f->default_v->integer.hi = 0; 116 | break; 117 | case PTYPE_BYTES: 118 | case PTYPE_MESSAGE: 119 | // bytes and message types have no default value 120 | f->default_v->m.buffer = 0; 121 | f->default_v->m.len = 0; 122 | break; 123 | default: 124 | f->default_v->integer.low = 0; 125 | f->default_v->integer.hi = 0; 126 | break; 127 | } 128 | } 129 | 130 | static void 131 | _register_field(struct pbc_rmessage * field, struct _field * f, struct _stringpool *pool) { 132 | f->id = pbc_rmessage_integer(field, "number", 0 , 0); 133 | f->type = pbc_rmessage_integer(field, "type", 0 , 0); // enum 134 | f->label = pbc_rmessage_integer(field, "label", 0, 0) - 1; // LABEL_OPTIONAL = 0 135 | if (pbc_rmessage_size(field , "options") > 0) { 136 | struct pbc_rmessage * options = pbc_rmessage_message(field, "options" , 0); 137 | int packed = pbc_rmessage_integer(options , "packed" , 0 , NULL); 138 | if (packed) { 139 | f->label = LABEL_PACKED; 140 | } 141 | } 142 | f->type_name.n = pbc_rmessage_string(field, "type_name", 0 , NULL) +1; // abandon prefix '.' 143 | int vsz; 144 | const char * default_value = pbc_rmessage_string(field, "default_value", 0 , &vsz); 145 | _set_default(pool , f , f->type, default_value , vsz); 146 | } 147 | 148 | static void 149 | _register_extension(struct pbc_env *p, struct _stringpool *pool , const char * prefix, int prefix_sz, struct pbc_rmessage * msg, pbc_array queue) { 150 | int extension_count = pbc_rmessage_size(msg , "extension"); 151 | if (extension_count <= 0) 152 | return; 153 | int i; 154 | 155 | const char * last = NULL; 156 | 157 | for (i=0;ifiles, filename)) { 260 | return CHECK_FILE_EXIST; 261 | } 262 | int sz = pbc_rmessage_size(file, "dependency"); 263 | int i; 264 | for (i=0;ifiles, dname) == NULL) { 268 | return CHECK_FILE_DEPENDENCY; 269 | } 270 | } 271 | 272 | *fname = filename; 273 | 274 | return CHECK_FILE_OK; 275 | } 276 | 277 | static int 278 | _register_no_dependency(struct pbc_env * p,struct pbc_rmessage ** files , int n ) { 279 | int r = 0; 280 | int i; 281 | for (i=0;ifiles , filename, pool); 296 | _register(p,files[i],pool); 297 | files[i] = NULL; 298 | } 299 | break; 300 | } 301 | } 302 | return r; 303 | } 304 | 305 | int 306 | pbc_register(struct pbc_env * p, struct pbc_slice *slice) { 307 | struct pbc_rmessage * message = pbc_rmessage_new(p, "google.protobuf.FileDescriptorSet", slice); 308 | if (message == NULL) { 309 | p->lasterror = "register open google.protobuf.FileDescriptorSet fail"; 310 | return 1; 311 | } 312 | int n = pbc_rmessage_size(message, "file"); 313 | struct pbc_rmessage ** files = (struct pbc_rmessage **)alloca(n * sizeof(struct pbc_rmessage *)); 314 | int i; 315 | if (n == 0) { 316 | p->lasterror = "register empty"; 317 | goto _error; 318 | } 319 | for (i=0;ilasterror = "register open fail"; 323 | goto _error; 324 | } 325 | } 326 | 327 | int r = n; 328 | do { 329 | int rr = _register_no_dependency(p,files , n); 330 | if (rr == r) { 331 | p->lasterror = "register dependency error"; 332 | goto _error; 333 | } 334 | r = rr; 335 | } while (r>0); 336 | 337 | pbc_rmessage_delete(message); 338 | return 0; 339 | _error: 340 | pbc_rmessage_delete(message); 341 | return 1; 342 | } 343 | -------------------------------------------------------------------------------- /3rd/pbc/src/stringpool.c: -------------------------------------------------------------------------------- 1 | #include "alloc.h" 2 | 3 | #include 4 | #include 5 | 6 | #define PAGE_SIZE 256 7 | 8 | struct _stringpool { 9 | char * buffer; 10 | size_t len; 11 | struct _stringpool *next; 12 | }; 13 | 14 | struct _stringpool * 15 | _pbcS_new(void) { 16 | struct _stringpool * ret = (struct _stringpool *)malloc(sizeof(struct _stringpool) + PAGE_SIZE); 17 | ret->buffer = (char *)(ret + 1); 18 | ret->len = 0; 19 | ret->next = NULL; 20 | return ret; 21 | } 22 | 23 | void 24 | _pbcS_delete(struct _stringpool *pool) { 25 | while(pool) { 26 | struct _stringpool *next = pool->next; 27 | free(pool); 28 | pool = next; 29 | } 30 | } 31 | 32 | const char * 33 | _pbcS_build(struct _stringpool *pool, const char * str , int sz) { 34 | size_t s = sz + 1; 35 | if (s < PAGE_SIZE - pool->len) { 36 | char * ret = pool->buffer + pool->len; 37 | memcpy(pool->buffer + pool->len, str, s); 38 | pool->len += s; 39 | return ret; 40 | } 41 | if (s > PAGE_SIZE) { 42 | struct _stringpool * next = (struct _stringpool *)malloc(sizeof(struct _stringpool) + s); 43 | next->buffer = (char *)(next + 1); 44 | memcpy(next->buffer, str, s); 45 | next->len = s; 46 | next->next = pool->next; 47 | pool->next = next; 48 | return next->buffer; 49 | } 50 | struct _stringpool *next = (struct _stringpool *)malloc(sizeof(struct _stringpool) + PAGE_SIZE); 51 | next->buffer = pool->buffer; 52 | next->next = pool->next; 53 | next->len = pool->len; 54 | 55 | pool->next = next; 56 | pool->buffer = (char *)(next + 1); 57 | memcpy(pool->buffer, str, s); 58 | pool->len = s; 59 | return pool->buffer; 60 | } 61 | -------------------------------------------------------------------------------- /3rd/pbc/src/stringpool.h: -------------------------------------------------------------------------------- 1 | #ifndef PROTOBUF_C_STRINGPOOL_H 2 | #define PROTOBUF_C_STRINGPOOL_H 3 | 4 | struct _stringpool; 5 | 6 | struct _stringpool * _pbcS_new(void); 7 | void _pbcS_delete(struct _stringpool *pool); 8 | const char * _pbcS_build(struct _stringpool *pool, const char * str , int sz); 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /3rd/pbc/src/varint.c: -------------------------------------------------------------------------------- 1 | #include "varint.h" 2 | 3 | #include "pbc.h" 4 | 5 | #include 6 | 7 | inline int 8 | _pbcV_encode32(uint32_t number, uint8_t buffer[10]) 9 | { 10 | if (number < 0x80) { 11 | buffer[0] = (uint8_t) number ; 12 | return 1; 13 | } 14 | buffer[0] = (uint8_t) (number | 0x80 ); 15 | if (number < 0x4000) { 16 | buffer[1] = (uint8_t) (number >> 7 ); 17 | return 2; 18 | } 19 | buffer[1] = (uint8_t) ((number >> 7) | 0x80 ); 20 | if (number < 0x200000) { 21 | buffer[2] = (uint8_t) (number >> 14); 22 | return 3; 23 | } 24 | buffer[2] = (uint8_t) ((number >> 14) | 0x80 ); 25 | if (number < 0x10000000) { 26 | buffer[3] = (uint8_t) (number >> 21); 27 | return 4; 28 | } 29 | buffer[3] = (uint8_t) ((number >> 21) | 0x80 ); 30 | buffer[4] = (uint8_t) (number >> 28); 31 | return 5; 32 | } 33 | 34 | int 35 | _pbcV_encode(uint64_t number, uint8_t buffer[10]) 36 | { 37 | if ((number & 0xffffffff) == number) { 38 | return _pbcV_encode32((uint32_t)number , buffer); 39 | } 40 | int i = 0; 41 | do { 42 | buffer[i] = (uint8_t)(number | 0x80); 43 | number >>= 7; 44 | ++i; 45 | } while (number >= 0x80); 46 | buffer[i] = (uint8_t)number; 47 | return i+1; 48 | } 49 | 50 | int 51 | _pbcV_decode(uint8_t buffer[10], struct longlong *result) { 52 | if (!(buffer[0] & 0x80)) { 53 | result->low = buffer[0]; 54 | result->hi = 0; 55 | return 1; 56 | } 57 | uint32_t r = buffer[0] & 0x7f; 58 | int i; 59 | for (i=1;i<4;i++) { 60 | r |= ((buffer[i]&0x7f) << (7*i)); 61 | if (!(buffer[i] & 0x80)) { 62 | result->low = r; 63 | result->hi = 0; 64 | return i+1; 65 | } 66 | } 67 | uint64_t lr = 0; 68 | for (i=4;i<10;i++) { 69 | lr |= ((uint64_t)(buffer[i] & 0x7f) << (7*(i-4))); 70 | if (!(buffer[i] & 0x80)) { 71 | result->hi = (uint32_t)(lr >> 4); 72 | result->low = r | (((uint32_t)lr & 0xf) << 28); 73 | return i+1; 74 | } 75 | } 76 | 77 | result->low = 0; 78 | result->hi = 0; 79 | return 10; 80 | } 81 | 82 | int 83 | _pbcV_zigzag32(int32_t n, uint8_t buffer[10]) 84 | { 85 | n = (n << 1) ^ (n >> 31); 86 | return _pbcV_encode32(n,buffer); 87 | } 88 | 89 | int 90 | _pbcV_zigzag(int64_t n, uint8_t buffer[10]) 91 | { 92 | n = (n << 1) ^ (n >> 63); 93 | return _pbcV_encode(n,buffer); 94 | } 95 | 96 | void 97 | _pbcV_dezigzag64(struct longlong *r) 98 | { 99 | uint32_t low = r->low; 100 | r->low = ((low >> 1) | ((r->hi & 1) << 31)) ^ - (low & 1); 101 | r->hi = (r->hi >> 1) ^ - (low & 1); 102 | } 103 | 104 | void 105 | _pbcV_dezigzag32(struct longlong *r) 106 | { 107 | uint32_t low = r->low; 108 | r->low = (low >> 1) ^ - (low & 1); 109 | r->hi = -(low >> 31); 110 | } 111 | -------------------------------------------------------------------------------- /3rd/pbc/src/varint.h: -------------------------------------------------------------------------------- 1 | #ifndef PROTOBUF_C_VARINT_H 2 | #define PROTOBUF_C_VARINT_H 3 | 4 | #include 5 | 6 | struct longlong { 7 | uint32_t low; 8 | uint32_t hi; 9 | }; 10 | 11 | int _pbcV_encode32(uint32_t number, uint8_t buffer[10]); 12 | int _pbcV_encode(uint64_t number, uint8_t buffer[10]); 13 | int _pbcV_zigzag32(int32_t number, uint8_t buffer[10]); 14 | int _pbcV_zigzag(int64_t number, uint8_t buffer[10]); 15 | 16 | int _pbcV_decode(uint8_t buffer[10], struct longlong *result); 17 | void _pbcV_dezigzag64(struct longlong *r); 18 | void _pbcV_dezigzag32(struct longlong *r); 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /3rd/pbc/test/addressbook.c: -------------------------------------------------------------------------------- 1 | #include "pbc.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | static void 9 | read_file (const char *filename , struct pbc_slice *slice) { 10 | FILE *f = fopen(filename, "rb"); 11 | if (f == NULL) { 12 | slice->buffer = NULL; 13 | slice->len = 0; 14 | return; 15 | } 16 | fseek(f,0,SEEK_END); 17 | slice->len = ftell(f); 18 | fseek(f,0,SEEK_SET); 19 | slice->buffer = malloc(slice->len); 20 | fread(slice->buffer, 1 , slice->len , f); 21 | fclose(f); 22 | } 23 | 24 | 25 | static void 26 | dump(uint8_t *buffer, int sz) { 27 | int i , j; 28 | for (i=0;i=32 && c<127) { 34 | printf("%c",c); 35 | } else { 36 | printf("."); 37 | } 38 | } 39 | printf("\n"); 40 | } 41 | } 42 | 43 | printf("\n"); 44 | } 45 | 46 | static void 47 | test_rmessage(struct pbc_env *env, struct pbc_slice *slice) { 48 | struct pbc_rmessage * m = pbc_rmessage_new(env, "tutorial.Person", slice); 49 | if (m==NULL) { 50 | printf("Error : %s",pbc_error(env)); 51 | return; 52 | } 53 | printf("name = %s\n", pbc_rmessage_string(m , "name" , 0 , NULL)); 54 | printf("id = %d\n", pbc_rmessage_integer(m , "id" , 0 , NULL)); 55 | printf("email = %s\n", pbc_rmessage_string(m , "email" , 0 , NULL)); 56 | 57 | int phone_n = pbc_rmessage_size(m, "phone"); 58 | int i; 59 | const char * field_name; 60 | pbc_type(env, "tutorial.Person", "phone", &field_name); 61 | printf("phone type [%s]\n",field_name); 62 | 63 | for (i=0;i 6 | 7 | int 8 | main() 9 | { 10 | pbc_array array; 11 | pbc_var v; 12 | 13 | _pbcA_open(array); 14 | 15 | int i ; 16 | 17 | for (i=0;i<100;i++) { 18 | v->real = (double)i; 19 | printf("push %d\n",i); 20 | _pbcA_push(array, v); 21 | } 22 | 23 | int s = pbc_array_size(array); 24 | 25 | for (i=0;ireal); 28 | } 29 | 30 | _pbcA_close(array); 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /3rd/pbc/test/decode.c: -------------------------------------------------------------------------------- 1 | #include "pbc.h" 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | static void 8 | read_file (const char *filename , struct pbc_slice *slice) { 9 | FILE *f = fopen(filename, "rb"); 10 | if (f == NULL) { 11 | slice->buffer = NULL; 12 | slice->len = 0; 13 | return; 14 | } 15 | fseek(f,0,SEEK_END); 16 | slice->len = ftell(f); 17 | fseek(f,0,SEEK_SET); 18 | slice->buffer = malloc(slice->len); 19 | fread(slice->buffer, 1 , slice->len , f); 20 | fclose(f); 21 | } 22 | 23 | static void 24 | decode_all(void *ud , int type, const char * typename , union pbc_value *v, int id, const char *key) { 25 | printf("%s : ", key ) ; 26 | switch(type & ~PBC_REPEATED) { 27 | case PBC_MESSAGE: 28 | printf("[%s] -> \n" , typename); 29 | pbc_decode(ud, typename, &(v->s), decode_all, ud); 30 | printf("---------\n"); 31 | break; 32 | case PBC_INT: 33 | printf("%d\n", (int)v->i.low); 34 | break; 35 | case PBC_REAL: 36 | printf("%lf\n", v->f); 37 | break; 38 | case PBC_BOOL: 39 | printf("<%s>\n", v->i.low ? "true" : "false"); 40 | break; 41 | case PBC_ENUM: 42 | printf("[%s:%d]\n", v->e.name , v->e.id); 43 | break; 44 | case PBC_STRING: { 45 | char buffer[v->s.len+1]; 46 | memcpy(buffer, v->s.buffer, v->s.len); 47 | buffer[v->s.len] = '\0'; 48 | printf("\"%s\"\n", buffer); 49 | break; 50 | } 51 | case PBC_BYTES: { 52 | int i; 53 | uint8_t *buffer = v->s.buffer; 54 | for (i=0;is.len;i++) { 55 | printf("%02X ",buffer[i]); 56 | } 57 | printf("\n"); 58 | break; 59 | } 60 | case PBC_INT64: { 61 | printf("0x%x%08x\n",v->i.hi, v->i.low); 62 | break; 63 | } 64 | case PBC_UINT: 65 | printf("%u\n",v->i.low); 66 | break; 67 | default: 68 | printf("!!! %d\n", type); 69 | break; 70 | } 71 | } 72 | 73 | void 74 | test_decode(struct pbc_env * env , const char * pb) 75 | { 76 | struct pbc_slice slice; 77 | read_file(pb, &slice); 78 | 79 | pbc_decode(env, "google.protobuf.FileDescriptorSet", &slice, decode_all , env); 80 | 81 | int ret = pbc_register(env, &slice); 82 | 83 | printf("Register %d\n",ret); 84 | 85 | free(slice.buffer); 86 | } 87 | 88 | int 89 | main(int argc, char *argv[]) 90 | { 91 | struct pbc_env * env = pbc_new(); 92 | 93 | test_decode(env,argv[1]); 94 | 95 | pbc_delete(env); 96 | 97 | 98 | return 0; 99 | } 100 | -------------------------------------------------------------------------------- /3rd/pbc/test/float.c: -------------------------------------------------------------------------------- 1 | #include "pbc.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | static void 9 | read_file (const char *filename , struct pbc_slice *slice) { 10 | FILE *f = fopen(filename, "rb"); 11 | if (f == NULL) { 12 | slice->buffer = NULL; 13 | slice->len = 0; 14 | return; 15 | } 16 | fseek(f,0,SEEK_END); 17 | slice->len = ftell(f); 18 | fseek(f,0,SEEK_SET); 19 | slice->buffer = malloc(slice->len); 20 | fread(slice->buffer, 1 , slice->len , f); 21 | fclose(f); 22 | } 23 | 24 | 25 | static void 26 | dump(uint8_t *buffer, int sz) { 27 | int i , j; 28 | for (i=0;i=32 && c<127) { 34 | printf("%c",c); 35 | } else { 36 | printf("."); 37 | } 38 | } 39 | printf("\n"); 40 | } 41 | } 42 | 43 | printf("\n"); 44 | } 45 | 46 | static void 47 | test_rmessage(struct pbc_env *env, struct pbc_slice *slice) { 48 | struct pbc_rmessage * m = pbc_rmessage_new(env, "real", slice); 49 | printf("f = %f\n", pbc_rmessage_real(m , "f" , 0 )); 50 | printf("d = %f\n", pbc_rmessage_real(m , "d" , 0 )); 51 | pbc_rmessage_delete(m); 52 | } 53 | 54 | static struct pbc_wmessage * 55 | test_wmessage(struct pbc_env * env) 56 | { 57 | struct pbc_wmessage * msg = pbc_wmessage_new(env, "real"); 58 | 59 | pbc_wmessage_real(msg, "f", 1.0); 60 | pbc_wmessage_real(msg, "d" , 4.0); 61 | 62 | return msg; 63 | } 64 | 65 | int 66 | main() 67 | { 68 | struct pbc_slice slice; 69 | read_file("float.pb", &slice); 70 | if (slice.buffer == NULL) 71 | return 1; 72 | struct pbc_env * env = pbc_new(); 73 | pbc_register(env, &slice); 74 | 75 | free(slice.buffer); 76 | 77 | struct pbc_wmessage *msg = test_wmessage(env); 78 | 79 | pbc_wmessage_buffer(msg, &slice); 80 | 81 | dump(slice.buffer, slice.len); 82 | 83 | test_rmessage(env, &slice); 84 | 85 | pbc_wmessage_delete(msg); 86 | pbc_delete(env); 87 | 88 | return 0; 89 | } 90 | -------------------------------------------------------------------------------- /3rd/pbc/test/float.proto: -------------------------------------------------------------------------------- 1 | message real { 2 | optional float f = 1; 3 | optional double d = 2; 4 | } 5 | -------------------------------------------------------------------------------- /3rd/pbc/test/map.c: -------------------------------------------------------------------------------- 1 | #include "src/map.h" 2 | 3 | #include 4 | #include 5 | 6 | int 7 | main() 8 | { 9 | struct map_kv kv[] = { 10 | {1,"alice"}, 11 | {3,"bob" }, 12 | {99,"carol"}, 13 | }; 14 | 15 | struct map_ip * map = _pbcM_ip_new(kv, sizeof(kv)/sizeof(kv[0])); 16 | struct map_si * map2 = _pbcM_si_new(kv, sizeof(kv)/sizeof(kv[0])); 17 | int i; 18 | 19 | for (i=0;i<100;i++) { 20 | void *p= _pbcM_ip_query(map,i); 21 | if (p) { 22 | int id = 0; 23 | _pbcM_si_query(map2,p,&id); 24 | printf("%d %s\n",id,(const char *)p); 25 | } 26 | } 27 | 28 | struct map_sp * map3 = _pbcM_sp_new(0, NULL); 29 | _pbcM_sp_insert(map3,"Alice","alice"); 30 | _pbcM_sp_insert(map3,"Bob","bob"); 31 | 32 | void ** r = _pbcM_sp_query_insert(map3, "Carol"); 33 | *r = "carol"; 34 | 35 | r = _pbcM_sp_query_insert(map3, "Alice"); 36 | *r = "not alice"; 37 | 38 | printf("%s\n",(const char *)_pbcM_sp_query(map3,"Alice")); 39 | printf("%s\n",(const char *)_pbcM_sp_query(map3,"Bob")); 40 | printf("%s\n",(const char *)_pbcM_sp_query(map3,"Carol")); 41 | 42 | const char * key = NULL; 43 | for (;;) { 44 | void * v = _pbcM_sp_next(map3, &key); 45 | if (key == NULL) 46 | break; 47 | printf("%s : %s\n", key, (const char *)v); 48 | } 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /3rd/pbc/test/pattern.c: -------------------------------------------------------------------------------- 1 | #include "pbc.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | static void 9 | read_file (const char *filename , struct pbc_slice *slice) { 10 | FILE *f = fopen(filename, "rb"); 11 | if (f == NULL) { 12 | slice->buffer = NULL; 13 | slice->len = 0; 14 | return; 15 | } 16 | fseek(f,0,SEEK_END); 17 | slice->len = ftell(f); 18 | fseek(f,0,SEEK_SET); 19 | slice->buffer = malloc(slice->len); 20 | fread(slice->buffer, 1 , slice->len , f); 21 | fclose(f); 22 | } 23 | 24 | static void 25 | dump(uint8_t *buffer, int sz) { 26 | int i , j; 27 | for (i=0;i=32 && c<127) { 33 | printf("%c",c); 34 | } else { 35 | printf("."); 36 | } 37 | } 38 | printf("\n"); 39 | } 40 | } 41 | 42 | printf("\n"); 43 | } 44 | 45 | static struct pbc_pattern *pat; 46 | static struct pbc_pattern *pat_phone; 47 | 48 | struct person_phone { 49 | struct pbc_slice number; 50 | int32_t type; 51 | }; 52 | 53 | struct person { 54 | struct pbc_slice name; 55 | int32_t id; 56 | struct pbc_slice email; 57 | pbc_array phone; 58 | pbc_array test; 59 | }; 60 | 61 | 62 | static void 63 | test_pattern_unpack(struct pbc_env *env, struct pbc_slice * slice) { 64 | struct person p; 65 | int r = pbc_pattern_unpack(pat, slice, &p); 66 | if (r>=0) { 67 | printf("name = %s\n",(const char *)p.name.buffer); 68 | printf("id = %d\n",p.id); 69 | printf("email = %s\n",(const char *)p.email.buffer); 70 | int n = pbc_array_size(p.phone); 71 | int i; 72 | for (i=0;ilen = 0; 116 | return slice->len; 117 | } 118 | 119 | pbc_array_push_slice(p.phone, &phone_slice); 120 | 121 | pbc_pattern_set_default(pat_phone, &phone); 122 | 123 | phone.number.buffer = (void *)"87654321"; 124 | phone.number.len = -1; 125 | 126 | char temp2[128]; 127 | struct pbc_slice phone_slice2 = { temp2, sizeof(temp2) }; 128 | 129 | unused = pbc_pattern_pack(pat_phone, &phone , &phone_slice2); 130 | 131 | if (unused < 0) { 132 | slice->len = 0; 133 | return slice->len; 134 | } 135 | 136 | pbc_array_push_slice(p.phone, &phone_slice2); 137 | 138 | int i; 139 | for (i=0;i<3;i++) { 140 | pbc_array_push_integer(p.test, -i*4,0); 141 | } 142 | 143 | int r = pbc_pattern_pack(pat, &p, slice); 144 | 145 | pbc_pattern_close_arrays(pat,&p); 146 | printf("pack into %d bytes\n", slice->len); 147 | 148 | return r; 149 | } 150 | 151 | int 152 | main() 153 | { 154 | struct pbc_slice slice; 155 | read_file("addressbook.pb", &slice); 156 | if (slice.buffer == NULL) 157 | return 1; 158 | struct pbc_env * env = pbc_new(); 159 | pbc_register(env, &slice); 160 | 161 | free(slice.buffer); 162 | 163 | pat = pbc_pattern_new(env, "tutorial.Person" , 164 | "name %s id %d email %s phone %a test %a", 165 | offsetof(struct person, name) , 166 | offsetof(struct person, id) , 167 | offsetof(struct person, email) , 168 | offsetof(struct person, phone) , 169 | offsetof(struct person, test)); 170 | 171 | pat_phone = pbc_pattern_new(env, "tutorial.Person.PhoneNumber", 172 | "number %s type %d", 173 | offsetof(struct person_phone, number), 174 | offsetof(struct person_phone, type)); 175 | 176 | 177 | char buffer[4096]; 178 | struct pbc_slice message = { buffer, sizeof(buffer) }; 179 | 180 | test_pattern_pack(env, &message); 181 | 182 | dump(message.buffer, message.len); 183 | 184 | test_pattern_unpack(env, &message); 185 | 186 | pbc_pattern_delete(pat); 187 | pbc_pattern_delete(pat_phone); 188 | 189 | pbc_delete(env); 190 | 191 | return 0; 192 | } 193 | -------------------------------------------------------------------------------- /3rd/pbc/test/pbc.c: -------------------------------------------------------------------------------- 1 | #include "pbc.h" 2 | 3 | #include 4 | #include 5 | 6 | static void 7 | read_file (const char *filename , struct pbc_slice *slice) { 8 | FILE *f = fopen(filename, "rb"); 9 | if (f == NULL) { 10 | slice->buffer = NULL; 11 | slice->len = 0; 12 | return; 13 | } 14 | fseek(f,0,SEEK_END); 15 | slice->len = ftell(f); 16 | fseek(f,0,SEEK_SET); 17 | slice->buffer = malloc(slice->len); 18 | fread(slice->buffer, 1 , slice->len , f); 19 | fclose(f); 20 | } 21 | 22 | void 23 | test_des(struct pbc_env * env , const char * pb) 24 | { 25 | struct pbc_slice slice; 26 | read_file(pb, &slice); 27 | 28 | struct pbc_rmessage * msg = pbc_rmessage_new(env, "google.protobuf.FileDescriptorSet", &slice); 29 | 30 | struct pbc_rmessage * file = pbc_rmessage_message(msg,"file",0); 31 | 32 | printf("name = %s\n",pbc_rmessage_string(file, "name", 0 , NULL)); 33 | printf("package = %s\n",pbc_rmessage_string(file, "package", 0 , NULL)); 34 | 35 | int sz = pbc_rmessage_size(file, "dependency"); 36 | printf("dependency[%d] =\n" , sz); 37 | int i; 38 | for (i=0;i 4 | #include 5 | #include 6 | 7 | #define COUNT 1000000 8 | 9 | static void 10 | read_file (const char *filename , struct pbc_slice *slice) { 11 | FILE *f = fopen(filename, "rb"); 12 | if (f == NULL) { 13 | slice->buffer = NULL; 14 | slice->len = 0; 15 | return; 16 | } 17 | fseek(f,0,SEEK_END); 18 | slice->len = ftell(f); 19 | fseek(f,0,SEEK_SET); 20 | slice->buffer = malloc(slice->len); 21 | fread(slice->buffer, 1 , slice->len , f); 22 | fclose(f); 23 | } 24 | 25 | static void 26 | test(struct pbc_env *env) { 27 | int i; 28 | for(i=0; i 2 | 3 | #include "varint.h" 4 | #include "pbc.h" 5 | 6 | static void 7 | dump(uint8_t buffer[10], int s) 8 | { 9 | int i; 10 | for (i=0;i 2 | #include 3 | #include 4 | #include 5 | 6 | #include "pbc.h" 7 | 8 | static void 9 | read_file (const char *filename , struct pbc_slice *slice) { 10 | FILE *f = fopen(filename, "rb"); 11 | if (f == NULL) { 12 | fprintf(stderr, "Can't open file %s\n", filename); 13 | exit(1); 14 | } 15 | fseek(f,0,SEEK_END); 16 | slice->len = ftell(f); 17 | fseek(f,0,SEEK_SET); 18 | slice->buffer = malloc(slice->len); 19 | fread(slice->buffer, 1 , slice->len , f); 20 | fclose(f); 21 | } 22 | 23 | // printf("%s : %d\n", key, t); 24 | 25 | static void dump_message(struct pbc_rmessage *m, int level); 26 | 27 | static void 28 | dump_value(struct pbc_rmessage *m, const char *key, int type, int idx, int level) { 29 | int i; 30 | for (i=0;i= data->len) { 127 | data->len *= 2; 128 | data->buffer = realloc(data->buffer, data->len); 129 | } 130 | ((uint8_t *)data->buffer)[idx] = (uint8_t)byte; 131 | } 132 | 133 | static void 134 | read_stdin(int mode, struct pbc_slice *data) { 135 | data->len = 128; 136 | data->buffer = malloc(data->len); 137 | int idx = 0; 138 | while(!feof(stdin)) { 139 | int byte; 140 | int r = scanf("%d" , &byte); 141 | if (r == 0) { 142 | break; 143 | } 144 | push_byte(byte, data, idx); 145 | ++idx; 146 | } 147 | data->len = idx; 148 | } 149 | 150 | static void 151 | usage(const char *argv0) { 152 | printf(" -h help.\n" 153 | " -p protobuf file\n" 154 | " -m \n" 155 | " -d \n" 156 | " -D input from stdin (DEC number)\n" 157 | ); 158 | } 159 | 160 | int 161 | main(int argc , char * argv[]) 162 | { 163 | int ch; 164 | const char * proto = NULL; 165 | const char * message = NULL; 166 | const char * datafile = NULL; 167 | int mode = 0; 168 | while ((ch = getopt(argc, argv, "hDp:m:d:")) != -1) { 169 | switch(ch) { 170 | case 'h': 171 | usage(argv[0]); 172 | return 0; 173 | case 'p': 174 | proto = optarg; 175 | break; 176 | case 'm': 177 | message = optarg; 178 | break; 179 | case 'd': 180 | datafile = optarg; 181 | break; 182 | case 'D': 183 | mode = 10; 184 | break; 185 | default: 186 | usage(argv[0]); 187 | return 1; 188 | } 189 | } 190 | 191 | if (proto == NULL || message == NULL) { 192 | usage(argv[0]); 193 | return 1; 194 | } 195 | 196 | struct pbc_slice data; 197 | 198 | if (datafile == NULL) { 199 | read_stdin(mode, &data); 200 | } else { 201 | read_file(datafile , &data); 202 | } 203 | 204 | dump(proto , message , &data); 205 | 206 | return 0; 207 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 ForthXu 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | zmkp 2 | ==== 3 | 4 | a card game build with skynet 5 | 6 | 云风skynet服务端框架研究 7 | http://forthxu.com/blog/skynet.html 8 | -------------------------------------------------------------------------------- /clean.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # echo "please input port:" 3 | # read PORT 4 | 5 | #skynet目录 6 | SKYNET_PATH="./skynet/" 7 | 8 | git checkout . && git clean -xdf 9 | cd ${SKYNET_PATH}; 10 | #git checkout . && git clean -xdf 11 | 12 | #日志目录 13 | LOG_PATH="../log/" 14 | 15 | #tmp目录 16 | TMP_PATH="../tmp/" 17 | 18 | rm -rf $LOG_PATH $TMP_PATH 19 | 20 | pids=`ps aux | grep skynet | awk -F " " '{if($11 != "grep")print $2;}'` 21 | for pid in $pids;do 22 | kill -9 $pid 23 | done 24 | -------------------------------------------------------------------------------- /configs/center.cfg: -------------------------------------------------------------------------------- 1 | root = "./" 2 | thread = 8 3 | logger = nil 4 | harbor = 1 5 | address = "127.0.0.1:10201" 6 | master = "127.0.0.1:10200" 7 | start = "main" -- main script 8 | bootstrap = "snlua bootstrap" -- The service for bootstrap 9 | standalone = "0.0.0.0:10200" 10 | luaservice = root.."service/?.lua;"..root.."test/?.lua;"..root.."../src/center/?.lua" 11 | snax = root.."../src/center/?.lua;"..root.."test/?.lua" 12 | cpath = root.."cservice/?.so" 13 | -------------------------------------------------------------------------------- /configs/log.cfg: -------------------------------------------------------------------------------- 1 | thread = 8 2 | mqueue = 256 3 | cpath = "./cservice/?.so" 4 | logger = nil 5 | -- logger = "../log/b.log" 6 | harbor = 2 7 | address = "127.0.0.1:12527" 8 | master = "127.0.0.1:12013" 9 | start = "main_log" 10 | luaservice ="./service/?.lua;./test/?.lua;./../src/log/?.lua" 11 | snax = "./../src/log/?.lua;./test/?.lua" 12 | -------------------------------------------------------------------------------- /configs/simpledb.cfg: -------------------------------------------------------------------------------- 1 | root = "./" 2 | thread = 8 3 | logger = nil 4 | -- logger = root.."../log/a.log" 5 | harbor = 1 6 | address = "127.0.0.1:12526" 7 | master = "127.0.0.1:12013" 8 | start = "main" -- main script 9 | bootstrap = "snlua bootstrap" -- The service for bootstrap 10 | standalone = "0.0.0.0:12013" 11 | luaservice = root.."service/?.lua;"..root.."test/?.lua;"..root.."../src/simpledb/?.lua" 12 | -- lualoader = "lualib/loader.luapreload = "./../src/simpledb/preload.lua" -- run preload.lua before every lua service run 13 | snax = root.."../src/simpledb/?.lua;"..root.."test/?.lua" 14 | cpath = root.."cservice/?.so" 15 | -------------------------------------------------------------------------------- /res/addressbook.proto: -------------------------------------------------------------------------------- 1 | package tutorial; 2 | 3 | option java_package = "com.example.tutorial"; 4 | option java_outer_classname = "AddressBookProtos"; 5 | 6 | message Person { 7 | required string name = 1; 8 | required int32 id = 2; // Unique ID number for this person. 9 | optional string email = 3; 10 | 11 | enum PhoneType { 12 | MOBILE = 0; 13 | HOME = 1; 14 | WORK = 2; 15 | } 16 | 17 | message PhoneNumber { 18 | required string number = 1; 19 | optional PhoneType type = 2 [default = HOME]; 20 | } 21 | 22 | repeated PhoneNumber phone = 4; 23 | repeated int32 test = 5 [packed=true]; 24 | 25 | extensions 10 to max; 26 | } 27 | 28 | message Ext { 29 | extend Person { 30 | optional int32 test = 10; 31 | } 32 | } 33 | 34 | // Our address book file is just one of these. 35 | message AddressBook { 36 | repeated Person person = 1; 37 | } 38 | -------------------------------------------------------------------------------- /src/center/agent.lua: -------------------------------------------------------------------------------- 1 | local skynet = require "skynet" 2 | local jsonpack = require "jsonpack" 3 | local netpack = require "netpack" 4 | local socket = require "socket" 5 | p=require("p.core") 6 | 7 | local CMD = {} 8 | 9 | local client_fd 10 | 11 | local function xfs_send(v) 12 | socket.write(client_fd, netpack.pack(v)) 13 | end 14 | 15 | 16 | skynet.register_protocol { 17 | name = "client", 18 | id = skynet.PTYPE_CLIENT, 19 | unpack = function (msg, sz) 20 | return skynet.tostring(msg,sz) 21 | end, 22 | dispatch = function (session, address, text) 23 | x = p.unpack(text) 24 | print("ok",x.msg) 25 | 26 | xfs_send(p.pack(1,1025,"data:"..x.msg)) 27 | xfs_send(p.pack(1,1026,"你好")) 28 | end 29 | } 30 | 31 | skynet.register_protocol { 32 | name = "xfs", 33 | id = 12, 34 | pack = skynet.pack, 35 | unpack = skynet.unpack, 36 | dispatch = function (session, address, text) 37 | print("[LOG]", skynet.address(address),text) 38 | -- xfs_send("Welcome to skynet\n") 39 | xfs_send(p.pack(1,1024,"Welcome to skynet\n")) 40 | skynet.retpack(text) 41 | end 42 | } 43 | 44 | function CMD.start(gate , fd) 45 | client_fd = fd 46 | skynet.call(gate, "lua", "forward", fd) 47 | end 48 | 49 | skynet.start(function() 50 | skynet.dispatch("lua", function(_,_, command, ...) 51 | local f = CMD[command] 52 | skynet.ret(skynet.pack(f(...))) 53 | end) 54 | end) 55 | -------------------------------------------------------------------------------- /src/center/center.lua: -------------------------------------------------------------------------------- 1 | local skynet = require "skynet" 2 | local db = {} 3 | 4 | local command = {} 5 | 6 | function command.GET(key) 7 | return db[key] 8 | end 9 | 10 | function command.SET(key, value) 11 | local last = db[key] 12 | db[key] = value 13 | return last 14 | end 15 | 16 | function command.P(key, value) 17 | local protobuf = require "protobuf" 18 | 19 | addr = io.open("../res/addressbook.pb","rb") 20 | buffer = addr:read "*a" 21 | addr:close() 22 | protobuf.register(buffer) 23 | 24 | local person = { 25 | name = "Alice", 26 | id = 123, 27 | phone = { 28 | { number = "123456789" , type = "MOBILE" }, 29 | { number = "87654321" , type = "HOME" }, 30 | } 31 | } 32 | 33 | local buffer = protobuf.encode("tutorial.Person", person) 34 | 35 | local t = protobuf.decode("tutorial.Person", buffer) 36 | 37 | for k,v in pairs(t) do 38 | if type(k) == "string" then 39 | print(k,v) 40 | end 41 | end 42 | 43 | print(t.phone[2].type) 44 | 45 | for k,v in pairs(t.phone[1]) do 46 | print(k,v) 47 | end 48 | 49 | return "ok" 50 | end 51 | 52 | skynet.start(function() 53 | skynet.dispatch("lua", function(session, address, cmd, ...) 54 | local f = command[string.upper(cmd)] 55 | skynet.ret(skynet.pack(f(...))) 56 | end) 57 | skynet.register "CENTER" 58 | end) 59 | -------------------------------------------------------------------------------- /src/center/class/login.lua: -------------------------------------------------------------------------------- 1 | local login = {} 2 | function login.echo() 3 | print "ok" 4 | end 5 | return login -------------------------------------------------------------------------------- /src/center/main.lua: -------------------------------------------------------------------------------- 1 | local skynet = require "skynet" 2 | 3 | local max_client = 64 4 | 5 | skynet.start(function() 6 | print("Server start") 7 | skynet.newservice("center") 8 | local watchdog = skynet.newservice("watchdog") 9 | skynet.call(watchdog, "lua", "start", { 10 | port = 10101, 11 | maxclient = max_client, 12 | }) 13 | 14 | skynet.exit() 15 | end) 16 | -------------------------------------------------------------------------------- /src/center/preload.lua: -------------------------------------------------------------------------------- 1 | -- This file will execute before every lua service start 2 | -- See config 3 | 4 | print("PRELOAD", ...) 5 | 6 | -------------------------------------------------------------------------------- /src/center/watchdog.lua: -------------------------------------------------------------------------------- 1 | local skynet = require "skynet" 2 | local netpack = require "netpack" 3 | 4 | package.path = string.format("%s;%s?.lua;", package.path, "./../src/center/class/") --增加lua文件的的路径 5 | local login = require "login" 6 | 7 | local CMD = {} 8 | local SOCKET = {} 9 | local gate 10 | local agent = {} 11 | 12 | 13 | 14 | 15 | function SOCKET.open(fd, addr) 16 | agent[fd] = skynet.newservice("agent") 17 | skynet.call(agent[fd], "lua", "start", gate, fd) 18 | skynet.call(agent[fd], "xfs", "start a new agent") 19 | end 20 | 21 | skynet.register_protocol { 22 | name = "xfs", 23 | id = 12, 24 | pack = skynet.pack, 25 | unpack = skynet.unpack, 26 | } 27 | 28 | local function close_agent(fd) 29 | local a = agent[fd] 30 | if a then 31 | skynet.kill(a) 32 | agent[fd] = nil 33 | end 34 | end 35 | 36 | function SOCKET.close(fd) 37 | print("socket close",fd) 38 | close_agent(fd) 39 | end 40 | 41 | function SOCKET.error(fd, msg) 42 | print("socket error",fd, msg) 43 | close_agent(fd) 44 | end 45 | 46 | function SOCKET.data(fd, msg) 47 | -- print("[data]",fd, msg) 48 | end 49 | 50 | function CMD.start(conf) 51 | skynet.call(gate, "lua", "open" , conf) 52 | end 53 | 54 | skynet.start(function() 55 | skynet.dispatch("lua", function(session, source, cmd, subcmd, ...) 56 | if cmd == "socket" then 57 | local f = SOCKET[subcmd] 58 | f(...) 59 | -- socket api don't need return 60 | else 61 | local f = assert(CMD[cmd]) 62 | skynet.ret(skynet.pack(f(subcmd, ...))) 63 | end 64 | end) 65 | 66 | gate = skynet.newservice("gate") 67 | end) 68 | -------------------------------------------------------------------------------- /src/copy/main.lua: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/forthxu/zmkp/f96bbf20263c9828629948a5e01373736f35602a/src/copy/main.lua -------------------------------------------------------------------------------- /src/log/globallog.lua: -------------------------------------------------------------------------------- 1 | local skynet = require "skynet" 2 | 3 | skynet.start(function() 4 | skynet.dispatch("text", function(session, address, text) 5 | print("[GLOBALLOG]", skynet.address(address),text) 6 | end) 7 | skynet.register "LOG" 8 | end) 9 | -------------------------------------------------------------------------------- /src/log/main_log.lua: -------------------------------------------------------------------------------- 1 | local skynet = require "skynet" 2 | 3 | skynet.start(function() 4 | print("Log server start") 5 | local service = skynet.newservice("service_mgr") 6 | skynet.monitor "simplemonitor" 7 | local log = skynet.newservice("globallog") 8 | skynet.exit() 9 | end) 10 | 11 | -------------------------------------------------------------------------------- /src/log/simplemonitor.lua: -------------------------------------------------------------------------------- 1 | local skynet = require "skynet" 2 | 3 | -- It's a simple service exit monitor, you can do something more when a service exit. 4 | 5 | local service_map = {} 6 | 7 | skynet.register_protocol { 8 | name = "client", 9 | id = skynet.PTYPE_CLIENT, -- PTYPE_CLIENT = 3 10 | unpack = function() end, 11 | dispatch = function(_, address) 12 | local w = service_map[address] 13 | if w then 14 | for watcher in pairs(w) do 15 | skynet.redirect(watcher, address, "error", "") 16 | end 17 | service_map[address] = false 18 | end 19 | end 20 | } 21 | 22 | local function monitor(session, watcher, command, service) 23 | assert(command, "WATCH") 24 | local w = service_map[service] 25 | if not w then 26 | if w == false then 27 | skynet.ret(skynet.pack(false)) 28 | return 29 | end 30 | w = {} 31 | service_map[service] = w 32 | end 33 | w[watcher] = true 34 | skynet.ret(skynet.pack(true)) 35 | end 36 | 37 | skynet.start(function() 38 | skynet.dispatch("lua", monitor) 39 | end) 40 | -------------------------------------------------------------------------------- /src/scene/main.lua: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/forthxu/zmkp/f96bbf20263c9828629948a5e01373736f35602a/src/scene/main.lua -------------------------------------------------------------------------------- /src/simpledb/agent.lua: -------------------------------------------------------------------------------- 1 | local skynet = require "skynet" 2 | local jsonpack = require "jsonpack" 3 | local netpack = require "netpack" 4 | local socket = require "socket" 5 | 6 | local CMD = {} 7 | 8 | local client_fd 9 | 10 | local function send_client(v) 11 | socket.write(client_fd, netpack.pack(jsonpack.pack(0,v))) 12 | end 13 | 14 | local function response_client(session,v) 15 | socket.write(client_fd, netpack.pack(jsonpack.response(session,v))) 16 | end 17 | 18 | skynet.register_protocol { 19 | name = "client", 20 | id = skynet.PTYPE_CLIENT, 21 | unpack = function (msg, sz) 22 | return jsonpack.unpack(skynet.tostring(msg,sz)) 23 | end, 24 | dispatch = function (_, _, session, args) 25 | local ok, result = pcall(skynet.call,"SIMPLEDB", "lua", table.unpack(args)) 26 | if ok then 27 | response_client(session, { true, result }) 28 | else 29 | response_client(session, { false, "Invalid command" }) 30 | end 31 | end 32 | } 33 | 34 | function CMD.start(gate , fd) 35 | client_fd = fd 36 | skynet.call(gate, "lua", "forward", fd) 37 | send_client "Welcome to skynet" 38 | end 39 | 40 | skynet.start(function() 41 | skynet.dispatch("lua", function(_,_, command, ...) 42 | local f = CMD[command] 43 | skynet.ret(skynet.pack(f(...))) 44 | end) 45 | end) 46 | -------------------------------------------------------------------------------- /src/simpledb/main.lua: -------------------------------------------------------------------------------- 1 | local skynet = require "skynet" 2 | 3 | local max_client = 64 4 | 5 | skynet.start(function() 6 | print("Server start") 7 | -- local console = skynet.newservice("console") 8 | -- skynet.newservice("debug_console",18000) 9 | skynet.newservice("simpledb") 10 | local watchdog = skynet.newservice("watchdog") 11 | skynet.call(watchdog, "lua", "start", { 12 | port = 10104, 13 | maxclient = max_client, 14 | }) 15 | 16 | skynet.exit() 17 | end) 18 | -------------------------------------------------------------------------------- /src/simpledb/preload.lua: -------------------------------------------------------------------------------- 1 | -- This file will execute before every lua service start 2 | -- See config 3 | 4 | print("PRELOAD", ...) 5 | 6 | -------------------------------------------------------------------------------- /src/simpledb/simpledb.lua: -------------------------------------------------------------------------------- 1 | local skynet = require "skynet" 2 | local db = {} 3 | 4 | local command = {} 5 | 6 | function command.GET(key) 7 | return db[key] 8 | end 9 | 10 | function command.SET(key, value) 11 | local last = db[key] 12 | db[key] = value 13 | return last 14 | end 15 | 16 | skynet.start(function() 17 | skynet.dispatch("lua", function(session, address, cmd, ...) 18 | local f = command[string.upper(cmd)] 19 | skynet.ret(skynet.pack(f(...))) 20 | end) 21 | skynet.register "SIMPLEDB" 22 | end) 23 | -------------------------------------------------------------------------------- /src/simpledb/watchdog.lua: -------------------------------------------------------------------------------- 1 | local skynet = require "skynet" 2 | local netpack = require "netpack" 3 | 4 | local CMD = {} 5 | local SOCKET = {} 6 | local gate 7 | local agent = {} 8 | 9 | function SOCKET.open(fd, addr) 10 | agent[fd] = skynet.newservice("agent") 11 | skynet.call(agent[fd], "lua", "start", gate, fd) 12 | end 13 | 14 | local function close_agent(fd) 15 | local a = agent[fd] 16 | if a then 17 | skynet.kill(a) 18 | agent[fd] = nil 19 | end 20 | end 21 | 22 | function SOCKET.close(fd) 23 | print("socket close",fd) 24 | close_agent(fd) 25 | end 26 | 27 | function SOCKET.error(fd, msg) 28 | print("socket error",fd, msg) 29 | close_agent(fd) 30 | end 31 | 32 | function SOCKET.data(fd, msg) 33 | end 34 | 35 | function CMD.start(conf) 36 | skynet.call(gate, "lua", "open" , conf) 37 | end 38 | 39 | skynet.start(function() 40 | skynet.dispatch("lua", function(session, source, cmd, subcmd, ...) 41 | if cmd == "socket" then 42 | local f = SOCKET[subcmd] 43 | f(...) 44 | -- socket api don't need return 45 | else 46 | local f = assert(CMD[cmd]) 47 | skynet.ret(skynet.pack(f(subcmd, ...))) 48 | end 49 | end) 50 | 51 | gate = skynet.newservice("gate") 52 | end) 53 | -------------------------------------------------------------------------------- /start.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # echo "please input port:" 3 | # read PORT 4 | 5 | #skynet目录 6 | SKYNET_PATH="./skynet/" 7 | 8 | #运行脚本 9 | run() 10 | { 11 | #检查启动程序 12 | if [ ! -x "${2}" ]; then 13 | echo "你妹啊,启动程序没有,${2} " 14 | exit 15 | fi 16 | #关闭程序 17 | if [ -a "${TMP_PATH}kill_${1}.sh" ]; then 18 | echo "关闭程序:" 19 | sh ${TMP_PATH}kill_${1}.sh 20 | fi 21 | #设置日志存储 22 | DATA_DAY=`date +%Y-%m-%d` 23 | DATA_SECOND=`date +%Y-%m-%d-%H-%M-%S` 24 | LOG_NAME="${LOG_PATH}${1}_${DATA_DAY}.log" 25 | BACKUP_LOG_NAME="${LOG_PATH}${1}_${DATA_SECOND}_old.log" 26 | #备份日志 27 | if [ -a "${LOG_NAME}" ]; then 28 | # mv ${LOG_NAME} ${BACKUP_LOG_NAME} 29 | rm -rf ${LOG_NAME} 30 | fi 31 | #启动 32 | nohup ${2} ${3} >> ${LOG_NAME} 2>&1 & 33 | # (${2} ${3} &) 34 | #生成关闭的程序 35 | echo "#!/bin/bash" > ${TMP_PATH}kill_${1}.sh 36 | echo "echo 'run: ${2} ${3} pid: $!'" >> ${TMP_PATH}kill_${1}.sh 37 | echo "kill -9 $!" >> ${TMP_PATH}kill_${1}.sh 38 | chmod 777 ${TMP_PATH}kill_${1}.sh 39 | # sleep 1 40 | #显示运行的程序 41 | echo "运行程序:" 42 | # echo "nohup ${2} ${3} >> ${LOG_NAME} 2>&1 &" 43 | echo "run:$2 $3 pid:$! log:${LOG_NAME} " 44 | #打印启动错误 45 | sleep 3 46 | if [ -s "${LOG_NAME}" ]; then 47 | echo "启动日志:" 48 | cat ${LOG_NAME} 49 | # exit 50 | fi 51 | sleep 1 52 | } 53 | 54 | echo " >>---------- 开始 ----------" 55 | echo " >>---------- 处理protocbuf ----------" 56 | cd ./3rd/pbc/ && make && cd ./binding/lua/ && make && cd ../../../../ && cp -f ./3rd/pbc/binding/lua/protobuf.lua ./skynet/lualib/ && cp -f ./3rd/pbc/binding/lua/protobuf.so ./skynet/luaclib/ 57 | protoc -o ./res/addressbook.pb ./res/addressbook.proto 58 | 59 | echo " >>---------- 处理协议 ----------" 60 | cd ./3rd/p/ && gcc -g -O2 -Wall -I../../skynet/3rd/lua -fPIC --shared ./lua-p.c -o ./p.so && cd ../../ && cp -f ./3rd/p/p.so ./skynet/luaclib/ 61 | 62 | echo " >>---------- 进入skynet目录 ----------" 63 | echo "" 64 | # git checkout . && git clean -xdf 65 | cd ${SKYNET_PATH}; 66 | # git checkout . && git clean -xdf 67 | #日志目录 68 | LOG_PATH="../log/" 69 | if [ ! -x "$LOG_PATH" ]; then 70 | mkdir "$LOG_PATH" 71 | fi 72 | 73 | #tmp目录 74 | TMP_PATH="../tmp/" 75 | if [ ! -x "$TMP_PATH" ]; then 76 | mkdir "$TMP_PATH" 77 | fi 78 | echo "" 79 | echo " >>---------- 编译 ----------" 80 | echo "" 81 | make linux; 82 | echo "" 83 | echo " >>---------- 执行 ---------" 84 | echo "" 85 | # run a ./skynet ../configs/simpledb.cfg 86 | # echo "----------------------------" 87 | # run b ./skynet ../configs/log.cfg 88 | run c ./skynet ../configs/center.cfg 89 | echo "" 90 | echo " >>---------- 结束 ----------" 91 | -------------------------------------------------------------------------------- /tests/ASClient/.actionScriptProperties: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /tests/ASClient/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | ClientTest 4 | 5 | 6 | 7 | 8 | 9 | com.adobe.flexbuilder.project.flexbuilder 10 | 11 | 12 | 13 | 14 | 15 | com.adobe.flexbuilder.project.actionscriptnature 16 | 17 | 18 | -------------------------------------------------------------------------------- /tests/ASClient/.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | #Thu Jan 16 22:25:31 CST 2014 2 | eclipse.preferences.version=1 3 | encoding/=utf-8 4 | -------------------------------------------------------------------------------- /tests/ASClient/bin-debug/ClientTest.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 13 | 14 | 15 | 16 | 17 | 23 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 61 | 62 | 63 | 67 |
68 |

69 | To view this page ensure that Adobe Flash Player version 70 | 11.8.0 or greater is installed. 71 |

72 | 77 |
78 | 79 | 107 | 108 | 109 | -------------------------------------------------------------------------------- /tests/ASClient/bin-debug/ClientTest.swf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/forthxu/zmkp/f96bbf20263c9828629948a5e01373736f35602a/tests/ASClient/bin-debug/ClientTest.swf -------------------------------------------------------------------------------- /tests/ASClient/bin-debug/assets/crossdomain.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /tests/ASClient/bin-debug/assets/policy.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /tests/ASClient/bin-debug/history/history.css: -------------------------------------------------------------------------------- 1 | /* This CSS stylesheet defines styles used by required elements in a flex application page that supports browser history */ 2 | 3 | #ie_historyFrame { width: 0px; height: 0px; display:none } 4 | #firefox_anchorDiv { width: 0px; height: 0px; display:none } 5 | #safari_formDiv { width: 0px; height: 0px; display:none } 6 | #safari_rememberDiv { width: 0px; height: 0px; display:none } 7 | -------------------------------------------------------------------------------- /tests/ASClient/bin-debug/history/historyFrame.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 27 | Hidden frame for Browser History support. 28 | 29 | 30 | -------------------------------------------------------------------------------- /tests/ASClient/bin-debug/playerProductInstall.swf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/forthxu/zmkp/f96bbf20263c9828629948a5e01373736f35602a/tests/ASClient/bin-debug/playerProductInstall.swf -------------------------------------------------------------------------------- /tests/ASClient/bin-debug/srcview/ClientTest.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/forthxu/zmkp/f96bbf20263c9828629948a5e01373736f35602a/tests/ASClient/bin-debug/srcview/ClientTest.zip -------------------------------------------------------------------------------- /tests/ASClient/bin-debug/srcview/SourceIndex.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | ClientTest 的源代码 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /tests/ASClient/bin-debug/srcview/SourceStyles.css: -------------------------------------------------------------------------------- 1 | body { 2 | font-family: Courier New, Courier, monospace; 3 | font-size: medium; 4 | } 5 | 6 | .CSS@font-face { 7 | color: #990000; 8 | font-weight: bold; 9 | } 10 | 11 | .CSS@import { 12 | color: #006666; 13 | font-weight: bold; 14 | } 15 | 16 | .CSS@media { 17 | color: #663333; 18 | font-weight: bold; 19 | } 20 | 21 | .CSS@namespace { 22 | color: #923196; 23 | } 24 | 25 | .CSSComment { 26 | color: #999999; 27 | } 28 | 29 | .CSSDefault_Text { 30 | } 31 | 32 | .CSSDelimiters { 33 | } 34 | 35 | .CSSProperty_Name { 36 | color: #330099; 37 | } 38 | 39 | .CSSProperty_Value { 40 | color: #3333cc; 41 | } 42 | 43 | .CSSSelector { 44 | color: #ff00ff; 45 | } 46 | 47 | .CSSString { 48 | color: #990000; 49 | } 50 | 51 | .ActionScriptASDoc { 52 | color: #3f5fbf; 53 | } 54 | 55 | .ActionScriptBracket/Brace { 56 | } 57 | 58 | .ActionScriptComment { 59 | color: #f5616b; 60 | font-weight: bold; 61 | } 62 | 63 | .ActionScriptDefault_Text { 64 | } 65 | 66 | .ActionScriptMetadata { 67 | color: #0033ff; 68 | font-weight: bold; 69 | } 70 | 71 | .ActionScriptOperator { 72 | } 73 | 74 | .ActionScriptReserved { 75 | color: #0033ff; 76 | font-weight: bold; 77 | } 78 | 79 | .ActionScriptString { 80 | color: #990000; 81 | font-weight: bold; 82 | } 83 | 84 | .ActionScriptclass { 85 | color: #9900cc; 86 | font-weight: bold; 87 | } 88 | 89 | .ActionScriptfunction { 90 | color: #339966; 91 | font-weight: bold; 92 | } 93 | 94 | .ActionScriptinterface { 95 | color: #9900cc; 96 | font-weight: bold; 97 | } 98 | 99 | .ActionScriptpackage { 100 | color: #9900cc; 101 | font-weight: bold; 102 | } 103 | 104 | .ActionScripttrace { 105 | color: #cc6666; 106 | font-weight: bold; 107 | } 108 | 109 | .ActionScriptvar { 110 | color: #6699cc; 111 | font-weight: bold; 112 | } 113 | 114 | .MXMLASDoc { 115 | color: #3f5fbf; 116 | } 117 | 118 | .MXMLComment { 119 | color: #800000; 120 | } 121 | 122 | .MXMLComponent_Tag { 123 | color: #0000ff; 124 | } 125 | 126 | .MXMLDefault_Text { 127 | } 128 | 129 | .MXMLProcessing_Instruction { 130 | } 131 | 132 | .MXMLSpecial_Tag { 133 | color: #006633; 134 | } 135 | 136 | .MXMLString { 137 | color: #990000; 138 | } 139 | 140 | -------------------------------------------------------------------------------- /tests/ASClient/bin-debug/srcview/SourceTree.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 13 | 14 | 15 | 16 | 21 | 27 | 28 | 29 | 33 | 34 | 35 | 66 | 67 | 68 | 72 |
73 |

74 | To view this page ensure that Adobe Flash Player version 75 | 10.0.0 or greater is installed. 76 |

77 | 82 |
83 | 84 | 112 | 113 | 114 | -------------------------------------------------------------------------------- /tests/ASClient/bin-debug/srcview/SourceTree.swf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/forthxu/zmkp/f96bbf20263c9828629948a5e01373736f35602a/tests/ASClient/bin-debug/srcview/SourceTree.swf -------------------------------------------------------------------------------- /tests/ASClient/bin-debug/srcview/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Source of ClientTest 7 | 8 | 9 | 10 | 11 | 12 | 13 | <body> 14 | </body> 15 | 16 | 17 | -------------------------------------------------------------------------------- /tests/ASClient/bin-debug/srcview/playerProductInstall.swf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/forthxu/zmkp/f96bbf20263c9828629948a5e01373736f35602a/tests/ASClient/bin-debug/srcview/playerProductInstall.swf -------------------------------------------------------------------------------- /tests/ASClient/bin-debug/srcview/source/ClientTest.as.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | ClientTest.as 7 | 8 | 9 | 10 |
package
11 | {
12 |     import flash.display.Sprite;
13 |     import flash.events.Event;
14 |     
15 |     import socket.ClientSocket;
16 |     
17 |     import view.TestView;
18 | 
19 | 
20 |     [( = "600", height = "600")]
21 |     /**
22 |      *
23 |      * @author Jason
24 |      */
25 |     public class ClientTest extends Sprite
26 |     {
27 |         private var _testView : TestView;
28 |         private var _socket:ClientSocket;
29 | 
30 |         public function ClientTest()
31 |         {
32 |             this.addEventListener(Event.ADDED_TO_STAGE, init);
33 |         }
34 | 
35 |         private function init(evt : Event) : void
36 |         {
37 |             _testView = TestView.getInstance();
38 |             this.addChild(_testView);
39 |             _socket=ClientSocket.getInstance();
40 |             
41 |         }
42 |     }
43 | }
44 | 
45 | 46 | -------------------------------------------------------------------------------- /tests/ASClient/bin-debug/srcview/source/assets/crossdomain.xml.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /tests/ASClient/bin-release/ClientTest.swf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/forthxu/zmkp/f96bbf20263c9828629948a5e01373736f35602a/tests/ASClient/bin-release/ClientTest.swf -------------------------------------------------------------------------------- /tests/ASClient/bin-release/assets/crossdomain.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /tests/ASClient/bin-release/assets/policy.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /tests/ASClient/bin-release/crossdomain.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /tests/ASClient/bin-release/srcview/ClientTest.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/forthxu/zmkp/f96bbf20263c9828629948a5e01373736f35602a/tests/ASClient/bin-release/srcview/ClientTest.zip -------------------------------------------------------------------------------- /tests/ASClient/bin-release/srcview/SourceIndex.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | ClientTest 的源代码 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /tests/ASClient/bin-release/srcview/SourceStyles.css: -------------------------------------------------------------------------------- 1 | body { 2 | font-family: Courier New, Courier, monospace; 3 | font-size: medium; 4 | } 5 | 6 | .CSS@font-face { 7 | color: #990000; 8 | font-weight: bold; 9 | } 10 | 11 | .CSS@import { 12 | color: #006666; 13 | font-weight: bold; 14 | } 15 | 16 | .CSS@media { 17 | color: #663333; 18 | font-weight: bold; 19 | } 20 | 21 | .CSS@namespace { 22 | color: #923196; 23 | } 24 | 25 | .CSSComment { 26 | color: #999999; 27 | } 28 | 29 | .CSSDefault_Text { 30 | } 31 | 32 | .CSSDelimiters { 33 | } 34 | 35 | .CSSProperty_Name { 36 | color: #330099; 37 | } 38 | 39 | .CSSProperty_Value { 40 | color: #3333cc; 41 | } 42 | 43 | .CSSSelector { 44 | color: #ff00ff; 45 | } 46 | 47 | .CSSString { 48 | color: #990000; 49 | } 50 | 51 | .ActionScriptASDoc { 52 | color: #3f5fbf; 53 | } 54 | 55 | .ActionScriptBracket/Brace { 56 | } 57 | 58 | .ActionScriptComment { 59 | color: #f5616b; 60 | font-weight: bold; 61 | } 62 | 63 | .ActionScriptDefault_Text { 64 | } 65 | 66 | .ActionScriptMetadata { 67 | color: #0033ff; 68 | font-weight: bold; 69 | } 70 | 71 | .ActionScriptOperator { 72 | } 73 | 74 | .ActionScriptReserved { 75 | color: #0033ff; 76 | font-weight: bold; 77 | } 78 | 79 | .ActionScriptString { 80 | color: #990000; 81 | font-weight: bold; 82 | } 83 | 84 | .ActionScriptclass { 85 | color: #9900cc; 86 | font-weight: bold; 87 | } 88 | 89 | .ActionScriptfunction { 90 | color: #339966; 91 | font-weight: bold; 92 | } 93 | 94 | .ActionScriptinterface { 95 | color: #9900cc; 96 | font-weight: bold; 97 | } 98 | 99 | .ActionScriptpackage { 100 | color: #9900cc; 101 | font-weight: bold; 102 | } 103 | 104 | .ActionScripttrace { 105 | color: #cc6666; 106 | font-weight: bold; 107 | } 108 | 109 | .ActionScriptvar { 110 | color: #6699cc; 111 | font-weight: bold; 112 | } 113 | 114 | .MXMLASDoc { 115 | color: #3f5fbf; 116 | } 117 | 118 | .MXMLComment { 119 | color: #800000; 120 | } 121 | 122 | .MXMLComponent_Tag { 123 | color: #0000ff; 124 | } 125 | 126 | .MXMLDefault_Text { 127 | } 128 | 129 | .MXMLProcessing_Instruction { 130 | } 131 | 132 | .MXMLSpecial_Tag { 133 | color: #006633; 134 | } 135 | 136 | .MXMLString { 137 | color: #990000; 138 | } 139 | 140 | -------------------------------------------------------------------------------- /tests/ASClient/bin-release/srcview/SourceTree.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 13 | 14 | 15 | 16 | 21 | 27 | 28 | 29 | 33 | 34 | 35 | 66 | 67 | 68 | 72 |
73 |

74 | To view this page ensure that Adobe Flash Player version 75 | 10.0.0 or greater is installed. 76 |

77 | 82 |
83 | 84 | 112 | 113 | 114 | -------------------------------------------------------------------------------- /tests/ASClient/bin-release/srcview/SourceTree.swf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/forthxu/zmkp/f96bbf20263c9828629948a5e01373736f35602a/tests/ASClient/bin-release/srcview/SourceTree.swf -------------------------------------------------------------------------------- /tests/ASClient/bin-release/srcview/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Source of ClientTest 7 | 8 | 9 | 10 | 11 | 12 | 13 | <body> 14 | </body> 15 | 16 | 17 | -------------------------------------------------------------------------------- /tests/ASClient/bin-release/srcview/playerProductInstall.swf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/forthxu/zmkp/f96bbf20263c9828629948a5e01373736f35602a/tests/ASClient/bin-release/srcview/playerProductInstall.swf -------------------------------------------------------------------------------- /tests/ASClient/bin-release/srcview/source/ClientTest.as.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | ClientTest.as 7 | 8 | 9 | 10 |
package
11 | {
12 |     import flash.display.Sprite;
13 |     import flash.events.Event;
14 |     
15 |     import socket.ClientSocket;
16 |     
17 |     import view.TestView;
18 | 
19 | 
20 |     [( = "600", height = "600")]
21 |     /**
22 |      *
23 |      * @author Jason
24 |      */
25 |     public class ClientTest extends Sprite
26 |     {
27 |         private var _testView : TestView;
28 |         private var _socket:ClientSocket;
29 | 
30 |         public function ClientTest()
31 |         {
32 |             this.addEventListener(Event.ADDED_TO_STAGE, init);
33 |         }
34 | 
35 |         private function init(evt : Event) : void
36 |         {
37 |             _testView = TestView.getInstance();
38 |             this.addChild(_testView);
39 |             _socket=ClientSocket.getInstance();
40 |             
41 |         }
42 |     }
43 | }
44 | 
45 | 46 | -------------------------------------------------------------------------------- /tests/ASClient/bin-release/srcview/source/assets/crossdomain.xml.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /tests/ASClient/src/ClientTest.as: -------------------------------------------------------------------------------- 1 | package 2 | { 3 | import flash.display.Sprite; 4 | import flash.events.Event; 5 | 6 | import socket.ClientSocket; 7 | 8 | import view.TestView; 9 | 10 | 11 | [SWF(width = "600", height = "600")] 12 | /** 13 | * 14 | * @author Jason 15 | */ 16 | public class ClientTest extends Sprite 17 | { 18 | private var _testView : TestView; 19 | private var _socket:ClientSocket; 20 | 21 | public function ClientTest() 22 | { 23 | this.addEventListener(Event.ADDED_TO_STAGE, init); 24 | } 25 | 26 | private function init(evt : Event) : void 27 | { 28 | _testView = TestView.getInstance(); 29 | this.addChild(_testView); 30 | _socket=ClientSocket.getInstance(); 31 | 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /tests/ASClient/src/assets/crossdomain.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /tests/ASClient/src/socket/ClientSocket.as: -------------------------------------------------------------------------------- 1 | package socket 2 | { 3 | import flash.events.Event; 4 | import flash.events.IOErrorEvent; 5 | import flash.events.ProgressEvent; 6 | import flash.events.SecurityErrorEvent; 7 | import flash.net.Socket; 8 | import flash.system.Security; 9 | 10 | import view.TestView; 11 | 12 | /** 13 | * 14 | * @author Jason 15 | */ 16 | public class ClientSocket extends Socket 17 | { 18 | private static var _this : ClientSocket; 19 | private static var _ip : String = "192.168.137.3"; 20 | 21 | private var _testView : TestView; 22 | 23 | public function ClientSocket(host : String = null, port : uint = 0) 24 | { 25 | super(); 26 | _testView = TestView.getInstance(); 27 | configureListeners(); 28 | if(host && port) 29 | { 30 | // Security.loadPolicyFile("http://192.168.54.227/ServerTest/crossdomain.xml"); 31 | Security.loadPolicyFile("xmlsocket://192.168.54.227:8866"); 32 | // Security.loadPolicyFile("xmlsocket://localhost:8866"); 33 | this.connect(host, port); 34 | } 35 | } 36 | 37 | public static function getInstance() : ClientSocket 38 | { 39 | if(!_this) 40 | _this = new ClientSocket(_ip, 10101); 41 | return _this; 42 | } 43 | 44 | private function configureListeners() : void 45 | { 46 | this.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler); 47 | this.addEventListener(Event.CLOSE, closeHandler); 48 | this.addEventListener(Event.CONNECT, connectHandler); 49 | this.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorEventHandler); 50 | this.addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler); 51 | } 52 | 53 | protected function socketDataHandler(event : ProgressEvent) : void 54 | { 55 | var str : String = readUTF(); 56 | _testView.appendInfo("收到:"+str); 57 | } 58 | 59 | public function sendMessage(data : Object) : void 60 | { 61 | if(!data) 62 | return; 63 | if(data is String) 64 | { 65 | try 66 | { 67 | writeUTF(data as String); 68 | } 69 | catch(error : Error) 70 | { 71 | _testView.appendInfo(error.message); 72 | return; 73 | } 74 | } 75 | this.flush(); 76 | _testView.appendInfo(data+" 已发送"); 77 | _testView.clearInput(); 78 | } 79 | 80 | protected function securityErrorEventHandler(event : SecurityErrorEvent) : void 81 | { 82 | _testView.appendInfo("securityErrorEventHandler: " + event+"\n"); 83 | } 84 | 85 | protected function connectHandler(evt : Event) : void 86 | { 87 | _testView.appendInfo("连接到服务器: " + _ip + "成功!\n"); 88 | 89 | } 90 | 91 | protected function closeHandler(event : Event) : void 92 | { 93 | _testView.appendInfo("与服务端断开连接: " + event+"\n"); 94 | } 95 | 96 | protected function ioErrorHandler(event : IOErrorEvent) : void 97 | { 98 | _testView.appendInfo("iOErrorEvent: " + event+"\n"); 99 | } 100 | } 101 | } -------------------------------------------------------------------------------- /tests/ASClient/src/view/TestView.as: -------------------------------------------------------------------------------- 1 | package view 2 | { 3 | import flash.display.Sprite; 4 | import flash.events.Event; 5 | import flash.events.KeyboardEvent; 6 | import flash.text.TextField; 7 | import flash.text.TextFieldType; 8 | import flash.text.TextFormat; 9 | import flash.ui.Keyboard; 10 | 11 | import socket.ClientSocket; 12 | 13 | /** 14 | * 15 | * @author Jason 16 | */ 17 | public class TestView extends Sprite 18 | { 19 | private static var _this:TestView; 20 | 21 | private var _outputPanel : TextField; 22 | private var _inputPanel : TextField; 23 | private var _clientSocket:ClientSocket; 24 | 25 | public function TestView() 26 | { 27 | this.addEventListener(Event.ADDED_TO_STAGE, initView); 28 | } 29 | 30 | protected function initView(evt : Event) : void 31 | { 32 | _outputPanel = new TextField(); 33 | _outputPanel.width = 595; 34 | _outputPanel.height = 498; 35 | _outputPanel.border = true; 36 | _outputPanel.type = TextFieldType.DYNAMIC; 37 | _outputPanel.defaultTextFormat = new TextFormat(null, 15, 0x0000FF, true); 38 | _outputPanel.wordWrap=true; 39 | this.addChild(_outputPanel); 40 | _outputPanel.x = 2; 41 | _outputPanel.y = 2; 42 | 43 | _inputPanel = new TextField(); 44 | _inputPanel.width = 595; 45 | _inputPanel.height = 85; 46 | _inputPanel.border = true; 47 | _inputPanel.type = TextFieldType.INPUT; 48 | _inputPanel.defaultTextFormat = new TextFormat(null, 15, 0x00000, true); 49 | this.addChild(_inputPanel); 50 | _inputPanel.x = 2; 51 | _inputPanel.y = 510; 52 | _inputPanel.addEventListener(KeyboardEvent.KEY_DOWN,fuSendString); 53 | 54 | _clientSocket=ClientSocket.getInstance(); 55 | } 56 | 57 | protected function fuSendString(evt:KeyboardEvent):void 58 | { 59 | if(evt.keyCode==Keyboard.ENTER) 60 | { 61 | if(_inputPanel.text!="") 62 | { 63 | _clientSocket.sendMessage(_inputPanel.text); 64 | } 65 | else 66 | { 67 | appendInfo("内容不能为空!"); 68 | } 69 | } 70 | } 71 | 72 | public function appendInfo(str:String):void 73 | { 74 | _outputPanel.appendText(str+"\n"); 75 | _outputPanel.scrollV=_outputPanel.maxScrollV; 76 | } 77 | 78 | public function clearInput():void 79 | { 80 | _inputPanel.text=""; 81 | } 82 | 83 | public static function getInstance():TestView 84 | { 85 | if(!_this) 86 | _this=new TestView(); 87 | return _this; 88 | } 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /tests/c.lua: -------------------------------------------------------------------------------- 1 | package.cpath = "./skynet/luaclib/?.so" 2 | 3 | local netpack = require "netpack" 4 | local socketdriver = require "socketdriver" 5 | 6 | local fd = socketdriver.connect("127.0.0.1", 10101) 7 | 8 | local last 9 | local result = {} 10 | 11 | local function dispatch() 12 | while true do 13 | local status 14 | status, last = socketdriver.readline(fd, result) 15 | if status == nil then 16 | error "Server closed" 17 | end 18 | if not status then 19 | break 20 | end 21 | for _, v in ipairs(result) do 22 | local session,t,str = string.match(v, "(%d+)(.)(.*)") 23 | assert(t == '-' or t == '+') 24 | session = tonumber(session) 25 | print("Response:",session, str) 26 | end 27 | end 28 | end 29 | 30 | local session = 0 31 | 32 | local function send_request(v) 33 | session = session + 1 34 | local str = string.format("%d+%s",session, v) 35 | print(str) 36 | socket.send(fd, str) 37 | print("Request:", session) 38 | end 39 | 40 | while true do 41 | dispatch() 42 | local cmd = socket.readline() 43 | if cmd then 44 | send_request(cmd) 45 | else 46 | socket.usleep(100) 47 | end 48 | end 49 | -------------------------------------------------------------------------------- /tests/client: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/forthxu/zmkp/f96bbf20263c9828629948a5e01373736f35602a/tests/client -------------------------------------------------------------------------------- /tests/client.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/forthxu/zmkp/f96bbf20263c9828629948a5e01373736f35602a/tests/client.c -------------------------------------------------------------------------------- /tests/client.lua: -------------------------------------------------------------------------------- 1 | package.cpath = "./skynet/luaclib/?.so" 2 | 3 | local socket = require "clientsocket" 4 | local cjson = require "cjson" 5 | 6 | local fd = socket.connect("127.0.0.1", 10101) 7 | 8 | local last 9 | local result = {} 10 | 11 | local function dispatch() 12 | while true do 13 | local status 14 | status, last = socket.recv(fd, last, result) 15 | if status == nil then 16 | error "Server closed" 17 | end 18 | if not status then 19 | break 20 | end 21 | for _, v in ipairs(result) do 22 | local session,t,str = string.match(v, "(%d+)(.)(.*)") 23 | assert(t == '-' or t == '+') 24 | session = tonumber(session) 25 | local result = cjson.decode(str) 26 | print("Response:",session, result[1], result[2]) 27 | end 28 | end 29 | end 30 | 31 | local session = 0 32 | 33 | local function send_request(v) 34 | session = session + 1 35 | local str = string.format("%d+%s",session, cjson.encode(v)) 36 | socket.send(fd, str) 37 | print("Request:", session) 38 | end 39 | 40 | while true do 41 | dispatch() 42 | local cmd = socket.readline() 43 | if cmd then 44 | local args = {} 45 | string.gsub(cmd, '[^ ]+', function(v) table.insert(args, v) end ) 46 | send_request(args) 47 | else 48 | socket.usleep(100) 49 | end 50 | end 51 | --------------------------------------------------------------------------------