├── .gitignore ├── LICENSE ├── README.md ├── client ├── IdcreatorClient.c ├── IdcreatorClient.h ├── IdcreatorClientMain.c ├── idcreatorclient ├── makefile ├── makefile4so └── test.sh ├── idcreator ├── Idcreator ├── Idcreator.c ├── IdcreatorConfig.c ├── IdcreatorConfig.h ├── IdcreatorCore.c ├── IdcreatorCore.h ├── IdcreatorHttpAccept.c ├── IdcreatorHttpAccept.h ├── IdcreatorProtocol.h ├── IdcreatorServerContext.c ├── IdcreatorServerContext.h ├── IdcreatorStateKeep.c ├── IdcreatorStateKeep.h ├── IdcreatorTcpAccept.c ├── IdcreatorTcpAccept.h ├── config │ └── idcreator.config └── makefile ├── lib └── libev-4.15.tar.gz └── spx ├── SpxDateTime.h ├── include ├── spx_alloc.h ├── spx_atomic.h ├── spx_collection.h ├── spx_configurtion.h ├── spx_defs.h ├── spx_env.h ├── spx_errno.h ├── spx_fixed_vector.h ├── spx_io.h ├── spx_job.h ├── spx_limits.h ├── spx_list.h ├── spx_log.h ├── spx_map.h ├── spx_message.h ├── spx_module.h ├── spx_module.hh ├── spx_network_module.h ├── spx_nio.h ├── spx_notifier_module.h ├── spx_path.h ├── spx_periodic.h ├── spx_properties.h ├── spx_queue.h ├── spx_rand.h ├── spx_skiplist.h ├── spx_socket.h ├── spx_socket_accept.h ├── spx_stdf.h ├── spx_string.h ├── spx_task.h ├── spx_task_module.h ├── spx_thread.h ├── spx_threadpool.h ├── spx_time.h ├── spx_types.h ├── spx_vector.h └── spx_web_server.h ├── spx_alloc.c ├── spx_configurtion.c ├── spx_env.c ├── spx_errno.c ├── spx_fixed_vector.c ├── spx_io.c ├── spx_job.c ├── spx_list.c ├── spx_log.c ├── spx_map.c ├── spx_message.c ├── spx_module.c ├── spx_network_module.c ├── spx_nio.c ├── spx_notifier_module.c ├── spx_path.c ├── spx_periodic.c ├── spx_properties.c ├── spx_queue.c ├── spx_rand.c ├── spx_skiplist.c ├── spx_socket.c ├── spx_socket_accept.c ├── spx_string.c ├── spx_task.c ├── spx_task_module.c ├── spx_thread.c ├── spx_threadpool.c ├── spx_time.c ├── spx_vector.c ├── spx_web_server.c └── test ├── SpxMemoryPool.c ├── SpxMemoryPool.h ├── SpxString.c ├── SpxString.h ├── log ├── makefile ├── spx_log_test.c └── spxlib_test │ └── log │ └── logtest20140529-132515.log ├── map ├── makefile ├── spx_map_test └── spx_map_test.c ├── skiplist ├── skiplist_test └── spx_skiplist_test.c ├── spx_mpool.c ├── spx_mpool.cc ├── spx_mpool.h ├── spx_nio_threadpool.c ├── spx_object.c ├── spx_object.h ├── spx_ref.c ├── spx_ref.h ├── spx_timer.c └── spx_timer.h /.gitignore: -------------------------------------------------------------------------------- 1 | # Object files 2 | *.o 3 | *.ko 4 | *.obj 5 | *.elf 6 | 7 | # Precompiled Headers 8 | *.gch 9 | *.pch 10 | 11 | # Libraries 12 | *.lib 13 | *.a 14 | *.la 15 | *.lo 16 | 17 | # Shared objects (inc. Windows DLLs) 18 | *.dll 19 | *.so 20 | *.so.* 21 | *.dylib 22 | 23 | # Executables 24 | *.exe 25 | *.out 26 | *.app 27 | *.i*86 28 | *.x86_64 29 | *.hex 30 | 31 | # Debug files 32 | *.dSYM/ 33 | *.svn 34 | *_svn 35 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016, 阅文集团 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 10 | * Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution. 13 | 14 | * Neither the name of idCreator nor the names of its 15 | contributors may be used to endorse or promote products derived from 16 | this software without specific prior written permission. 17 | 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 22 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 24 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 25 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 26 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # idCreator 2 | idCreator是我们设计并且开发一个分布式的id生成器。它主要为业务系统提供唯一、索引友好、 3 | 可排序的id。它解决了互联网行业中,使用int自增id或者是string类型的自定义id而导致的 4 | 无法方便的分库分表或者是id排序不友好问题。提到id生成器,twitter的snowflake算法 5 | 不得不被提起,但是snowflake的算法因为使用了二进制的移位做法,导致其生成的算法过 6 | 度的对于机器友好,对于人类并不是那么的友好,也就是说snowflake生成的id对于人类来 7 | 说并不能“望文生义”。 8 | 9 | 我们目前的idCreator主要提供3种规则的id: 10 | 1. 如果你喜欢snowflake算法,你一样可以选择我们的idCreator,它也有算法支持; 11 | 2. 如果你需要做数据路由,也就是分库分表操作,我们的idCreator就是生成这种带有 12 | 路由信息性质的id的,你更应该选择它; 13 | 3. 如果你仅仅自是想做排序id,那么我们的idCreator为你提供了每秒递增的id; 14 | 再不久的将来,目前已经加入了我们的计划。我们将再提供一种id: 15 | 4. 按照时间和自定义步长严格递增的id,主要用来作为状态值等使用。也可以用来做强类型 16 | 的数据路由。 17 | 18 | # idCreator安装方法 19 | 1. 下载并且安装libev 20 | 2. 下载当前idCreator源码 21 | 3. 进入idcreator目录,执行make即可 22 | 4. make执行完成后,在idcreator目录下,有idCreator可执行文件 23 | 5. 进入config文件夹,根据你实际的情况配置id生成器信息 24 | 5. 执行./idCreator config/idcreator.config 即可运行 25 | 26 | #idCreator 限制 27 | 1. 目前每台机器一秒大概可以生成10k的id 28 | 2. 目前id生成器的机器总数被限定在10台,mid从0--9 29 | 3. 目前id生成器被设计成明确生成类型的最大值为100,也就是为100种数据生成id 30 | 4. 分库的位被设计成空余了2位,也就是说最多支持一个业务被拆分成100个数据库,从0-99 31 | 5. 目前,id生成器只支持linux运行 32 | 33 | #idCreator客户端 34 | 1. C,本身idCreator就是c写的,所以它有c的客户端没有什么意外 35 | 2. java,但是目前该客户端需要和albianj一起被使用 36 | 3. http,屏蔽各种语言,idCreator支持通过http访问,其内置有web服务器, 37 | expl:http://10.97.19.58:8988/?type=1 可以使用这样的路径访问id生成器,你会得到一串 38 | json的响应:{error:0, errorMessage:success, result:4159860000002400},其中result就是你需要的id值 39 | 40 | 详细文档请查看[wiki](https://github.com/crosg/idCreator/wiki) 41 | 42 | #idCreator 文档列表和qq群:   43 | [idCreator的设计思路文档] (http://www.94geek.com/2015/idcreator.html "idCreator设计思路文档")   44 | qq群:528658887 45 | 46 | # 关于我们: 47 | 48 | 我们是阅文集团的技术团队,阅文集团于2015年成立,统一管理和运营原本属于盛大文学 49 | 和腾讯文学旗下的起点中文网、创世中文网、小说阅读网、潇湘书院、红袖添香、云起书 50 | 院、榕树下、QQ阅读、中智博文、华文天下等网文品牌。 51 | 52 | 53 | -------------------------------------------------------------------------------- /client/IdcreatorClient.c: -------------------------------------------------------------------------------- 1 | /************************************************************* 2 | * _ooOoo_ 3 | * o8888888o 4 | * 88" . "88 5 | * (| -_- |) 6 | * O\ = /O 7 | * ____/`---'\____ 8 | * .' \\| |// `. 9 | * / \\||| : |||// \ 10 | * / _||||| -:- |||||- \ 11 | * | | \\\ - /// | | 12 | * | \_| ''\---/'' | | 13 | * \ .-\__ `-` ___/-. / 14 | * ___`. .' /--.--\ `. . __ 15 | * ."" '< `.___\_<|>_/___.' >'"". 16 | * | | : `- \`.;`\ _ /`;.`/ - ` : | | 17 | * \ \ `-. \_ __\ /__ _/ .-` / / 18 | * ======`-.____`-.___\_____/___.-`____.-'====== 19 | * `=---=' 20 | * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 21 | * 佛祖保佑 永无BUG 22 | * 23 | * ========================================================================== 24 | * 25 | * this software or lib may be copied only under the terms of the gnu general 26 | * public license v3, which may be found in the source kit. 27 | * 28 | * Filename: IdcreatorClient.c 29 | * Created: 2015年04月09日 13时32分49秒 30 | * Author: Seapeak.Xu (www.94geek.com), xvhfeng@gmail.com 31 | * Company: Tencent Literature 32 | * Remark: 33 | * 34 | ***********************************************************************/ 35 | 36 | #include 37 | #include 38 | #include 39 | 40 | 41 | #include "spx_types.h" 42 | #include "spx_socket.h" 43 | #include "spx_alloc.h" 44 | #include "spx_io.h" 45 | #include "spx_message.h" 46 | 47 | #include "IdcreatorProtocol.h" 48 | 49 | struct IdcreatorClientContext{ 50 | int fd; 51 | struct spx_msg_header inheader; 52 | struct spx_msg_header outheader; 53 | char inbuf[SpxMsgHeaderSize + sizeof(u64_t)]; 54 | char outbuf[SpxMsgHeaderSize + sizeof(u32_t)]; 55 | }; 56 | 57 | u64_t idcreatorClientMakeId(char *ip,int port,int type,err_t *err){ 58 | struct IdcreatorClientContext *icc = spx_alloc_alone(sizeof(*icc),err); 59 | if(!icc) return 0; 60 | u64_t id = 0; 61 | 62 | icc->fd = spx_socket_new(err); 63 | if (*err) return 0; 64 | 65 | *err = spx_socket_set(icc->fd,true,3,3,3,false,0,false,true,2); 66 | if(*err) goto end; 67 | *err = spx_socket_connect_nb(icc->fd,ip,port,2); 68 | if(*err) goto end; 69 | 70 | icc->outheader.protocol = IdcreatorMakeId; 71 | icc->outheader.version = IdcreatorVersion; 72 | icc->outheader.bodylen = sizeof(u32_t); 73 | 74 | spx_header_pack(icc->outbuf,&(icc->outheader)); 75 | spx_msg_i2b((uchar_t *) icc->outbuf + SpxMsgHeaderSize,type); 76 | 77 | size_t len = 0; 78 | *err = spx_write_nb(icc->fd,(byte_t *) icc->outbuf,SpxMsgHeaderSize + sizeof(u32_t),&len); 79 | if(!spx_socket_read_timeout(icc->fd,20)){ 80 | *err = EBUSY; 81 | goto end; 82 | } 83 | len = 0; 84 | *err = spx_read_nb(icc->fd,(byte_t *) icc->inbuf,SpxMsgHeaderSize + sizeof(u64_t),&len); 85 | spx_header_unpack(icc->inbuf,&(icc->inheader)); 86 | if(icc->inheader.err){ 87 | *err = icc->inheader.err; 88 | goto end; 89 | } 90 | if(IdcreatorMakeId != icc->inheader.protocol){ 91 | *err = ENODEV; 92 | goto end; 93 | } 94 | id = spx_msg_b2ul((uchar_t *) icc->inbuf + SpxMsgHeaderSize); 95 | 96 | end: 97 | if(icc->fd) close(icc->fd); 98 | SpxFree(icc); 99 | return id; 100 | } 101 | 102 | -------------------------------------------------------------------------------- /client/IdcreatorClient.h: -------------------------------------------------------------------------------- 1 | /*********************************************************************** 2 | * _ooOoo_ 3 | * o8888888o 4 | * 88" . "88 5 | * (| -_- |) 6 | * O\ = /O 7 | * ____/`---'\____ 8 | * . ' \\| |// `. 9 | * / \\||| : |||// \ 10 | * / _||||| -:- |||||- \ 11 | * | | \\\ - /// | | 12 | * | \_| ''\---/'' | | 13 | * \ .-\__ `-` ___/-. / 14 | * ___`. .' /--.--\ `. . __ 15 | * ."" '< `.___\_<|>_/___.' >'"". 16 | * | | : `- \`.;`\ _ /`;.`/ - ` : | | 17 | * \ \ `-. \_ __\ /__ _/ .-` / / 18 | * ======`-.____`-.___\_____/___.-`____.-'====== 19 | * `=---=' 20 | * ............................................. 21 | * 佛祖镇楼 BUG辟易 22 | * 佛曰: 23 | * 写字楼里写字间,写字间里程序员; 24 | * 程序人员写程序,又拿程序换酒钱。 25 | * 酒醒只在网上坐,酒醉还来网下眠; 26 | * 酒醉酒醒日复日,网上网下年复年。 27 | * 但愿老死电脑间,不愿鞠躬老板前; 28 | * 奔驰宝马贵者趣,公交自行程序员。 29 | * 别人笑我忒疯癫,我笑自己命太贱; 30 | * 不见满街漂亮妹,哪个归得程序员? 31 | * ========================================================================== 32 | * 33 | * this software or lib may be copied only under the terms of the gnu general 34 | * public license v3, which may be found in the source kit. 35 | * 36 | * Filename: IdcreatorClient.h 37 | * Created: 2015年04月09日 13时37分38秒 38 | * Author: Seapeak.Xu (www.94geek.com), xvhfeng@gmail.com 39 | * Company: Tencent Literature 40 | * Remark: 41 | * 42 | ****************************************************************************/ 43 | #ifndef _IDCREATORCLIENT_H_ 44 | #define _IDCREATORCLIENT_H_ 45 | #ifdef __cplusplus 46 | extern "C" { 47 | #endif 48 | 49 | 50 | #include 51 | #include 52 | 53 | #include "spx_types.h" 54 | 55 | u64_t idcreatorClientMakeId(char *ip,int port,int type,err_t *err); 56 | 57 | #ifdef __cplusplus 58 | } 59 | #endif 60 | #endif 61 | -------------------------------------------------------------------------------- /client/IdcreatorClientMain.c: -------------------------------------------------------------------------------- 1 | /************************************************************* 2 | * _ooOoo_ 3 | * o8888888o 4 | * 88" . "88 5 | * (| -_- |) 6 | * O\ = /O 7 | * ____/`---'\____ 8 | * .' \\| |// `. 9 | * / \\||| : |||// \ 10 | * / _||||| -:- |||||- \ 11 | * | | \\\ - /// | | 12 | * | \_| ''\---/'' | | 13 | * \ .-\__ `-` ___/-. / 14 | * ___`. .' /--.--\ `. . __ 15 | * ."" '< `.___\_<|>_/___.' >'"". 16 | * | | : `- \`.;`\ _ /`;.`/ - ` : | | 17 | * \ \ `-. \_ __\ /__ _/ .-` / / 18 | * ======`-.____`-.___\_____/___.-`____.-'====== 19 | * `=---=' 20 | * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 21 | * 佛祖保佑 永无BUG 22 | * 23 | * ========================================================================== 24 | * 25 | * this software or lib may be copied only under the terms of the gnu general 26 | * public license v3, which may be found in the source kit. 27 | * 28 | * Filename: IdcreatorClientMain.c 29 | * Created: 2015年04月09日 13时40分32秒 30 | * Author: Seapeak.Xu (www.94geek.com), xvhfeng@gmail.com 31 | * Company: Tencent Literature 32 | * Remark: 33 | * 34 | ***********************************************************************/ 35 | 36 | #include 37 | #include 38 | #include 39 | 40 | #include "spx_types.h" 41 | #include "spx_thread.h" 42 | #include "spx_periodic.h" 43 | 44 | #include "IdcreatorClient.h" 45 | static char *ip = NULL; 46 | static int port = 0; 47 | 48 | static void *callback(void *arg); 49 | int main(int argc,char **argv) { 50 | if(4 != argc){ 51 | printf("please use this cmd: idcreatorclient ip port type\n"); 52 | return 0; 53 | } 54 | 55 | 56 | ip = argv[1]; 57 | port = atoi(argv[2]); 58 | int type = atoi(argv[3]); 59 | err_t err = 0; 60 | 61 | u64_t id = idcreatorClientMakeId(ip,port,type,&err); 62 | printf("%ld \n",id); 63 | 64 | // pthread_t t0 = spx_thread_new(NULL,128 * 1024,callback,&type,&err); 65 | /* 66 | pthread_t t1 = spx_thread_new(NULL,128 * 1024,callback,&type,&err); 67 | pthread_t t2 = spx_thread_new(NULL,128 * 1024,callback,&type,&err); 68 | pthread_t t3 = spx_thread_new(NULL,128 * 1024,callback,&type,&err); 69 | pthread_t t4 = spx_thread_new(NULL,128 * 1024,callback,&type,&err); 70 | pthread_t t5 = spx_thread_new(NULL,128 * 1024,callback,&type,&err); 71 | pthread_t t6 = spx_thread_new(NULL,128 * 1024,callback,&type,&err); 72 | pthread_t t7 = spx_thread_new(NULL,128 * 1024,callback,&type,&err); 73 | pthread_t t8 = spx_thread_new(NULL,128 * 1024,callback,&type,&err); 74 | pthread_t t9 = spx_thread_new(NULL,128 * 1024,callback,&type,&err); 75 | */ 76 | 77 | // spx_periodic_sleep(600000,0); 78 | return 0; 79 | } 80 | 81 | static void *callback(void *arg){ 82 | err_t err = 0; 83 | int *type = (int *) arg; 84 | while(true) { 85 | u64_t id = idcreatorClientMakeId(ip,port,*type,&err); 86 | printf("%ld \n",id); 87 | } 88 | return NULL; 89 | } 90 | 91 | 92 | 93 | -------------------------------------------------------------------------------- /client/idcreatorclient: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/crosg/idCreator/d4873281d18365ffad3f87dc49a8b01e4a19fc1f/client/idcreatorclient -------------------------------------------------------------------------------- /client/makefile: -------------------------------------------------------------------------------- 1 | .SUFFIXES: .c .o 2 | 3 | SPX_SRC_PATH = ../spx/ 4 | SPX_HEADER_PATH = ../spx/include/ 5 | YDB_COMMON_HEADER_PATH = ../idcreator/ 6 | TARGET = idcreatorclient 7 | TARGET_PATH = /usr/local/bin 8 | HEADER_TARGET_PATH = /usr/local/include/ 9 | 10 | CC = gcc 11 | CFLAG = -pipe -o0 -W -Wall -Werror -g \ 12 | -Wpointer-arith -Wno-unused-parameter -Wno-unused-function -Wno-unused-variable -Wunused-value 13 | INC_PATH = -I/usr/local/include -I/usr/include \ 14 | -I$(SPX_HEADER_PATH) \ 15 | -I$(YDB_COMMON_HEADER_PATH) 16 | LIB_PATH = -L/usr/local/lib -lev -lpthread 17 | DEFS := 18 | 19 | SYS := $(strip $(shell uname -s | tr '[:upper:]' '[:lower:]')) 20 | ifeq ($(SYS), linux) 21 | DEFS += -DSpxLinux 22 | else 23 | ifeq ($(SYS),unix) 24 | DEFS += -DSpxUnix 25 | else 26 | ifeq ($(SYS),darwin) 27 | DEFS += -DSpxMac 28 | endif 29 | endif 30 | endif 31 | 32 | BITS := $(findstring 64,$(shell uname -m )) 33 | ifeq (64, $(BITS)) 34 | DEFS += -DSpx64 35 | else 36 | ifeq (32, $(BITS)) 37 | DEFS += -DSpx32 38 | else 39 | DEFS += -DSpx32 40 | endif 41 | endif 42 | 43 | SRC_FILE = $(wildcard *.c) \ 44 | $(wildcard $(SPX_SRC_PATH)/*.c) 45 | 46 | HEADER_FILE = $(wildcard *.h) \ 47 | $(wildcard $(SPX_HEADER_PATH)/*.h) 48 | 49 | SHARED_OBJS = $(patsubst %.c,%.o,$(SRC_FILE) ) 50 | 51 | all:$(TARGET) 52 | 53 | %.o: %.c 54 | $(CC) $(CFLAG) -c $< -o $@ $(LIB_PATH) $(INC_PATH) $(DEFS) 55 | 56 | $(TARGET) : $(SHARED_OBJS) 57 | $(CC) $(CFLAG) $(SHARED_OBJS) $(LIB_PATH) $(INC_PATH) -o $(TARGET) $(DEFS) 58 | 59 | install: 60 | cp -f $(TARGET) $(TARGET_PATH) 61 | cp -f $(HEADER_FILE) $(HEADER_TARGET_PATH) 62 | clean: 63 | rm -f $(SHARED_OBJS) $(TARGET) 64 | uninstall: 65 | cd $(TARGET_PATH) 66 | rm -f $(TARGET) 67 | cd $(HEADER_TARGET_PATH) 68 | rm -f $(HEADER_FILE) 69 | 70 | 71 | -------------------------------------------------------------------------------- /client/makefile4so: -------------------------------------------------------------------------------- 1 | .SUFFIXES: .c .o 2 | 3 | #type means the progame type 4 | #if type is so means the .so file 5 | #if type is exe means the execute file 6 | TYPE=so 7 | SPX_SRC_PATH = ../spx/ 8 | SPX_HEADER_PATH = ../spx/include/ 9 | YDB_COMMON_HEADER_PATH = ../idcreator 10 | NAME = idcreatorclient 11 | TARGET_PATH = /usr/local/lib/ 12 | HEADER_TARGET_PATH = /usr/local/include/ 13 | 14 | CC = gcc 15 | CFLAG = -pipe -o0 -W -Wall -Werror -g \ 16 | -Wpointer-arith -Wno-unused-parameter -Wno-unused-function -Wno-unused-variable -Wunused-value 17 | INC_PATH = -I/usr/local/include \ 18 | -I$(SPX_HEADER_PATH) \ 19 | -I$(YDB_COMMON_HEADER_PATH) 20 | 21 | LIB_PATH = -L/usr/local/lib -lev -lpthread 22 | 23 | DEFS := 24 | TARGET := 25 | 26 | 27 | ifeq ($(TYPE),so) 28 | CFLAG += -fPIC -shared -DPIC 29 | TARGET = $(addprefix lib,$(addsuffix .so,$(NAME))) 30 | else 31 | TARGET = $(NAME) 32 | endif 33 | 34 | SYS := $(strip $(shell uname -s | tr '[:upper:]' '[:lower:]')) 35 | ifeq ($(SYS), linux) 36 | DEFS += -DSpxLinux 37 | else 38 | ifeq ($(SYS),unix) 39 | DEFS += -DSpxUnix 40 | else 41 | ifeq ($(SYS),darwin) 42 | DEFS += -DSpxMac 43 | endif 44 | endif 45 | endif 46 | 47 | BITS := $(findstring 64,$(shell uname -m )) 48 | ifeq (64, $(BITS)) 49 | DEFS += -DSpx64 50 | else 51 | ifeq (32, $(BITS)) 52 | DEFS += -DSpx32 53 | else 54 | DEFS += -DSpx32 55 | endif 56 | endif 57 | 58 | SRC_FILE = $(wildcard *.c) \ 59 | $(wildcard $(SPX_SRC_PATH)/*.c) 60 | 61 | HEADER_FILE = $(wildcard *.h) \ 62 | $(wildcard $(SPX_HEADER_PATH)/*.h) 63 | 64 | SHARED_OBJS = $(patsubst %.c,%.o,$(SRC_FILE) ) 65 | 66 | all:$(TARGET) 67 | 68 | %.o: %.c 69 | $(CC) $(CFLAG) -c $< -o $@ $(LIB_PATH) $(INC_PATH) $(DEFS) 70 | 71 | $(TARGET) : $(SHARED_OBJS) 72 | $(CC) $(CFLAG) $(SHARED_OBJS) $(LIB_PATH) $(INC_PATH) -o $(TARGET) $(DEFS) 73 | 74 | install: 75 | cp -f $(TARGET) $(TARGET_PATH) 76 | cp -f $(HEADER_FILE) $(HEADER_TARGET_PATH) 77 | clean: 78 | rm -f $(SHARED_OBJS) $(TARGET) 79 | uninstall: 80 | cd $(TARGET_PATH) 81 | rm -f $(TARGET) 82 | cd $(HEADER_TARGET_PATH) 83 | rm -f $(HEADER_FILE) 84 | 85 | 86 | -------------------------------------------------------------------------------- /client/test.sh: -------------------------------------------------------------------------------- 1 | for((j=0;j<1000000;j++)) 2 | do 3 | MAX=2048 4 | for((i=0;i_/___.' >'"". 16 | * | | : `- \`.;`\ _ /`;.`/ - ` : | | 17 | * \ \ `-. \_ __\ /__ _/ .-` / / 18 | * ======`-.____`-.___\_____/___.-`____.-'====== 19 | * `=---=' 20 | * ............................................. 21 | * 佛祖镇楼 BUG辟易 22 | * 佛曰: 23 | * 写字楼里写字间,写字间里程序员; 24 | * 程序人员写程序,又拿程序换酒钱。 25 | * 酒醒只在网上坐,酒醉还来网下眠; 26 | * 酒醉酒醒日复日,网上网下年复年。 27 | * 但愿老死电脑间,不愿鞠躬老板前; 28 | * 奔驰宝马贵者趣,公交自行程序员。 29 | * 别人笑我忒疯癫,我笑自己命太贱; 30 | * 不见满街漂亮妹,哪个归得程序员? 31 | * ========================================================================== 32 | * 33 | * this software or lib may be copied only under the terms of the gnu general 34 | * public license v3, which may be found in the source kit. 35 | * 36 | * Filename: IdcreatorConfig.h 37 | * Created: 2015年03月03日 11时25分07秒 38 | * Author: Seapeak.Xu (www.94geek.com), xvhfeng@gmail.com 39 | * Company: Tencent Literature 40 | * Remark: 41 | * 42 | ****************************************************************************/ 43 | #ifndef _idcreatorCONFIG_H_ 44 | #define _idcreatorCONFIG_H_ 45 | #ifdef __cplusplus 46 | extern "C" { 47 | #endif 48 | 49 | 50 | #include 51 | #include 52 | 53 | #include "spx_types.h" 54 | 55 | struct IdcreatorConfig{ 56 | SpxLogDelegate *log; 57 | string_t ip; 58 | int port; 59 | int http_port; 60 | 61 | //web server 62 | int new_http_port; 63 | int retry_times; 64 | int context_pool_size; 65 | 66 | u32_t connectTimeout; 67 | u64_t stackSize; 68 | u32_t serverContextSize; 69 | string_t logpath; 70 | string_t logprefix; 71 | string_t id_state_file; 72 | u64_t logsize; 73 | i32_t loglevel; 74 | bool_t daemon; 75 | u32_t waitting; 76 | u32_t timeout; 77 | u32_t mid; 78 | }; 79 | 80 | 81 | void *idcreatorConfigInit(SpxLogDelegate *log,err_t *err); 82 | void idcreatorConfigParser(string_t line,void *config,err_t *err); 83 | 84 | 85 | 86 | #ifdef __cplusplus 87 | } 88 | #endif 89 | #endif 90 | -------------------------------------------------------------------------------- /idcreator/IdcreatorCore.c: -------------------------------------------------------------------------------- 1 | /************************************************************* 2 | * _ooOoo_ 3 | * o8888888o 4 | * 88" . "88 5 | * (| -_- |) 6 | * O\ = /O 7 | * ____/`---'\____ 8 | * .' \\| |// `. 9 | * / \\||| : |||// \ 10 | * / _||||| -:- |||||- \ 11 | * | | \\\ - /// | | 12 | * | \_| ''\---/'' | | 13 | * \ .-\__ `-` ___/-. / 14 | * ___`. .' /--.--\ `. . __ 15 | * ."" '< `.___\_<|>_/___.' >'"". 16 | * | | : `- \`.;`\ _ /`;.`/ - ` : | | 17 | * \ \ `-. \_ __\ /__ _/ .-` / / 18 | * ======`-.____`-.___\_____/___.-`____.-'====== 19 | * `=---=' 20 | * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 21 | * 佛祖保佑 永无BUG 22 | * 23 | * ========================================================================== 24 | * 25 | * this software or lib may be copied only under the terms of the gnu general 26 | * public license v3, which may be found in the source kit. 27 | * 28 | * Filename: idcreatorDispatcher.c 29 | * Created: 2015年03月03日 15时38分56秒 30 | * Author: Seapeak.Xu (www.94geek.com), xvhfeng@gmail.com 31 | * Company: Tencent Literature 32 | * Remark: 33 | * 34 | ***********************************************************************/ 35 | 36 | #include 37 | #include 38 | #include 39 | 40 | #include "spx_types.h" 41 | #include "spx_string.h" 42 | #include "spx_defs.h" 43 | #include "spx_log.h" 44 | #include "spx_module.h" 45 | #include "spx_alloc.h" 46 | #include "spx_periodic.h" 47 | #include "spx_atomic.h" 48 | #include "spx_time.h" 49 | 50 | #include "IdcreatorConfig.h" 51 | #include "IdcreatorServerContext.h" 52 | #include "IdcreatorProtocol.h" 53 | #include "IdcreatorStateKeep.h" 54 | 55 | 56 | //static u32_t bitSeed = 0; 57 | static u32_t seed[0x400] = {0}; 58 | volatile int seed_99 = 0; 59 | volatile int seed_98 = 0; 60 | volatile u32_t curr_time = 0; 61 | 62 | u64_t IdGenerator(int type, int mid, err_t *err){ 63 | volatile u64_t id = 0l; 64 | time_t token = spx_get_token(); 65 | switch(type){ 66 | case 0x4FF ://specific one,for contract in yuewen company 67 | { 68 | int days = spx_get_tokendays(); 69 | volatile int next = 0; 70 | do{ 71 | if(10000 <= contract_next) 72 | SpxAtomicVCas(contract_next,10000,0); 73 | next = SpxAtomicLazyVIncr(contract_next); 74 | } while(10000 <= contract_next); 75 | idcreator_statf_sync(next); 76 | id =(u64_t) (days * pow(10,5) + next * 10 + mid); 77 | break; 78 | } 79 | case 0://book 80 | case 1://author 81 | case 2://configurtion 82 | case 50:// for qidian 83 | { 84 | //free the last 2 bit,for client use by itself 85 | volatile int next = 0; 86 | do{ 87 | if(10000 <= seed[type]) 88 | SpxAtomicVCas(seed[type],10000,0); 89 | next = SpxAtomicLazyVIncr(seed[type]); 90 | } while(10000 <= seed[type]); 91 | id =(u64_t) ((((u64_t) (0xFFFFFFFF & token)) * pow(10,8)) 92 | + (((u64_t) (0xF & mid)) * pow(10,6)) 93 | + next * 100); 94 | break; 95 | } 96 | case 98:{ 97 | volatile int next = 0; 98 | do{ 99 | if(curr_time == token){ 100 | next = __sync_add_and_fetch(&seed_98,1); 101 | } else { 102 | SpxAtomicVSet(curr_time,token); 103 | SpxAtomicVSet(seed_98,0); 104 | } 105 | } while(10000 <= seed_98); 106 | id =(u64_t) ((((u64_t) (0xFFFFFFFF & token)) * pow(10,8)) 107 | + (((u64_t) (0xF & mid)) * pow(10,6)) 108 | + (u64_t) (type * pow(10,4))) 109 | + (u64_t) next ; 110 | break; 111 | } 112 | case 99:{//incr in the type,but now not used 113 | volatile int next = 0; 114 | do{ 115 | if(10000 <= seed_99) 116 | SpxAtomicVCas(seed_99,10000,0); 117 | next = SpxAtomicLazyVIncr(seed_99); 118 | } while(10000 <= seed_99); 119 | id =(u64_t) ((((u64_t) (0xFFFFFFFF & token)) * pow(10,8)) 120 | + (((u64_t) (0xF & mid)) * pow(10,6)) 121 | + (type * pow(10,4))) 122 | + next ; 123 | break; 124 | } 125 | default://all other 126 | { 127 | volatile int next = 0; 128 | do{ 129 | if(0x3FFF <= seed[type]) 130 | SpxAtomicVCas(seed[type],0x3fff,0); 131 | next = SpxAtomicLazyVIncr(seed[type]); 132 | } while(0x3FFF <= seed[type]); 133 | id = (u64_t) ( 134 | ((u64_t) (0xFFFFFFFF & token) << 28) 135 | | (((u64_t) (0xF & mid)) << 24 ) 136 | | (((u64_t) (0x3FF & type)) << 14) 137 | | (((u64_t) (0x3FFF & next))) 138 | ); 139 | break; 140 | } 141 | } 142 | 143 | return id; 144 | } 145 | 146 | 147 | 148 | 149 | -------------------------------------------------------------------------------- /idcreator/IdcreatorCore.h: -------------------------------------------------------------------------------- 1 | /*********************************************************************** 2 | * _ooOoo_ 3 | * o8888888o 4 | * 88" . "88 5 | * (| -_- |) 6 | * O\ = /O 7 | * ____/`---'\____ 8 | * . ' \\| |// `. 9 | * / \\||| : |||// \ 10 | * / _||||| -:- |||||- \ 11 | * | | \\\ - /// | | 12 | * | \_| ''\---/'' | | 13 | * \ .-\__ `-` ___/-. / 14 | * ___`. .' /--.--\ `. . __ 15 | * ."" '< `.___\_<|>_/___.' >'"". 16 | * | | : `- \`.;`\ _ /`;.`/ - ` : | | 17 | * \ \ `-. \_ __\ /__ _/ .-` / / 18 | * ======`-.____`-.___\_____/___.-`____.-'====== 19 | * `=---=' 20 | * ............................................. 21 | * 佛祖镇楼 BUG辟易 22 | * 佛曰: 23 | * 写字楼里写字间,写字间里程序员; 24 | * 程序人员写程序,又拿程序换酒钱。 25 | * 酒醒只在网上坐,酒醉还来网下眠; 26 | * 酒醉酒醒日复日,网上网下年复年。 27 | * 但愿老死电脑间,不愿鞠躬老板前; 28 | * 奔驰宝马贵者趣,公交自行程序员。 29 | * 别人笑我忒疯癫,我笑自己命太贱; 30 | * 不见满街漂亮妹,哪个归得程序员? 31 | * ========================================================================== 32 | * 33 | * this software or lib may be copied only under the terms of the gnu general 34 | * public license v3, which may be found in the source kit. 35 | * 36 | * Filename: IdcreatorDispatcher.h 37 | * Created: 2015年03月03日 15时38分51秒 38 | * Author: Seapeak.Xu (www.94geek.com), xvhfeng@gmail.com 39 | * Company: Tencent Literature 40 | * Remark: 41 | * 42 | ****************************************************************************/ 43 | #ifndef _idcreatorDISPATCHER_H_ 44 | #define _idcreatorDISPATCHER_H_ 45 | #ifdef __cplusplus 46 | extern "C" { 47 | #endif 48 | 49 | 50 | #include 51 | #include 52 | #include 53 | 54 | #include "IdcreatorServerContext.h" 55 | 56 | 57 | u64_t IdGenerator(int type, int mid, err_t *err); 58 | 59 | #ifdef __cplusplus 60 | } 61 | #endif 62 | #endif 63 | -------------------------------------------------------------------------------- /idcreator/IdcreatorHttpAccept.h: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: ydb_tracker_monitor.h 3 | > Author: shuaixiang 4 | > Mail: 5 | > Created Time: Fri 27 Nov 2015 08:11:59 AM UTC 6 | ************************************************************************/ 7 | #ifndef _YDB_TRACKER_MONITOR_H_ 8 | #define _YDB_TRACKER_BALANCE_H_ 9 | #ifdef __cplusplus 10 | extern "C"{ 11 | #endif 12 | 13 | #include "spx_types.h" 14 | #include "spx_io.h" 15 | #include "IdcreatorConfig.h" 16 | 17 | struct param_node{ 18 | char key[100]; 19 | char value[100]; 20 | struct param_node *next_param; 21 | }; 22 | 23 | struct monitor_http_context{ 24 | struct IdcreatorConfig *c; 25 | char life_cycle; 26 | int fd; 27 | ev_tstamp timeout; 28 | ev_async async_watcher; 29 | struct monitor_http_context *p_nextCTX; 30 | struct monitor_http_context *p_preCTX; 31 | 32 | //request 33 | size_t req_retry; 34 | size_t req_len; 35 | size_t req_size; 36 | byte_t *request; 37 | struct param_node *param_head; 38 | struct param_node *param_tail; 39 | 40 | //response 41 | size_t resp_retry; 42 | size_t resp_size; 43 | size_t split_size; 44 | byte_t *response; 45 | size_t resp_len; 46 | }; 47 | 48 | pthread_t NewIdCreatorHttp(SpxLogDelegate *log, struct IdcreatorConfig *c, err_t *err); 49 | 50 | //pthread_t ydb_tracker_monitor_thread_new(SpxLogDelegate *log, struct ydb_tracker_configurtion *c, err_t *err); 51 | // 52 | //spx_private struct param_node* InsertParam(struct monitor_http_context* mtr_ctx, char *key, int key_len, char *val, int val_len); 53 | // 54 | //spx_private struct param_node* GetParam(struct monitor_http_context *mtr_ctx, char *key); 55 | // 56 | //spx_private err_t FreeParam(struct monitor_http_context *mtr_ctx); 57 | // 58 | //spx_private void * ydb_tracker_monitor_mainsocket_create(void *arg); 59 | // 60 | //spx_private err_t NewMonitorCTXPool(int pool_size); 61 | // 62 | //spx_private void FreeMonitorCTXPool(); 63 | // 64 | //spx_private void ydb_tracker_monitor_io_watcher_register(ev_io *watcher ,int fd, void (*cb)(struct ev_loop *loop, ev_io *watcher, int revents), int revents, void *data); 65 | // 66 | //spx_private void ydb_tracker_monitor_socket_response_SendResponse(EV_P_ ev_async *watcher, int revents); 67 | // 68 | //spx_private void ydb_tracker_monitor_async_watcher_register(ev_async *watcher, void(*cb)(struct ev_loop *loop, ev_async *watcher, int revents), void *data); 69 | // 70 | //spx_private void ydb_tracker_monitor_socket_main_reciver(struct ev_loop *loop, ev_io *watcher, int revents); 71 | // 72 | //spx_private void ydb_tracker_monitor_socket_request(struct ev_loop *loop, ev_async *watcher, int revents); 73 | // 74 | //spx_private int ydb_tracker_monitor_socket_request_GetRequest_ReadRequest_IsEnd(byte_t* buf, size_t len); 75 | // 76 | //spx_private err_t ydb_tracker_monitor_socket_request_GetRequest_ReadRequest(int fd, byte_t *buf, size_t *len); 77 | // 78 | //spx_private void ydb_tracker_monitor_socket_request_GetRequest(int revents, void *arg); 79 | // 80 | //spx_private void ydb_tracker_monitor_socket_parser(EV_P_ ev_async *watcher, int revents); 81 | // 82 | //spx_private void bad_request(int client); 83 | // 84 | //spx_private void headers(int client, int content_length); 85 | // 86 | //spx_private void unimplemented(int client); 87 | // 88 | //spx_private struct storage_list *ydb_tracker_monitor_socket_response_visitor(string_t groupname); 89 | // 90 | //spx_private void ydb_tracker_monitor_socket_response(EV_P_ ev_async *watcher, int revents); 91 | // 92 | //spx_private char * ydb_tracker_monitor_socket_response_Storage2JSON(struct ydb_remote_storage * storage); 93 | // 94 | //spx_private void ydb_tracker_monitor_socket_response_FreeStorageList(struct storage_list *list); 95 | // 96 | //spx_private void RequestFinish(struct monitor_http_context *mtr_ctx); 97 | // 98 | //spx_private void RequestException(struct monitor_http_context * mtr_ctx); 99 | // 100 | //spx_private void CloseMonitor(struct monitor_http_context *mtr_ctx); 101 | 102 | #ifdef _cplusplus 103 | } 104 | #endif 105 | #endif 106 | -------------------------------------------------------------------------------- /idcreator/IdcreatorProtocol.h: -------------------------------------------------------------------------------- 1 | /*********************************************************************** 2 | * _ooOoo_ 3 | * o8888888o 4 | * 88" . "88 5 | * (| -_- |) 6 | * O\ = /O 7 | * ____/`---'\____ 8 | * . ' \\| |// `. 9 | * / \\||| : |||// \ 10 | * / _||||| -:- |||||- \ 11 | * | | \\\ - /// | | 12 | * | \_| ''\---/'' | | 13 | * \ .-\__ `-` ___/-. / 14 | * ___`. .' /--.--\ `. . __ 15 | * ."" '< `.___\_<|>_/___.' >'"". 16 | * | | : `- \`.;`\ _ /`;.`/ - ` : | | 17 | * \ \ `-. \_ __\ /__ _/ .-` / / 18 | * ======`-.____`-.___\_____/___.-`____.-'====== 19 | * `=---=' 20 | * ............................................. 21 | * 佛祖镇楼 BUG辟易 22 | * 佛曰: 23 | * 写字楼里写字间,写字间里程序员; 24 | * 程序人员写程序,又拿程序换酒钱。 25 | * 酒醒只在网上坐,酒醉还来网下眠; 26 | * 酒醉酒醒日复日,网上网下年复年。 27 | * 但愿老死电脑间,不愿鞠躬老板前; 28 | * 奔驰宝马贵者趣,公交自行程序员。 29 | * 别人笑我忒疯癫,我笑自己命太贱; 30 | * 不见满街漂亮妹,哪个归得程序员? 31 | * ========================================================================== 32 | * 33 | * this software or lib may be copied only under the terms of the gnu general 34 | * public license v3, which may be found in the source kit. 35 | * 36 | * Filename: IdcreatorProtocol.h 37 | * Created: 2015年04月08日 15时29分23秒 38 | * Author: Seapeak.Xu (www.94geek.com), xvhfeng@gmail.com 39 | * Company: Tencent Literature 40 | * Remark: 41 | * 42 | ****************************************************************************/ 43 | #ifndef _IDCREATORPROTOCOL_H_ 44 | #define _IDCREATORPROTOCOL_H_ 45 | #ifdef __cplusplus 46 | extern "C" { 47 | #endif 48 | 49 | 50 | #include 51 | #include 52 | 53 | #define IdcreatorVersion 0x01 54 | #define IdcreatorMakeId 0x01 55 | 56 | #ifdef __cplusplus 57 | } 58 | #endif 59 | #endif 60 | -------------------------------------------------------------------------------- /idcreator/IdcreatorServerContext.c: -------------------------------------------------------------------------------- 1 | /************************************************************* 2 | * _ooOoo_ 3 | * o8888888o 4 | * 88" . "88 5 | * (| -_- |) 6 | * O\ = /O 7 | * ____/`---'\____ 8 | * .' \\| |// `. 9 | * / \\||| : |||// \ 10 | * / _||||| -:- |||||- \ 11 | * | | \\\ - /// | | 12 | * | \_| ''\---/'' | | 13 | * \ .-\__ `-` ___/-. / 14 | * ___`. .' /--.--\ `. . __ 15 | * ."" '< `.___\_<|>_/___.' >'"". 16 | * | | : `- \`.;`\ _ /`;.`/ - ` : | | 17 | * \ \ `-. \_ __\ /__ _/ .-` / / 18 | * ======`-.____`-.___\_____/___.-`____.-'====== 19 | * `=---=' 20 | * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 21 | * 佛祖保佑 永无BUG 22 | * 23 | * ========================================================================== 24 | * 25 | * this software or lib may be copied only under the terms of the gnu general 26 | * public license v3, which may be found in the source kit. 27 | * 28 | * Filename: IdcreatorServerContext.c 29 | * Created: 2015年03月03日 09时59分41秒 30 | * Author: Seapeak.Xu (www.94geek.com), xvhfeng@gmail.com 31 | * Company: Tencent Literature 32 | * Remark: 33 | * 34 | ***********************************************************************/ 35 | 36 | #include 37 | #include 38 | #include 39 | #include 40 | #include 41 | #include 42 | #include 43 | #include 44 | #include 45 | #include 46 | #include 47 | #include 48 | #include 49 | #include 50 | #include 51 | #include 52 | 53 | #include "spx_types.h" 54 | #include "spx_string.h" 55 | #include "spx_defs.h" 56 | #include "spx_socket.h" 57 | #include "spx_module.h" 58 | #include "spx_job.h" 59 | #include "spx_time.h" 60 | #include "spx_io.h" 61 | #include "spx_alloc.h" 62 | 63 | #include "IdcreatorServerContext.h" 64 | #include "IdcreatorConfig.h" 65 | 66 | struct IdcreatorServerContextPool *gIdcreatorServerContextPool = NULL; 67 | 68 | spx_private void *idcreatorServerContextNew(size_t idx,void *arg,err_t *err); 69 | spx_private err_t idcreatorServerContextFree(void **arg); 70 | spx_private void idcreatorServerContextClear(struct IdcreatorServerContext *isc); 71 | 72 | spx_private void *idcreatorServerContextNew(size_t idx,void *arg,err_t *err){/*{{{*/ 73 | SpxTypeConvert2(struct IdcreatorServerContextTransport,isct,arg); 74 | struct IdcreatorServerContext *isc = NULL; 75 | isc = spx_alloc_alone(sizeof(*isc),err); 76 | if(NULL == isc){ 77 | return NULL; 78 | } 79 | isc->log = isct->log; 80 | isc->idx = idx; 81 | isc->c = isct->c; 82 | isc->inHandler = isct->inHandler; 83 | isc->outHandler = isct->outHandler; 84 | isc->inlen = SpxMsgHeaderSize + sizeof(i32_t); 85 | isc->outlen = SpxMsgHeaderSize + sizeof(u64_t); 86 | return isc; 87 | 88 | }/*}}}*/ 89 | 90 | spx_private err_t idcreatorServerContextFree(void **arg){/*{{{*/ 91 | struct IdcreatorServerContext **isc = (struct IdcreatorServerContext **) arg; 92 | idcreatorServerContextClear(*isc); 93 | SpxFree(*isc); 94 | return 0; 95 | }/*}}}*/ 96 | 97 | spx_private void idcreatorServerContextClear(struct IdcreatorServerContext *isc){/*{{{*/ 98 | if(NULL != isc->client_ip){ 99 | SpxStringFree(isc->client_ip); 100 | } 101 | 102 | isc->err = 0; 103 | isc->moore = SpxNioMooreNormal; 104 | isc->loop = NULL; 105 | memset(isc->inbuf,0,SpxMsgHeaderSize + sizeof(i32_t)); 106 | memset(isc->outbuf,0,SpxMsgHeaderSize + sizeof(u64_t)); 107 | memset(&(isc->inheader),0,sizeof(isc->inheader)); 108 | memset(&(isc->outheader),0,sizeof(isc->outheader)); 109 | isc->offset = 0; 110 | if(0 < isc->fd){ 111 | SpxClose(isc->fd); 112 | } 113 | }/*}}}*/ 114 | 115 | 116 | void idcreatorServerContextReset(struct IdcreatorServerContext *isc){ 117 | } 118 | 119 | 120 | struct IdcreatorServerContextPool *idcreatorServerContextPoolNew(SpxLogDelegate *log,\ 121 | void *c, 122 | size_t size, 123 | IdcreatorWatcherDelegate *inHandler, 124 | IdcreatorWatcherDelegate *outHandler, 125 | err_t *err){/*{{{*/ 126 | if(0 == size){ 127 | *err = EINVAL; 128 | } 129 | struct IdcreatorServerContextPool *pool = NULL; 130 | pool = spx_alloc_alone(sizeof(*pool),err); 131 | if(NULL == pool){ 132 | return NULL; 133 | } 134 | 135 | struct IdcreatorServerContextTransport arg; 136 | SpxZero(arg); 137 | arg.log = log; 138 | arg.c = c; 139 | arg.inHandler = inHandler; 140 | arg.outHandler = outHandler; 141 | 142 | pool->pool = spx_fixed_vector_new(log,size,\ 143 | idcreatorServerContextNew,\ 144 | &arg,\ 145 | idcreatorServerContextFree,\ 146 | err); 147 | 148 | if(NULL == pool->pool){ 149 | SpxFree(pool); 150 | return NULL; 151 | } 152 | return pool; 153 | }/*}}}*/ 154 | 155 | struct IdcreatorServerContext *idcreatorServerContextPoolPop(struct IdcreatorServerContextPool *pool,err_t *err){/*{{{*/ 156 | struct IdcreatorServerContext *isc = spx_fixed_vector_pop(pool->pool,err); 157 | if(NULL == isc){ 158 | *err = 0 == *err ? ENOENT : *err; 159 | return NULL; 160 | } 161 | return isc; 162 | }/*}}}*/ 163 | 164 | err_t idcreatorServerContextPoolPush(struct IdcreatorServerContextPool *pool,struct IdcreatorServerContext *isc){/*{{{*/ 165 | idcreatorServerContextClear(isc); 166 | return spx_fixed_vector_push(pool->pool,isc); 167 | }/*}}}*/ 168 | 169 | err_t idcreatorServerContextPoolFree(struct IdcreatorServerContextPool **pool){/*{{{*/ 170 | err_t err = 0; 171 | err = spx_fixed_vector_free(&((*pool)->pool)); 172 | SpxFree(*pool); 173 | return err; 174 | }/*}}}*/ 175 | 176 | 177 | 178 | -------------------------------------------------------------------------------- /idcreator/IdcreatorServerContext.h: -------------------------------------------------------------------------------- 1 | /*********************************************************************** 2 | * _ooOoo_ 3 | * o8888888o 4 | * 88" . "88 5 | * (| -_- |) 6 | * O\ = /O 7 | * ____/`---'\____ 8 | * . ' \\| |// `. 9 | * / \\||| : |||// \ 10 | * / _||||| -:- |||||- \ 11 | * | | \\\ - /// | | 12 | * | \_| ''\---/'' | | 13 | * \ .-\__ `-` ___/-. / 14 | * ___`. .' /--.--\ `. . __ 15 | * ."" '< `.___\_<|>_/___.' >'"". 16 | * | | : `- \`.;`\ _ /`;.`/ - ` : | | 17 | * \ \ `-. \_ __\ /__ _/ .-` / / 18 | * ======`-.____`-.___\_____/___.-`____.-'====== 19 | * `=---=' 20 | * ............................................. 21 | * 佛祖镇楼 BUG辟易 22 | * 佛曰: 23 | * 写字楼里写字间,写字间里程序员; 24 | * 程序人员写程序,又拿程序换酒钱。 25 | * 酒醒只在网上坐,酒醉还来网下眠; 26 | * 酒醉酒醒日复日,网上网下年复年。 27 | * 但愿老死电脑间,不愿鞠躬老板前; 28 | * 奔驰宝马贵者趣,公交自行程序员。 29 | * 别人笑我忒疯癫,我笑自己命太贱; 30 | * 不见满街漂亮妹,哪个归得程序员? 31 | * ========================================================================== 32 | * 33 | * this software or lib may be copied only under the terms of the gnu general 34 | * public license v3, which may be found in the source kit. 35 | * 36 | * Filename: IdcreatorServerContext.h 37 | * Created: 2015年03月03日 09时59分39秒 38 | * Author: Seapeak.Xu (www.94geek.com), xvhfeng@gmail.com 39 | * Company: Tencent Literature 40 | * Remark: 41 | * 42 | ****************************************************************************/ 43 | #ifndef _idcreatorSERVERCONTEXT_H_ 44 | #define _idcreatorSERVERCONTEXT_H_ 45 | #ifdef __cplusplus 46 | extern "C" { 47 | #endif 48 | 49 | 50 | #include 51 | #include 52 | #include 53 | 54 | #include "spx_types.h" 55 | #include "spx_defs.h" 56 | #include "spx_message.h" 57 | #include "spx_properties.h" 58 | #include "spx_module.h" 59 | 60 | #define IdcreatorMooreNormal 0 61 | #define IdcreatorMooreIn 1 62 | #define IdcreatorMooreOut 2 63 | 64 | struct IdcreatorServerContext; 65 | 66 | typedef void IdcreatorWatcherDelegate(int revents,void *arg); 67 | typedef void IdcreatorDispatcherDelegate(struct ev_loop *loop, 68 | int idx,struct IdcreatorServerContext *tsc); 69 | 70 | struct IdcreatorServerContextTransport{ 71 | SpxLogDelegate *log; 72 | void *c; 73 | IdcreatorWatcherDelegate *inHandler; 74 | IdcreatorWatcherDelegate *outHandler; 75 | }; 76 | 77 | struct IdcreatorServerContext{ 78 | SpxLogDelegate *log; 79 | struct ev_async async; 80 | int fd; 81 | int use; 82 | size_t idx; 83 | err_t err; 84 | IdcreatorWatcherDelegate *inHandler; 85 | IdcreatorWatcherDelegate *outHandler; 86 | // IdcreatorDispatcherDelegate *dispatcher; 87 | u32_t moore; 88 | struct ev_loop *loop; 89 | string_t client_ip; 90 | void *c; 91 | 92 | struct spx_msg_header inheader; 93 | struct spx_msg_header outheader; 94 | 95 | char inbuf[SpxMsgHeaderSize + sizeof(i32_t)]; 96 | char outbuf[SpxMsgHeaderSize + sizeof(u64_t)]; 97 | int inlen; 98 | int outlen; 99 | u32_t offset; 100 | }; 101 | 102 | struct IdcreatorServerContextPool { 103 | SpxLogDelegate *log; 104 | struct spx_fixed_vector *pool; 105 | }; 106 | 107 | 108 | extern struct IdcreatorServerContextPool *gIdcreatorServerContextPool; 109 | 110 | struct IdcreatorServerContextPool *idcreatorServerContextPoolNew(SpxLogDelegate *log,\ 111 | void *c, 112 | size_t size, 113 | IdcreatorWatcherDelegate *inHandler, 114 | IdcreatorWatcherDelegate *outHandler, 115 | err_t *err); 116 | struct IdcreatorServerContext *idcreatorServerContextPoolPop(struct IdcreatorServerContextPool *pool,err_t *err); 117 | err_t idcreatorServerContextPoolPush(struct IdcreatorServerContextPool *pool,struct IdcreatorServerContext *tsc); 118 | err_t idcreatorServerContextPoolFree(struct IdcreatorServerContextPool **pool); 119 | 120 | #ifdef __cplusplus 121 | } 122 | #endif 123 | #endif 124 | -------------------------------------------------------------------------------- /idcreator/IdcreatorStateKeep.c: -------------------------------------------------------------------------------- 1 | // ===================================================================================== 2 | // 3 | // Filename: IdcreatorStateKeep.c 4 | // 5 | // Description: 6 | // 7 | // Version: 1.0 8 | // Created: 2016年11月28日 18时38分30秒 9 | // Revision: none 10 | // Compiler: g++ 11 | // 12 | // Author: Seapeak.Xu (www.94geek.com), xvhfeng@gmail.com 13 | // Company: 14 | // 15 | // ===================================================================================== 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #include "spx_types.h" 26 | #include "spx_defs.h" 27 | #include "spx_string.h" 28 | #include "spx_message.h" 29 | #include "spx_io.h" 30 | #include "spx_path.h" 31 | 32 | #include "IdcreatorConfig.h" 33 | 34 | 35 | 36 | u32_t contract_next = 0; 37 | static int fd = 0; 38 | static char *mptr = NULL; 39 | 40 | err_t idcreator_statef_open( 41 | struct IdcreatorConfig *c 42 | ){ 43 | err_t err = 0; 44 | bool_t fexist = SpxFileExist(c->id_state_file); 45 | if(!fexist){ 46 | string_t basepath = spx_basepath(c->id_state_file,&err); 47 | if(NULL == basepath){ 48 | SpxLogFmt2(c->log,SpxLogError,err,\ 49 | "creator finder for id-state-file:%s is fail.", 50 | c->id_state_file); 51 | return err; 52 | } 53 | 54 | err = spx_mkdir(c->log,basepath,0x777); 55 | SpxStringFree(basepath); 56 | if(0 != err){ 57 | SpxLogFmt2(c->log,SpxLogError,err,\ 58 | "creator finder for id-state-file:%s is fail.", 59 | c->id_state_file); 60 | return err; 61 | } 62 | } 63 | 64 | fd = open(c->id_state_file, 65 | O_RDWR|O_CREAT,SpxFileMode); 66 | if(0 >= fd){ 67 | err = errno; 68 | SpxLogFmt2(c->log,SpxLogError,err,\ 69 | "open id state file:%s is fail.", 70 | c->id_state_file); 71 | return err; 72 | } 73 | 74 | if(!fexist) { 75 | if(0 != (err = ftruncate(fd,16))){ 76 | err = errno; 77 | SpxLogFmt2(c->log,SpxLogError,err,\ 78 | "truncate id state file:%s to size:%lld is fail.", 79 | c->id_state_file,16); 80 | SpxClose(fd); 81 | return err; 82 | } 83 | } 84 | mptr = mmap(NULL,\ 85 | 16,PROT_READ | PROT_WRITE ,\ 86 | MAP_SHARED,fd,0); 87 | if(MAP_FAILED == mptr){ 88 | err = errno; 89 | SpxLogFmt2(c->log,SpxLogError,err,\ 90 | "mmap the id state file file:%s to memory is fail.", 91 | c->id_state_file); 92 | SpxClose(fd); 93 | return err; 94 | } 95 | if(fexist){ 96 | uchar_t buff[4] = {0}; 97 | memcpy(buff,mptr,sizeof(int)); 98 | contract_next = spx_msg_b2i(buff); 99 | } 100 | return 0 ; 101 | } 102 | 103 | void idcreator_statf_sync(int next){ 104 | uchar_t buff[4] = {0}; 105 | spx_msg_i2b(buff,next); 106 | memcpy(mptr,buff,sizeof(int)); 107 | msync(mptr,sizeof(int),MS_SYNC); 108 | } 109 | 110 | void idcreator_statf_close(){ 111 | if(NULL != mptr) { 112 | munmap(mptr,16); 113 | mptr = NULL; 114 | } 115 | if(0 != fd) { 116 | SpxClose(fd); 117 | } 118 | } 119 | 120 | -------------------------------------------------------------------------------- /idcreator/IdcreatorStateKeep.h: -------------------------------------------------------------------------------- 1 | // ===================================================================================== 2 | // 3 | // Filename: IdcreatorStateKeep.h 4 | // 5 | // Description: 6 | // 7 | // Version: 1.0 8 | // Created: 2016年11月28日 18时38分33秒 9 | // Revision: none 10 | // Compiler: g++ 11 | // 12 | // Author: Seapeak.Xu (www.94geek.com), xvhfeng@gmail.com 13 | // Company: 14 | // 15 | // ===================================================================================== 16 | #ifndef _IDCREATORSTATEKEEP_H_ 17 | #define _IDCREATORSTATEKEEP_H_ 18 | #ifdef __cplusplus 19 | extern "C" { 20 | #endif 21 | 22 | extern u32_t contract_next; 23 | 24 | 25 | err_t idcreator_statef_open( 26 | struct IdcreatorConfig *c 27 | ); 28 | void idcreator_statf_sync(int next); 29 | void idcreator_statf_close(); 30 | 31 | #ifdef __cplusplus 32 | } 33 | #endif 34 | #endif 35 | -------------------------------------------------------------------------------- /idcreator/IdcreatorTcpAccept.h: -------------------------------------------------------------------------------- 1 | /*********************************************************************** 2 | * _ooOoo_ 3 | * o8888888o 4 | * 88" . "88 5 | * (| -_- |) 6 | * O\ = /O 7 | * ____/`---'\____ 8 | * . ' \\| |// `. 9 | * / \\||| : |||// \ 10 | * / _||||| -:- |||||- \ 11 | * | | \\\ - /// | | 12 | * | \_| ''\---/'' | | 13 | * \ .-\__ `-` ___/-. / 14 | * ___`. .' /--.--\ `. . __ 15 | * ."" '< `.___\_<|>_/___.' >'"". 16 | * | | : `- \`.;`\ _ /`;.`/ - ` : | | 17 | * \ \ `-. \_ __\ /__ _/ .-` / / 18 | * ======`-.____`-.___\_____/___.-`____.-'====== 19 | * `=---=' 20 | * ............................................. 21 | * 佛祖镇楼 BUG辟易 22 | * 佛曰: 23 | * 写字楼里写字间,写字间里程序员; 24 | * 程序人员写程序,又拿程序换酒钱。 25 | * 酒醒只在网上坐,酒醉还来网下眠; 26 | * 酒醉酒醒日复日,网上网下年复年。 27 | * 但愿老死电脑间,不愿鞠躬老板前; 28 | * 奔驰宝马贵者趣,公交自行程序员。 29 | * 别人笑我忒疯癫,我笑自己命太贱; 30 | * 不见满街漂亮妹,哪个归得程序员? 31 | * ========================================================================== 32 | * 33 | * this software or lib may be copied only under the terms of the gnu general 34 | * public license v3, which may be found in the source kit. 35 | * 36 | * Filename: IdcreatorTcpAccept.h 37 | * Created: 2015年03月03日 11时27分31秒 38 | * Author: Seapeak.Xu (www.94geek.com), xvhfeng@gmail.com 39 | * Company: Tencent Literature 40 | * Remark: 41 | * 42 | ****************************************************************************/ 43 | #ifndef _idcreatorTCPACCEPT_H_ 44 | #define _idcreatorTCPACCEPT_H_ 45 | #ifdef __cplusplus 46 | extern "C" { 47 | #endif 48 | 49 | 50 | #include 51 | #include 52 | 53 | #include 54 | 55 | #include "spx_types.h" 56 | #include "IdcreatorConfig.h" 57 | 58 | pthread_t idcreatorMainTcpThreadNew(SpxLogDelegate *log,struct IdcreatorConfig *c,err_t *err); 59 | 60 | void idcreatorNetworkReceiverHandler(int revents,void *arg); 61 | void idcreatorNetworkSenderHandler(int revents,void *arg); 62 | 63 | #ifdef __cplusplus 64 | } 65 | #endif 66 | #endif 67 | -------------------------------------------------------------------------------- /idcreator/config/idcreator.config: -------------------------------------------------------------------------------- 1 | ip = 127.0.0.1 2 | port = 9048 3 | http_port = 9049 4 | 5 | //for spx_web_server 6 | 7 | 8 | connectTimeout = 10s 9 | 10 | waitting = 10s 11 | 12 | stackSize = 64kb 13 | 14 | serverContextSize = 10000 15 | 16 | logpath=/opt/idcreator/log 17 | logprefix=idcreator 18 | 19 | id-state-file=/opt/idcreator/id.sf 20 | 21 | #unit KB MB GB 22 | logsize=10mb 23 | 24 | # Debug 25 | # Info 26 | # Warn 27 | # Error 28 | # Mark 29 | loglevel=Info 30 | 31 | daemon = false 32 | 33 | timeout=300 34 | 35 | mid = 0 36 | -------------------------------------------------------------------------------- /idcreator/makefile: -------------------------------------------------------------------------------- 1 | .SUFFIXES: .c .o 2 | 3 | SPX_SRC_PATH = ../spx/ 4 | SPX_HEADER_PATH = ../spx/include/ 5 | YDB_COMMON_HEADER_PATH = ./ 6 | TARGET = Idcreator 7 | TARGET_PATH = /usr/local/bin 8 | HEADER_TARGET_PATH = /usr/local/include/ 9 | 10 | CC = gcc 11 | CFLAG = -pipe -o0 -W -Wall -Werror -g \ 12 | -Wpointer-arith -Wno-unused-parameter -Wno-unused-function -Wno-unused-variable -Wunused-value 13 | INC_PATH = -I/usr/local/include -I/usr/include \ 14 | -I$(SPX_HEADER_PATH) \ 15 | -I$(YDB_COMMON_HEADER_PATH) 16 | LIB_PATH = -L/usr/local/lib -lev -lpthread 17 | DEFS := 18 | 19 | SYS := $(strip $(shell uname -s | tr '[:upper:]' '[:lower:]')) 20 | ifeq ($(SYS), linux) 21 | DEFS += -DSpxLinux 22 | else 23 | ifeq ($(SYS),unix) 24 | DEFS += -DSpxUnix 25 | else 26 | ifeq ($(SYS),darwin) 27 | DEFS += -DSpxMac 28 | endif 29 | endif 30 | endif 31 | 32 | BITS := $(findstring 64,$(shell uname -m )) 33 | ifeq (64, $(BITS)) 34 | DEFS += -DSpx64 35 | else 36 | ifeq (32, $(BITS)) 37 | DEFS += -DSpx32 38 | else 39 | DEFS += -DSpx32 40 | endif 41 | endif 42 | 43 | SRC_FILE = $(wildcard *.c) \ 44 | $(wildcard $(SPX_SRC_PATH)/*.c) 45 | 46 | HEADER_FILE = $(wildcard *.h) \ 47 | $(wildcard $(SPX_HEADER_PATH)/*.h) 48 | 49 | SHARED_OBJS = $(patsubst %.c,%.o,$(SRC_FILE) ) 50 | 51 | all:$(TARGET) 52 | 53 | %.o: %.c 54 | $(CC) $(CFLAG) -c $< -o $@ $(LIB_PATH) $(INC_PATH) $(DEFS) 55 | 56 | $(TARGET) : $(SHARED_OBJS) 57 | $(CC) $(CFLAG) $(SHARED_OBJS) $(LIB_PATH) $(INC_PATH) -o $(TARGET) $(DEFS) 58 | 59 | install: 60 | cp -f $(TARGET) $(TARGET_PATH) 61 | cp -f $(HEADER_FILE) $(HEADER_TARGET_PATH) 62 | clean: 63 | rm -f $(SHARED_OBJS) $(TARGET) 64 | uninstall: 65 | cd $(TARGET_PATH) 66 | rm -f $(TARGET) 67 | cd $(HEADER_TARGET_PATH) 68 | rm -f $(HEADER_FILE) 69 | 70 | 71 | -------------------------------------------------------------------------------- /lib/libev-4.15.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/crosg/idCreator/d4873281d18365ffad3f87dc49a8b01e4a19fc1f/lib/libev-4.15.tar.gz -------------------------------------------------------------------------------- /spx/SpxDateTime.h: -------------------------------------------------------------------------------- 1 | /*********************************************************************** 2 | * _ooOoo_ 3 | * o8888888o 4 | * 88" . "88 5 | * (| -_- |) 6 | * O\ = /O 7 | * ____/`---'\____ 8 | * . ' \\| |// `. 9 | * / \\||| : |||// \ 10 | * / _||||| -:- |||||- \ 11 | * | | \\\ - /// | | 12 | * | \_| ''\---/'' | | 13 | * \ .-\__ `-` ___/-. / 14 | * ___`. .' /--.--\ `. . __ 15 | * ."" '< `.___\_<|>_/___.' >'"". 16 | * | | : `- \`.;`\ _ /`;.`/ - ` : | | 17 | * \ \ `-. \_ __\ /__ _/ .-` / / 18 | * ======`-.____`-.___\_____/___.-`____.-'====== 19 | * `=---=' 20 | * ............................................. 21 | * 佛祖镇楼 BUG辟易 22 | * 佛曰: 23 | * 写字楼里写字间,写字间里程序员; 24 | * 程序人员写程序,又拿程序换酒钱。 25 | * 酒醒只在网上坐,酒醉还来网下眠; 26 | * 酒醉酒醒日复日,网上网下年复年。 27 | * 但愿老死电脑间,不愿鞠躬老板前; 28 | * 奔驰宝马贵者趣,公交自行程序员。 29 | * 别人笑我忒疯癫,我笑自己命太贱; 30 | * 不见满街漂亮妹,哪个归得程序员? 31 | * ========================================================================== 32 | * 33 | * this software or lib may be copied only under the terms of the gnu general 34 | * public license v3, which may be found in the source kit. 35 | * 36 | * Filename: SpxDateTime.h 37 | * Created: 2015年01月14日 15时25分22秒 38 | * Author: Seapeak.Xu (www.94geek.com), xvhfeng@gmail.com 39 | * Company: Tencent Literature 40 | * Remark: 41 | * 42 | ****************************************************************************/ 43 | #ifndef _SPXDATETIME_H_ 44 | #define _SPXDATETIME_H_ 45 | #ifdef __cplusplus 46 | extern "C" { 47 | #endif 48 | 49 | 50 | #include 51 | #include 52 | 53 | u64_t spxClock(); 54 | 55 | #ifdef __cplusplus 56 | } 57 | #endif 58 | #endif 59 | -------------------------------------------------------------------------------- /spx/include/spx_alloc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * */ 3 | #ifndef SPX_ALLOC_H 4 | #define SPX_ALLOC_H 5 | #ifdef __cplusplus 6 | extern "C" { 7 | #endif 8 | 9 | #include 10 | 11 | #include "spx_types.h" 12 | 13 | 14 | #if SYSBIT == 32 15 | #define SPX_ALIGN_SIZE 4 16 | #elif SYSBIT == 64 17 | #define SPX_ALIGN_SIZE 8 18 | #else 19 | #define SPX_ALIGN_SIZE 8 20 | #endif 21 | 22 | #define SpxFree(ptr) do\ 23 | {\ 24 | if(NULL != ptr)\ 25 | {\ 26 | free(ptr);\ 27 | ptr = NULL; \ 28 | }\ 29 | }while(false) 30 | 31 | void *spx_malloc(const size_t s,err_t *err); 32 | void *spx_alloc(const size_t numbs,const size_t s,err_t *err); 33 | void *spx_alloc_alone(const size_t s,err_t *err); 34 | void *spx_memalign_alloc(const size_t size,err_t *err); 35 | /* alloc the pointer array */ 36 | void *spx_alloc_mptr(const size_t numbs,err_t *err); 37 | void *spx_realloc(void *p,const size_t size,err_t *err); 38 | 39 | spx_private spx_inline err_t spx_free(void **v){ 40 | SpxFree(*v); 41 | return 0; 42 | } 43 | 44 | 45 | #ifdef __cplusplus 46 | } 47 | #endif 48 | #endif 49 | 50 | -------------------------------------------------------------------------------- /spx/include/spx_atomic.h: -------------------------------------------------------------------------------- 1 | #ifndef _SPX_ATOMIC_H_ 2 | #define _SPX_ATOMIC_H_ 3 | #ifdef __cplusplus 4 | extern "C" { 5 | #endif 6 | 7 | #define SpxAtomicAdd(ptr,v) __sync_add_and_fetch(ptr,v) 8 | #define SpxAtomicSub(ptr,v) __sync_sub_and_fetch(ptr,v) 9 | #define SpxAtomicIncr(ptr) __sync_add_and_fetch(ptr,1) 10 | #define SpxAtomicDecr(ptr) __sync_sub_and_fetch(ptr,1) 11 | #define SpxAtomicLazyAdd(ptr,v) __sync_fetch_and_add(ptr,v) 12 | #define SpxAtomicLazySub(ptr,v) __sync_fetch_and_sub(ptr,v) 13 | #define SpxAtomicLazyIncr(ptr) __sync_fetch_and_add(ptr,1) 14 | #define SpxAtomicLazyDecr(ptr) __sync_fetch_and_sub(ptr,1) 15 | #define SpxAtomicRelease(ptr) __sync_lock_release(ptr) 16 | #define SpxAtomicSet(ptr,v) __sync_lock_test_and_set(ptr,v) 17 | #define SpxAtomicIsCas(ptr,o,v) __sync_bool_compare_and_swap(ptr,o,v) 18 | #define SpxAtomicCas(ptr,o,v) __sync_val_compare_and_swap(ptr,o,v) 19 | 20 | 21 | 22 | #define SpxAtomicVAdd(v,a) __sync_add_and_fetch(&(v),a) 23 | #define SpxAtomicVSub(v,s) __sync_sub_and_fetch(&(v),s) 24 | #define SpxAtomicVIncr(v) __sync_add_and_fetch(&(v),1) 25 | #define SpxAtomicVDecr(v) __sync_sub_and_fetch(&(v),1) 26 | #define SpxAtomicLazyVAdd(v,a) __sync_fetch_and_add(&(v),a) 27 | #define SpxAtomicLazyVSub(v,s) __sync_fetch_and_sub(&(v),s) 28 | #define SpxAtomicLazyVIncr(v) __sync_fetch_and_add(&(v),1) 29 | #define SpxAtomicLazyVDecr(v) __sync_fetch_and_sub(&(v),1) 30 | #define SpxAtomicVRelease(v) __sync_lock_release(&(v)) 31 | #define SpxAtomicVSet(v,n) __sync_lock_test_and_set(&(v),n) 32 | #define SpxAtomicVIsCas(v,o,n) __sync_bool_compare_and_swap(&(v),o,n) 33 | #define SpxAtomicVCas(v,o,n) __sync_val_compare_and_swap(&(v),o,n) 34 | 35 | 36 | 37 | #define SpxAtomicMB() __sync_synchronize() 38 | #define SpxAtomicRMB() SpxAtomicMB() 39 | #define SpxAtomicWMB() SpxAtomicMB() 40 | 41 | 42 | #ifdef __cplusplus 43 | } 44 | #endif 45 | #endif 46 | -------------------------------------------------------------------------------- /spx/include/spx_collection.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: spx_collection.h 5 | * 6 | * Description: 7 | * 8 | * Version: 1.0 9 | * Created: 2014/06/09 10时25分48秒 10 | * Revision: none 11 | * Compiler: gcc 12 | * 13 | * Author: YOUR NAME (), 14 | * Organization: 15 | * 16 | * ===================================================================================== 17 | */ 18 | #ifndef _SPX_COLLECTION_H_ 19 | #define _SPX_COLLECTION_H_ 20 | #ifdef __cplusplus 21 | extern "C" { 22 | #endif 23 | 24 | #include 25 | #include 26 | 27 | #include "spx_types.h" 28 | #include "spx_string.h" 29 | 30 | typedef u64_t SpxCollectionKeyHashDelegate(void *k,size_t kl); 31 | typedef int SpxCollectionCmperDelegate(\ 32 | void *k1,u32_t l1,void *k2,u32_t l2); 33 | typedef err_t SpxCollectionKeyFreeDelegate(void **k); 34 | typedef err_t SpxCollectionValueFreeDelegate(void **v); 35 | typedef string_t SpxCollectionKeyPrintfDelegate(\ 36 | void *k,err_t *err); 37 | 38 | spx_private spx_inline int spx_collection_i32_default_cmper(\ 39 | void *k1,u32_t l1,void *k2,u32_t l2){ 40 | if(NULL == k1 || NULL == k2) errno = EINVAL; 41 | i32_t *i1 = 0,*i2 = 0; 42 | i1 = (i32_t *) k1; 43 | i2 = (i32_t *) k2; 44 | if(*i1 < *i2) return -1; 45 | else if(*i1 > *i2) return 1; 46 | else return 0; 47 | } 48 | spx_private spx_inline int spx_collection_i64_default_cmper(\ 49 | void *k1,u32_t l1,void *k2,u32_t l2){ 50 | if(NULL == k1 || NULL == k2) errno = EINVAL; 51 | i64_t *i1 = 0,*i2 = 0; 52 | i1 = (i64_t *) k1; 53 | i2 = (i64_t *) k2; 54 | if(*i1 < *i2) return -1; 55 | else if(*i1 > *i2) return 1; 56 | else return 0; 57 | } 58 | spx_private spx_inline int spx_collection_u32_default_cmper(\ 59 | void *k1,u32_t l1,void *k2,u32_t l2){ 60 | if(NULL == k1 || NULL == k2) errno = EINVAL; 61 | u32_t *i1 = 0,*i2 = 0; 62 | i1 = (u32_t *) k1; 63 | i2 = (u32_t *) k2; 64 | if(*i1 < *i2) return -1; 65 | else if(*i1 > *i2) return 1; 66 | else return 0; 67 | } 68 | spx_private spx_inline int spx_collection_u64_default_cmper(\ 69 | void *k1,u32_t l1,void *k2,u32_t l2){ 70 | if(NULL == k1 || NULL == k2) errno = EINVAL; 71 | u64_t *i1 = 0,*i2 = 0; 72 | i1 = (u64_t *) k1; 73 | i2 = (u64_t *) k2; 74 | if(*i1 < *i2) return -1; 75 | else if(*i1 > *i2) return 1; 76 | else return 0; 77 | } 78 | spx_private spx_inline int spx_collection_string_default_cmper(\ 79 | void *k1,u32_t l1,void *k2,u32_t l2){ 80 | if(NULL == k1 || NULL == k2) errno = EINVAL; 81 | string_t s1 = (string_t) k1; 82 | string_t s2 = (string_t) k2; 83 | if(SpxStringIsEmpty(s1) \ 84 | && !SpxStringIsNullOrEmpty(s2)) return -1; 85 | if(!SpxStringIsEmpty(s1) \ 86 | && SpxStringIsEmpty(s2)) return 1; 87 | if(SpxStringIsEmpty(s1) \ 88 | && SpxStringIsEmpty(s2)) return 0; 89 | return spx_string_cmp(s1,s2); 90 | } 91 | spx_private spx_inline int spx_collection_time_default_cmper(\ 92 | void *k1,u32_t l1,void *k2,u32_t l2){ 93 | if(NULL == k1 || NULL == k2) errno = EINVAL; 94 | time_t *i1 = 0,*i2 = 0; 95 | i1 = (time_t *) k1; 96 | i2 = (time_t *) k2; 97 | if(*i1 < *i2) return -1; 98 | else if(*i1 > *i2) return 1; 99 | else return 0; 100 | } 101 | 102 | 103 | spx_private spx_inline string_t spx_collection_i32_default_printf(\ 104 | void *k,err_t *err){ 105 | i32_t *n = (i32_t *)k; 106 | return spx_string_from_i64(*n,err); 107 | } 108 | spx_private spx_inline string_t spx_collection_u32_default_printf(\ 109 | void *k,err_t *err){ 110 | u32_t *n = (u32_t *)k; 111 | return spx_string_from_i64(*n,err); 112 | } 113 | spx_private spx_inline string_t spx_collection_i64_default_printf(\ 114 | void *k,err_t *err){ 115 | i64_t *n = (i64_t *)k; 116 | return spx_string_from_i64(*n,err); 117 | } 118 | spx_private spx_inline string_t spx_collection_u64_default_printf(\ 119 | void *k,err_t *err){ 120 | u64_t *n = (u64_t *)k; 121 | return spx_string_from_i64(*n,err); 122 | } 123 | spx_private spx_inline string_t spx_collection_time_default_printf(\ 124 | void *k,err_t *err){ 125 | u64_t *n = (u64_t *)k; 126 | return spx_string_from_i64(*n,err); 127 | } 128 | 129 | #define BIT_IN_INT ( sizeof(int) * CHAR_BIT ) 130 | #define THREE_QUARTERS ((int) ((BIT_IN_INT * 3) / 4)) 131 | #define ONE_EIGHTH ((int) (BIT_IN_INT / 8)) 132 | #define HIGH_BITS ( ~((unsigned int)(~0) >> ONE_EIGHTH )) 133 | 134 | spx_private spx_inline u64_t spx_pjw(void *buff,size_t len) { 135 | char *data = (char *) buff; 136 | u32_t val, i; 137 | for (val = 0; *data; ++data) { 138 | val = (val << ONE_EIGHTH ) + *data; 139 | if ((i = val & HIGH_BITS)!= 0 )val 140 | = (val ^ (i >> THREE_QUARTERS )) & ~HIGH_BITS; 141 | } 142 | return val; 143 | } 144 | 145 | spx_private spx_inline u64_t spx_elf(void *data,size_t len) { 146 | unsigned char *buff = (unsigned char *)data; 147 | u64_t h = 0, g; 148 | while (*buff) { 149 | h = (h << 4) + *buff++; 150 | if (0 != (g = (h & 0xF0000000))) 151 | h ^= g >> 24; 152 | h &= ~g; 153 | } 154 | return h; 155 | } 156 | 157 | 158 | #ifdef __cplusplus 159 | } 160 | #endif 161 | #endif 162 | -------------------------------------------------------------------------------- /spx/include/spx_configurtion.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * this software or lib may be copied only under the terms of the gnu general 5 | * public license v3, which may be found in the source kit. 6 | * 7 | * Filename: spx_configurtion.h 8 | * Created: 2014/07/16 16时38分03秒 9 | * Author: Seapeak.Xu (seapeak.cnblog.com), xvhfeng@gmail.com 10 | * Company: Tencent Literature 11 | * Remark: 12 | * 13 | * ===================================================================================== 14 | */ 15 | #ifndef _SPX_CONFIGURTION_H_ 16 | #define _SPX_CONFIGURTION_H_ 17 | #ifdef __cplusplus 18 | extern "C" { 19 | #endif 20 | 21 | #include 22 | #include 23 | 24 | #include "spx_types.h" 25 | 26 | typedef void SpxConfigurtionLineParserDelegate(string_t line,\ 27 | void *config,err_t *err); 28 | typedef void *SpxConfigurtionParserBeforeDelegate(SpxLogDelegate *log, err_t *err); 29 | typedef err_t SpxConfigurtionParserAfterDelegate(void *config); 30 | 31 | void *spx_configurtion_parser(SpxLogDelegate *log,\ 32 | SpxConfigurtionParserBeforeDelegate *before,\ 33 | SpxConfigurtionParserAfterDelegate *after,\ 34 | string_t filename,\ 35 | SpxConfigurtionLineParserDelegate *lineparser,\ 36 | err_t *err); 37 | 38 | 39 | #ifdef __cplusplus 40 | } 41 | #endif 42 | #endif 43 | -------------------------------------------------------------------------------- /spx/include/spx_defs.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef SPX_DEFS_H 3 | #define SPX_DEFS_H 4 | 5 | #ifdef __cplusplus 6 | extern "C" { 7 | #endif 8 | 9 | #include 10 | #include 11 | 12 | #include "spx_types.h" 13 | #include "spx_errno.h" 14 | 15 | #define SpxLogDebug 0 16 | #define SpxLogInfo 1 17 | #define SpxLogWarn 2 18 | #define SpxLogError 3 19 | #define SpxLogMark 4 20 | 21 | 22 | spx_private char *SpxLogDesc[] = { 23 | "Debug.", 24 | "Info.", 25 | "Warn.", 26 | "Error.", 27 | "Mark." 28 | }; 29 | 30 | spx_private int SpxLogDescSize[] = { 31 | 6, 32 | 5, 33 | 5, 34 | 6, 35 | 5 36 | }; 37 | 38 | 39 | #define SpxLogStack(log,level) \ 40 | if(NULL != (log)) {\ 41 | (log)(level,((string_t) "File:%s,Line:%d,Func:%s.function be called."), \ 42 | __FILE__,__LINE__,__FUNCTION__); \ 43 | } 44 | 45 | 46 | #define SpxLog1(log,level,info) \ 47 | if(NULL != (log)) {\ 48 | (log)(level,((string_t) "File:%s,Line:%d,Func:%s.%s."), \ 49 | __FILE__,__LINE__,__FUNCTION__,info); \ 50 | } 51 | 52 | #define SpxLog2(log,level,err,info) \ 53 | if(NULL != (log)) {\ 54 | (log)(level,((string_t) "File:%s,Line:%d,Func:%s.errno:%d,info:%s.%s."),\ 55 | __FILE__,__LINE__,__FUNCTION__,err,err >= SpxSuccess ? spx_strerror(err) : strerror(err),info);\ 56 | } 57 | 58 | #define SpxLogFmt1(log,level,fmt,...) \ 59 | if(NULL != (log)) {\ 60 | (log)(level,((string_t) "File:%s,Line:%d,Func:%s."fmt),\ 61 | __FILE__,__LINE__,__FUNCTION__,__VA_ARGS__);\ 62 | } 63 | 64 | #define SpxLogFmt2(log,level,err,fmt,...) \ 65 | if(NULL != (log)) {\ 66 | (log)(level,((string_t) "File:%s,Line:%d,Func:%s.errno:%d,info:%s."fmt),\ 67 | __FILE__,__LINE__,__FUNCTION__,err,err >= SpxSuccess ? spx_strerror(err) : strerror(err),__VA_ARGS__);\ 68 | } 69 | 70 | #define SpxErrReset errno = 0 71 | #define SpxErr1 (0 != errno) 72 | #define SpxErr2(rc) (0 != (rc = errno)) 73 | 74 | #define SpxPathMode 0777 75 | #define SpxFileMode 0777 76 | #define SpxPathSize 1023 77 | #define SpxFileNameSize 127 78 | #define SpxPathDlmt '/' 79 | #define SpxSuffixDlmt '.' 80 | #define SpxSuffixDlmtString "." 81 | #define SpxSuffixDlmtLen 1 82 | #define SpxPathDlmtLen 1 83 | #define SpxPathDlmtString "/" 84 | #define SpxLineSize 2047 85 | #define SpxLineEndDlmtString "\n" 86 | #define SpxLineEndDlmt '\n' 87 | #define SpxLineEndDlmtLen 1 88 | #define SpxKeyStringSize 255 89 | #define SpxHostNameSize 255 90 | 91 | #define SpxGB (1024 * 1024 * 1024) 92 | #define SpxMB (1024 * 1024) 93 | #define SpxKB (1024) 94 | 95 | #define SpxSecondTick 1 96 | #define SpxMinuteTick 60 97 | #define SpxHourTick (60 * 60) 98 | #define SpxDayTick (24 * 60 * 60) 99 | 100 | #define SpxDiskUnitPB 0 101 | #define SpxDiskUnitTB 1 102 | #define SpxDiskUnitGB 2 103 | #define SpxDiskUnitMB 3 104 | #define SpxDiskUnitKB 4 105 | #define SpxDiskUnitB 5 106 | #define SpxSecondsOfDay (24 * 60 * 60) 107 | #define SpxBoolTransportSize (sizeof(char)) 108 | 109 | spx_private char *spx_diskunit_desc[]={ 110 | "PB", 111 | "TB", 112 | "GB", 113 | "MB", 114 | "KB", 115 | "B" 116 | }; 117 | 118 | #define SpxIpv4Size 15 119 | 120 | #define SpxMin(a,b) ((a) < (b) ? (a) : (b)) 121 | #define SpxMax(a,b) ((a) > (b) ? (a) : (b)) 122 | #define SpxAbs(a) ((a) < 0 ? -(a) : a) 123 | 124 | #ifdef Spx64 125 | #define SpxPtrSize 8 126 | #elif Spx32 127 | #define SpxPtrSize 4 128 | #else 129 | #define SpxPtrSize 4 130 | #endif 131 | 132 | //#define SpxI32Size (sizeof("-2147483648") - 1) 133 | //#define SpxI64Size (sizeof("-9223372036854775808") - 1) 134 | 135 | #define LF (u_char) 10 136 | #define CR (u_char) 13 137 | #define CRLF "\x0d\x0a" 138 | 139 | 140 | //#if Spx32 141 | //#define SpxIntSize SpxI32Size 142 | //#else 143 | //#define SpxIntSize SpxI64Size 144 | //#endif 145 | 146 | //#if (Spx64) 147 | //#define SpxAtomicSize (sizeof("-9223372036854775808") - 1) 148 | //#else 149 | //#define SpxAtomicSize (sizeof("-2147483648") - 1) 150 | //#endif 151 | 152 | #define SpxTypeConvert(tp,name,old) tp name = (tp) (old) 153 | #define SpxTypeConvert2(t,newp,old) t *newp = (t *) (old) 154 | 155 | 156 | #define SpxClose(fd) \ 157 | do { \ 158 | if(0 != fd) { \ 159 | close(fd);\ 160 | fd = 0;\ 161 | } \ 162 | }while(false) 163 | 164 | 165 | #define SpxAlign(d, a) (((d) + (a - 1)) & ~(a - 1)) 166 | 167 | #if Spx32 168 | #define SpxAlignSize 4 169 | #elif Spx64 170 | #define SpxAlignSize 8 171 | #else 172 | #define SpxAlignSize 8 173 | #endif 174 | 175 | #define SpxMemIncr(p,s) (((char *) p) + (s)) 176 | #define SpxMemDecr(p,s) (((char *) p) - (s)) 177 | #define SpxPtrDecr(p1,p2) ((size_t ) (((char *) p1) - ((char *) p2))) 178 | #define SpxRightMovePointer(p,s) (((char *) p) + (s)) 179 | #define SpxLeftMovePointer(p,s) (((char *) p) - (s)) 180 | 181 | #define SpxSSet(o,p,v) o->p = (v) 182 | 183 | #ifdef __cplusplus 184 | } 185 | #endif 186 | #endif 187 | -------------------------------------------------------------------------------- /spx/include/spx_env.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: spx_env.h 5 | * 6 | * Description: 7 | * 8 | * Version: 1.0 9 | * Created: 2014/06/30 14时34分03秒 10 | * Revision: none 11 | * Compiler: gcc 12 | * 13 | * Author: YOUR NAME (), 14 | * Organization: 15 | * 16 | * ===================================================================================== 17 | */ 18 | #ifndef _SPX_ENV_H_ 19 | #define _SPX_ENV_H_ 20 | #ifdef __cplusplus 21 | extern "C" { 22 | #endif 23 | 24 | #include "spx_types.h" 25 | typedef void SpxSigActionDelegate(int sig); 26 | 27 | void spx_env_daemon(); 28 | err_t spx_set_group_and_user(SpxLogDelegate *log,string_t gname,string_t uname); 29 | void spx_env_sigaction(int sig,SpxSigActionDelegate *act); 30 | 31 | #ifdef __cplusplus 32 | } 33 | #endif 34 | #endif 35 | -------------------------------------------------------------------------------- /spx/include/spx_errno.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef SPX_ERRNO_H 3 | #define SPX_ERRNO_H 4 | 5 | #ifdef __cplusplus 6 | extern "C" { 7 | #endif 8 | 9 | #include 10 | #include 11 | 12 | #include "spx_types.h" 13 | 14 | #define SpxSuccess 512 15 | #define ENODLMT 513 16 | #define EBADHEADER 514 17 | 18 | char *spx_strerror(err_t err); 19 | 20 | 21 | #ifdef __cplusplus 22 | } 23 | #endif 24 | #endif 25 | -------------------------------------------------------------------------------- /spx/include/spx_fixed_vector.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: spx_fixed_vector.h 5 | * 6 | * Description: 7 | * 8 | * Version: 1.0 9 | * Created: 2014/06/10 23时01分36秒 10 | * Revision: none 11 | * Compiler: gcc 12 | * 13 | * Author: YOUR NAME (), 14 | * Organization: 15 | * 16 | * ===================================================================================== 17 | */ 18 | #ifndef _SPX_FIXED_VECTOR_H_ 19 | #define _SPX_FIXED_VECTOR_H_ 20 | #ifdef __cplusplus 21 | extern "C" { 22 | #endif 23 | 24 | #include "spx_types.h" 25 | #include "spx_vector.h" 26 | #include "spx_thread.h" 27 | 28 | 29 | typedef void *SpxFixedVectorValueNewDeledate(size_t idx,void *arg,err_t *err); 30 | 31 | struct spx_fixed_vector{ 32 | struct spx_vector_node *header; 33 | struct spx_vector_node *tail; 34 | struct spx_vector_node *busy_header; 35 | struct spx_vector_node *busy_tail; 36 | SpxVectorValueFreeDelegate *node_free_handle; 37 | size_t size; 38 | size_t busysize; 39 | SpxLogDelegate *log; 40 | pthread_mutex_t *locker; 41 | }; 42 | 43 | 44 | struct spx_fixed_vector *spx_fixed_vector_new(SpxLogDelegate *log,\ 45 | size_t size,\ 46 | SpxFixedVectorValueNewDeledate *value_new_handle, 47 | void *arg,\ 48 | SpxVectorValueFreeDelegate *node_free_handle,\ 49 | err_t *err); 50 | err_t spx_fixed_vector_free(struct spx_fixed_vector **vector); 51 | err_t spx_fixed_vector_push(struct spx_fixed_vector *vector,void *v); 52 | void *spx_fixed_vector_pop(struct spx_fixed_vector *vector, err_t *err); 53 | 54 | 55 | struct spx_fixed_vector_iter{ 56 | struct spx_fixed_vector *vector; 57 | struct spx_vector_node *curr; 58 | }; 59 | struct spx_fixed_vector_iter *spx_fixed_vector_iter_init(struct spx_fixed_vector *vector,\ 60 | err_t *err); 61 | err_t spx_fixed_vector_iter_destroy(struct spx_vector_iter **iter); 62 | void *spx_fixed_vector_iter_next(struct spx_vector_iter *iter) ; 63 | 64 | 65 | 66 | 67 | #ifdef __cplusplus 68 | } 69 | #endif 70 | #endif 71 | -------------------------------------------------------------------------------- /spx/include/spx_io.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: spx_io.h 5 | * 6 | * Description: 7 | * 8 | * Version: 1.0 9 | * Created: 2014/06/12 11时47分07秒 10 | * Revision: none 11 | * Compiler: gcc 12 | * 13 | * Author: YOUR NAME (), 14 | * Organization: 15 | * 16 | * ===================================================================================== 17 | */ 18 | #ifndef _SPX_IO_H_ 19 | #define _SPX_IO_H_ 20 | #ifdef __cplusplus 21 | extern "C" { 22 | #endif 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | #include "spx_types.h" 33 | #include "spx_message.h" 34 | #include "spx_string.h" 35 | 36 | #define SpxWriteOpen(filename,is_clear) \ 37 | open(filename,is_clear \ 38 | ? O_RDWR|O_CREAT|O_TRUNC \ 39 | : O_RDWR|O_CREAT|O_APPEND,0744) 40 | #define SpxReadOpen(filename) open(filename,O_RDONLY) 41 | #define SpxFWriteOpen(filename,is_clear) fopen(filename,is_clear ? "w" : "a") 42 | #define SpxFReadOpen(filename) fopen(filename,"r") 43 | 44 | #define SpxFileExist(filename) ( 0 == access(filename,F_OK)) 45 | #define SpxFileReadable(filename) (0 == access(filename,R_OK)) 46 | #define SpxFileWritable(filename) (0 == access(filename,W_OK)) 47 | #define SpxFileExecutable(filename) (0 == access(filename,X_OK)) 48 | 49 | #define SpxMmap(fd,begin,len) mmap(NULL,len,PROT_READ | PROT_WRITE ,MAP_SHARED,fd,begin) 50 | 51 | err_t spx_read(int fd,byte_t *buf,const size_t size,size_t *len); 52 | err_t spx_write(int fd,byte_t *buf,const size_t size,size_t *len); 53 | err_t spx_read_nb(int fd,byte_t *buf,const size_t size,size_t *len); 54 | err_t spx_write_nb(int fd,byte_t *buf,const size_t size,size_t *len); 55 | err_t spx_read_to_msg(int fd,struct spx_msg *ctx,const size_t size,size_t *len); 56 | err_t spx_read_to_msg_nb(int fd,struct spx_msg *ctx,const size_t size,size_t *len); 57 | err_t spx_write_from_msg(int fd,struct spx_msg *ctx,const size_t size,size_t *len); 58 | err_t spx_write_from_msg_nb(int fd,struct spx_msg *ctx,const size_t size,size_t *len); 59 | err_t spx_fwrite_string(FILE *fp,string_t s,size_t size,size_t *len); 60 | 61 | err_t spx_read_ack(int fd,byte_t *buf,const size_t size,size_t *len); 62 | err_t spx_perread_ack(int fd,byte_t *buf,const size_t size,size_t *len); 63 | err_t spx_write_ack(int fd,byte_t *buf,const size_t size,size_t *len); 64 | err_t spx_read_msg_ack(int fd,struct spx_msg *msg,const size_t size,size_t *len); 65 | err_t spx_write_msg_ack(int fd,struct spx_msg *ctx,size_t *len); 66 | err_t spx_sendfile_ack(int sock,int fd,off_t offset,size_t size,size_t *len); 67 | 68 | 69 | 70 | err_t spx_sendfile(int sock,int fd,off_t offset,size_t size,size_t *len); 71 | err_t spx_set_nb(int fd); 72 | 73 | spx_private spx_inline size_t spx_mmap_form_msg(char *p,off_t offset,struct spx_msg *ctx){ 74 | size_t s = (size_t) ( ctx->last - ctx->buf); 75 | memcpy( p + offset,ctx->buf,s); 76 | return s; 77 | } 78 | 79 | spx_private spx_inline size_t spx_mmap_form_msg_with_offset(char *p, 80 | off_t offset,struct spx_msg *ctx,off_t off){ 81 | size_t s = (size_t) (ctx->last - (ctx->buf + off)); 82 | memcpy( p + offset,ctx->buf,s); 83 | return s; 84 | } 85 | 86 | 87 | 88 | err_t spx_write_context(SpxLogDelegate *log,int fd,struct spx_msg_context *ctx); 89 | err_t spx_write_context_nb(SpxLogDelegate *log,int fd,struct spx_msg_context *ctx); 90 | struct spx_msg_header *spx_read_header(SpxLogDelegate *log,int fd,err_t *err); 91 | struct spx_msg_header *spx_read_header_nb(SpxLogDelegate *log,int fd,err_t *err); 92 | struct spx_msg *spx_read_body(SpxLogDelegate *log,int fd,size_t size,err_t *err); 93 | struct spx_msg *spx_read_body_nb(SpxLogDelegate *log,int fd,size_t size,err_t *err); 94 | err_t spx_lazy_recv(SpxLogDelegate *log,int fd,int sock,size_t size); 95 | err_t spx_lazy_recv_nb(SpxLogDelegate *log,int fd,int sock,size_t size); 96 | err_t spx_lazy_mmap(SpxLogDelegate *log,char *ptr,int sock,size_t size,off_t begin); 97 | err_t spx_lazy_mmap_nb(SpxLogDelegate *log,char *ptr,int sock,size_t size,off_t begin); 98 | 99 | 100 | #ifdef __cplusplus 101 | } 102 | #endif 103 | #endif 104 | -------------------------------------------------------------------------------- /spx/include/spx_limits.h: -------------------------------------------------------------------------------- 1 | /*********************************************************************** 2 | * _ooOoo_ 3 | * o8888888o 4 | * 88" . "88 5 | * (| -_- |) 6 | * O\ = /O 7 | * ____/`---'\____ 8 | * . ' \\| |// `. 9 | * / \\||| : |||// \ 10 | * / _||||| -:- |||||- \ 11 | * | | \\\ - /// | | 12 | * | \_| ''\---/'' | | 13 | * \ .-\__ `-` ___/-. / 14 | * ___`. .' /--.--\ `. . __ 15 | * ."" '< `.___\_<|>_/___.' >'"". 16 | * | | : `- \`.;`\ _ /`;.`/ - ` : | | 17 | * \ \ `-. \_ __\ /__ _/ .-` / / 18 | * ======`-.____`-.___\_____/___.-`____.-'====== 19 | * `=---=' 20 | * ............................................. 21 | * 佛祖镇楼 BUG辟易 22 | * 佛曰: 23 | * 写字楼里写字间,写字间里程序员; 24 | * 程序人员写程序,又拿程序换酒钱。 25 | * 酒醒只在网上坐,酒醉还来网下眠; 26 | * 酒醉酒醒日复日,网上网下年复年。 27 | * 但愿老死电脑间,不愿鞠躬老板前; 28 | * 奔驰宝马贵者趣,公交自行程序员。 29 | * 别人笑我忒疯癫,我笑自己命太贱; 30 | * 不见满街漂亮妹,哪个归得程序员? 31 | * ========================================================================== 32 | * 33 | * this software or lib may be copied only under the terms of the gnu general 34 | * public license v3, which may be found in the source kit. 35 | * 36 | * Filename: spx_limits.h 37 | * Created: 2014/10/24 17时30分29秒 38 | * Author: Seapeak.Xu (seapeak.cnblog.com), xvhfeng@gmail.com 39 | * Company: Tencent Literature 40 | * Remark: 41 | * 42 | ****************************************************************************/ 43 | #ifndef _SPX_LIMITS_H_ 44 | #define _SPX_LIMITS_H_ 45 | #ifdef __cplusplus 46 | extern "C" { 47 | #endif 48 | 49 | 50 | #include 51 | #include 52 | 53 | #define SpxI8Max 127 54 | #define SpxI8Min (-128) 55 | #define SpxU8Mac 255 56 | #define SpxI8MaxLength sizeof("-127") 57 | #define SpxU8MaxLength SpxI8MaxLength 58 | 59 | #define SpxI16Max 32767 60 | #define SpxI16Min (-32768) 61 | #define SpxU16Max 65535 62 | #define SpxI16MaxLength sizeof("-32768") 63 | #define SpxU16MaxLength SpxI16MaxLength 64 | 65 | 66 | #define SpxI32Max 2147483647 67 | #define SpxI32Min (-2147483648) 68 | #define SpxU32Max 4294967295 69 | #define SpxI32MaxLength sizeof("-4294967295") 70 | #define SpxU32MaxLength SpxI32MaxLength 71 | 72 | #define SpxI64Max 9223372036854775807 73 | #define SpxI64Min -9223372036854775808 74 | #define SpxU64Max 18446744073709551615L 75 | #define SpxI64MaxLength sizeof("-9223372036854775808") 76 | #define SpxU64MaxLength SpxI64MaxLength 77 | 78 | #ifdef __cplusplus 79 | } 80 | #endif 81 | #endif 82 | -------------------------------------------------------------------------------- /spx/include/spx_list.h: -------------------------------------------------------------------------------- 1 | #ifndef SPX_LIST_H 2 | #define SPX_LIST_H 3 | #ifdef __cplusplus 4 | extern "C" { 5 | #endif 6 | 7 | #include 8 | 9 | #include "spx_types.h" 10 | 11 | typedef err_t (SpxListNodeFreeDelegate)(void **arg); 12 | typedef void *(SpxListNodeNewDelegate)(size_t i,void *arg,err_t *err); 13 | 14 | struct spx_list_node{ 15 | void *v; 16 | }; 17 | 18 | struct spx_list{ 19 | SpxLogDelegate *log; 20 | size_t free_size; 21 | size_t busy_size; 22 | size_t size; 23 | SpxListNodeFreeDelegate *node_free; 24 | struct spx_list_node *nodes; 25 | }; 26 | 27 | struct spx_list *spx_list_new(SpxLogDelegate *log,\ 28 | size_t init_size,\ 29 | SpxListNodeFreeDelegate *node_free,\ 30 | err_t *err); 31 | 32 | struct spx_list *spx_list_init(SpxLogDelegate *log,\ 33 | size_t init_size,\ 34 | SpxListNodeNewDelegate *new,\ 35 | void *arg,\ 36 | SpxListNodeFreeDelegate *free,\ 37 | err_t *err 38 | ); 39 | 40 | void *spx_list_get(struct spx_list *list,int idx); 41 | void *spx_list_get_and_out(struct spx_list *list,int idx); 42 | err_t spx_list_delete(struct spx_list *list,int idx); 43 | err_t spx_list_insert(struct spx_list *list,int idx,void *v); 44 | err_t spx_list_add(struct spx_list *list,void *v); 45 | err_t spx_list_free(struct spx_list **list); 46 | err_t spx_list_set(struct spx_list *list,int idx,void *v); 47 | 48 | #ifdef __cplusplus 49 | } 50 | #endif 51 | #endif 52 | -------------------------------------------------------------------------------- /spx/include/spx_log.h: -------------------------------------------------------------------------------- 1 | #ifndef SPX_LOG_H 2 | #define SPX_LOG_H 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | #include "spx_types.h" 13 | #include "spx_string.h" 14 | #include "spx_defs.h" 15 | 16 | //the macro to string,it will use to debug 17 | #define spx_m2s_d(m) #m 18 | #define spx_m2s(m) spx_m2s_d(m) 19 | 20 | struct spx_log{ 21 | off_t offset; 22 | FILE *fp; 23 | size_t size; 24 | u8_t level; 25 | string_t path; 26 | string_t name; 27 | SpxLogDelegate *log; 28 | pthread_mutex_t *mlock; 29 | }; 30 | 31 | err_t spx_log_new(SpxLogDelegate log,\ 32 | const string_t path,\ 33 | const string_t name,const u64_t max_size,\ 34 | const u8_t level); 35 | void spx_log(int level,char *fmt,...); 36 | void spx_log_free(); 37 | 38 | #ifdef __cplusplus 39 | } 40 | #endif 41 | #endif 42 | -------------------------------------------------------------------------------- /spx/include/spx_map.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: spx_map.h 5 | * 6 | * Description: 7 | * 8 | * Version: 1.0 9 | * Created: 2014/06/06 10时46分03秒 10 | * Revision: none 11 | * Compiler: gcc 12 | * 13 | * Author: YOUR NAME (), 14 | * Organization: 15 | * 16 | * ===================================================================================== 17 | */ 18 | #ifndef _SPX_MAP_H_ 19 | #define _SPX_MAP_H_ 20 | #ifdef __cplusplus 21 | extern "C" { 22 | #endif 23 | #include "spx_types.h" 24 | #include "spx_collection.h" 25 | 26 | struct spx_map_node; 27 | struct spx_map_key_node; 28 | 29 | struct spx_map_key_node{ 30 | struct spx_map_key_node *prev; 31 | struct spx_map_key_node *next; 32 | struct spx_map_node *node; 33 | }; 34 | 35 | struct spx_map_node{ 36 | u64_t hash; 37 | void *k; 38 | size_t kl; 39 | void *v; 40 | size_t vl; 41 | struct spx_map_key_node *p; 42 | struct spx_map_node *prev; 43 | struct spx_map_node *next; 44 | }; 45 | 46 | struct spx_map_slot{ 47 | struct spx_map_node *header; 48 | struct spx_map_node *tail; 49 | size_t real_size; 50 | }; 51 | 52 | struct spx_map{ 53 | size_t numbs; 54 | size_t slots_count; 55 | SpxLogDelegate *log; 56 | SpxCollectionCmperDelegate *cmper; 57 | SpxCollectionKeyPrintfDelegate *kprintf; 58 | SpxCollectionKeyFreeDelegate *kfree; 59 | SpxCollectionValueFreeDelegate *vfree; 60 | SpxCollectionKeyHashDelegate *hash; 61 | struct spx_map_slot *slots; 62 | struct spx_map_key_node *keys_header; 63 | struct spx_map_key_node *keys_tail; 64 | }; 65 | 66 | struct spx_map_iter{ 67 | struct spx_map *map; 68 | struct spx_map_key_node *curr; 69 | }; 70 | 71 | struct spx_map *spx_map_new(SpxLogDelegate *log,\ 72 | SpxCollectionKeyHashDelegate *hash,\ 73 | SpxCollectionCmperDelegate *cmper,\ 74 | SpxCollectionKeyPrintfDelegate *kprintf,\ 75 | SpxCollectionKeyFreeDelegate *kfree,\ 76 | SpxCollectionValueFreeDelegate *vfree,\ 77 | err_t *err); 78 | 79 | err_t spx_map_insert(struct spx_map *map,\ 80 | void *k,size_t kl,void *v,size_t vl); 81 | 82 | void *spx_map_get(struct spx_map *map,\ 83 | void *k,size_t kl,size_t *vl); 84 | 85 | bool_t spx_map_exist_key(struct spx_map *map,\ 86 | void *k,size_t kl); 87 | 88 | void *spx_map_out(struct spx_map *map,\ 89 | void *k,size_t kl,size_t *vl); 90 | 91 | err_t spx_map_delete(struct spx_map *map,\ 92 | void *k,size_t kl); 93 | 94 | err_t spx_map_free(struct spx_map **map); 95 | 96 | struct spx_map_iter *spx_map_iter_new(struct spx_map *map,err_t *err); 97 | struct spx_map_node *spx_map_iter_next(struct spx_map_iter *iter,err_t *err); 98 | err_t spx_map_iter_free(struct spx_map_iter ** iter); 99 | void spx_map_iter_reset(struct spx_map_iter *iter); 100 | 101 | spx_private spx_inline size_t spx_map_numbs(struct spx_map *map){ 102 | return map->numbs; 103 | } 104 | 105 | spx_private spx_inline size_t spx_map_slots_count(struct spx_map *map){ 106 | return map->slots_count; 107 | } 108 | 109 | #ifdef __cplusplus 110 | } 111 | #endif 112 | #endif 113 | -------------------------------------------------------------------------------- /spx/include/spx_message.h: -------------------------------------------------------------------------------- 1 | #ifndef SPX_MESSAGE_H 2 | #define SPX_MESSAGE_H 3 | #ifdef __cplusplus 4 | extern "C" { 5 | #endif 6 | 7 | #include 8 | #include 9 | 10 | #include "spx_types.h" 11 | 12 | #define SpxMsgHeaderSize (3 * sizeof(u32_t) + 2 * sizeof(u64_t) + sizeof(char)) 13 | 14 | #define SpxMsgSeekSet 0 15 | #define SpxMsgSeekCurrent 1 16 | #define SpxMsgSeekEnd 2 17 | 18 | struct spx_msg{ 19 | uchar_t *buf; 20 | uchar_t *last; 21 | size_t busylen; 22 | size_t s; 23 | err_t err; 24 | }; 25 | 26 | 27 | //add the keepalive for unclosed-connection 28 | //but if you use unclosed-connection,you must regedit event by yourself 29 | //when the request is doing over 30 | struct spx_msg_header{ 31 | u32_t version; 32 | u32_t protocol; 33 | u64_t bodylen; 34 | u64_t offset; 35 | bool_t is_keepalive; 36 | u32_t err; 37 | }; 38 | 39 | struct spx_msg_context{ 40 | struct spx_msg_header *header; 41 | struct spx_msg *body; 42 | bool_t is_lazy_recv; 43 | bool_t is_sendfile; 44 | int sendfile_fd; 45 | off_t sendfile_begin; 46 | size_t sendfile_size; 47 | }; 48 | 49 | struct spx_msg *spx_msg_new(const size_t len,err_t *err); 50 | err_t spx_msg_free(struct spx_msg **ctx); 51 | err_t spx_msg_seek(struct spx_msg *ctx,off_t offset,int whence); 52 | void spx_msg_peek(struct spx_msg *ctx,off_t off); 53 | void spx_msg_front(struct spx_msg *ctx); 54 | err_t spx_msg_align(struct spx_msg *ctx,off_t offset); 55 | void spx_msg_clear(struct spx_msg *ctx); 56 | 57 | err_t spx_msg_pack_int( struct spx_msg *ctx,const int v); 58 | err_t spx_msg_pack_i8(struct spx_msg *ctx,const i8_t v); 59 | err_t spx_msg_pack_i32( struct spx_msg *ctx,const i32_t v); 60 | err_t spx_msg_pack_i64( struct spx_msg *ctx,const i64_t v); 61 | err_t spx_msg_pack_u8( struct spx_msg *ctx,const u8_t v); 62 | err_t spx_msg_pack_u32( struct spx_msg *ctx,const u32_t v); 63 | err_t spx_msg_pack_u64( struct spx_msg *ctx,const u64_t v); 64 | err_t spx_msg_pack_double( struct spx_msg *ctx,const double v); 65 | err_t spx_msg_pack_float( struct spx_msg *ctx,const float v); 66 | err_t spx_msg_pack_true( struct spx_msg *ctx); 67 | err_t spx_msg_pack_false( struct spx_msg *ctx); 68 | err_t spx_msg_pack_string( struct spx_msg *ctx,string_t s); 69 | err_t spx_msg_pack_fixed_string( struct spx_msg *ctx,string_t s,size_t len); 70 | err_t spx_msg_pack_ubytes( struct spx_msg *ctx,const ubyte_t *b,const size_t len); 71 | err_t spx_msg_pack_bytes( struct spx_msg *ctx,const byte_t *b,const size_t len); 72 | err_t spx_msg_pack_fixed_chars( struct spx_msg *ctx,const char *b,const size_t len); 73 | 74 | int spx_msg_unpack_int( struct spx_msg *ctx); 75 | i8_t spx_msg_unpack_i8( struct spx_msg *ctx); 76 | i32_t spx_msg_unpack_i32( struct spx_msg *ctx); 77 | i64_t spx_msg_unpack_i64( struct spx_msg *ctx); 78 | u8_t spx_msg_unpack_u8( struct spx_msg *ctx); 79 | u32_t spx_msg_unpack_u32( struct spx_msg *ctx); 80 | u64_t spx_msg_unpack_u64( struct spx_msg *ctx); 81 | double spx_msg_unpack_double( struct spx_msg *ctx); 82 | float spx_msg_unpack_float( struct spx_msg *ctx); 83 | bool_t spx_msg_unpack_bool( struct spx_msg *ctx); 84 | string_t spx_msg_unpack_string( struct spx_msg *ctx,const size_t len,err_t *err); 85 | ubyte_t *spx_msg_unpack_ubytes( struct spx_msg *ctx,const size_t len,err_t *err); 86 | byte_t *spx_msg_unpack_bytes( struct spx_msg *ctx,const size_t len,err_t *err); 87 | 88 | 89 | struct spx_msg_header *spx_msg_to_header(struct spx_msg *ctx,err_t *err); 90 | struct spx_msg *spx_header_to_msg(struct spx_msg_header *header,size_t len,err_t *err); 91 | 92 | void spx_header_pack(char *buf,struct spx_msg_header *h); 93 | void spx_header_unpack(char *buf,struct spx_msg_header *h); 94 | 95 | void spx_msg_i2b(uchar_t *b,const i32_t n); 96 | i32_t spx_msg_b2i(uchar_t *b); 97 | void spx_msg_l2b(uchar_t *b,const i64_t n) ; 98 | i64_t spx_msg_b2l(uchar_t *b); 99 | void spx_msg_i2b_le(uchar_t *b,const i32_t n); 100 | i32_t spx_msg_b2i_le(uchar_t *b); 101 | 102 | void spx_msg_ul2b(uchar_t *b,const u64_t n); 103 | u64_t spx_msg_b2ul(uchar_t *b); 104 | 105 | spx_private spx_inline size_t spx_msg_size(struct spx_msg *ctx){ 106 | return ctx->s; 107 | } 108 | 109 | spx_private spx_inline size_t spx_msg_len(struct spx_msg *ctx){ 110 | return ctx->busylen; 111 | } 112 | 113 | #define SpxMsgFree(ctx) \ 114 | do{ \ 115 | if(NULL != (ctx)){ \ 116 | spx_msg_free(&(ctx)); \ 117 | ctx = NULL; \ 118 | } \ 119 | }while(false) 120 | 121 | #ifdef __cplusplus 122 | } 123 | #endif 124 | #endif 125 | -------------------------------------------------------------------------------- /spx/include/spx_module.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * this software or lib may be copied only under the terms of the gnu general 5 | * public license v3, which may be found in the source kit. 6 | * 7 | * Filename: spx_thread_context.h 8 | * Created: 2014/07/22 16时09分11秒 9 | * Author: Seapeak.Xu (seapeak.cnblog.com), xvhfeng@gmail.com 10 | * Company: Tencent Literature 11 | * Remark: 12 | * 13 | * ===================================================================================== 14 | */ 15 | #ifndef _SPX_MODULE_H_ 16 | #define _SPX_MODULE_H_ 17 | #ifdef __cplusplus 18 | extern "C" { 19 | #endif 20 | 21 | 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | #include "spx_types.h" 28 | #include "spx_list.h" 29 | #include "spx_fixed_vector.h" 30 | 31 | 32 | typedef void (SpxReceiveTriggerDelegate)(struct ev_loop *loop,ev_io *watcher,int revents); 33 | typedef void (SpxDispatchTriggerDelegate)(int revents,void *arg); 34 | 35 | struct spx_thread_context{ 36 | struct ev_loop *loop; 37 | SpxLogDelegate *log; 38 | size_t idx; 39 | pthread_t tid; 40 | int pipe[2]; 41 | void *c;// add the config info at do dsync 42 | }; 43 | 44 | struct spx_module_context{ 45 | struct spx_list *threadpool; 46 | //usually,receive notification watcher size is equal to thread size 47 | //but dispatch notice watcher size maybe hanving to more than thread size, 48 | //because,receive watcher follow loop and banding with loop by 1:1 49 | //dispatch watcher is maybe n:1 banding with loop when loop is busy 50 | //so we set the n equal 2,donot ask me why,this is a magic 51 | struct spx_list *receive_triggers; 52 | SpxLogDelegate *log; 53 | }; 54 | 55 | 56 | struct spx_receive_context{ 57 | ev_io watcher; 58 | SpxReceiveTriggerDelegate *receive_handler; 59 | SpxLogDelegate *log; 60 | size_t idx; 61 | }; 62 | 63 | 64 | 65 | //today is 2014-07-22,I say to xj about the ydb work over this month, 66 | //but now,I make a mistaken and redo thread-notify,so I can not over the work. 67 | //if he kown this,he want to kill me.is not it?? 68 | 69 | struct spx_module_context *spx_module_new(\ 70 | SpxLogDelegate *log,\ 71 | u32_t threadsize,\ 72 | size_t stack_size,\ 73 | SpxReceiveTriggerDelegate *receive_handler,\ 74 | err_t *err); 75 | 76 | err_t spx_module_free(struct spx_module_context **mc); 77 | 78 | //void spx_module_dispatch(SpxDispatchTriggerDelegate *dispatcher,void *msg); 79 | 80 | #define SpxModuleDispatch(dispatcher,msg) dispatcher(EV_WRITE,msg) 81 | 82 | spx_private struct spx_thread_context *spx_get_thread(struct spx_module_context *mc,size_t idx){ 83 | struct spx_thread_context *stc = spx_list_get(mc->threadpool,idx); 84 | return stc; 85 | } 86 | 87 | struct spx_thread_context *spx_thread_context_new_alone(SpxLogDelegate *log,err_t *err); 88 | #ifdef __cplusplus 89 | } 90 | #endif 91 | #endif 92 | -------------------------------------------------------------------------------- /spx/include/spx_module.hh: -------------------------------------------------------------------------------- 1 | /* 2 | * the functions support the mempool. 3 | * WARN: 4 | * 1: the functions is not thread-safe. 5 | * 2 :the alone object can free \ 6 | * but the small-object cannot free,\ 7 | * and destroy it by clear the buf 8 | * 3: the clenup support to res struct, 9 | * such as fd,locker and so on 10 | */ 11 | 12 | #ifndef SPX_MPOOL_H 13 | #define SPX_MPOOL_H 14 | #ifdef __cplusplus 15 | extern "C" { 16 | #endif 17 | #include 18 | 19 | #include "spx_types.h" 20 | 21 | typedef err_t (SpxMempoolCleanDelegate)(void *e); 22 | 23 | struct spx_mpool_buf; 24 | struct spx_mpool_alone; 25 | struct spx_mpool_cleanup; 26 | struct spx_mpool; 27 | 28 | 29 | extern struct spx_mpool *spx_mpool_init(const size_t size,\ 30 | const size_t limit,err_t *err); 31 | extern void *spx_mpool_alloc(struct spx_mpool * const p,\ 32 | const size_t s,err_t *err); 33 | 34 | void *spx_mpool_cleanup_alloc(struct spx_mpool * const p, 35 | const size_t size,SpxMempoolCleanDelegate *f, 36 | err_t *err); 37 | err_t spx_mpool_cleanup_free(const struct spx_mpool * const p, 38 | void **e); 39 | err_t spx_mpool_reset(struct spx_mpool * const p); 40 | extern err_t spx_mpool_free(struct spx_mpool * const p,void **e); 41 | extern err_t spx_mpool_destroy(struct spx_mpool **p); 42 | 43 | #ifdef __cplusplus 44 | } 45 | #endif 46 | #endif 47 | -------------------------------------------------------------------------------- /spx/include/spx_network_module.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * this software or lib may be copied only under the terms of the gnu general 5 | * public license v3, which may be found in the source kit. 6 | * 7 | * Filename: spx_nio_thread.h 8 | * Created: 2014/07/24 10时23分03秒 9 | * Author: Seapeak.Xu (seapeak.cnblog.com), xvhfeng@gmail.com 10 | * Company: Tencent Literature 11 | * Remark: 12 | * 13 | * ===================================================================================== 14 | */ 15 | #ifndef _SPX_NETWORK_MODULE_H_ 16 | #define _SPX_NETWORK_MODULE_H_ 17 | #ifdef __cplusplus 18 | extern "C" { 19 | #endif 20 | 21 | 22 | #include 23 | #include 24 | #include 25 | 26 | #include "spx_module.h" 27 | #include "spx_job.h" 28 | 29 | extern struct spx_module_context *g_spx_network_module; 30 | 31 | void spx_network_module_receive_handler(struct ev_loop *loop,ev_io *w,int revents); 32 | // void spx_network_module_wakeup_handler(struct ev_loop *loop,ev_io *w,int revents); 33 | void spx_network_module_wakeup_handler(int revents,void *arg); 34 | 35 | spx_private int spx_network_module_wakeup_idx(struct spx_job_context *jc){ 36 | return jc->idx % g_spx_network_module->threadpool->size; 37 | } 38 | 39 | #ifdef __cplusplus 40 | } 41 | #endif 42 | #endif 43 | -------------------------------------------------------------------------------- /spx/include/spx_nio.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: spx_nio.h 5 | * 6 | * Description: ,e 7 | * 8 | * Version: 1.0 9 | * Created: 2014/06/09 17时43分29秒 10 | * Revision: none 11 | * Compiler: gcc 12 | * 13 | * Author: YOUR NAME (), 14 | * Organization: 15 | * 16 | * ===================================================================================== 17 | */ 18 | #ifndef _SPX_NIO_H_ 19 | #define _SPX_NIO_H_ 20 | #ifdef __cplusplus 21 | extern "C" { 22 | #endif 23 | 24 | #include 25 | #include 26 | 27 | #include "spx_types.h" 28 | #include "spx_job.h" 29 | #include "spx_task.h" 30 | 31 | typedef void (SpxAsyncDelegate)(struct ev_loop *loop, ev_async *w, int revents); 32 | typedef void (SpxSateDelegate)(struct ev_loop *loop, ev_stat *w, int revents); 33 | 34 | err_t spx_dio_regedit_async(ev_async *w, 35 | SpxAsyncDelegate *reader,void *data); 36 | 37 | void spx_dio_async_launch(struct ev_loop *loop,ev_async *w, 38 | SpxAsyncDelegate *hander,void *data); 39 | 40 | void spx_dio_regedit_stat(ev_stat *w,string_t path,double ts,SpxSateDelegate *cb,void *arg); 41 | 42 | 43 | 44 | void spx_nio_reader_with_timeout(int revents,void *arg); 45 | void spx_nio_writer_with_timeout(int revents,void *arg); 46 | void spx_nio_reader_with_timeout_fast(int revents,void *arg); 47 | void spx_nio_writer_with_timeout_fast(int revents,void *arg); 48 | 49 | 50 | 51 | #ifdef __cplusplus 52 | } 53 | #endif 54 | #endif 55 | -------------------------------------------------------------------------------- /spx/include/spx_notifier_module.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * this software or lib may be copied only under the terms of the gnu general 5 | * public license v3, which may be found in the source kit. 6 | * 7 | * Filename: spx_sio_thread.h 8 | * Created: 2014/07/23 15时31分02秒 9 | * Author: Seapeak.Xu (seapeak.cnblog.com), xvhfeng@gmail.com 10 | * Company: Tencent Literature 11 | * Remark: 12 | * 13 | * ===================================================================================== 14 | */ 15 | #ifndef _SPX_NOTIFIER_MODULE_H_ 16 | #define _SPX_NOTIFIER_MODULE_H_ 17 | #ifdef __cplusplus 18 | extern "C" { 19 | #endif 20 | 21 | 22 | #include 23 | #include 24 | #include 25 | 26 | #include "spx_module.h" 27 | 28 | extern struct spx_module_context *g_spx_notifier_module; 29 | 30 | void spx_notifier_module_receive_handler(struct ev_loop *loop,ev_io *w,int revents); 31 | // void spx_notifier_module_wakeup_handler(struct ev_loop *loop,ev_io *w,int revents); 32 | void spx_notifier_module_wakeup_handler(int revents,void *arg); 33 | 34 | #ifdef __cplusplus 35 | } 36 | #endif 37 | #endif 38 | -------------------------------------------------------------------------------- /spx/include/spx_path.h: -------------------------------------------------------------------------------- 1 | #ifndef SPX_PATH_H 2 | #define SPX_PATH_H 3 | #ifdef __cplusplus 4 | extern "C" { 5 | #endif 6 | 7 | #include 8 | 9 | #include "spx_types.h" 10 | 11 | #define SpxFileExist(filename) ( 0 == access(filename,F_OK)) 12 | 13 | bool_t spx_is_dir(const string_t path,err_t *err); 14 | err_t spx_mkdir(SpxLogDelegate *log,const string_t path,const mode_t mode); 15 | string_t spx_fullname(const string_t path,const string_t filename,\ 16 | err_t *err); 17 | string_t spx_basepath(const string_t path,err_t *err); 18 | string_t spx_file_suffix(string_t fname,err_t *err); 19 | 20 | u64_t spx_mountpoint_availsize(string_t path,err_t *err); 21 | u64_t spx_mountpoint_totalsize(string_t path,err_t *err); 22 | 23 | #ifdef __splusplus 24 | } 25 | #endif 26 | #endif 27 | -------------------------------------------------------------------------------- /spx/include/spx_periodic.h: -------------------------------------------------------------------------------- 1 | #ifndef _SPX_PERIODIC_H_ 2 | #define _SPX_PERIODIC_H_ 3 | #ifdef __cplusplus 4 | extern "C" { 5 | #endif 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | #include"spx_types.h" 12 | 13 | /** 14 | * Remark: periodic is not the same with timeout-timer. 15 | * this periodic is not acuracy,because periodic and callback 16 | * in the same poll,and if callback is blocking or the time-conuming 17 | * handler,such as:disk-io,network io,bigger-compute... 18 | * they are all need more cpu time to deal. 19 | * so if you want to the periodic become acuracy,you maybe having noblocking. 20 | * and if you want to control everyting and you are not er-ba-dao in C, 21 | * you can use spx_sleep or maked select DIY. 22 | * well,usually,the periodic handler execute is later than the appointed time. 23 | * if you use functions by named *aync*,you will create a pthread 24 | * to deal async. 25 | * */ 26 | 27 | typedef void *(SpxPeriodicDelegate)(void *arg); 28 | 29 | #define SpxperiodicRunning 0 30 | #define SpxperiodicSetPausing 1 31 | #define SpxperiodicPausing 2 32 | 33 | struct spx_periodic{ 34 | SpxLogDelegate *log; 35 | SpxPeriodicDelegate *periodic_handler; 36 | u32_t sec; 37 | u64_t usec; 38 | err_t err; 39 | bool_t is_run; 40 | i32_t status; 41 | void *arg; 42 | pthread_t tid; 43 | pthread_mutex_t *mlock; 44 | pthread_cond_t *clock; 45 | }; 46 | 47 | 48 | void spx_periodic_sleep(int sec,int usec); 49 | 50 | void spx_periodic_run(SpxLogDelegate *log, 51 | u32_t secs,u64_t usecs, 52 | SpxPeriodicDelegate *timeout_handler, 53 | void *arg, 54 | err_t *err); 55 | 56 | struct spx_periodic *spx_periodic_async_run(SpxLogDelegate *log, 57 | u32_t secs,u64_t usecs, 58 | SpxPeriodicDelegate *timeout_handler, 59 | void *arg, 60 | size_t stacksize, 61 | err_t *err); 62 | 63 | void spx_periodic_run_once(SpxLogDelegate *log, 64 | u32_t secs,u64_t usecs, 65 | SpxPeriodicDelegate *timeout_handler, 66 | void *arg, 67 | err_t *err); 68 | 69 | struct spx_periodic *spx_periodic_async_run_once(SpxLogDelegate *log, 70 | u32_t secs,u64_t usecs, 71 | SpxPeriodicDelegate *timeout_handler, 72 | void *arg, 73 | size_t stacksize, 74 | err_t *err); 75 | 76 | void spx_periodic_exec_and_run(SpxLogDelegate *log, 77 | u32_t secs,u64_t usecs, 78 | SpxPeriodicDelegate *timeout_handler, 79 | void *arg, 80 | err_t *err); 81 | 82 | struct spx_periodic *spx_periodic_async_exec_and_run(SpxLogDelegate *log, 83 | u32_t secs,u64_t usecs, 84 | SpxPeriodicDelegate *timeout_handler, 85 | void *arg, 86 | size_t stacksize, 87 | err_t *err); 88 | 89 | struct spx_periodic *spx_periodic_exec_and_async_run(SpxLogDelegate *log, 90 | u32_t secs,u64_t usecs, 91 | SpxPeriodicDelegate *timeout_handler, 92 | void *arg, 93 | size_t stacksize, 94 | err_t *err); 95 | 96 | 97 | /** 98 | * pausing the periodic at next poll 99 | */ 100 | bool_t spx_periodic_async_suspend(struct spx_periodic *t); 101 | bool_t spx_periodic_async_resume(struct spx_periodic *t); 102 | 103 | void spx_periodic_stop( 104 | struct spx_periodic **periodic, 105 | bool_t isblocking); 106 | 107 | #ifdef __cplusplus 108 | } 109 | #endif 110 | #endif 111 | -------------------------------------------------------------------------------- /spx/include/spx_properties.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: spx_properties.h 5 | * 6 | * Description: 7 | * 8 | * Version: 1.0 9 | * Created: 2014/06/19 13时54分14秒 10 | * Revision: none 11 | * Compiler: gcc 12 | * 13 | * Author: YOUR NAME (), 14 | * Organization: 15 | * 16 | * ===================================================================================== 17 | */ 18 | #ifndef _SPX_PROPERTIES_H_ 19 | #define _SPX_PROPERTIES_H_ 20 | #ifdef __cplusplus 21 | extern "C" { 22 | #endif 23 | 24 | #include "spx_types.h" 25 | #include "spx_map.h" 26 | 27 | struct spx_properties; 28 | 29 | typedef void SpxPropertyLineDeserializeDelegate(string_t line,struct spx_properties *p,err_t *err); 30 | typedef string_t SpxPropertyLineSerializeDelegate(struct spx_map_node *node,err_t *err); 31 | typedef err_t SpxPropertyParserBeforeDelegate(struct spx_properties *p); 32 | typedef err_t SpxPropertyParserAfterDelegate(struct spx_properties *p); 33 | 34 | struct spx_properties{ 35 | SpxLogDelegate *log; 36 | // struct spx_mpool *mpool; 37 | struct spx_map *configurtion; 38 | SpxPropertyParserBeforeDelegate *before; 39 | SpxPropertyParserAfterDelegate *after; 40 | SpxPropertyLineDeserializeDelegate *line_deserialize; 41 | SpxPropertyLineSerializeDelegate *line_serialize; 42 | }; 43 | 44 | struct spx_properties *spx_properties_new(SpxLogDelegate *log,\ 45 | SpxPropertyLineDeserializeDelegate *line_deserialize,\ 46 | SpxPropertyLineSerializeDelegate *line_serialize,\ 47 | SpxPropertyParserBeforeDelegate *before,\ 48 | SpxPropertyParserAfterDelegate *after,\ 49 | err_t *err); 50 | struct spx_properties *spx_properties_parser(struct spx_properties *p,string_t filename,err_t *err); 51 | void *spx_properties_get(struct spx_properties *p,string_t name,size_t *size); 52 | err_t spx_properties_set(struct spx_properties *p,string_t name,void *value,size_t size); 53 | string_t spx_properties_tostring(struct spx_properties *p,err_t *err); 54 | void spx_properties_store(struct spx_properties *p,string_t filename,err_t *err); 55 | void spx_properties_free(struct spx_properties **p); 56 | 57 | #ifdef __cplusplus 58 | } 59 | #endif 60 | #endif 61 | -------------------------------------------------------------------------------- /spx/include/spx_queue.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * this software or lib may be copied only under the terms of the gnu general 5 | * public license v3, which may be found in the source kit. 6 | * 7 | * Filename: spx_queue.h 8 | * Created: 2014/08/08 17时53分42秒 9 | * Author: Seapeak.Xu (seapeak.cnblog.com), xvhfeng@gmail.com 10 | * Company: Tencent Literature 11 | * Remark: 12 | * 13 | * ===================================================================================== 14 | */ 15 | #ifndef _SPX_QUEUE_H_ 16 | #define _SPX_QUEUE_H_ 17 | #ifdef __cplusplus 18 | extern "C" { 19 | #endif 20 | 21 | 22 | #include 23 | #include 24 | 25 | #include "spx_types.h" 26 | 27 | typedef err_t SpxQueueNodeFreeDelegate(void **v); 28 | struct spx_queue_node{ 29 | struct spx_queue_node *prev; 30 | void *v; 31 | }; 32 | 33 | struct spx_queue{ 34 | size_t size; 35 | SpxLogDelegate *log; 36 | struct spx_queue_node *header; 37 | struct spx_queue_node *tail; 38 | SpxQueueNodeFreeDelegate *free; 39 | }; 40 | 41 | struct spx_queue *spx_queue_new(SpxLogDelegate *log, 42 | SpxQueueNodeFreeDelegate *free, 43 | err_t *err); 44 | 45 | void *spx_queue_pop(struct spx_queue *q,err_t *err); 46 | err_t spx_queue_push(struct spx_queue *q,void *n); 47 | err_t spx_queue_free(struct spx_queue **q); 48 | err_t spx_queue_clear(struct spx_queue *q); 49 | 50 | #ifdef __cplusplus 51 | } 52 | #endif 53 | #endif 54 | -------------------------------------------------------------------------------- /spx/include/spx_rand.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * this software or lib may be copied only under the terms of the gnu general 5 | * public license v3, which may be found in the source kit. 6 | * 7 | * Filename: spx_rand.h 8 | * Created: 2014/07/15 11时49分05秒 9 | * Author: Seapeak.Xu (seapeak.cnblog.com), xvhfeng@gmail.com 10 | * Company: Tencent Literature 11 | * Remark: 12 | * 13 | * ===================================================================================== 14 | */ 15 | #ifndef _SPX_RAND_H_ 16 | #define _SPX_RAND_H_ 17 | #ifdef __cplusplus 18 | extern "C" { 19 | #endif 20 | 21 | #include 22 | #include 23 | 24 | #include "spx_types.h" 25 | 26 | u32_t spx_random(u32_t idx); 27 | u32_t spx_srandom(); 28 | 29 | 30 | #ifdef __cplusplus 31 | } 32 | #endif 33 | #endif 34 | -------------------------------------------------------------------------------- /spx/include/spx_skiplist.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: spx_skiplist.h 5 | * 6 | * Description: 7 | * 8 | * Version: 1.0 9 | * Created: 2014/05/21 16时57分57秒 10 | * Revision: none 11 | * Compiler: gcc 12 | * 13 | * Author: YOUR NAME (), 14 | * Organization: 15 | * 16 | * ===================================================================================== 17 | */ 18 | #ifndef _SPX_SKIPLIST_H_ 19 | #define _SPX_SKIPLIST_H_ 20 | #ifdef __cplusplus 21 | extern "C" { 22 | #endif 23 | 24 | /* 25 | #include "spx_types.h" 26 | #include "spx_vector.h" 27 | #include "spx_collection.h" 28 | 29 | #define SPX_SKIPLIST_LEVEL_DEFAULT 16 30 | #define SPX_SKIPLIST_IDX_I32 0 31 | #define SPX_SKIPLIST_IDX_I64 1 32 | #define SPX_SKIPLIST_IDX_U32 2 33 | #define SPX_SKIPLIST_IDX_U64 3 34 | #define SPX_SKIPLIST_IDX_STRING 4 35 | #define SPX_SKIPLIST_IDX_TIME 5 36 | #define SPX_SKIPLIST_IDX_OBJECT 6 37 | 38 | typedef int SpxSkipListRangeCmperDelegate(\ 39 | void *k1,u32_t l1,void *k2,u32_t l2); 40 | typedef bool_t SpxSkipListUniqueInspectorDelegate(\ 41 | void *v1,size_t l1,void *v2,size_t l2); 42 | 43 | 44 | struct spx_skiplist_n{ 45 | u32_t level; 46 | void *k; 47 | u32_t kl; 48 | union{ 49 | struct spx_vector *list; 50 | struct spx_skiplist_v *obj; 51 | }v; 52 | struct spx_skiplist_n *next[]; 53 | }; 54 | 55 | struct spx_skiplist_v{ 56 | void *v; 57 | size_t s; 58 | }; 59 | 60 | struct spx_skiplist { 61 | u32_t type; 62 | u32_t level; 63 | u32_t maxlevel; 64 | u64_t key_count; 65 | u64_t val_count; 66 | bool_t allow_conflict; 67 | struct spx_skiplist_n *header; 68 | SpxCollectionCmperDelegate *cmp; 69 | SpxSkipListUniqueInspectorDelegate *inspector; 70 | SpxCollectionKeyFreeDelegate *kfree; 71 | SpxCollectionValueFreeDelegate *vfree; 72 | SpxCollectionKeyPrintfDelegate *kprintf; 73 | SpxLogDelegate *log; 74 | }; 75 | 76 | struct spx_skiplist *spx_skiplist_new(SpxLogDelegate *log,\ 77 | int type,u32_t maxlevel,\ 78 | bool_t allow_conflict, 79 | SpxCollectionCmperDelegate cmper,\ 80 | SpxSkipListUniqueInspectorDelegate *inspector,\ 81 | SpxCollectionKeyPrintfDelegate *printf,\ 82 | SpxCollectionKeyFreeDelegate *kfree,\ 83 | SpxCollectionValueFreeDelegate *vfree,\ 84 | err_t *err); 85 | 86 | err_t spx_skiplist_insert(struct spx_skiplist *spl,\ 87 | void *k,u32_t kl,void *v,u64_t vl, 88 | int level); 89 | 90 | err_t spx_skiplist_out(struct spx_skiplist *spl,\ 91 | void *k,u32_t kl,void **v,u64_t *vl, 92 | SpxSkipListRangeCmperDelegate *searcher); 93 | 94 | err_t spx_skiplist_delete(struct spx_skiplist *spl,\ 95 | void *k,u32_t l); 96 | err_t spx_skiplist_search(struct spx_skiplist *spl,\ 97 | void *min,u32_t l1,void *max,u32_t l2); 98 | 99 | err_t spx_skiplist_find(struct spx_skiplist *spl,\ 100 | void *k,u32_t l); 101 | void spx_skiplist_free(struct spx_skiplist **spl); 102 | 103 | */ 104 | #ifdef __cplusplus 105 | } 106 | #endif 107 | #endif 108 | -------------------------------------------------------------------------------- /spx/include/spx_socket.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: spx_socket.h 5 | * 6 | * Description: 7 | * 8 | * 9 | * Version: 1.0 10 | * Created: 2014/06/11 14时33分39秒 11 | * Revision: none 12 | * Compiler: gcc 13 | * 14 | * Author: YOUR NAME (), 15 | * Organization: 16 | * 17 | * ===================================================================================== 18 | */ 19 | #ifndef _SPX_SOCKET_H_ 20 | #define _SPX_SOCKET_H_ 21 | #ifdef __cplusplus 22 | extern "C" { 23 | #endif 24 | 25 | #include "spx_types.h" 26 | 27 | 28 | 29 | #define SpxKeepAlive true 30 | #define SpxAliveTimeout 30 31 | #define SpxDetectTimes 3 32 | #define SpxDetectTimeout 30 33 | #define SpxLinger false 34 | #define SpxLingerTimeout 0 35 | #define SpxNodelay true 36 | 37 | int spx_socket_new(err_t *err); 38 | //void spx_socket_accept_nb(int fd); 39 | err_t spx_socket_start(const int fd,\ 40 | string_t ip,const int port,\ 41 | bool_t is_keepalive,size_t alive_timeout,\ 42 | size_t detect_times,size_t detect_timeout,\ 43 | bool_t is_linger,size_t linger_timeout,\ 44 | bool_t is_nodelay,\ 45 | bool_t is_timeout,size_t timeout,\ 46 | size_t listens); 47 | string_t spx_ip_get(int sock,err_t *err); 48 | 49 | string_t spx_host_tostring(struct spx_host *host,err_t *err); 50 | 51 | err_t spx_socket_set(const int fd,\ 52 | bool_t is_keepalive,size_t alive_timeout,\ 53 | size_t detect_times,size_t detect_timeout,\ 54 | bool_t is_linger,size_t linger_timeout,\ 55 | bool_t is_nodelay,\ 56 | bool_t is_timeout,size_t timeout); 57 | err_t spx_socket_connect(int fd,string_t ip,int port); 58 | err_t spx_socket_connect_nb(int fd,string_t ip,int port,u32_t timeout); 59 | string_t spx_socket_getipbyname(string_t name,err_t *err); 60 | bool_t spx_socket_is_ip(string_t ip); 61 | err_t spx_tcp_nodelay(int fd,bool_t enable); 62 | bool_t spx_socket_ready_read(int fd,u32_t timeout); 63 | bool_t spx_socket_read_timeout(int fd,u32_t timeout); 64 | bool_t spx_socket_test(int fd); 65 | 66 | #ifdef __cplusplus 67 | } 68 | #endif 69 | #endif 70 | -------------------------------------------------------------------------------- /spx/include/spx_socket_accept.h: -------------------------------------------------------------------------------- 1 | #ifndef _SPX_SOCKET_ACCEPT_H_ 2 | #define _SPX_SOCKET_ACCEPT_H_ 3 | #ifdef __cplusplus 4 | extern "C" { 5 | #endif 6 | 7 | #include 8 | 9 | #include "spx_types.h" 10 | 11 | //void spx_socket_accept_nb(SpxLogDelegate *log,int fd); 12 | void spx_socket_accept_nb(SpxLogDelegate *log, 13 | struct ev_loop *loop,int fd); 14 | 15 | #ifdef __cplusplus 16 | } 17 | #endif 18 | #endif 19 | -------------------------------------------------------------------------------- /spx/include/spx_stdf.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef SPX_STDF_H 3 | #define SPX_STDF_H 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | #include 9 | #include "spx_types.h" 10 | 11 | struct spx_stdf{ 12 | FILE *p; 13 | bool_t isbry; 14 | bool_t is_stp; 15 | uchar_t stp; 16 | size_t max; 17 | off_t offset; 18 | enum spx_fcs{ 19 | Line, 20 | Buf, 21 | Size 22 | } cs; 23 | }; 24 | #ifdef __cplusplus 25 | } 26 | #endif 27 | #endif 28 | -------------------------------------------------------------------------------- /spx/include/spx_task.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * this software or lib may be copied only under the terms of the gnu general 5 | * public license v3, which may be found in the source kit. 6 | * 7 | * Filename: spx_dio_context.h 8 | * Created: 2014/07/24 17时53分07秒 9 | * Author: Seapeak.Xu (seapeak.cnblog.com), xvhfeng@gmail.com 10 | * Company: Tencent Literature 11 | * Remark: 12 | * 13 | * ===================================================================================== 14 | */ 15 | #ifndef _SPX_TASK_H_ 16 | #define _SPX_TASK_H_ 17 | #ifdef __cplusplus 18 | extern "C" { 19 | #endif 20 | 21 | 22 | #include 23 | #include 24 | #include 25 | 26 | #include "spx_types.h" 27 | #include "spx_defs.h" 28 | #include "spx_message.h" 29 | #include "spx_properties.h" 30 | 31 | 32 | //play a trick 33 | //and look at me how to implemant sub-calss 34 | //it must be putted into sub-class with begin. 35 | //if you do not understand,please no modifition. 36 | #define SpxDioMembers \ 37 | int fd 38 | 39 | // struct spx_dio_file{ 40 | // SpxDioMembers; 41 | // }; 42 | 43 | struct spx_task_context; 44 | typedef void (SpxDioDelegate)(struct ev_loop *loop,ev_io *watcher,int revents); 45 | typedef err_t (SpxDioProcessDelegate)(struct ev_loop *loop,int idx,struct spx_task_context *tcontext); 46 | 47 | struct spx_task_context{ 48 | ev_io watcher; 49 | size_t idx; 50 | size_t tidx; 51 | err_t err; 52 | //the memory means balcking is true 53 | //and the callback(dio_handler) must keep 54 | //monopolizing the sharing resource 55 | //and the noblacking io(Inc.disk io) means false 56 | // bool_t blacking; 57 | // SpxDioDelegate *dio_handler; 58 | SpxDioProcessDelegate *dio_process_handler; 59 | SpxLogDelegate *log; 60 | struct spx_job_context *jcontext; 61 | // int events; 62 | }; 63 | 64 | struct spx_task_context_transport{ 65 | SpxLogDelegate *log; 66 | SpxDioProcessDelegate *dio_process_handler; 67 | }; 68 | 69 | struct spx_task_pool { 70 | SpxLogDelegate *log; 71 | struct spx_fixed_vector *pool; 72 | }; 73 | 74 | extern struct spx_task_pool *g_spx_task_pool; 75 | 76 | void *spx_task_context_new(size_t idx,void *arg,err_t *err); 77 | err_t spx_task_context_free(void **arg); 78 | void spx_task_context_clear(struct spx_task_context *tcontext); 79 | 80 | struct spx_task_pool *spx_task_pool_new(\ 81 | SpxLogDelegate *log,\ 82 | size_t size,\ 83 | SpxDioProcessDelegate *dio_process_handler,\ 84 | err_t *err); 85 | 86 | struct spx_task_context *spx_task_pool_pop(\ 87 | struct spx_task_pool *pool,\ 88 | err_t *err); 89 | 90 | err_t spx_task_pool_push(\ 91 | struct spx_task_pool *pool,\ 92 | struct spx_task_context *tcontext); 93 | 94 | err_t spx_task_pool_free(\ 95 | struct spx_task_pool **pool); 96 | 97 | 98 | 99 | 100 | #ifdef __cplusplus 101 | } 102 | #endif 103 | #endif 104 | -------------------------------------------------------------------------------- /spx/include/spx_task_module.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * this software or lib may be copied only under the terms of the gnu general 5 | * public license v3, which may be found in the source kit. 6 | * 7 | * Filename: spx_dio_thread.h 8 | * Created: 2014/07/24 10时23分15秒 9 | * Author: Seapeak.Xu (seapeak.cnblog.com), xvhfeng@gmail.com 10 | * Company: Tencent Literature 11 | * Remark: 12 | * 13 | * ===================================================================================== 14 | */ 15 | #ifndef _SPX_TASK_MODULE_H_ 16 | #define _SPX_TASK_MODULE_H_ 17 | #ifdef __cplusplus 18 | extern "C" { 19 | #endif 20 | 21 | 22 | #include 23 | #include 24 | #include 25 | 26 | #include "spx_types.h" 27 | #include "spx_defs.h" 28 | #include "spx_task.h" 29 | #include "spx_module.h" 30 | 31 | extern struct spx_module_context *g_spx_task_module; 32 | void spx_task_module_receive_handler(struct ev_loop *loop,ev_io *w,int revents); 33 | // void spx_task_module_wakeup_handler(struct ev_loop *loop,ev_io *w,int revents); 34 | void spx_task_module_wakeup_handler(int revents,void *arg); 35 | 36 | spx_private spx_inline int spx_task_module_wakeup_idx(int idx){ 37 | return 0 > idx ? g_spx_task_module->threadpool->size - 1 : idx % (g_spx_task_module->threadpool->size -2); 38 | } 39 | 40 | #ifdef __cplusplus 41 | } 42 | #endif 43 | #endif 44 | -------------------------------------------------------------------------------- /spx/include/spx_thread.h: -------------------------------------------------------------------------------- 1 | /*********************************************************************** 2 | * _ooOoo_ 3 | * o8888888o 4 | * 88" . "88 5 | * (| -_- |) 6 | * O\ = /O 7 | * ____/`---'\____ 8 | * . ' \\| |// `. 9 | * / \\||| : |||// \ 10 | * / _||||| -:- |||||- \ 11 | * | | \\\ - /// | | 12 | * | \_| ''\---/'' | | 13 | * \ .-\__ `-` ___/-. / 14 | * ___`. .' /--.--\ `. . __ 15 | * ."" '< `.___\_<|>_/___.' >'"". 16 | * | | : `- \`.;`\ _ /`;.`/ - ` : | | 17 | * \ \ `-. \_ __\ /__ _/ .-` / / 18 | * ======`-.____`-.___\_____/___.-`____.-'====== 19 | * `=---=' 20 | * ............................................. 21 | * 佛祖镇楼 BUG辟易 22 | * 佛曰: 23 | * 写字楼里写字间,写字间里程序员; 24 | * 程序人员写程序,又拿程序换酒钱。 25 | * 酒醒只在网上坐,酒醉还来网下眠; 26 | * 酒醉酒醒日复日,网上网下年复年。 27 | * 但愿老死电脑间,不愿鞠躬老板前; 28 | * 奔驰宝马贵者趣,公交自行程序员。 29 | * 别人笑我忒疯癫,我笑自己命太贱; 30 | * 不见满街漂亮妹,哪个归得程序员? 31 | * ========================================================================== 32 | * 33 | * this software or lib may be copied only under the terms of the gnu general 34 | * public license v3, which may be found in the source kit. 35 | * 36 | * Filename: spx_thread.h 37 | * Created: 2014/09/01 17时27分29秒 38 | * Author: Seapeak.Xu (seapeak.cnblog.com), xvhfeng@gmail.com 39 | * Company: Tencent Literature 40 | * Remark: 41 | * 42 | ****************************************************************************/ 43 | #ifndef _SPX_THREAD_H_ 44 | #define _SPX_THREAD_H_ 45 | #ifdef __cplusplus 46 | extern "C" { 47 | #endif 48 | 49 | 50 | #include 51 | #include 52 | #include 53 | 54 | #include "spx_types.h" 55 | 56 | typedef void (SpxThreadCleanupDelegate)(void *arg); 57 | 58 | /** 59 | * this function alloc mutex-locker must use 60 | * spx_thread_mutex_free function to free the mutex locker 61 | */ 62 | pthread_mutex_t *spx_thread_mutex_new(SpxLogDelegate *log, 63 | err_t *err); 64 | void spx_thread_mutex_free(pthread_mutex_t **mlock); 65 | 66 | pthread_cond_t *spx_thread_cond_new(SpxLogDelegate *log, 67 | err_t *err); 68 | void spx_thread_cond_free(pthread_cond_t **clock); 69 | 70 | pthread_t spx_thread_new(SpxLogDelegate *log, 71 | size_t stacksize, 72 | void *(*start_routine)(void*), 73 | void *arg, 74 | err_t *err); 75 | 76 | pthread_t spx_detach_thread_new(SpxLogDelegate *log, 77 | size_t stacksize, 78 | void *(*start_routine)(void*), 79 | void *arg, 80 | err_t *err); 81 | 82 | pthread_t spx_thread_new_cancelability(SpxLogDelegate *log, 83 | size_t stacksize, 84 | void *(*start_routine)(void *), 85 | void *arg, 86 | err_t *err); 87 | #ifdef __cplusplus 88 | } 89 | #endif 90 | #endif 91 | -------------------------------------------------------------------------------- /spx/include/spx_threadpool.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * this software or lib may be copied only under the terms of the gnu general 5 | * public license v3, which may be found in the source kit. 6 | * 7 | * Filename: spx_threadpool.h 8 | * Created: 2014/09/01 11时41分08秒 9 | * Author: Seapeak.Xu (seapeak.cnblog.com), xvhfeng@gmail.com 10 | * Company: Tencent Literature 11 | * Remark: 12 | * 13 | * 14 | * ===================================================================================== 15 | */ 16 | 17 | #ifndef _SPX_THREADPOOL_H_ 18 | #define _SPX_THREADPOOL_H_ 19 | #ifdef __cplusplus 20 | extern "C" { 21 | #endif 22 | 23 | #include 24 | #include 25 | #include 26 | 27 | #include "spx_types.h" 28 | #include "spx_list.h" 29 | 30 | typedef void (SpxThreadpoolExecuteDelegate)(void *); 31 | 32 | enum spx_pooling_thread_state { 33 | uninitialized, initializing, initialized, uninstalling, uninstalled, 34 | }; 35 | 36 | struct spx_threadpool; 37 | 38 | struct spx_thread { 39 | SpxLogDelegate *log; 40 | pthread_t id; 41 | pthread_mutex_t mutex_locker; 42 | pthread_cond_t run_locker; 43 | SpxThreadpoolExecuteDelegate *func; 44 | struct spx_threadpool *parent; 45 | void *arg; 46 | struct spx_thread *next; 47 | } thread_t; 48 | 49 | struct spx_threadpool { 50 | SpxLogDelegate *log; 51 | // struct spx_thread **threads; 52 | struct spx_thread *busy; 53 | struct spx_thread *free; 54 | pthread_mutex_t mutex_locker; 55 | pthread_cond_t run_locker; 56 | pthread_cond_t full_locker; 57 | pthread_cond_t empty_locker; 58 | enum spx_pooling_thread_state state; 59 | size_t totalsize; 60 | size_t currsize; 61 | size_t thread_stack_size; 62 | size_t waits; 63 | // size_t idx; 64 | }; 65 | 66 | struct spx_threadpool *spx_threadpool_new(SpxLogDelegate *log, 67 | size_t max,size_t thread_stack_size,err_t *err); 68 | err_t spx_threadpool_execute(struct spx_threadpool *p, 69 | SpxThreadpoolExecuteDelegate *func,void *arg); 70 | err_t spx_threadpool_free(struct spx_threadpool **p); 71 | 72 | 73 | 74 | 75 | 76 | #ifdef __cplusplus 77 | } 78 | #endif 79 | #endif 80 | -------------------------------------------------------------------------------- /spx/include/spx_time.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: spx_time.h 5 | * 6 | * Description: 7 | * 8 | * Version: 1.0 9 | * Created: 2014/05/20 10时20分41秒 10 | * Revision: none 11 | * Compiler: gcc 12 | * 13 | * Author: YOUR NAME (), 14 | * Organization: 15 | * 16 | * ===================================================================================== 17 | */ 18 | 19 | #ifndef SPX_TIME_H 20 | #define SPX_TIME_H 21 | #if __cplusplus 22 | extern "C" { 23 | #endif 24 | 25 | #include "spx_types.h" 26 | 27 | #define SpxYear(dt) ((dt)->d.year) 28 | #define SpxMonth(dt) ((dt)->d.month) 29 | #define SpxDay(dt) ((dt)->d.day) 30 | #define SpxHour(dt) ((dt)->t.hour) 31 | #define SpxMinute(dt) ((dt)->t.min) 32 | #define SpxSecond(dt) ((dt)->t.sec) 33 | 34 | 35 | 36 | void spx_get_curr_datetime(struct spx_datetime *dt); 37 | struct spx_date *spx_get_today(struct spx_date *d); 38 | time_t spx_now(); 39 | u64_t spx_now_usec(); 40 | time_t spx_mktime(struct spx_datetime *dt); 41 | struct spx_datetime *spx_datetime_dup(struct spx_datetime *dt,err_t *err); 42 | struct spx_datetime *spx_datetime_add_days(struct spx_datetime *dt,int days); 43 | struct spx_datetime *spx_get_datetime(time_t *t,struct spx_datetime *dt); 44 | struct spx_date *spx_get_date(time_t *t,struct spx_date *d); 45 | struct spx_date *spx_get_today(struct spx_date *d); 46 | time_t spx_zero(struct spx_date *d); 47 | struct spx_date *spx_date_add(struct spx_date *d,int days); 48 | bool_t spx_date_is_before(struct spx_date *d); 49 | bool_t spx_date_is_after(struct spx_date *d); 50 | bool_t spx_date_is_today(struct spx_date *d); 51 | /* * 52 | * cmp d1 and d2 53 | * if d1 < d2 return -1 54 | * if d1 == d2 return 0 55 | * if d1 > d2 return 1 56 | * notes:base of cmper is 00:00:00 57 | * */ 58 | int spx_date_cmp(struct spx_date *d1,struct spx_date *d2); 59 | 60 | /* * 61 | * cmp dt1 and dt2 62 | * if d1 < d2 return -1 63 | * if d1 == d2 return 0 64 | * if d1 > d2 return 1 65 | * */ 66 | int spx_datetime_cmp(struct spx_datetime *dt1,struct spx_datetime *dt2); 67 | 68 | struct spx_date *spx_date_convert(SpxLogDelegate *log, 69 | struct spx_date *d, 70 | string_t s,char *fmt,err_t *err); 71 | 72 | struct spx_time *spx_time_convert(SpxLogDelegate *log, 73 | struct spx_time *t, 74 | string_t s,char *fmt,err_t *err); 75 | 76 | struct spx_datetime *spx_datetime_convert(SpxLogDelegate *log, 77 | struct spx_datetime *dt, 78 | string_t s,char *fmt,err_t *err); 79 | 80 | err_t spx_modify_filetime( 81 | const string_t fname, 82 | u64_t secs 83 | ); 84 | time_t spx_get_token(); 85 | 86 | int spx_get_tokendays(); 87 | #if __cplusplus 88 | } 89 | #endif 90 | #endif 91 | -------------------------------------------------------------------------------- /spx/include/spx_types.h: -------------------------------------------------------------------------------- 1 | #ifndef SPX_TYPES_H 2 | #define SPX_TYPES_H 3 | #ifdef __cplusplus 4 | extern "C" { 5 | #endif 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | #ifndef bool_t 14 | typedef enum { 15 | false = 0, 16 | true = 1 17 | }bool_t; 18 | #endif 19 | 20 | 21 | #ifndef byte_t 22 | typedef signed char byte_t; 23 | #endif 24 | 25 | #ifndef ubyte_t 26 | typedef unsigned char ubyte_t; 27 | #endif 28 | 29 | #ifndef uchar_t 30 | typedef unsigned char uchar_t; 31 | #endif 32 | 33 | #ifndef bstring_t 34 | typedef struct { 35 | size_t s; 36 | uchar_t v[0]; 37 | }bstring_t; 38 | #endif 39 | 40 | //i think we do not need to define the struct for string 41 | //and string not must need length in the struct 42 | //but i think we must have the sign('\0') of the string end 43 | #ifndef string_t 44 | typedef char * string_t; 45 | #endif 46 | 47 | #ifndef wstring_t 48 | typedef wchar_t * wstring_t; 49 | #endif 50 | 51 | #ifndef u64_t 52 | typedef u_int64_t u64_t; 53 | #endif 54 | 55 | #ifndef u32_t 56 | typedef u_int32_t u32_t; 57 | #endif 58 | 59 | #ifndef u16_t 60 | typedef u_int16_t u16_t; 61 | #endif 62 | 63 | #ifndef u8_t 64 | typedef u_int8_t u8_t; 65 | #endif 66 | 67 | #ifndef i64_t 68 | typedef int64_t i64_t; 69 | #endif 70 | 71 | #ifndef i32_t 72 | typedef int32_t i32_t; 73 | #endif 74 | 75 | #ifndef i16_t 76 | typedef int16_t i16_t; 77 | #endif 78 | 79 | #ifndef i8_t 80 | typedef int8_t i8_t; 81 | #endif 82 | 83 | #ifndef SpxLogDelegate 84 | typedef void (SpxLogDelegate)(int level,string_t fmt,...); 85 | #endif 86 | 87 | #ifndef ptr_t 88 | typedef char * ptr_t; 89 | #endif 90 | 91 | #ifndef spx_atomic_t 92 | typedef long spx_atomic_t; 93 | #endif 94 | 95 | #ifndef spx_uatomit_t 96 | typedef unsigned long spx_uatomic_t; 97 | #endif 98 | 99 | #ifndef err_t 100 | #define err_t int 101 | #endif 102 | 103 | #ifndef spx_inline 104 | #define spx_inline inline 105 | #endif 106 | 107 | #ifndef spx_private 108 | #define spx_private static 109 | #endif 110 | 111 | #ifndef spx_public 112 | #define spx_public 113 | #endif 114 | 115 | struct spx_host{ 116 | string_t ip; 117 | int port; 118 | }; 119 | 120 | struct spx_date{ 121 | int year; 122 | int month; 123 | int day; 124 | }; 125 | 126 | struct spx_time{ 127 | int hour; 128 | int min; 129 | int sec; 130 | }; 131 | 132 | struct spx_datetime{ 133 | struct spx_date d; 134 | struct spx_time t; 135 | }; 136 | 137 | spx_private char *spx_bool_desc[] = { 138 | "false", 139 | "true" 140 | }; 141 | 142 | 143 | 144 | 145 | #ifndef spx_res_t 146 | typedef struct spx_res{ 147 | int t; 148 | union{ 149 | int fd; 150 | struct{ 151 | int fd; 152 | char *ptr; 153 | }mmap; 154 | FILE *fp; 155 | }r; 156 | }spx_res_t; 157 | #define SpxFd(r) (r->r.file.fd) 158 | #define SpxMapFd(r) (r->r.mmap.fd) 159 | #define SpxMap(r) (r->r.mmap.ptr) 160 | #define SpxFILE(r) (r->r.fp); 161 | #endif 162 | 163 | 164 | #ifdef __cplusplus 165 | } 166 | #endif 167 | #endif 168 | 169 | -------------------------------------------------------------------------------- /spx/include/spx_vector.h: -------------------------------------------------------------------------------- 1 | #ifndef SPX_VECTOR_H 2 | #define SPX_VECTOR_H 3 | #ifdef __cplusplus 4 | extern "C" { 5 | #endif 6 | 7 | #include "spx_types.h" 8 | 9 | struct spx_vector_node{ 10 | struct spx_vector_node *prev; 11 | struct spx_vector_node *next; 12 | void *v; 13 | }; 14 | 15 | typedef err_t (SpxVectorValueFreeDelegate)(void **v); 16 | 17 | struct spx_vector{ 18 | struct spx_vector_node *header; 19 | struct spx_vector_node *tail; 20 | SpxVectorValueFreeDelegate *handle; 21 | size_t size; 22 | SpxLogDelegate *log; 23 | }; 24 | 25 | 26 | struct spx_vector_iter{ 27 | struct spx_vector *vector; 28 | struct spx_vector_node *curr; 29 | }; 30 | 31 | struct spx_vector *spx_vector_init(SpxLogDelegate *log,\ 32 | SpxVectorValueFreeDelegate *handle,err_t *err); 33 | err_t spx_vector_free(struct spx_vector **vector); 34 | err_t spx_vector_add(struct spx_vector *vector,void *v); 35 | void *spx_vector_get(struct spx_vector *vector,size_t idx,err_t *err); 36 | err_t spx_vector_push(struct spx_vector *vector,void *v); 37 | void *spx_vector_pop(struct spx_vector *vector, err_t *err); 38 | 39 | 40 | 41 | 42 | struct spx_vector_iter *spx_vector_iter_new(struct spx_vector *vector,\ 43 | err_t *err); 44 | err_t spx_vector_iter_free(struct spx_vector_iter **iter); 45 | void *spx_vector_iter_next(struct spx_vector_iter *iter) ; 46 | void spx_vector_iter_reset(struct spx_vector_iter *iter); 47 | 48 | 49 | #ifdef __cplusplus 50 | } 51 | #endif 52 | #endif 53 | -------------------------------------------------------------------------------- /spx/include/spx_web_server.h: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: spx_web_server.h 3 | > Author: shuaixiang 4 | > Mail: 5 | > Created Time: Fri 27 Nov 2015 08:11:59 AM UTC 6 | ************************************************************************/ 7 | #ifndef SPX_WEB_SERVER_H 8 | #define SPX_WEB_SERVER_H 9 | #ifdef __cplusplus 10 | extern "C" { 11 | #endif 12 | 13 | #include "ev.h" 14 | #include "spx_types.h" 15 | 16 | #define MAX_LEN 1024 17 | #define HTTP_HEADER_SIZE 1024 18 | 19 | struct server_config{ 20 | SpxLogDelegate *log; 21 | u64_t stacksize; 22 | string_t ip; 23 | i32_t port; 24 | u32_t timeout; 25 | u32_t retry_times; 26 | u32_t context_pool_size; 27 | }; 28 | 29 | struct param_set_node{ 30 | char key[MAX_LEN]; 31 | char value[MAX_LEN]; 32 | struct param_set_node *next_param; 33 | }; 34 | 35 | struct param_set{ 36 | struct param_set_node *param_head; 37 | struct param_set_node *param_tail; 38 | }; 39 | 40 | struct request{ 41 | size_t retry; 42 | size_t header_len; 43 | size_t header_size; 44 | size_t content_len; 45 | size_t content_size; 46 | struct param_set *p_set; 47 | 48 | char header[HTTP_HEADER_SIZE]; 49 | char *content; 50 | }; 51 | 52 | struct response{ 53 | size_t retry; 54 | size_t header_len; 55 | size_t header_size; 56 | size_t split_size; 57 | size_t content_len; 58 | size_t content_size; 59 | 60 | char header[HTTP_HEADER_SIZE]; 61 | char *content; 62 | }; 63 | 64 | struct server_context{ 65 | char life_cycle; 66 | int fd; 67 | ev_tstamp timeout; 68 | ev_async async_watcher; 69 | struct server_context *p_next_ctx; 70 | struct server_context *p_pre_ctx; 71 | char *(*ServerHandler)(struct request *req); 72 | 73 | struct request *req; 74 | struct response *resp; 75 | }; 76 | 77 | err_t spx_web_server_start(char *(*ServerHandler)(struct request *req), struct server_config *conf); 78 | struct param_set_node* spx_web_server_param_set_get(struct request *req, char *key); 79 | 80 | #ifdef __cplusplus 81 | } 82 | #endif 83 | #endif 84 | -------------------------------------------------------------------------------- /spx/spx_alloc.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "spx_types.h" 6 | #include "spx_defs.h" 7 | #include "spx_alloc.h" 8 | 9 | void *spx_malloc(const size_t s,err_t *err){ 10 | if(0 == s){ 11 | *err = EINVAL; 12 | return NULL; 13 | } 14 | void *p = malloc(s); 15 | if(NULL == p){ 16 | *err = 0 == errno ? ENOMEM : errno; 17 | } 18 | return p; 19 | } 20 | 21 | void *spx_alloc(const size_t numbs,const size_t s,err_t *err){ 22 | if(0 == s || 0 == numbs){ 23 | *err = EINVAL; 24 | return NULL; 25 | } 26 | void *p = calloc(numbs,s); 27 | if(NULL == p){ 28 | *err = 0 == errno ? ENOMEM : errno; 29 | return NULL; 30 | } 31 | return p; 32 | } 33 | 34 | void *spx_alloc_alone(const size_t s,err_t *err){ 35 | if(0 == s){ 36 | *err = EINVAL; 37 | return NULL; 38 | } 39 | return spx_alloc(1,s,err); 40 | } 41 | 42 | void *spx_memalign_alloc(const size_t size,err_t *err) { 43 | void *p = NULL; 44 | if(0 != (*err = posix_memalign(&p, SPX_ALIGN_SIZE, size))){ 45 | return NULL; 46 | } 47 | return p; 48 | } 49 | 50 | void *spx_alloc_mptr(const size_t numbs,err_t *err){ 51 | void *p = calloc(numbs,SpxPtrSize); 52 | if(NULL == p){ 53 | *err = 0 == errno ? ENOMEM : errno; 54 | } 55 | return p; 56 | } 57 | 58 | void *spx_realloc(void *p,const size_t size,err_t *err){ 59 | void *ptr = realloc(p,size); 60 | if(NULL == ptr){ 61 | *err = 0 == errno ? ENOMEM : errno; 62 | return NULL; 63 | } 64 | return ptr; 65 | } 66 | -------------------------------------------------------------------------------- /spx/spx_configurtion.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * this software or lib may be copied only under the terms of the gnu general 5 | * public license v3, which may be found in the source kit. 6 | * 7 | * Filename: spx_configurtion.c 8 | * Created: 2014/07/16 16时37分49秒 9 | * Author: Seapeak.Xu (seapeak.cnblog.com), xvhfeng@gmail.com 10 | * Company: Tencent Literature 11 | * Remark: 12 | * 13 | * ===================================================================================== 14 | */ 15 | 16 | #include 17 | #include 18 | 19 | #include "spx_types.h" 20 | #include "spx_defs.h" 21 | #include "spx_string.h" 22 | #include "spx_configurtion.h" 23 | 24 | void *spx_configurtion_parser(SpxLogDelegate *log,\ 25 | SpxConfigurtionParserBeforeDelegate *before,\ 26 | SpxConfigurtionParserAfterDelegate *after,\ 27 | string_t filename,\ 28 | SpxConfigurtionLineParserDelegate *lineparser,\ 29 | err_t *err) { 30 | SpxErrReset; 31 | void *config = NULL; 32 | if(NULL != before){ 33 | config = before(log,err); 34 | if(0 != *err){ 35 | return NULL; 36 | } 37 | } 38 | FILE *f = fopen(filename,"r"); 39 | if(NULL == f){ 40 | *err = errno; 41 | return config; 42 | } 43 | 44 | string_t line = spx_string_newlen(NULL,SpxStringRealSize(SpxLineSize),err); 45 | if(NULL == line){ 46 | goto r1; 47 | } 48 | 49 | while(NULL != fgets(line,SpxLineSize,f)){ 50 | spx_string_updatelen(line); 51 | spx_string_trim(line," "); 52 | if(SpxStringEndWith(line,SpxLineEndDlmt)){ 53 | spx_string_rtrim(line,SpxLineEndDlmtString); 54 | } 55 | if(!SpxStringBeginWith(line,'#') 56 | && 0 != spx_string_len(line)){ 57 | lineparser(line,config,err); 58 | if(0 != *err){ 59 | goto r1; 60 | } 61 | } 62 | spx_string_clear(line); 63 | } 64 | if(NULL != after){ 65 | *err = after(config); 66 | } 67 | 68 | r1: 69 | fclose(f); 70 | return config; 71 | } 72 | -------------------------------------------------------------------------------- /spx/spx_env.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: spx_env.c 5 | * 6 | * Description: 7 | * 8 | * Version: 1.0 9 | * Created: 2014/06/09 14时28分33秒 10 | * Revision: none 11 | * Compiler: gcc 12 | * 13 | * Author: YOUR NAME (), 14 | * Organization: 15 | * 16 | * ===================================================================================== 17 | */ 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #include "spx_types.h" 26 | #include "spx_string.h" 27 | #include "spx_defs.h" 28 | #include "spx_env.h" 29 | 30 | 31 | void spx_env_daemon() {/*{{{*/ 32 | pid_t pid; 33 | 34 | //no cmd 35 | if ((pid = fork()) != 0) { 36 | exit(0); 37 | } 38 | 39 | //sid == pid and pgid == pid 40 | setsid(); 41 | 42 | //no open cmd again and cpid != sid 43 | if ((pid = fork()) != 0) { 44 | exit(0); 45 | } 46 | return; 47 | }/*}}}*/ 48 | 49 | err_t spx_set_group_and_user(SpxLogDelegate *log,string_t gname,string_t uname){/*{{{*/ 50 | err_t err = 0; 51 | SpxErrReset; 52 | if(!SpxStringIsNullOrEmpty(gname)){ 53 | struct group *g = getgrnam(gname); 54 | if(NULL == g){ 55 | SpxLogFmt1(log,SpxLogError,\ 56 | "not found the group:%s.",gname); 57 | err = 0 == errno ? EACCES : errno; 58 | return err; 59 | } 60 | if(0 != setegid(g->gr_gid)){ 61 | err = 0 == errno ? EPERM : errno; 62 | SpxLogFmt2(log,SpxLogError,err,\ 63 | "set the process group id:%d by gname:%s is fail.", 64 | g->gr_gid,gname); 65 | return err; 66 | } 67 | } 68 | if(!SpxStringIsNullOrEmpty(uname)){ 69 | struct passwd *u = getpwnam(uname); 70 | if(NULL == u){ 71 | SpxLogFmt1(log,SpxLogError,\ 72 | "not foung the user:%s.",uname); 73 | err = 0 == errno ? EACCES : errno; 74 | } 75 | if (0 != seteuid(u->pw_uid)) { 76 | err = 0 == errno ? EPERM : errno; 77 | SpxLogFmt2(log,SpxLogError,err,\ 78 | "set the process uid:%d by uname:%s is fail.",\ 79 | u->pw_uid,uname); 80 | return err; 81 | } 82 | return err; 83 | } 84 | return err; 85 | }/*}}}*/ 86 | 87 | void spx_env_sigaction(int sig,SpxSigActionDelegate *act){ 88 | struct sigaction sa; 89 | SpxZero(sa); 90 | if(NULL == act) { 91 | sa.sa_handler = SIG_IGN; 92 | } else { 93 | sa.sa_handler = act; 94 | } 95 | sigemptyset(&(sa.sa_mask)); 96 | sa.sa_flags = SA_RESTART; 97 | sigaction(sig,&sa,NULL); 98 | } 99 | 100 | -------------------------------------------------------------------------------- /spx/spx_errno.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | 6 | #include "spx_types.h" 7 | #include "spx_errno.h" 8 | 9 | char *spx_err_info_list[] = { 10 | "Success.", 11 | "No delimiter.", 12 | "bad header." 13 | }; 14 | 15 | char *spx_strerror(err_t err){ 16 | int e = 0; 17 | e = 0 > err ? (0 - err) : err; 18 | if(err >= SpxSuccess) { 19 | e = e - SpxSuccess; 20 | return spx_err_info_list[e]; 21 | } else { 22 | char *p = strerror(e); 23 | return p; 24 | } 25 | return NULL; 26 | } 27 | -------------------------------------------------------------------------------- /spx/spx_network_module.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * this software or lib may be copied only under the terms of the gnu general 5 | * public license v3, which may be found in the source kit. 6 | * 7 | * Filename: spx_nio_threaed.c 8 | * Created: 2014/07/24 11时05分58秒 9 | * Author: Seapeak.Xu (seapeak.cnblog.com), xvhfeng@gmail.com 10 | * Company: Tencent Literature 11 | * Remark: 12 | * 13 | * ===================================================================================== 14 | */ 15 | 16 | #include 17 | #include 18 | #include 19 | 20 | #include "spx_types.h" 21 | #include "spx_io.h" 22 | #include "spx_defs.h" 23 | #include "spx_module.h" 24 | #include "spx_network_module.h" 25 | #include "spx_job.h" 26 | #include "spx_socket.h" 27 | #include "spx_nio.h" 28 | 29 | struct spx_module_context *g_spx_network_module = NULL; 30 | 31 | 32 | void spx_network_module_receive_handler(struct ev_loop *loop,ev_io *w,int revents){ 33 | struct spx_job_context *jc = NULL; 34 | size_t len = 0; 35 | struct spx_receive_context *rc = (struct spx_receive_context *) w;//magic,yeah 36 | err_t err= 0; 37 | err = spx_read_nb(w->fd,(byte_t *) &jc,sizeof(jc),&len); 38 | if(0 != err){ 39 | SpxLog2(rc->log,SpxLogError,err,\ 40 | "read the nio context is fail."\ 41 | "forced push jc to pool."); 42 | return; 43 | } 44 | 45 | if (NULL == jc) { 46 | SpxLog1(rc->log,SpxLogError, 47 | "read the nio context is fail,"\ 48 | "tne nio context is null."\ 49 | "forced push jc to pool."); 50 | return; 51 | } 52 | 53 | switch(jc->moore){ 54 | case SpxNioMooreRequest:{ 55 | jc->lifecycle = SpxNioLifeCycleHeader; 56 | ev_once(loop,jc->fd,EV_READ,jc->waitting,jc->nio_reader,(void *) jc); 57 | break; 58 | } 59 | case SpxNioMooreResponse:{ 60 | 61 | jc->lifecycle = SpxNioLifeCycleHeader; 62 | spx_nio_writer_with_timeout(EV_WRITE,jc); 63 | break; 64 | } 65 | case SpxNioMooreNormal: 66 | default: 67 | { 68 | SpxLog1(jc->log,SpxLogError,\ 69 | "the jc moore is normal,and no the handler."\ 70 | "forced push jc to pool."); 71 | goto r1; 72 | } 73 | } 74 | return ; 75 | r1: 76 | spx_job_pool_push(g_spx_job_pool,jc); 77 | return ; 78 | } 79 | 80 | 81 | void spx_network_module_wakeup_handler(int revents,void *arg){ 82 | err_t err = 0; 83 | struct spx_job_context *jc = (struct spx_job_context *) arg; 84 | if((revents & EV_TIMEOUT) || (revents & EV_ERROR)){ 85 | SpxLog1(jc->log,SpxLogError,\ 86 | "wake up network module is fail."); 87 | spx_job_pool_push(g_spx_job_pool,jc); 88 | } 89 | if(revents & EV_WRITE){ 90 | size_t len = 0; 91 | SpxLogFmt1(jc->log,SpxLogDebug,"wakeup thread:%d.",jc->tc->idx ); 92 | err = spx_write_nb(jc->tc->pipe[1],(byte_t *) &jc,sizeof(jc),&len); 93 | if (0 != err) { 94 | SpxLog1(jc->log,SpxLogError,\ 95 | "wake up network module is fail."); 96 | spx_job_pool_push(g_spx_job_pool,jc); 97 | } 98 | } 99 | } 100 | 101 | -------------------------------------------------------------------------------- /spx/spx_notifier_module.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * this software or lib may be copied only under the terms of the gnu general 5 | * public license v3, which may be found in the source kit. 6 | * 7 | * Filename: spx_sio_thread.c 8 | * Created: 2014/07/23 15时31分32秒 9 | * Author: Seapeak.Xu (seapeak.cnblog.com), xvhfeng@gmail.com 10 | * Company: Tencent Literature 11 | * Remark: 12 | * 13 | * ===================================================================================== 14 | */ 15 | 16 | #include 17 | #include 18 | #include 19 | 20 | #include "spx_types.h" 21 | #include "spx_io.h" 22 | #include "spx_defs.h" 23 | #include "spx_module.h" 24 | #include "spx_notifier_module.h" 25 | #include "spx_network_module.h" 26 | #include "spx_socket.h" 27 | #include "spx_job.h" 28 | #include "spx_time.h" 29 | 30 | struct spx_module_context *g_spx_notifier_module = NULL; 31 | 32 | void spx_notifier_module_receive_handler(struct ev_loop *loop,ev_io *w,int revents){ 33 | struct spx_job_context *jc = NULL; 34 | 35 | size_t len = 0; 36 | struct spx_receive_context *rc = (struct spx_receive_context *) w;//magic,yeah 37 | err_t err= 0; 38 | err = spx_read_nb(w->fd,(byte_t *) &jc,sizeof(jc),&len); 39 | if(NULL == jc){ 40 | SpxLog2(rc->log,SpxLogError,err, 41 | "recv job context is fail."); 42 | return; 43 | } 44 | 45 | jc->client_ip = spx_ip_get(jc->fd,&err); 46 | spx_set_nb(jc->fd); 47 | spx_tcp_nodelay(jc->fd,true); 48 | jc->moore = SpxNioMooreRequest; 49 | size_t idx = spx_network_module_wakeup_idx(jc); 50 | SpxLogFmt1(rc->log,SpxLogDebug,\ 51 | "recv the client:%s connection.sock:%d."\ 52 | "and send to thread:%d to deal.", 53 | jc->client_ip,jc->fd,idx); 54 | struct spx_thread_context *tc = spx_get_thread(g_spx_network_module,idx); 55 | jc->tc = tc; 56 | SpxModuleDispatch(spx_network_module_wakeup_handler,jc); 57 | return ; 58 | 59 | } 60 | 61 | //void spx_notifier_module_wakeup_handler(struct ev_loop *loop,ev_io *w,int revents){ 62 | void spx_notifier_module_wakeup_handler(int revents,void *arg){ 63 | err_t err = 0; 64 | struct spx_job_context *jc = (struct spx_job_context *) arg; 65 | if((revents & EV_TIMEOUT) || (revents & EV_ERROR)){ 66 | SpxLogFmt1(jc->log,SpxLogError, 67 | "wake up notify module is fail,no:%d.",revents); 68 | spx_job_pool_push(g_spx_job_pool,jc); 69 | } 70 | if(revents & EV_WRITE){ 71 | SpxLogFmt1(jc->log,SpxLogDebug,"wakeup thread:%d.",jc->tc->idx ); 72 | 73 | size_t len = 0; 74 | err = spx_write_nb(jc->tc->pipe[1],(byte_t *) &jc,sizeof(jc),&len); 75 | if (0 != err || sizeof(jc) != len) { 76 | spx_job_pool_push(g_spx_job_pool,jc); 77 | SpxLog1(jc->log,SpxLogError,\ 78 | "wake up notify module is fail."); 79 | } 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /spx/spx_path.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #ifdef SpxLinux 12 | #include 13 | #else 14 | #ifdef SpxMac 15 | #include 16 | #include 17 | #endif 18 | #endif 19 | //#include 20 | 21 | 22 | #include "spx_types.h" 23 | #include "spx_path.h" 24 | #include "spx_defs.h" 25 | #include "spx_string.h" 26 | #include "spx_errno.h" 27 | 28 | bool_t spx_is_dir(const string_t path,err_t *err) {/*{{{*/ 29 | SpxErrReset; 30 | struct stat buf; 31 | if (-1 == stat(SpxString2Char2(path), &buf)) { 32 | if(ENOENT == errno){ 33 | SpxErrReset; 34 | return false; 35 | } 36 | *err = 0 == errno ? EACCES : errno; 37 | return false; 38 | } 39 | return S_ISDIR(buf.st_mode); 40 | }/*}}}*/ 41 | 42 | err_t spx_mkdir(SpxLogDelegate *log,const string_t path,const mode_t mode){/*{{{*/ 43 | err_t rc = 0; 44 | if(SpxStringIsNullOrEmpty(path)) { 45 | return EINVAL; 46 | } 47 | char ptr[SpxPathSize + 1] = {0}; 48 | memcpy(ptr,SpxString2Char2(path),SpxMin(SpxPathSize,spx_string_len(path))); 49 | bool_t isdir = false; 50 | isdir = spx_is_dir(path,&rc); 51 | if(0 != rc) { 52 | SpxLogFmt2(log,SpxLogError,rc,\ 53 | "check dir:%s is exist is fail.",path); 54 | return rc; 55 | } 56 | if(isdir) return rc; 57 | char *p = ptr; 58 | if( SpxPathDlmt == *p){ 59 | p += SpxPathDlmtLen; 60 | } 61 | while(NULL != (p = strchr(p,SpxPathDlmt))){ 62 | *p = 0; 63 | isdir = spx_is_dir(ptr,&rc); 64 | if(0 != rc) { 65 | SpxLogFmt2(log,SpxLogError,rc,\ 66 | "check dir:%s is fail.",ptr); 67 | break; 68 | } 69 | if(!isdir && (0 != (rc = mkdir(ptr,mode)))){ 70 | SpxLogFmt2(log,SpxLogError,rc,\ 71 | "create dir:%s is fail.",ptr); 72 | break; 73 | } 74 | *p = SpxPathDlmt; 75 | p += SpxPathDlmtLen; 76 | } 77 | isdir = spx_is_dir(ptr,&rc); 78 | if(0 != rc) { 79 | SpxLogFmt2(log,SpxLogError,rc,\ 80 | "check dir:%s is fail.",ptr); 81 | return rc; 82 | } 83 | if(!isdir && (0 != (rc = mkdir(ptr,mode)))){ 84 | SpxLogFmt2(log,SpxLogError,rc,\ 85 | "create dir:%s is fail.",ptr); 86 | return rc; 87 | } 88 | return rc; 89 | }/*}}}*/ 90 | 91 | 92 | string_t spx_fullname(const string_t path,const string_t filename,\ 93 | err_t *err){/*{{{*/ 94 | if(SpxStringIsNullOrEmpty(path) 95 | || SpxStringIsNullOrEmpty(filename)){ 96 | *err = EINVAL; 97 | return NULL; 98 | } 99 | string_t fn = spx_string_empty(err); 100 | string_t newfn = NULL; 101 | if(NULL == fn) return NULL; 102 | if(SpxStringEndWith(path,SpxPathDlmt)){ 103 | newfn = spx_string_cat_printf(err,fn,"%s%s",path,filename); 104 | } else { 105 | newfn = spx_string_cat_printf(err,fn,"%s%c%s",path,SpxPathDlmt,filename); 106 | } 107 | if(NULL == newfn){ 108 | spx_string_free(fn); 109 | } 110 | return newfn; 111 | }/*}}}*/ 112 | 113 | string_t spx_basepath(const string_t path,err_t *err){/*{{{*/ 114 | if(SpxStringIsNullOrEmpty(path)){ 115 | *err = EINVAL; 116 | return NULL; 117 | } 118 | const char *tmp = SpxString2Char2(path); 119 | string_t newbp = NULL; 120 | ptr_t p = NULL; 121 | if(NULL != (p =(ptr_t) rindex(tmp,(int) SpxPathDlmt))){ 122 | ptrdiff_t pd = p - tmp; 123 | newbp = spx_string_newlen(path,pd,err); 124 | } 125 | return newbp; 126 | }/*}}}*/ 127 | 128 | u64_t spx_mountpoint_availsize(string_t path,err_t *err){ 129 | struct statfs buf; 130 | SpxZero(buf); 131 | if(0 > statfs(path,&buf)) { 132 | *err = errno; 133 | return 0; 134 | } 135 | //not use buf.f_free,because 5%(ofter) for root 136 | return (u64_t) buf.f_bavail * buf.f_bsize; 137 | } 138 | 139 | u64_t spx_mountpoint_totalsize(string_t path,err_t *err){ 140 | struct statfs buf; 141 | SpxZero(buf); 142 | if(0 > statfs(path,&buf)) { 143 | *err = errno; 144 | return 0; 145 | } 146 | return (u64_t) buf.f_blocks * buf.f_bsize; 147 | } 148 | 149 | string_t spx_file_suffix(string_t fname,err_t *err){ 150 | if(SpxStringIsNullOrEmpty(fname)){ 151 | *err = EINVAL; 152 | return NULL; 153 | } 154 | const char *tmp = SpxString2Char2(fname); 155 | string_t newbp = NULL; 156 | ptr_t p = NULL; 157 | if(NULL != (p =(ptr_t) rindex(tmp,(int) SpxSuffixDlmt))){ 158 | ptrdiff_t pd = p - tmp; 159 | newbp = spx_string_newlen(fname,pd,err); 160 | } 161 | return newbp; 162 | } 163 | 164 | 165 | -------------------------------------------------------------------------------- /spx/spx_properties.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: spx_properties.c 5 | * 6 | * Description: 7 | * 8 | * Version: 1.0 9 | * Created: 2014/06/19 13时54分06秒 10 | * Revision: none 11 | * Compiler: gcc 12 | * 13 | * Author: YOUR NAME (), 14 | * Organization: 15 | * 16 | * ===================================================================================== 17 | */ 18 | #include 19 | #include 20 | 21 | 22 | #include "spx_types.h" 23 | #include "spx_properties.h" 24 | #include "spx_collection.h" 25 | #include "spx_alloc.h" 26 | #include "spx_defs.h" 27 | #include "spx_path.h" 28 | #include "spx_io.h" 29 | 30 | struct spx_properties *spx_properties_new(SpxLogDelegate *log,\ 31 | SpxPropertyLineDeserializeDelegate *line_deserialize,\ 32 | SpxPropertyLineSerializeDelegate *line_serialize,\ 33 | SpxPropertyParserBeforeDelegate *before,\ 34 | SpxPropertyParserAfterDelegate *after,\ 35 | err_t *err){ 36 | struct spx_properties *p = spx_alloc_alone(sizeof(*p),err); 37 | if(NULL == p){ 38 | return NULL; 39 | } 40 | p->log = log; 41 | 42 | p->configurtion = spx_map_new(log,spx_pjw, 43 | spx_collection_string_default_cmper, 44 | NULL, 45 | NULL, 46 | NULL, 47 | err); 48 | if(NULL == p->configurtion){ 49 | goto r1; 50 | } 51 | p->line_serialize = line_serialize; 52 | p->line_deserialize = line_deserialize; 53 | p->before = before; 54 | p->after = after; 55 | return p; 56 | 57 | r1: 58 | SpxFree(p); 59 | return NULL; 60 | 61 | } 62 | struct spx_properties *spx_properties_parser(struct spx_properties *p,\ 63 | string_t filename,err_t *err){ 64 | SpxErrReset; 65 | if(NULL != p->before){ 66 | *err = p->before(p); 67 | if(0 != *err){ 68 | return NULL; 69 | } 70 | } 71 | FILE *f = fopen(filename,"r"); 72 | if(NULL == f){ 73 | *err = errno; 74 | return p; 75 | } 76 | 77 | string_t line = spx_string_newlen(NULL,SpxStringRealSize(SpxLineSize),err); 78 | if(NULL == line){ 79 | goto r1; 80 | } 81 | 82 | while(NULL != fgets(line,SpxLineSize,f)){ 83 | spx_string_updatelen(line); 84 | spx_string_trim(line," "); 85 | if(!SpxStringBeginWith(line,'#')){ 86 | p->line_deserialize(line,p,err); 87 | } 88 | spx_string_clear(line); 89 | } 90 | if(NULL != p->after){ 91 | *err = p->after(p); 92 | } 93 | 94 | r1: 95 | fclose(f); 96 | return p; 97 | } 98 | 99 | void *spx_properties_get(struct spx_properties *p,string_t name,size_t *size){ 100 | return spx_map_get(p->configurtion,name,spx_string_len(name),size); 101 | } 102 | 103 | err_t spx_properties_set(struct spx_properties *p,string_t name,void *value,size_t size){ 104 | return spx_map_insert(p->configurtion,name,spx_string_len(name),value,size); 105 | } 106 | 107 | string_t spx_properties_tostring(struct spx_properties *p,err_t *err){ 108 | string_t context = NULL; 109 | struct spx_map_iter *iter = spx_map_iter_new(p->configurtion,err); 110 | if(NULL == iter){ 111 | return NULL; 112 | } 113 | context = spx_string_empty(err); 114 | if(NULL == context){ 115 | } 116 | struct spx_map_node *node = NULL; 117 | while(NULL != (node = spx_map_iter_next(iter,err))){ 118 | if(0 != *err){ 119 | goto r2; 120 | } 121 | string_t line = p->line_serialize(node,err); 122 | if(NULL == line){ 123 | goto r2; 124 | } 125 | 126 | string_t new_context = spx_string_cat_string(context,line,err); 127 | if(NULL == new_context){ 128 | goto r2; 129 | } 130 | context = new_context; 131 | } 132 | goto r1; 133 | r2: 134 | spx_string_free(context); 135 | r1: 136 | spx_map_iter_free(&iter); 137 | return context; 138 | } 139 | 140 | void spx_properties_store(struct spx_properties *p,string_t filename,err_t *err){ 141 | string_t context = spx_properties_tostring(p,err); 142 | if(NULL == context){ 143 | return; 144 | } 145 | 146 | string_t basepath = spx_basepath(filename,err); 147 | if(NULL == basepath){ 148 | goto r1; 149 | } 150 | 151 | if(!spx_is_dir(basepath,err)){ 152 | if(0 != *err){ 153 | goto r2; 154 | } 155 | if(0 != (*err = spx_mkdir(p->log,basepath,SpxPathMode))){ 156 | goto r2; 157 | } 158 | } 159 | 160 | FILE *fp = fopen(filename,"a+"); 161 | if(NULL == fp){ 162 | *err = errno; 163 | } 164 | size_t len = 0; 165 | if(0 != (*err = spx_fwrite_string(fp,context,spx_string_len(context),&len))){ 166 | goto r3; 167 | } 168 | 169 | r3: 170 | fclose(fp); 171 | 172 | r2: 173 | spx_string_free(basepath); 174 | r1: 175 | spx_string_free(context); 176 | return; 177 | } 178 | 179 | void spx_properties_free(struct spx_properties **p){ 180 | if(NULL != (*p)->configurtion){ 181 | spx_map_free(&((*p)->configurtion)); 182 | } 183 | SpxFree(*p); 184 | return; 185 | } 186 | -------------------------------------------------------------------------------- /spx/spx_queue.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * this software or lib may be copied only under the terms of the gnu general 5 | * public license v3, which may be found in the source kit. 6 | * 7 | * Filename: spx_queue.c 8 | * Created: 2014/08/08 17时53分32秒 9 | * Author: Seapeak.Xu (seapeak.cnblog.com), xvhfeng@gmail.com 10 | * Company: Tencent Literature 11 | * Remark: 12 | * 13 | * ===================================================================================== 14 | */ 15 | 16 | #include 17 | #include 18 | 19 | #include "spx_types.h" 20 | #include "spx_queue.h" 21 | #include "spx_alloc.h" 22 | #include "spx_defs.h" 23 | 24 | struct spx_queue *spx_queue_new(SpxLogDelegate *log, 25 | SpxQueueNodeFreeDelegate *free, 26 | err_t *err){ 27 | struct spx_queue *q = (struct spx_queue *) spx_alloc_alone(sizeof(*q),err); 28 | if(NULL == q){ 29 | SpxLog2(log,SpxLogError,*err,\ 30 | "alloc the queue is fail."); 31 | return NULL; 32 | } 33 | q->free = free; 34 | q->log = log; 35 | return q; 36 | } 37 | 38 | 39 | void *spx_queue_pop(struct spx_queue *q,err_t *err){ 40 | if(NULL == q->tail){ 41 | *err = ENOENT; 42 | return NULL; 43 | } 44 | struct spx_queue_node *p = NULL; 45 | do{ 46 | p = q->tail; 47 | }while(!__sync_bool_compare_and_swap(&(q->tail),p,p->prev)); 48 | void *v = p->v; 49 | SpxFree(p); 50 | return v; 51 | } 52 | 53 | err_t spx_queue_push(struct spx_queue *q,void *v){ 54 | err_t err = 0; 55 | struct spx_queue_node *n = (struct spx_queue_node *) \ 56 | spx_alloc_alone(sizeof(*n),&err); 57 | if(NULL == n){ 58 | 59 | } 60 | struct spx_queue_node *h = q->header; 61 | n->v = v; 62 | if(__sync_bool_compare_and_swap(&h,NULL,n)){ 63 | __sync_bool_compare_and_swap(&(q->tail),NULL,n); 64 | return 0; 65 | } 66 | 67 | struct spx_queue_node *p = q->header; 68 | struct spx_queue_node *old = p; 69 | int trys = 0; 70 | do{ 71 | while(3 < trys && NULL != p->prev){ 72 | p = p->prev; 73 | } 74 | trys++; 75 | }while(!__sync_bool_compare_and_swap(&(p->prev),NULL,n)); 76 | __sync_bool_compare_and_swap(&(q->header),old,n); 77 | return 0; 78 | } 79 | 80 | 81 | err_t spx_queue_free(struct spx_queue **q){ 82 | err_t err = 0; 83 | err = spx_queue_clear(*q); 84 | if(ENOENT == err){ 85 | SpxFree(*q); 86 | } else{ 87 | SpxLog2((*q)->log,SpxLogError,err, 88 | "clear queue is fail."); 89 | return err; 90 | } 91 | return err; 92 | } 93 | 94 | err_t spx_queue_clear(struct spx_queue *q){ 95 | err_t err = 0; 96 | void *v = NULL; 97 | while(NULL != ( v= spx_queue_pop(q,&err)) 98 | && 0 == err){ 99 | q->free(&v); 100 | } 101 | return err; 102 | } 103 | -------------------------------------------------------------------------------- /spx/spx_rand.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * this software or lib may be copied only under the terms of the gnu general 5 | * public license v3, which may be found in the source kit. 6 | * 7 | * Filename: spx_rand.c 8 | * Created: 2014/07/15 11时50分25秒 9 | * Author: Seapeak.Xu (seapeak.cnblog.com), xvhfeng@gmail.com 10 | * Company: Tencent Literature 11 | * Remark: 12 | * 13 | * ===================================================================================== 14 | */ 15 | 16 | #include 17 | #include 18 | #include 19 | #include 20 | 21 | #include "spx_types.h" 22 | #include "spx_time.h" 23 | 24 | #define PHI 161803398 25 | u32_t spx_random(u32_t idx){ 26 | time_t time = spx_now_usec(); 27 | srand(time); 28 | u32_t r = 1+(int) (100.0*rand()/(RAND_MAX+1.0)); 29 | return r; 30 | } 31 | 32 | u32_t spx_srandom(){ 33 | return spx_random(1); 34 | } 35 | -------------------------------------------------------------------------------- /spx/spx_socket_accept.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | #include "spx_types.h" 19 | #include "spx_string.h" 20 | #include "spx_defs.h" 21 | #include "spx_socket.h" 22 | #include "spx_notifier_module.h" 23 | #include "spx_module.h" 24 | #include "spx_job.h" 25 | #include "spx_socket_accept.h" 26 | #include "spx_time.h" 27 | 28 | spx_private ev_io main_watcher; 29 | spx_private void spx_socket_main_reciver(struct ev_loop *loop, 30 | ev_io *watcher,int revents); 31 | 32 | void spx_socket_accept_nb(SpxLogDelegate *log, 33 | struct ev_loop *loop,int fd){ 34 | SpxZero(main_watcher); 35 | ev_io_init(&main_watcher,spx_socket_main_reciver,fd,EV_READ); 36 | main_watcher.data = log; 37 | ev_io_start(loop,&(main_watcher)); 38 | ev_run(loop,0); 39 | } 40 | 41 | spx_private void spx_socket_main_reciver(struct ev_loop *loop,ev_io *watcher,int revents){ 42 | ev_io_stop(loop,watcher); 43 | SpxLogDelegate *log = (SpxLogDelegate *) watcher->data; 44 | err_t err = 0; 45 | while(true){ 46 | struct sockaddr_in client_addr; 47 | unsigned int socket_len = 0; 48 | int client_sock = 0; 49 | socket_len = sizeof(struct sockaddr_in); 50 | client_sock = accept(watcher->fd, (struct sockaddr *) &client_addr, 51 | &socket_len); 52 | if (0 > client_sock) { 53 | if (EWOULDBLOCK == errno || EAGAIN == errno) { 54 | break; 55 | } 56 | break; 57 | } 58 | 59 | if (0 == client_sock) { 60 | break; 61 | } 62 | 63 | size_t idx = client_sock % g_spx_notifier_module->threadpool->size; 64 | struct spx_job_context *jc = spx_job_pool_pop(g_spx_job_pool,&err); 65 | if(NULL == jc){ 66 | SpxClose(client_sock); 67 | SpxLog1(log,SpxLogError,\ 68 | "pop nio context is fail."); 69 | break; 70 | } 71 | 72 | SpxLogFmt1(log,SpxLogDebug,"recv socket conntect.wakeup notifier module idx:%d.jc idx:%d." 73 | ,idx,jc->idx); 74 | 75 | jc->fd = client_sock; 76 | jc->request_timespan = spx_now(); 77 | 78 | 79 | struct spx_thread_context *tc = spx_get_thread(g_spx_notifier_module,idx); 80 | jc->tc = tc; 81 | // spx_module_dispatch(tc,spx_notifier_module_wakeup_handler, jc); 82 | // spx_notifier_module_wakeup_handler(EV_WRITE,jc); 83 | SpxModuleDispatch(spx_notifier_module_wakeup_handler,jc); 84 | } 85 | ev_io_start(loop,watcher); 86 | } 87 | 88 | 89 | 90 | -------------------------------------------------------------------------------- /spx/spx_task.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * this software or lib may be copied only under the terms of the gnu general 5 | * public license v3, which may be found in the source kit. 6 | * 7 | * Filename: spx_task_context.c 8 | * Created: 2014/07/25 10时28分18秒 9 | * Author: Seapeak.Xu (seapeak.cnblog.com), xvhfeng@gmail.com 10 | * Company: Tencent Literature 11 | * Remark: 12 | * 13 | * ===================================================================================== 14 | */ 15 | 16 | #include 17 | #include 18 | 19 | 20 | #include "spx_types.h" 21 | #include "spx_task.h" 22 | #include "spx_alloc.h" 23 | #include "spx_fixed_vector.h" 24 | 25 | struct spx_task_pool *g_spx_task_pool = NULL; 26 | 27 | void *spx_task_context_new(size_t idx,void *arg,err_t *err){ 28 | struct spx_task_context_transport *tct = (struct spx_task_context_transport *) arg; 29 | struct spx_task_context *tcontext = (struct spx_task_context *) \ 30 | spx_alloc_alone(sizeof(*tcontext),err); 31 | if(NULL == tcontext){ 32 | return NULL; 33 | } 34 | tcontext->log = tct->log; 35 | tcontext->idx = idx; 36 | tcontext->tidx = idx; 37 | tcontext->dio_process_handler = tct->dio_process_handler; 38 | return tcontext; 39 | } 40 | 41 | err_t spx_task_context_free(void **arg){ 42 | struct spx_task_context **tcontext = (struct spx_task_context **) arg; 43 | if(NULL != *tcontext){ 44 | SpxFree(*tcontext); 45 | } 46 | return 0; 47 | } 48 | 49 | void spx_task_context_clear(struct spx_task_context *tcontext){ 50 | tcontext->jcontext = NULL; 51 | tcontext->tidx = tcontext->idx; 52 | return; 53 | } 54 | 55 | struct spx_task_pool *spx_task_pool_new(\ 56 | SpxLogDelegate *log,\ 57 | size_t size,\ 58 | SpxDioProcessDelegate *dio_process_handler,\ 59 | err_t *err){ 60 | struct spx_task_pool *pool = NULL; 61 | pool = spx_alloc_alone(sizeof(*pool),err); 62 | if(NULL == pool){ 63 | return NULL; 64 | } 65 | struct spx_task_context_transport tct; 66 | SpxZero(tct); 67 | tct.log = log; 68 | tct.dio_process_handler = dio_process_handler; 69 | 70 | pool->pool = spx_fixed_vector_new(log,size,\ 71 | spx_task_context_new,\ 72 | &tct,\ 73 | spx_task_context_free,\ 74 | err); 75 | 76 | if(NULL == pool->pool){ 77 | SpxFree(pool); 78 | return NULL; 79 | } 80 | return pool; 81 | } 82 | 83 | struct spx_task_context *spx_task_pool_pop(\ 84 | struct spx_task_pool *pool,\ 85 | err_t *err){ 86 | struct spx_task_context *tcontext = spx_fixed_vector_pop(pool->pool,err); 87 | return tcontext; 88 | } 89 | 90 | err_t spx_task_pool_push(struct spx_task_pool *pool,struct spx_task_context *tcontext){ 91 | spx_task_context_clear(tcontext); 92 | return spx_fixed_vector_push(pool->pool,tcontext); 93 | } 94 | 95 | err_t spx_task_pool_free(struct spx_task_pool **pool){ 96 | err_t err = 0; 97 | err = spx_fixed_vector_free(&((*pool)->pool)); 98 | SpxFree(*pool); 99 | return err; 100 | } 101 | 102 | 103 | -------------------------------------------------------------------------------- /spx/spx_task_module.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * this software or lib may be copied only under the terms of the gnu general 5 | * public license v3, which may be found in the source kit. 6 | * 7 | * Filename: spx_dio_thread.c 8 | * Created: 2014/07/24 13时14分46秒 9 | * Author: Seapeak.Xu (seapeak.cnblog.com), xvhfeng@gmail.com 10 | * Company: Tencent Literature 11 | * Remark: 12 | * 13 | * ===================================================================================== 14 | */ 15 | 16 | #include 17 | #include 18 | #include 19 | 20 | #include "spx_types.h" 21 | #include "spx_io.h" 22 | #include "spx_defs.h" 23 | #include "spx_module.h" 24 | #include "spx_network_module.h" 25 | #include "spx_job.h" 26 | #include "spx_socket.h" 27 | #include "spx_nio.h" 28 | #include "spx_task.h" 29 | 30 | struct spx_module_context *g_spx_task_module = NULL; 31 | 32 | void spx_task_module_receive_handler(struct ev_loop *loop,ev_io *w,int revents){ 33 | struct spx_task_context *task = NULL; 34 | struct spx_receive_context *tc = (struct spx_receive_context *) w;//magic,yeah 35 | size_t len = 0; 36 | err_t err= 0; 37 | err = spx_read_nb(w->fd,(byte_t *) &task,sizeof(task),&len); 38 | if(0 != err){ 39 | SpxLog2(tc->log,SpxLogError,err,\ 40 | "read the dio context is fail."); 41 | return; 42 | } 43 | 44 | if (NULL == task) { 45 | SpxLog1(tc->log,SpxLogError, 46 | "read the dio context is fail,"\ 47 | "tne dio context is null."); 48 | return; 49 | } 50 | 51 | task->dio_process_handler(loop,tc->idx,task); 52 | /* here no deal the error from dio_process_handler 53 | * as we donot know deal flow whether use noblacking or blacking 54 | * so, we must deal the error in the handler by yourself 55 | */ 56 | 57 | return ; 58 | } 59 | 60 | 61 | //void spx_task_module_wakeup_handler(struct ev_loop *loop,ev_io *w,int revents){ 62 | void spx_task_module_wakeup_handler(int revents,void *arg){ 63 | err_t err = 0; 64 | struct spx_task_context *task = (struct spx_task_context *) arg; 65 | struct spx_job_context *jc = task->jcontext; 66 | if((revents & EV_TIMEOUT) || (revents & EV_ERROR)){ 67 | SpxLog1(task->log,SpxLogError, 68 | "wakeup task thread is fail."); 69 | spx_task_pool_push(g_spx_task_pool,task); 70 | spx_job_pool_push(g_spx_job_pool,jc); 71 | } 72 | if(revents & EV_WRITE){ 73 | SpxLogFmt1(jc->log,SpxLogDebug,"task is well.tc idx:%d,thread idx:%d.",task->idx,task->tidx); 74 | size_t len = 0; 75 | err = spx_write_nb(jc->tc->pipe[1],(byte_t *) &task,sizeof(task),&len); 76 | if (0 != err || sizeof(jc) != len) { 77 | SpxLog1(jc->log,SpxLogError,\ 78 | "send task to dio thread is fail."\ 79 | "then dispatch notice to nio thread deal."); 80 | spx_task_pool_push(g_spx_task_pool,task); 81 | jc->err = err; 82 | jc->moore = SpxNioMooreResponse; 83 | size_t idx = spx_network_module_wakeup_idx(jc); 84 | struct spx_thread_context *tc = spx_get_thread(g_spx_network_module,idx); 85 | jc->tc = tc; 86 | SpxModuleDispatch(spx_network_module_wakeup_handler,jc); 87 | } 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /spx/spx_vector.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "spx_vector.h" 6 | #include "spx_types.h" 7 | #include "spx_defs.h" 8 | #include "spx_errno.h" 9 | #include "spx_alloc.h" 10 | 11 | struct spx_vector *spx_vector_init(SpxLogDelegate *log,\ 12 | SpxVectorValueFreeDelegate *handle,err_t *err){/*{{{*/ 13 | err_t rc = 0; 14 | struct spx_vector *vector = NULL; 15 | vector = spx_alloc_alone(sizeof(*vector),err); 16 | if(NULL == vector) { 17 | SpxLog2(log,SpxLogError,rc,\ 18 | "alloc vector is fail."); 19 | return NULL; 20 | } 21 | vector->log = log; 22 | vector->handle = handle; 23 | vector->size = 0; 24 | vector->header = NULL; 25 | return vector; 26 | }/*}}}*/ 27 | 28 | err_t spx_vector_free(struct spx_vector **vector) {/*{{{*/ 29 | if(NULL == *vector){ 30 | return 0; 31 | } 32 | err_t rc = 0; 33 | struct spx_vector_node *node = NULL; 34 | while(NULL !=(node = (*vector)->header)){ 35 | (*vector)->header = (*vector)->header->next; 36 | if(NULL != (*vector)->handle){ 37 | (*vector)->handle(&(node->v)); 38 | } 39 | SpxFree(node); 40 | } 41 | (*vector)->size = 0; 42 | (*vector)->tail = NULL; 43 | SpxFree(*vector); 44 | return rc; 45 | }/*}}}*/ 46 | 47 | err_t spx_vector_add(struct spx_vector *vector,void *v){/*{{{*/ 48 | err_t rc = 0; 49 | struct spx_vector_node *node = NULL; 50 | node = spx_alloc_alone(sizeof(*node),&rc); 51 | if(NULL == node) { 52 | SpxLog2(vector->log,SpxLogError,rc,\ 53 | "alloc vector node is fail."); 54 | return rc; 55 | } 56 | node->v = v; 57 | node->next = NULL; 58 | if(NULL == vector->header){ 59 | vector->header = node; 60 | vector->tail = node; 61 | }else { 62 | vector->tail->next = node; 63 | node->prev = vector->tail; 64 | vector->tail = node; 65 | vector->size++; 66 | } 67 | return rc; 68 | }/*}}}*/ 69 | 70 | void *spx_vector_get(struct spx_vector *vector,size_t idx,err_t *err){/*{{{*/ 71 | if(idx > vector->size){ 72 | *err = EINVAL; 73 | SpxLog1(vector->log,SpxLogError,"the idx overflow the vector size."); 74 | return NULL; 75 | } 76 | struct spx_vector_node *node = vector->header; 77 | void *v = NULL; 78 | size_t i = 0; 79 | for (i = 0; i < vector->size; i++) { 80 | if(i == idx){ 81 | v = node->v; 82 | break; 83 | } 84 | node = node->next; 85 | } 86 | return v; 87 | }/*}}}*/ 88 | 89 | err_t spx_vector_push(struct spx_vector *vector,void *v){ 90 | err_t rc = 0; 91 | struct spx_vector_node *node = NULL; 92 | node = spx_alloc_alone(sizeof(*node),&rc); 93 | if(NULL == node){ 94 | SpxLog2(vector->log,SpxLogError,rc,\ 95 | "alloc vector node is fail."); 96 | return rc; 97 | } 98 | node->v = v; 99 | node->next = NULL; 100 | if(NULL == vector->header){ 101 | vector->header = node; 102 | vector->tail = node; 103 | }else { 104 | vector->tail->next = node; 105 | node->prev = vector->tail; 106 | vector->tail = node; 107 | vector->size++; 108 | } 109 | vector->size++; 110 | return rc; 111 | } 112 | 113 | void *spx_vector_pop(struct spx_vector *vector, err_t *err){ 114 | if(NULL == vector || 0 == vector->size){ 115 | *err = EINVAL; 116 | SpxLog1(vector->log,SpxLogError,"the vector argument is fail."); 117 | return NULL; 118 | } 119 | 120 | struct spx_vector_node *node = NULL; 121 | void *v = NULL; 122 | if(NULL != vector->header){ 123 | node = vector->header; 124 | vector->header = node->next; 125 | if(NULL != vector->header){ 126 | vector->header->prev = NULL; 127 | } 128 | v = node->v; 129 | vector->size --; 130 | SpxFree(node); 131 | } else { 132 | v = NULL; 133 | } 134 | if(NULL == vector->header){ 135 | vector->tail = NULL; 136 | } 137 | return v; 138 | } 139 | 140 | struct spx_vector_iter *spx_vector_iter_new(struct spx_vector *vector,err_t *err){/*{{{*/ 141 | if(NULL == vector){ 142 | *err = EINVAL; 143 | return NULL; 144 | } 145 | struct spx_vector_iter *iter = NULL; 146 | iter = spx_alloc_alone(sizeof(*iter),err); 147 | if(NULL == iter){ 148 | SpxLog2(vector->log,SpxLogError,*err,\ 149 | "allo the vector iter is fail."); 150 | return NULL; 151 | } 152 | iter->vector = vector; 153 | iter->curr = NULL; 154 | return iter; 155 | }/*}}}*/ 156 | 157 | err_t spx_vector_iter_free(struct spx_vector_iter **iter){/*{{{*/ 158 | if(NULL == *iter){ 159 | return 0; 160 | } 161 | if(NULL == (*iter)->vector){ 162 | return 0; 163 | } 164 | SpxFree(*iter); 165 | return 0; 166 | }/*}}}*/ 167 | 168 | void *spx_vector_iter_next(struct spx_vector_iter *iter) {/*{{{*/ 169 | if(NULL == iter){ 170 | return NULL; 171 | } 172 | struct spx_vector *vector = iter->vector; 173 | if(NULL == vector){ 174 | return NULL; 175 | } 176 | if(iter->curr == iter->vector->tail){ 177 | return NULL; 178 | } 179 | void *v= NULL; 180 | iter->curr = NULL == iter->curr \ 181 | ? vector->header : iter->curr->next; 182 | v = iter->curr->v; 183 | return v; 184 | }/*}}}*/ 185 | 186 | void spx_vector_iter_reset(struct spx_vector_iter *iter){/*{{{*/ 187 | if(NULL == iter){ 188 | return; 189 | } 190 | iter->curr = NULL; 191 | return; 192 | }/*}}}*/ 193 | 194 | -------------------------------------------------------------------------------- /spx/test/SpxMemoryPool.h: -------------------------------------------------------------------------------- 1 | /*********************************************************************** 2 | * _ooOoo_ 3 | * o8888888o 4 | * 88" . "88 5 | * (| -_- |) 6 | * O\ = /O 7 | * ____/`---'\____ 8 | * . ' \\| |// `. 9 | * / \\||| : |||// \ 10 | * / _||||| -:- |||||- \ 11 | * | | \\\ - /// | | 12 | * | \_| ''\---/'' | | 13 | * \ .-\__ `-` ___/-. / 14 | * ___`. .' /--.--\ `. . __ 15 | * ."" '< `.___\_<|>_/___.' >'"". 16 | * | | : `- \`.;`\ _ /`;.`/ - ` : | | 17 | * \ \ `-. \_ __\ /__ _/ .-` / / 18 | * ======`-.____`-.___\_____/___.-`____.-'====== 19 | * `=---=' 20 | * ............................................. 21 | * 佛祖镇楼 BUG辟易 22 | * 佛曰: 23 | * 写字楼里写字间,写字间里程序员; 24 | * 程序人员写程序,又拿程序换酒钱。 25 | * 酒醒只在网上坐,酒醉还来网下眠; 26 | * 酒醉酒醒日复日,网上网下年复年。 27 | * 但愿老死电脑间,不愿鞠躬老板前; 28 | * 奔驰宝马贵者趣,公交自行程序员。 29 | * 别人笑我忒疯癫,我笑自己命太贱; 30 | * 不见满街漂亮妹,哪个归得程序员? 31 | * ========================================================================== 32 | * 33 | * this software or lib may be copied only under the terms of the gnu general 34 | * public license v3, which may be found in the source kit. 35 | * 36 | * Filename: SpxMemoryPool.h 37 | * Created: 2014/12/03 10时04分57秒 38 | * Author: Seapeak.Xu (seapeak.cnblog.com), xvhfeng@gmail.com 39 | * Company: Tencent Literature 40 | * Remark: 41 | * 42 | ****************************************************************************/ 43 | #ifndef _SPXMEMORYPOOL_H_ 44 | #define _SPXMEMORYPOOL_H_ 45 | #ifdef __cplusplus 46 | extern "C" { 47 | #endif 48 | 49 | #include 50 | #include 51 | 52 | #include "spx_defs.h" 53 | #include "spx_types.h" 54 | #include "spx_atomic.h" 55 | #include "SpxObject.h" 56 | 57 | struct SpxMemoryBuffer{ 58 | char *p; 59 | struct SpxMemoryBuffer *next; 60 | size_t freeSize; 61 | char buf[0]; 62 | }; 63 | 64 | struct SpxLargeObject{//large is extends spx_object; 65 | struct SpxLargeObject *prev; 66 | struct SpxLargeObject *next; 67 | SpxObjectBase ;//must input there top buff 68 | char buf[0]; 69 | }; 70 | 71 | struct SpxMemoryPool{ 72 | SpxLogDelegate *log; 73 | size_t poolingMax; 74 | size_t bufferSize; 75 | size_t keepBufferSize; 76 | struct SpxMemoryBuffer *bufferHeader; 77 | struct SpxMemoryBuffer *bufferCurrent; 78 | struct SpxLargeObject *largeObjectHeader; 79 | struct SpxLargeObject *largeObjectTail; 80 | }; 81 | 82 | struct SpxMemoryPool *spxMemoryPoolNew( 83 | SpxLogDelegate *log, 84 | const size_t poolingMax, 85 | const size_t bufferSize, 86 | const size_t keepBufferSize, 87 | err_t *err 88 | ); 89 | 90 | void *spxMemoryPoolAllocNumbs( 91 | struct SpxMemoryPool *pool, 92 | const size_t numbs, 93 | const size_t size, 94 | err_t *err 95 | ); 96 | 97 | void *spxMemoryPoolAlloc( 98 | struct SpxMemoryPool *pool, 99 | const size_t size, 100 | err_t *err 101 | ); 102 | 103 | void *spxMemoryPoolReAlloc( 104 | struct SpxMemoryPool *pool, 105 | void *p, 106 | const size_t s, 107 | err_t *err 108 | ); 109 | 110 | bool_t spxMemoryPoolFree( 111 | struct SpxMemoryPool *pool, 112 | void *p 113 | ); 114 | 115 | bool_t spxMemoryPoolFreeForce( 116 | struct SpxMemoryPool *pool, 117 | void *p 118 | ); 119 | 120 | err_t spxMemoryPoolClear( 121 | struct SpxMemoryPool *pool 122 | ); 123 | 124 | err_t spxMemoryPoolDestory( 125 | struct SpxMemoryPool *pool 126 | ); 127 | 128 | #define SpxMemoryPoolFree(pool,p) \ 129 | do { \ 130 | if(NULL != p && (spxMemoryPoolFree(pool,p))){ \ 131 | p = NULL; \ 132 | } \ 133 | }while(false) 134 | 135 | #define SpxMemoryPoolFreeForce(pool,p) \ 136 | do { \ 137 | if(NULL != p) {\ 138 | spxMemoryPoolFreeForce(pool,p);\ 139 | p = NULL; \ 140 | } \ 141 | }while(false) 142 | 143 | 144 | #define SpxMemoryPoolDestory(p) \ 145 | do { \ 146 | if(NULL != p) {\ 147 | spxMemoryPoolDestory(p); \ 148 | p = NULL; \ 149 | } \ 150 | }while(false) 151 | 152 | 153 | #ifdef __cplusplus 154 | } 155 | #endif 156 | #endif 157 | 158 | #ifdef __cplusplus 159 | } 160 | #endif 161 | #endif 162 | -------------------------------------------------------------------------------- /spx/test/log/makefile: -------------------------------------------------------------------------------- 1 | .SUFFIXES: .c .o 2 | 3 | CC = gcc 4 | CFLAG = -pipe -o0 -W -Wall -Werror -g \ 5 | -Wpointer-arith -Wno-unused-parameter -Wunused-function -Wunused-variable -Wunused-value 6 | INC_PATH = -I/usr/local/include \ 7 | -I../../include 8 | LIB_PATH = -L/usr/local/lib -lev 9 | DEFS := 10 | TARGET_PATH = ./ 11 | TARGET = log_test 12 | DIRS = ../../ 13 | 14 | SYS := $(strip $(shell uname -s | tr '[:upper:]' '[:lower:]')) 15 | ifeq ($(SYS), linux) 16 | DEFS += -DSpxLinux 17 | else 18 | ifeq ($(SYS),unix) 19 | DEFS += -DSpxUnix 20 | else 21 | ifeq ($(SYS),darwin) 22 | DEFS += -DSpxMac 23 | endif 24 | endif 25 | endif 26 | 27 | BITS := $(findstring 64,$(shell uname -m )) 28 | ifeq (64, $(BITS)) 29 | DEFS += -DSpx64 30 | else 31 | ifeq (32, $(BITS)) 32 | DEFS += -DSpx32 33 | else 34 | DEFS += -DSpx32 35 | endif 36 | endif 37 | 38 | SRC_FILE = $(wildcard *.c) \ 39 | $(wildcard ../../*.c) 40 | 41 | SHARED_OBJS = $(patsubst %.c,%.o,$(SRC_FILE) ) 42 | 43 | all:$(TARGET) 44 | 45 | %.o: %.c 46 | $(CC) $(CFLAG) -c $< -o $@ $(LIB_PATH) $(INC_PATH) $(DEFS) 47 | 48 | $(TARGET) : $(SHARED_OBJS) 49 | $(CC) $(CFLAG) $(SHARED_OBJS) $(LIB_PATH) $(INC_PATH) -o $(TARGET) $(DEFS) 50 | 51 | install: 52 | cp -f $(TARGET) $(TARGET_PATH) 53 | clean: 54 | rm -f $(SHARED_OBJS) $(TARGET) 55 | -------------------------------------------------------------------------------- /spx/test/log/spx_log_test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: spx_log_test.c 5 | * 6 | * Description: the spx_log test 7 | * 8 | * Version: 1.0 9 | * Created: 2014/05/21 09时09分21秒 10 | * Revision: none 11 | * Compiler: gcc 12 | * 13 | * Author: YOUR NAME (), 14 | * Organization: 15 | * 16 | * ===================================================================================== 17 | */ 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #include "spx_defs.h" 26 | #include "spx_types.h" 27 | #include "spx_string.h" 28 | #include "spx_log.h" 29 | 30 | int main(int argc,char **argv){ 31 | umask(0); 32 | string_t path = (string_t) "./spxlib_test/log"; 33 | string_t name =(string_t) "logtest"; 34 | u64_t size = 1 * SpxMB; 35 | u8_t level = SpxLogDebug; 36 | err_t err = 0; 37 | SpxLogDelegate *log = spx_log; 38 | SpxLog1(log,SpxLogMark,"test np the og file."); 39 | if( 0 != (err = spx_log_new(spx_log,path,name,size,level))){ 40 | SpxLog2(log,SpxLogInfo,err,"create log is fail."); 41 | return 0; 42 | } 43 | 44 | spx_log(SpxLogDebug,(string_t) "test log deug,errno:%d.",ENOMEM); 45 | spx_log(SpxLogMark,(string_t) "mark tet"); 46 | spx_log(SpxLogInfo,(string_t )("test")); 47 | SpxLog1(log,SpxLogDebug,"printf info by SpxLog1."); 48 | SpxLog2(log,SpxLogInfo,EEXIST,"printf log info by SpxLog2"); 49 | 50 | SpxLogFmt1(log,SpxLogMark,"printf log info by SpxLogFmt1.%s","success") \ 51 | SpxLogFmt2(log,SpxLogError,\ 52 | EACCES,"printf log info by SpxLogFmt2.%s","success") \ 53 | spx_log_free(); 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /spx/test/map/makefile: -------------------------------------------------------------------------------- 1 | .SUFFIXES: .c .o 2 | 3 | CC = gcc 4 | CFLAG = -pipe -o1 -W -Wall -Werror -g \ 5 | -Wpointer-arith -Wno-unused-parameter -Wunused-function -Wunused-variable -Wunused-value 6 | INC_PATH = -I/usr/local/include \ 7 | -I../../include 8 | LIB_PATH = 9 | DEFS := 10 | TARGET_PATH = ./ 11 | TARGET = spx_map_test 12 | DIRS = ../../ 13 | 14 | SYS := $(strip $(shell uname -s | tr '[:upper:]' '[:lower:]')) 15 | ifeq ($(SYS), linux) 16 | DEFS += -DSpxLinux 17 | else 18 | ifeq ($(SYS),unix) 19 | DEFS += -DSpxUnix 20 | else 21 | ifeq ($(SYS),darwin) 22 | DEFS += -DSpxMac 23 | endif 24 | endif 25 | endif 26 | 27 | BITS := $(findstring 64,$(shell uname -m )) 28 | ifeq (64, $(BITS)) 29 | DEFS += -DSpx64 30 | else 31 | ifeq (32, $(BITS)) 32 | DEFS += -DSpx32 33 | else 34 | DEFS += -DSpx32 35 | endif 36 | endif 37 | 38 | SRC_FILE = $(wildcard *.c) \ 39 | $(wildcard ../../*.c) 40 | 41 | SHARED_OBJS = $(patsubst %.c,%.o,$(SRC_FILE) ) 42 | 43 | all:$(TARGET) 44 | 45 | %.o: %.c 46 | $(CC) $(CFLAG) -c $< -o $@ $(LIB_PATH) $(INC_PATH) $(DEFS) 47 | 48 | $(TARGET) : $(SHARED_OBJS) 49 | $(CC) $(CFLAG) $(SHARED_OBJS) $(LIB_PATH) $(INC_PATH) -o $(TARGET) $(DEFS) 50 | 51 | install: 52 | cp -f $(TARGET) $(TARGET_PATH) 53 | clean: 54 | rm -f $(SHARED_OBJS) $(TARGET) 55 | -------------------------------------------------------------------------------- /spx/test/map/spx_map_test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/crosg/idCreator/d4873281d18365ffad3f87dc49a8b01e4a19fc1f/spx/test/map/spx_map_test -------------------------------------------------------------------------------- /spx/test/map/spx_map_test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: spx_map_test.c 5 | * 6 | * Description: 7 | * 8 | * Version: 1.0 9 | * Created: 2014/06/09 10时18分27秒 10 | * Revision: none 11 | * Compiler: gcc 12 | * 13 | * Author: YOUR NAME (), 14 | * Organization: 15 | * 16 | * ===================================================================================== 17 | */ 18 | #include 19 | 20 | 21 | #include "spx_types.h" 22 | #include "spx_log.h" 23 | #include "spx_map.h" 24 | #include "spx_collection.h" 25 | #include "spx_errno.h" 26 | #include "spx_string.h" 27 | 28 | err_t spx_map_free(void **s){ 29 | string_t sk = (string_t) *s; 30 | spx_string_free(sk); 31 | return 0; 32 | } 33 | int main(int argc,char **argv){ 34 | err_t err = 0; 35 | SpxLogDelegate *log = spx_log; 36 | struct spx_map *map = spx_map_new(log,\ 37 | spx_pjw, 38 | spx_collection_string_default_cmper, 39 | NULL, 40 | spx_map_free, 41 | spx_map_free, 42 | &err); 43 | if(NULL == map){ 44 | return err; 45 | } 46 | 47 | string_t aaa = spx_string_new("aaa",&err); 48 | string_t bbb = spx_string_new("bbb",&err); 49 | string_t ccc = spx_string_new("ccc",&err); 50 | string_t ddd = spx_string_new("ddd",&err); 51 | err = spx_map_insert(map,aaa,spx_string_len(aaa),\ 52 | aaa,spx_string_len(aaa)); 53 | err = spx_map_insert(map,bbb,spx_string_len(bbb),\ 54 | bbb,spx_string_len(bbb)); 55 | err = spx_map_insert(map,ccc,spx_string_len(ccc),\ 56 | ccc,spx_string_len(ccc)); 57 | err = spx_map_insert(map,ddd,spx_string_len(ddd),\ 58 | ddd,spx_string_len(ddd)); 59 | 60 | string_t newaaa,newbbb,newccc,newddd; 61 | size_t newalen,newblen,newclen,newdlen; 62 | 63 | err = spx_map_get(map,aaa,spx_string_len(aaa),(void **) &newaaa,&newalen); 64 | err = spx_map_get(map,aaa,spx_string_len(bbb),(void **) &newbbb,&newblen); 65 | err = spx_map_get(map,aaa,spx_string_len(ccc),(void **) &newccc,&newclen); 66 | err = spx_map_get(map,aaa,spx_string_len(ddd),(void **) &newddd,&newdlen); 67 | 68 | err = spx_map_out(map,aaa,spx_string_len(aaa),(void **) &newaaa,&newalen); 69 | err = spx_map_out(map,aaa,spx_string_len(bbb),(void **) &newbbb,&newblen); 70 | 71 | err = spx_map_delete(map,ccc,spx_string_len(ccc)); 72 | err = spx_map_delete(map,ddd,spx_string_len(ddd)); 73 | 74 | err = spx_map_insert(map,aaa,spx_string_len(aaa),\ 75 | aaa,spx_string_len(aaa)); 76 | err = spx_map_insert(map,bbb,spx_string_len(bbb),\ 77 | bbb,spx_string_len(bbb)); 78 | err = spx_map_insert(map,ccc,spx_string_len(ccc),\ 79 | ccc,spx_string_len(ccc)); 80 | err = spx_map_insert(map,ddd,spx_string_len(ddd),\ 81 | ddd,spx_string_len(ddd)); 82 | 83 | struct spx_map_iter *iter = spx_map_iter_new(map,&err); 84 | struct spx_map_node *node = NULL; 85 | while(NULL != (node = spx_map_iter_next(iter,&err))){ 86 | string_t ks = node->k; 87 | size_t kl = node->kl; 88 | string_t vs = node->v; 89 | size_t vl = node->vl; 90 | printf("key:%s,key size:%lld,value:%s,value size:%lld.",\ 91 | ks,(long long)kl,vs,(long long)vl); 92 | } 93 | err = spx_map_iter_destory(&iter); 94 | 95 | err = spx_map_destory(&map); 96 | return 0; 97 | } 98 | -------------------------------------------------------------------------------- /spx/test/skiplist/skiplist_test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/crosg/idCreator/d4873281d18365ffad3f87dc49a8b01e4a19fc1f/spx/test/skiplist/skiplist_test -------------------------------------------------------------------------------- /spx/test/spx_mpool.h: -------------------------------------------------------------------------------- 1 | /*********************************************************************** 2 | * _ooOoo_ 3 | * o8888888o 4 | * 88" . "88 5 | * (| -_- |) 6 | * O\ = /O 7 | * ____/`---'\____ 8 | * . ' \\| |// `. 9 | * / \\||| : |||// \ 10 | * / _||||| -:- |||||- \ 11 | * | | \\\ - /// | | 12 | * | \_| ''\---/'' | | 13 | * \ .-\__ `-` ___/-. / 14 | * ___`. .' /--.--\ `. . __ 15 | * ."" '< `.___\_<|>_/___.' >'"". 16 | * | | : `- \`.;`\ _ /`;.`/ - ` : | | 17 | * \ \ `-. \_ __\ /__ _/ .-` / / 18 | * ======`-.____`-.___\_____/___.-`____.-'====== 19 | * `=---=' 20 | * ............................................. 21 | * 佛祖镇楼 BUG辟易 22 | * 佛曰: 23 | * 写字楼里写字间,写字间里程序员; 24 | * 程序人员写程序,又拿程序换酒钱。 25 | * 酒醒只在网上坐,酒醉还来网下眠; 26 | * 酒醉酒醒日复日,网上网下年复年。 27 | * 但愿老死电脑间,不愿鞠躬老板前; 28 | * 奔驰宝马贵者趣,公交自行程序员。 29 | * 别人笑我忒疯癫,我笑自己命太贱; 30 | * 不见满街漂亮妹,哪个归得程序员? 31 | * ========================================================================== 32 | * 33 | * this software or lib may be copied only under the terms of the gnu general 34 | * public license v3, which may be found in the source kit. 35 | * 36 | * Filename: spx_mpool.h 37 | * Created: 2014/10/11 08时52分00秒 38 | * Author: Seapeak.Xu (seapeak.cnblog.com), xvhfeng@gmail.com 39 | * Company: Tencent Literature 40 | * Remark: 41 | * 42 | ****************************************************************************/ 43 | #ifndef _SPX_MPOOL_H_ 44 | #define _SPX_MPOOL_H_ 45 | #ifdef __cplusplus 46 | extern "C" { 47 | #endif 48 | 49 | 50 | #include 51 | #include 52 | 53 | #include "spx_defs.h" 54 | #include "spx_types.h" 55 | #include "spx_atomic.h" 56 | 57 | struct spx_mbuff{ 58 | struct spx_mbuff *next; 59 | size_t freesize; 60 | char *ptr; 61 | char buff[0]; 62 | }; 63 | 64 | struct spx_large{//large is extends spx_object; 65 | struct spx_large *prev; 66 | struct spx_large *next; 67 | SpxObjectStruct;//must input there top buff 68 | char buff[0]; 69 | }; 70 | 71 | struct spx_mpool{ 72 | SpxLogDelegate *log; 73 | size_t pooling_size; 74 | size_t mbuff_size; 75 | size_t keep_mbuff_size; 76 | struct spx_mbuff *mb_header; 77 | struct spx_mbuff *mb_curr; 78 | struct spx_large *lg_header; 79 | struct spx_large *lg_tail; 80 | }; 81 | 82 | struct spx_mpool *spx_mpool_new( 83 | SpxLogDelegate *log, 84 | const size_t pooling_size, 85 | const size_t mbuff_size, 86 | const size_t keep_mbuff_size, 87 | err_t *err 88 | ); 89 | 90 | void *spx_mpool_malloc( 91 | struct spx_mpool *pool, 92 | const size_t size, 93 | err_t *err 94 | ); 95 | 96 | void *spx_mpool_realloc( 97 | struct spx_mpool *pool, 98 | void *p, 99 | const size_t s, 100 | err_t *err 101 | ); 102 | 103 | void *spx_mpool_alloc( 104 | struct spx_mpool *pool, 105 | const size_t numbs, 106 | const size_t size, 107 | err_t *err 108 | ); 109 | 110 | void *spx_mpool_alloc_alone( 111 | struct spx_mpool *pool, 112 | const size_t size, 113 | err_t *err 114 | ); 115 | 116 | bool_t spx_mpool_free( 117 | struct spx_mpool *pool, 118 | void *p 119 | ); 120 | 121 | bool_t spx_mpool_free_force( 122 | struct spx_mpool *pool, 123 | void *p 124 | ); 125 | 126 | err_t spx_mpool_clear( 127 | struct spx_mpool *pool 128 | ); 129 | 130 | err_t spx_mpool_destory( 131 | struct spx_mpool *pool 132 | ); 133 | 134 | #define SpxMemPoolFree(pool,p) \ 135 | do { \ 136 | if(NULL != p && (spx_mpool_free(pool,p))){ \ 137 | p = NULL; \ 138 | } \ 139 | }while(false) 140 | 141 | #define SpxMemPoolFreeForce(pool,p) \ 142 | do { \ 143 | if(NULL != p) {\ 144 | spx_mpool_free_force(pool,p);\ 145 | p = NULL; \ 146 | } \ 147 | }while(false) 148 | 149 | 150 | #define SpxMemPoolDestory(p) \ 151 | do { \ 152 | if(NULL != p) {\ 153 | spx_mpool_destory(p); \ 154 | p = NULL; \ 155 | } \ 156 | }while(false) 157 | 158 | 159 | #ifdef __cplusplus 160 | } 161 | #endif 162 | #endif 163 | -------------------------------------------------------------------------------- /spx/test/spx_nio_threadpool.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * this software or lib may be copied only under the terms of the gnu general 5 | * public license v3, which may be found in the source kit. 6 | * 7 | * Filename: spx_nio_threadpool.c 8 | * Created: 2014/07/21 17时14分27秒 9 | * Author: Seapeak.Xu (seapeak.cnblog.com), xvhfeng@gmail.com 10 | * Company: Tencent Literature 11 | * Remark: 12 | * 13 | * ===================================================================================== 14 | */ 15 | 16 | #include 17 | #include 18 | #include 19 | #include 20 | 21 | #include "include/spx_types.h" 22 | #include "include/spx_alloc.h" 23 | #include "include/spx_defs.h" 24 | #include "include/spx_nio_threadpool.h" 25 | #include "include/spx_list.h" 26 | #include "include/spx_string.h" 27 | #include "include/spx_io.h" 28 | 29 | struct spx_thread_context_node{ 30 | SpxLogDelegate *log; 31 | SpxThreadNotifyDelegate *thread_notify; 32 | }; 33 | 34 | spx_private void *spx_nio_thread_listen(void *arg); 35 | spx_private void *spx_nio_thread_context_new(size_t idx,void *arg,err_t *err); 36 | spx_private err_t spx_nio_thread_context_free(void **arg); 37 | 38 | spx_private void *spx_nio_thread_listen(void *arg){ 39 | struct spx_nio_thread_context *context = (struct spx_nio_thread_context *) arg; 40 | if(NULL == context){ 41 | return NULL; 42 | } 43 | ev_io_init(&(context->watcher),context->thread_notify_handle,context->pipe[0],EV_READ); 44 | context->watcher.data = context;//libev not the set function 45 | ev_io_start(context->loop,&(context->watcher)); 46 | ev_run(context->loop,0); 47 | return NULL; 48 | } 49 | 50 | spx_private void *spx_nio_thread_context_new(size_t idx,void *arg,err_t *err){ 51 | struct spx_thread_context_node *tcn = (struct spx_thread_context_node *)arg; 52 | struct spx_nio_thread_context *context = spx_alloc_alone(sizeof(*context),err); 53 | if(NULL == context){ 54 | SpxLog2(tcn->log,SpxLogError,*err,\ 55 | "alloc nio thread context is fail."); 56 | return NULL; 57 | } 58 | 59 | context->loop = ev_loop_new(EVFLAG_AUTO); 60 | context->log = tcn->log; 61 | context->idx = idx; 62 | context->thread_notify_handle = tcn->thread_notify; 63 | if(-1 == pipe(context->pipe)){ 64 | SpxLog2(tcn->log,SpxLogError,*err,\ 65 | "open the nio thread pips is fail."); 66 | *err = errno; 67 | SpxFree(context); 68 | return NULL; 69 | } 70 | if((0 != (*err = spx_set_nb(context->pipe[0]))) \ 71 | ||(0 != (*err = spx_set_nb(context->pipe[0])))){ 72 | SpxLog2(tcn->log,SpxLogError,*err,\ 73 | "set pipe noblacking is fail."); 74 | SpxFree(context); 75 | return NULL; 76 | } 77 | return context; 78 | } 79 | 80 | spx_private err_t spx_nio_thread_context_free(void **arg){ 81 | struct spx_nio_thread_context **context = (struct spx_nio_thread_context **) arg; 82 | ev_break((*context)->loop,EVBREAK_ALL); 83 | ev_loop_destroy((*context)->loop); 84 | SpxClose((*context)->pipe[0]); 85 | SpxClose((*context)->pipe[1]); 86 | SpxFree(*context); 87 | return 0; 88 | } 89 | 90 | struct spx_list *spx_nio_threadpool_create(\ 91 | SpxLogDelegate *log,\ 92 | u32_t threadsize,\ 93 | size_t stack_size,\ 94 | SpxThreadNotifyDelegate *thread_notify_handle,\ 95 | err_t *err){ 96 | struct spx_thread_context_node tcn; 97 | SpxZero(tcn); 98 | tcn.thread_notify = thread_notify_handle; 99 | tcn.log = log; 100 | struct spx_list *nio_thread_contexts = spx_list_init(log,threadsize,\ 101 | spx_nio_thread_context_new,&tcn,\ 102 | spx_nio_thread_context_free,\ 103 | err); 104 | if(0 == threadsize){ 105 | SpxLog1(log,SpxLogError,\ 106 | "the argument:threadsize is 0"); 107 | return NULL; 108 | } 109 | 110 | if(NULL == nio_thread_contexts){ 111 | SpxLog2(log,SpxLogError,*err,\ 112 | "alloc nio thread context is fail."); 113 | return NULL; 114 | } 115 | 116 | pthread_attr_t attr; 117 | pthread_attr_init(&attr); 118 | size_t ostack_size = 0; 119 | pthread_attr_getstacksize(&attr, &ostack_size); 120 | do{ 121 | if (ostack_size != stack_size 122 | && (0 != (*err = pthread_attr_setstacksize(&attr,stack_size)))){ 123 | SpxLog2(log,SpxLogError,*err,\ 124 | "set thread stack size is fail."); 125 | goto r1; 126 | } 127 | u32_t i = 0; 128 | for( ; i < threadsize; i++){ 129 | struct spx_nio_thread_context * n = spx_list_get(nio_thread_contexts,i); 130 | n->log = log; 131 | if (0 !=(*err = pthread_create(&(n->tid), &attr, spx_nio_thread_listen, 132 | n))){ 133 | SpxLog2(log,SpxLogError,*err,\ 134 | "create nio thread is fail."); 135 | goto r1; 136 | } 137 | } 138 | }while(false); 139 | pthread_attr_destroy(&attr); 140 | return nio_thread_contexts; 141 | r1: 142 | pthread_attr_destroy(&attr); 143 | spx_list_free(&nio_thread_contexts); 144 | return NULL; 145 | } 146 | 147 | err_t spx_nio_threadpool_free(struct spx_list **nio_thread_contexts){ 148 | return spx_list_free(nio_thread_contexts); 149 | } 150 | 151 | -------------------------------------------------------------------------------- /spx/test/spx_object.c: -------------------------------------------------------------------------------- 1 | /************************************************************* 2 | * _ooOoo_ 3 | * o8888888o 4 | * 88" . "88 5 | * (| -_- |) 6 | * O\ = /O 7 | * ____/`---'\____ 8 | * .' \\| |// `. 9 | * / \\||| : |||// \ 10 | * / _||||| -:- |||||- \ 11 | * | | \\\ - /// | | 12 | * | \_| ''\---/'' | | 13 | * \ .-\__ `-` ___/-. / 14 | * ___`. .' /--.--\ `. . __ 15 | * ."" '< `.___\_<|>_/___.' >'"". 16 | * | | : `- \`.;`\ _ /`;.`/ - ` : | | 17 | * \ \ `-. \_ __\ /__ _/ .-` / / 18 | * ======`-.____`-.___\_____/___.-`____.-'====== 19 | * `=---=' 20 | * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 21 | * 佛祖保佑 永无BUG 22 | * 23 | * ========================================================================== 24 | * 25 | * this software or lib may be copied only under the terms of the gnu general 26 | * public license v3, which may be found in the source kit. 27 | * 28 | * Filename: spx_object.c 29 | * Created: 2014/10/11 13时15分17秒 30 | * Author: Seapeak.Xu (seapeak.cnblog.com), xvhfeng@gmail.com 31 | * Company: Tencent Literature 32 | * Remark: 33 | * 34 | ***********************************************************************/ 35 | 36 | #include 37 | #include 38 | #include 39 | #include 40 | 41 | #include "spx_types.h" 42 | #include "spx_defs.h" 43 | #include "spx_alloc.h" 44 | #include "spx_atomic.h" 45 | #include "spx_object.h" 46 | 47 | 48 | void *spx_object_new(const size_t s,err_t *err){ 49 | if(0 == s){ 50 | *err = EINVAL; 51 | return NULL; 52 | } 53 | size_t realsize = s + SpxObjectAlignSize; 54 | struct spx_object *o = (struct spx_object *) 55 | malloc(realsize); 56 | if(NULL == o){ 57 | *err = 0 == errno ? ENOMEM : errno; 58 | } 59 | o->spx_object_refs = 1; 60 | o->spx_object_is_pooling = false; 61 | o->spx_object_size = s; 62 | return SpxMemIncr(o,SpxObjectAlignSize); 63 | } 64 | 65 | void *spx_object_news(const size_t numbs,const size_t s,err_t *err){ 66 | if(0 == s || 0 == numbs){ 67 | *err = EINVAL; 68 | return NULL; 69 | } 70 | size_t realsize = SpxObjectAlignSize + numbs * s; 71 | struct spx_object *o = calloc(realsize,sizeof(char)); 72 | if(NULL == o){ 73 | *err = 0 == errno ? ENOMEM : errno; 74 | return NULL; 75 | } 76 | o->spx_object_refs = 1; 77 | o->spx_object_is_pooling = false; 78 | o->spx_object_size = s * numbs; 79 | return SpxMemIncr(o,SpxObjectAlignSize); 80 | } 81 | 82 | void *spx_object_new_alone(const size_t s,err_t *err){ 83 | if(0 == s){ 84 | *err = EINVAL; 85 | return NULL; 86 | } 87 | return spx_alloc(1,s,err); 88 | } 89 | 90 | void *spx_object_new_algin(const size_t s,err_t *err) { 91 | struct spx_object *o = NULL; 92 | size_t realsize = SpxObjectAlignSize + SpxAlign(s,SpxAlignSize); 93 | if(0 != (*err = posix_memalign((void **) &o, SpxAlignSize, realsize))){ 94 | return NULL; 95 | } 96 | o->spx_object_refs = 1; 97 | o->spx_object_is_pooling = false; 98 | o->spx_object_size = s; 99 | return SpxMemIncr(o,SpxObjectAlignSize); 100 | } 101 | 102 | 103 | void *spx_object_renew(void *p,const size_t s,err_t *err){ 104 | struct spx_object *o = (struct spx_object *) ((char *) p - SpxObjectAlignSize); 105 | 106 | size_t realsize = SpxObjectAlignSize + s; 107 | struct spx_object *ptr = realloc(o,realsize); 108 | if(NULL == ptr){ 109 | *err = 0 == errno ? ENOMEM : errno; 110 | return NULL; 111 | } 112 | return SpxMemIncr(ptr,SpxObjectAlignSize); 113 | } 114 | 115 | bool_t spx_object_free(void *p){ 116 | struct spx_object *o = (struct spx_object *) SpxMemDecr(p,SpxObjectAlignSize); 117 | if(!o->spx_object_is_pooling){ 118 | if(0 == SpxAtomicDecr(&(o->spx_object_refs))){ 119 | SpxFree(o); 120 | return true; 121 | } 122 | } 123 | return false; 124 | } 125 | 126 | bool_t spx_object_free_force(void *p){ 127 | struct spx_object *o = (struct spx_object *) SpxMemDecr(p,SpxObjectAlignSize); 128 | if(!o->spx_object_is_pooling){ 129 | SpxFree(o); 130 | return true; 131 | } 132 | return false; 133 | } 134 | 135 | void *spx_object_ref(void *p){ 136 | if(NULL == p){ 137 | return NULL; 138 | } 139 | struct spx_object *o = (struct spx_object *) ((char *) p - SpxObjectAlignSize); 140 | if(0 == o->spx_object_refs){ 141 | return NULL; 142 | } 143 | SpxAtomicVIncr(o->spx_object_refs); 144 | return p; 145 | } 146 | 147 | void *spx_object_unref(void *p){ 148 | if(NULL == p){ 149 | return NULL; 150 | } 151 | struct spx_object *o = (struct spx_object *) ((char *) p - SpxObjectAlignSize); 152 | if(0 == o->spx_object_refs){ 153 | return NULL; 154 | } 155 | if(0 == SpxAtomicVDecr(o->spx_object_refs)){ 156 | if(!o->spx_object_is_pooling){ 157 | SpxFree(o); 158 | return NULL; 159 | } 160 | } 161 | return p; 162 | } 163 | -------------------------------------------------------------------------------- /spx/test/spx_object.h: -------------------------------------------------------------------------------- 1 | /*********************************************************************** 2 | * _ooOoo_ 3 | * o8888888o 4 | * 88" . "88 5 | * (| -_- |) 6 | * O\ = /O 7 | * ____/`---'\____ 8 | * . ' \\| |// `. 9 | * / \\||| : |||// \ 10 | * / _||||| -:- |||||- \ 11 | * | | \\\ - /// | | 12 | * | \_| ''\---/'' | | 13 | * \ .-\__ `-` ___/-. / 14 | * ___`. .' /--.--\ `. . __ 15 | * ."" '< `.___\_<|>_/___.' >'"". 16 | * | | : `- \`.;`\ _ /`;.`/ - ` : | | 17 | * \ \ `-. \_ __\ /__ _/ .-` / / 18 | * ======`-.____`-.___\_____/___.-`____.-'====== 19 | * `=---=' 20 | * ............................................. 21 | * 佛祖镇楼 BUG辟易 22 | * 佛曰: 23 | * 写字楼里写字间,写字间里程序员; 24 | * 程序人员写程序,又拿程序换酒钱。 25 | * 酒醒只在网上坐,酒醉还来网下眠; 26 | * 酒醉酒醒日复日,网上网下年复年。 27 | * 但愿老死电脑间,不愿鞠躬老板前; 28 | * 奔驰宝马贵者趣,公交自行程序员。 29 | * 别人笑我忒疯癫,我笑自己命太贱; 30 | * 不见满街漂亮妹,哪个归得程序员? 31 | * ========================================================================== 32 | * 33 | * this software or lib may be copied only under the terms of the gnu general 34 | * public license v3, which may be found in the source kit. 35 | * 36 | * Filename: spx_object.h 37 | * Created: 2014/10/11 13时15分19秒 38 | * Author: Seapeak.Xu (seapeak.cnblog.com), xvhfeng@gmail.com 39 | * Company: Tencent Literature 40 | * Remark: 41 | * 42 | ****************************************************************************/ 43 | #ifndef _SPX_OBJECT_H_ 44 | #define _SPX_OBJECT_H_ 45 | #ifdef __cplusplus 46 | extern "C" { 47 | #endif 48 | 49 | 50 | #include 51 | #include 52 | 53 | #include "spx_types.h" 54 | #include "spx_defs.h" 55 | 56 | void *spx_object_new(const size_t s,err_t *err); 57 | void *spx_object_news(const size_t numbs,const size_t s,err_t *err); 58 | void *spx_object_new_alone(const size_t s,err_t *err); 59 | void *spx_object_new_algin(const size_t s,err_t *err) ; 60 | void *spx_object_renew(void *p,const size_t s,err_t *err); 61 | bool_t spx_object_free(void *p); 62 | bool_t spx_object_free_force(void *p); 63 | void *spx_object_ref(void *p); 64 | void *spx_object_unref(void *p); 65 | 66 | spx_private u32_t spx_object_refcount(void *p){ 67 | if(NULL == p){ 68 | return 0; 69 | } 70 | struct spx_object *o = (struct spx_object *) ((char *) p - SpxObjectAlignSize); 71 | if(0 == o->spx_object_refs){ 72 | return 0; 73 | } 74 | return o->spx_object_refs; 75 | } 76 | 77 | #define SpxObjectFree(p) \ 78 | do { \ 79 | if(NULL != p && spx_object_free(p)) { \ 80 | p = NULL; \ 81 | } \ 82 | }while(false) 83 | 84 | #define SpxObjectFreeForce(p) \ 85 | do { \ 86 | if(NULL != p) {\ 87 | spx_object_free_force(p);\ 88 | p = NULL; \ 89 | } \ 90 | }while(false) 91 | #ifdef __cplusplus 92 | } 93 | #endif 94 | #endif 95 | -------------------------------------------------------------------------------- /spx/test/spx_ref.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: spx_ref.c 5 | * 6 | * Description: 7 | * 8 | * Version: 1.0 9 | * Created: 2014/06/25 15时32分44秒 10 | * Revision: none 11 | * Compiler: gcc 12 | * 13 | * Author: YOUR NAME (), 14 | * Organization: 15 | * 16 | * ===================================================================================== 17 | */ 18 | #include 19 | 20 | 21 | #include "spx_types.h" 22 | #include "spx_alloc.h" 23 | #include "spx_ref.h" 24 | #include "spx_string.h" 25 | 26 | void *spx_ref_new(void *v,err_t *err){ 27 | if (NULL == v) { 28 | *err = EINVAL; 29 | return NULL; 30 | } 31 | struct spx_ref *ref = spx_alloc_alone(sizeof(*ref),err); 32 | if(NULL == ref){ 33 | return NULL; 34 | } 35 | ref->p = v; 36 | ref->ref ++; 37 | return ref->p; 38 | } 39 | 40 | string_t spx_ref_string_new(string_t s,err_t *err){ 41 | if(NULL == s){ 42 | *err = EINVAL; 43 | return NULL; 44 | } 45 | struct spx_ref *ref = spx_alloc_alone(sizeof(*ref),err); 46 | if(NULL == ref){ 47 | return NULL; 48 | } 49 | struct sds *sh = (void*)(s-sizeof *sh); 50 | ref->p = sh; 51 | ref->ref ++; 52 | return ((char *) ref->p) + sizeof(*sh); 53 | } 54 | 55 | void *spx_ref(void *v){ 56 | if(NULL != v){ 57 | struct spx_ref *ref =(struct spx_ref *) ((char *) v) - sizeof(u32_t); 58 | ref->ref ++; 59 | return ref->p; 60 | } 61 | return NULL; 62 | } 63 | 64 | string_t spx_ref_string(string_t s){ 65 | if(NULL != s) { 66 | struct sds *sh = (void*)(s-sizeof *sh); 67 | struct spx_ref *ref =(struct spx_ref *) ((char *) sh) - sizeof(u32_t); 68 | ref->ref ++; 69 | return ((char *) ref->p) + sizeof(*sh); 70 | } 71 | return NULL; 72 | } 73 | 74 | bool_t spx_ref_free(void *v){ 75 | if(NULL !=v){ 76 | struct spx_ref *ref =(struct spx_ref *) ((char *) v) - sizeof(u32_t); 77 | ref->ref --; 78 | if(0 == ref){ 79 | SpxFree(ref); 80 | return true; 81 | } 82 | } 83 | return false; 84 | } 85 | 86 | bool_t spx_ref_string_free(string_t s){ 87 | if(NULL != s) { 88 | struct sds *sh = (void*)(s-sizeof *sh); 89 | struct spx_ref *ref =(struct spx_ref *) ((char *) sh) - sizeof(u32_t); 90 | ref->ref --; 91 | if(0 == ref){ 92 | SpxFree(ref); 93 | return true; 94 | } 95 | } 96 | return false; 97 | } 98 | 99 | 100 | -------------------------------------------------------------------------------- /spx/test/spx_ref.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: spx_ref.h 5 | * 6 | * Description: 7 | * 8 | * Version: 1.0 9 | * Created: 2014/06/25 15时32分50秒 10 | * Revision: none 11 | * Compiler: gcc 12 | * 13 | * Author: YOUR NAME (), 14 | * Organization: 15 | * 16 | * ===================================================================================== 17 | */ 18 | #ifndef _SPX_REF_H_ 19 | #define _SPX_REF_H_ 20 | #ifdef __cplusplus 21 | extern "C" { 22 | #endif 23 | 24 | /* 25 | * if you use the ref count please make sure you konw the memory status 26 | * aways. 27 | * and if you dont care of the memory a moment,it is very dangerous. 28 | * !!!!!!!MAKE SURE THE MEMORY 29 | * 30 | * if the of pointer is the struct by pointer-compute, 31 | * then it can use the ref-count directly. and you must add the 32 | * wrapper function,just as string_t and struct sds 33 | */ 34 | 35 | #include "spx_types.h" 36 | 37 | struct spx_ref{ 38 | u32_t ref; 39 | void *p; 40 | }; 41 | 42 | void *spx_ref_new(void *v,err_t *err); 43 | void *spx_ref(void *v); 44 | bool_t spx_ref_free(void *p); 45 | string_t spx_ref_string_new(string_t s,err_t *err); 46 | string_t spx_ref_string(string_t s); 47 | bool_t spx_ref_string_free(string_t s); 48 | 49 | #ifdef __cplusplus 50 | } 51 | #endif 52 | #endif 53 | -------------------------------------------------------------------------------- /spx/test/spx_timer.h: -------------------------------------------------------------------------------- 1 | /*********************************************************************** 2 | * _ooOoo_ 3 | * o8888888o 4 | * 88" . "88 5 | * (| -_- |) 6 | * O\ = /O 7 | * ____/`---'\____ 8 | * . ' \\| |// `. 9 | * / \\||| : |||// \ 10 | * / _||||| -:- |||||- \ 11 | * | | \\\ - /// | | 12 | * | \_| ''\---/'' | | 13 | * \ .-\__ `-` ___/-. / 14 | * ___`. .' /--.--\ `. . __ 15 | * ."" '< `.___\_<|>_/___.' >'"". 16 | * | | : `- \`.;`\ _ /`;.`/ - ` : | | 17 | * \ \ `-. \_ __\ /__ _/ .-` / / 18 | * ======`-.____`-.___\_____/___.-`____.-'====== 19 | * `=---=' 20 | * ............................................. 21 | * 佛祖镇楼 BUG辟易 22 | * 佛曰: 23 | * 写字楼里写字间,写字间里程序员; 24 | * 程序人员写程序,又拿程序换酒钱。 25 | * 酒醒只在网上坐,酒醉还来网下眠; 26 | * 酒醉酒醒日复日,网上网下年复年。 27 | * 但愿老死电脑间,不愿鞠躬老板前; 28 | * 奔驰宝马贵者趣,公交自行程序员。 29 | * 别人笑我忒疯癫,我笑自己命太贱; 30 | * 不见满街漂亮妹,哪个归得程序员? 31 | * ========================================================================== 32 | * 33 | * this software or lib may be copied only under the terms of the gnu general 34 | * public license v3, which may be found in the source kit. 35 | * 36 | * Filename: spx_timer.h 37 | * Created: 2014/11/08 09时32分51秒 38 | * Author: Seapeak.Xu (seapeak.cnblog.com), xvhfeng@gmail.com 39 | * Company: Tencent Literature 40 | * Remark: 41 | * 42 | ****************************************************************************/ 43 | #ifndef _SPX_TIMER_H_ 44 | #define _SPX_TIMER_H_ 45 | #ifdef __cplusplus 46 | extern "C" { 47 | #endif 48 | 49 | 50 | #include 51 | #include 52 | 53 | #include "spx_types.h" 54 | 55 | typedef void *(SpxExpiredDelegate)(void *arg); 56 | 57 | struct SpxTimerElement{ 58 | u64_t id; 59 | u64_t basetime; 60 | u32_t expired; 61 | SpxExpiredDelegate *expiredHander; 62 | void *arg; 63 | struct SpxTimerElement *prev; 64 | struct SpxTimerElement *next; 65 | }; 66 | 67 | struct SpxTimerSlot{ 68 | u64_t count; 69 | u32_t idx; 70 | struct SpxTimerElement *header; 71 | struct SpxTimerElement *tail; 72 | }; 73 | 74 | struct SpxTimer{ 75 | u64_t idx; 76 | SpxLogDelegate *log; 77 | u64_t basetime; 78 | u32_t slotsCount; 79 | bool_t running; 80 | struct SpxTimerSlot *header; 81 | struct SpxTimerSlot *current; 82 | }; 83 | 84 | struct SpxTimer *SpxTimerNew(SpxLogDelegate *log, 85 | u32_t slotsCount,err_t *err); 86 | 87 | struct SpxTimerElement *SpxTimerAdd(struct SpxTimer *timer, 88 | u32_t expired, 89 | SpxExpiredDelegate *expiredHandler, 90 | void *arg, 91 | err_t *err); 92 | 93 | err_t SpxTimerRemove(struct SpxTimer *timer, 94 | struct SpxTimerElement *e); 95 | 96 | err_t SpxTimerElement *SpxTimerModify(struct SpxTimer *timer, 97 | struct SpxTimerElement *e,u32_t expired); 98 | 99 | struct SpxTimerElement *SpxTimerRunning(struct SpxTimer *timer, 100 | u32_t *count); 101 | 102 | err_t SpxTimerFree(struct SpxTimer **timer); 103 | 104 | 105 | #ifdef __cplusplus 106 | } 107 | #endif 108 | #endif 109 | --------------------------------------------------------------------------------