├── Makefile ├── README ├── base ├── CmdDispatcher.h ├── MyConstHash.h ├── MyCsvDataDefine.h ├── MyCsvParse.cpp ├── MyCsvParse.h ├── MyHashFunction.cpp ├── MyHashFunction.h ├── MyLock.h ├── MyQueue.h ├── MySingleton.h ├── MySockTask.cpp ├── MySockTask.h ├── MySockTaskManager.cpp ├── MySockTaskManager.h ├── MySockTaskPool.cpp ├── MySockTaskPool.h ├── MySocket.cpp ├── MySocket.h ├── MyTask.cpp ├── MyTask.h ├── MyThread.cpp ├── MyThread.h ├── MyTime.h ├── mq.h ├── mysqlConnectPool │ ├── DBDefine.h │ ├── DBPool.cpp │ ├── DBPool.h │ ├── Makefile │ ├── MySingleton.h │ ├── sql │ │ ├── import.sh │ │ └── sql.sql │ └── test.cpp └── redisConnectPool │ ├── Makefile │ ├── redisDataType.h │ ├── redisOpt.cpp │ ├── redisOpt.h │ ├── test │ └── test.cpp ├── client ├── Makefile ├── MyClient.cpp ├── MyClient.h ├── MyClientTask.cpp ├── MyClientTask.h ├── MyClientTaskManager.cpp ├── MyClientTaskManager.h ├── MySockClientTask.cpp ├── MySockClientTask.h ├── MySockClientTaskPool.cpp ├── MySockClientTaskPool.h ├── MyTcpClient.cpp ├── MyTcpClient.h └── test.cpp ├── lib ├── hiredis │ ├── .travis.yml │ ├── CHANGELOG.md │ ├── COPYING │ ├── Makefile │ ├── README.md │ ├── adapters │ │ ├── ae.h │ │ ├── glib.h │ │ ├── ivykis.h │ │ ├── libev.h │ │ ├── libevent.h │ │ ├── libuv.h │ │ ├── macosx.h │ │ └── qt.h │ ├── async.c │ ├── async.h │ ├── dict.c │ ├── dict.h │ ├── examples │ │ ├── example-ae.c │ │ ├── example-glib.c │ │ ├── example-ivykis.c │ │ ├── example-libev.c │ │ ├── example-libevent.c │ │ ├── example-libuv.c │ │ ├── example-macosx.c │ │ ├── example-qt.cpp │ │ ├── example-qt.h │ │ └── example.c │ ├── fmacros.h │ ├── hiredis.c │ ├── hiredis.h │ ├── net.c │ ├── net.h │ ├── read.c │ ├── read.h │ ├── sds.c │ ├── sds.h │ ├── test.c │ └── win32.h └── tinyxml │ ├── Makefile │ ├── changes.txt │ ├── docs │ ├── annotated.html │ ├── classTiXmlAttribute-members.html │ ├── classTiXmlAttribute.html │ ├── classTiXmlAttribute.png │ ├── classTiXmlBase-members.html │ ├── classTiXmlBase.html │ ├── classTiXmlBase.png │ ├── classTiXmlComment-members.html │ ├── classTiXmlComment.html │ ├── classTiXmlComment.png │ ├── classTiXmlDeclaration-members.html │ ├── classTiXmlDeclaration.html │ ├── classTiXmlDeclaration.png │ ├── classTiXmlDocument-members.html │ ├── classTiXmlDocument.html │ ├── classTiXmlDocument.png │ ├── classTiXmlElement-members.html │ ├── classTiXmlElement.html │ ├── classTiXmlElement.png │ ├── classTiXmlHandle-members.html │ ├── classTiXmlHandle.html │ ├── classTiXmlNode-members.html │ ├── classTiXmlNode.html │ ├── classTiXmlNode.png │ ├── classTiXmlPrinter-members.html │ ├── classTiXmlPrinter.html │ ├── classTiXmlPrinter.png │ ├── classTiXmlText-members.html │ ├── classTiXmlText.html │ ├── classTiXmlText.png │ ├── classTiXmlUnknown-members.html │ ├── classTiXmlUnknown.html │ ├── classTiXmlUnknown.png │ ├── classTiXmlVisitor-members.html │ ├── classTiXmlVisitor.html │ ├── classTiXmlVisitor.png │ ├── classes.html │ ├── deprecated.html │ ├── doxygen.css │ ├── doxygen.png │ ├── files.html │ ├── functions.html │ ├── functions_0x63.html │ ├── functions_0x64.html │ ├── functions_0x65.html │ ├── functions_0x66.html │ ├── functions_0x67.html │ ├── functions_0x69.html │ ├── functions_0x6c.html │ ├── functions_0x6e.html │ ├── functions_0x6f.html │ ├── functions_0x70.html │ ├── functions_0x71.html │ ├── functions_0x72.html │ ├── functions_0x73.html │ ├── functions_0x74.html │ ├── functions_0x75.html │ ├── functions_0x76.html │ ├── functions_enum.html │ ├── functions_func.html │ ├── functions_func_0x63.html │ ├── functions_func_0x64.html │ ├── functions_func_0x65.html │ ├── functions_func_0x66.html │ ├── functions_func_0x67.html │ ├── functions_func_0x69.html │ ├── functions_func_0x6c.html │ ├── functions_func_0x6e.html │ ├── functions_func_0x70.html │ ├── functions_func_0x71.html │ ├── functions_func_0x72.html │ ├── functions_func_0x73.html │ ├── functions_func_0x74.html │ ├── functions_func_0x75.html │ ├── functions_func_0x76.html │ ├── functions_rela.html │ ├── functions_vars.html │ ├── hierarchy.html │ ├── index.html │ ├── pages.html │ ├── tab_b.gif │ ├── tab_l.gif │ ├── tab_r.gif │ ├── tabs.css │ ├── tinystr_8h_source.html │ ├── tinyxml_8h_source.html │ └── tutorial0.html │ ├── libtinyxml.a │ ├── readme.txt │ ├── tinystr.cpp │ ├── tinystr.h │ ├── tinyxml.cpp │ ├── tinyxml.h │ ├── tinyxmlerror.cpp │ ├── tinyxmlparser.cpp │ ├── utf8test.gif │ ├── utf8test.xml │ ├── utf8testverify.xml │ └── xmltest.cpp ├── proto ├── BaseCmd.h ├── CmdNumber.h ├── LoadBalanceCmd.h ├── LoginProcessCmd.h └── protobuf │ ├── gen_protobuf │ └── test.proto ├── server ├── ConnectionLoadBalance.cpp ├── ConnectionLoadBalance.h ├── Makefile ├── MyBaseServer.cpp ├── MyBaseServer.h ├── MyServer.cpp ├── MyServer.h ├── MyServerMsgProcess.cpp ├── MyServerMsgProcess.h ├── MyServerTask.cpp ├── MyServerTask.h ├── MyTcpServer.cpp ├── MyTcpServer.h └── test.cpp └── test ├── LoginProcess.cpp ├── LoginProcess.h ├── Makefile ├── Server.cpp ├── Server.h ├── XmlconfigParse.cpp ├── XmlconfigParse.h ├── configure └── clientAddress.xml ├── csv ├── 111.csv └── csvConfig.xml ├── main.cpp └── types.h /Makefile: -------------------------------------------------------------------------------- 1 | ROOT=`pwd` 2 | SUB_DIR=$(ROOT)/server $(ROOT)/client $(ROOT)/test 3 | LIB_DIR=$(ROOT)/server $(ROOT)/client 4 | TEST_DIR=$(ROOT)/test 5 | 6 | 7 | .PHONY : all 8 | all: 9 | (cd proto/protobuf && ./gen_protobuf) 10 | @for i in $(LIB_DIR); do \ 11 | (cd $$i && make && make install); \ 12 | done 13 | @for i in $(TEST_DIR); do \ 14 | (cd $$i && make); \ 15 | done 16 | (cd lib/tinyxml && make) 17 | 18 | .PHONY : clean 19 | clean: 20 | @for i in $(SUB_DIR); do \ 21 | (cd $$i && make clean);\ 22 | done 23 | 24 | #install: 25 | # @for i in $(LIB_DIR); do \ 26 | # (cd $$i && make install);\ 27 | # done 28 | 29 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | 1. make 2 | 2. test directory is a complete server 3 | 3. server and client directory have example 4 | 4. lib is a shared library directory 5 | -------------------------------------------------------------------------------- /base/CmdDispatcher.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: CmdDispatcher.h 5 | * 6 | * Description: message dispatcher 7 | * 8 | * Version: 1.0 9 | * Created: 09/28/2015 01:44:09 PM 10 | * Revision: none 11 | * Compiler: gcc 12 | * 13 | * Author: huangyun (hy), 895175589@qq.com 14 | * Organization: 15 | * 16 | * ===================================================================================== 17 | */ 18 | 19 | #ifndef __CMDDISPATCHER_H_ 20 | #define __CMDDISPATCHER_H_ 21 | 22 | #include 23 | #include 24 | //#include 25 | #include 26 | #include 27 | #include 28 | #include "../proto/BaseCmd.h" 29 | 30 | namespace MyNameSpace 31 | { 32 | using CallBackFunT = std::function< bool(const Command::BaseCommand *, uint32_t, int)>; 33 | class Dispatcher 34 | { 35 | public: 36 | Dispatcher(const std::string & name) : mName(name){} 37 | ~Dispatcher(){} 38 | const std::string & getName() const { return mName;} 39 | void regCallback(uint32_t cmdId, CallBackFunT fun) 40 | { 41 | // funTable.insert(std::make_pair > >(cmdId, fun)); 42 | funTable[cmdId] = fun; 43 | } 44 | 45 | bool dispatcher(const Command::BaseCommand *cmd, uint32_t cmdLen, int taskId) 46 | { 47 | CallBackFunT fun = funTable[cmd->mCmdId]; 48 | if (fun) 49 | { 50 | return fun(cmd, cmdLen, taskId); 51 | } 52 | else 53 | { 54 | std::cerr<<"cmdId:"<mCmdId<<"not regeister callback"<<" taskId: "< > funTable; 61 | std::map funTable; 62 | std::string mName; 63 | }; 64 | } 65 | 66 | #endif 67 | -------------------------------------------------------------------------------- /base/MyCsvDataDefine.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: MyCsvDataDefine.h 5 | * 6 | * Description: Csv Data Define 7 | * 8 | * Version: 1.0 9 | * Created: 11/20/2015 03:45:15 PM 10 | * Revision: none 11 | * Compiler: gcc 12 | * 13 | * Author: huangyun (hy), 895175589@qq.com 14 | * Organization: 15 | * 16 | * ===================================================================================== 17 | */ 18 | 19 | #ifndef __MYCSVDATADEFINE_H__ 20 | #define __MYCSVDATADEFINE_H__ 21 | 22 | #include 23 | #include 24 | 25 | namespace MyNameSpace 26 | { 27 | #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE*)0)->MEMBER) 28 | 29 | class CsvConfigBase 30 | { 31 | }; 32 | 33 | struct stTest111Data 34 | { 35 | stTest111Data() : userLevel(0), consume(0), bloodVolume(0), attackPower(0) 36 | { 37 | memberOffsetVec.push_back(offsetof(stTest111Data, userLevel)); 38 | memberOffsetVec.push_back(offsetof(stTest111Data, consume)); 39 | memberOffsetVec.push_back(offsetof(stTest111Data, bloodVolume)); 40 | memberOffsetVec.push_back(offsetof(stTest111Data, attackPower)); 41 | memberOffsetVec.push_back(offsetof(stTest111Data, descrption)); 42 | } 43 | uint32_t userLevel; 44 | uint32_t consume; 45 | uint32_t bloodVolume; 46 | uint32_t attackPower; 47 | std::string descrption; 48 | std::vector memberOffsetVec; 49 | }; 50 | 51 | struct stTest111 : public CsvConfigBase 52 | { 53 | std::vector mData; 54 | }; 55 | } 56 | 57 | #endif 58 | -------------------------------------------------------------------------------- /base/MyCsvParse.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: MyCsvParse.h 5 | * 6 | * Description: CSV file Parse 7 | * 8 | * Version: 1.0 9 | * Created: 11/19/2015 02:58:23 PM 10 | * Revision: none 11 | * Compiler: gcc 12 | * 13 | * Author: huangyun (hy), 895175589@qq.com 14 | * Organization: 15 | * 16 | * ===================================================================================== 17 | */ 18 | 19 | #ifndef __MYCSVPARSE_H__ 20 | #define __MYCSVPARSE_H__ 21 | 22 | #include 23 | #include 24 | #include "MySingleton.h" 25 | #include "MyCsvDataDefine.h" 26 | #include 27 | #include 28 | 29 | namespace MyNameSpace 30 | { 31 | class MyCsvParse : public MySingleton 32 | { 33 | public: 34 | enum DATA_TYPE 35 | { 36 | eINT32, 37 | eUINT32, 38 | eFLOAT, 39 | eDOUBLE, 40 | eSTRING, 41 | 42 | eMax, 43 | }; 44 | 45 | public: 46 | MyCsvParse() 47 | { 48 | } 49 | 50 | ~MyCsvParse() 51 | { 52 | } 53 | 54 | static std::shared_ptr GetConfigByName(const std::string &className) 55 | { 56 | std::shared_ptr res; 57 | auto iter = mConfigMap.find(className); 58 | if (mConfigMap.end() != iter) 59 | { 60 | return res = iter->second; 61 | } 62 | return res; 63 | } 64 | bool init(); 65 | 66 | private: 67 | bool parseXml(); 68 | bool ParseCsvFile(); 69 | private: 70 | // std::string mFileName; 71 | // std::string mSeparator; 72 | 73 | public: 74 | static std::map mDataTypeMap; 75 | static std::unordered_map> mConfigMap; 76 | static std::map> mParseFunc; 77 | }; 78 | } 79 | 80 | #endif 81 | -------------------------------------------------------------------------------- /base/MyHashFunction.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: MyHashFunction.cpp 5 | * 6 | * Description: Common Hash Function 7 | * 8 | * Version: 1.0 9 | * Created: 09/28/2015 04:37:47 PM 10 | * Revision: none 11 | * Compiler: gcc 12 | * 13 | * Author: huangyun (hy), 895175589@qq.com 14 | * Organization: 15 | * 16 | * ===================================================================================== 17 | */ 18 | 19 | #include "MyHashFunction.h" 20 | #include 21 | 22 | namespace MyNameSpace 23 | { 24 | uint32_t SDBMHash(const std::string &str) 25 | { 26 | uint32_t hash = 0; 27 | for_each(str.begin(), str.end(), [&hash](char c){ hash = c + (hash<<6) + (hash<<16) - hash; }); 28 | return (hash & 0x7FFFFFFF); 29 | } 30 | uint32_t RSHash(const std::string &str) 31 | { 32 | uint32_t b = 378551; 33 | uint32_t a = 63689; 34 | uint32_t hash = 0; 35 | for_each (str.begin(), str.end(), [&hash, &a, b](char c) 36 | { 37 | hash = hash * a + c; 38 | a *= b; 39 | }); 40 | return (hash & 0X7FFFFFF); 41 | } 42 | uint32_t JSHash(const std::string &str) 43 | { 44 | uint32_t hash = 1315423911; 45 | for_each (str.begin(), str.end(), [&hash](char c){ hash ^= ((hash<<5) + c + (hash>>2)); }); 46 | return (hash & 0X7FFFFFF); 47 | } 48 | uint32_t PJWHash(const std::string &str) 49 | { 50 | uint32_t BitsInUnignedInt = (uint32_t)(sizeof(uint32_t) * 8); 51 | uint32_t ThreeQuarters = (uint32_t)((BitsInUnignedInt * 3) / 4); 52 | uint32_t OneEighth = (uint32_t)(BitsInUnignedInt / 8); 53 | uint32_t HighBits = (uint32_t)(0xFFFFFFFF) << (BitsInUnignedInt - OneEighth); 54 | uint32_t hash = 0; 55 | uint32_t test = 0; 56 | for_each (str.begin(), str.end(), [&hash, &test, ThreeQuarters, HighBits, OneEighth, BitsInUnignedInt](char c) 57 | { 58 | hash = (hash << OneEighth) + c; 59 | if ((test = hash & HighBits) != 0) 60 | { 61 | hash = ((hash ^ (test >> ThreeQuarters)) & (~HighBits)); 62 | } 63 | }); 64 | return hash & 0x7FFFFFFF; 65 | } 66 | uint32_t ELFHash(const std::string &str) 67 | { 68 | uint32_t hash = 0; 69 | uint32_t x = 0; 70 | for_each (str.begin(), str.end(), [&hash, &x](char c) 71 | { 72 | hash = (hash << 4) + c; 73 | if ((x = hash & 0xF000000L) != 0) 74 | { 75 | hash ^= (x >> 24); 76 | hash &= ~x; 77 | } 78 | }); 79 | return (hash & 0x7FFFFFFF); 80 | } 81 | uint32_t BKDRHash(const std::string &str) 82 | { 83 | uint32_t seed = 131; 84 | uint32_t hash = 0; 85 | for_each(str.begin(), str.end(), [&hash, seed](char c){ hash = hash *seed + c;}); 86 | return (hash & 0x7FFFFFFF); 87 | } 88 | uint32_t DJBHash(const std::string &str) 89 | { 90 | uint32_t hash = 5381; 91 | for_each (str.begin(), str.end(), [&hash](char c) { hash += (hash << 5) + c;}); 92 | return (hash & 0x7FFFFFFF); 93 | } 94 | uint32_t APHash(const std::string &str) 95 | { 96 | uint32_t hash = 0; 97 | int i; 98 | for_each (str.begin(), str.end(), [&hash, &i](char c) 99 | { 100 | if ((i & 1) == 0) 101 | { 102 | hash ^= ((hash << 7) ^ c ^(hash >> 3)); 103 | } 104 | else 105 | { 106 | hash ^= (~((hash << 11) ^ c ^ (hash >> 5))); 107 | } 108 | }); 109 | return (hash & 0x7FFFFFFF); 110 | } 111 | 112 | uint32_t FNVHash(const std::string &str) 113 | { 114 | const int prime = 0X811C9DC5; 115 | uint32_t hash = 0; 116 | for_each(str.begin(), str.end(), [&hash](char c) { hash *= prime; hash ^= c;}); 117 | return (hash & 0x7FFFFFFF); 118 | } 119 | 120 | } 121 | -------------------------------------------------------------------------------- /base/MyHashFunction.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: MyHashFunction.h 5 | * 6 | * Description: Common Hash Function 7 | * 8 | * Version: 1.0 9 | * Created: 09/28/2015 04:28:08 PM 10 | * Revision: none 11 | * Compiler: gcc 12 | * 13 | * Author: huangyun (hy), 895175589@qq.com 14 | * Organization: 15 | * 16 | * ===================================================================================== 17 | */ 18 | 19 | #ifndef __MYHASHFUNCTION_H_ 20 | #define __MYHASHFUNCTION_H_ 21 | 22 | #include 23 | #include 24 | #include 25 | 26 | namespace MyNameSpace 27 | { 28 | // typedef uint32_t (*HashFunction)(const std::string &); 29 | typedef std::function HashFunction; 30 | uint32_t SDBMHash(const std::string &str); 31 | uint32_t RSHash(const std::string &str); 32 | uint32_t JSHash(const std::string &str); 33 | uint32_t PJWHash(const std::string &str); 34 | uint32_t ELFHash(const std::string &str); 35 | uint32_t BKDRHash(const std::string &str); 36 | uint32_t DJBHash(const std::string &str); 37 | uint32_t APHash(const std::string &str); 38 | uint32_t FNVHash(const std::string &str); 39 | } 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /base/MyLock.h: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: mylock.h 3 | > Author: huangyun 4 | > Mail: 895175589@qq.com 5 | > Created Time: Fri 26 Jun 2015 09:25:52 PM 6 | ************************************************************************/ 7 | #ifndef __MYLOCK_H 8 | #define __MYLOCK_H 9 | #include 10 | 11 | namespace MyNameSpace 12 | { 13 | class MyLock 14 | { 15 | public: 16 | MyLock() 17 | { 18 | pthread_mutex_init(&mutex, NULL); 19 | } 20 | ~MyLock() 21 | { 22 | pthread_mutex_destroy(&mutex); 23 | } 24 | void lock() 25 | { 26 | pthread_mutex_lock(&mutex); 27 | } 28 | void unlock() 29 | { 30 | pthread_mutex_unlock(&mutex); 31 | } 32 | 33 | pthread_mutex_t *getMutex() 34 | { 35 | return &mutex; 36 | } 37 | private: 38 | pthread_mutex_t mutex; 39 | }; 40 | 41 | class MyScopeLock 42 | { 43 | public: 44 | MyScopeLock(MyLock & lock) : myLock(lock) 45 | { 46 | myLock.lock(); 47 | } 48 | ~MyScopeLock() 49 | { 50 | myLock.unlock(); 51 | } 52 | private: 53 | MyLock &myLock; 54 | }; 55 | 56 | class MyCond 57 | { 58 | public: 59 | MyCond(pthread_mutex_t *mutex = NULL) : pMutex(mutex) 60 | { 61 | pthread_cond_init(&cond, NULL); 62 | } 63 | ~MyCond() 64 | { 65 | pthread_cond_destroy(&cond); 66 | } 67 | void init(pthread_mutex_t *mutex) 68 | { 69 | pMutex = mutex; 70 | } 71 | void wait() 72 | { 73 | pthread_cond_wait(&cond, pMutex); 74 | } 75 | void notify() 76 | { 77 | pthread_cond_signal(&cond); 78 | } 79 | 80 | void broadCast() 81 | { 82 | pthread_cond_broadcast(&cond); 83 | } 84 | private: 85 | pthread_cond_t cond; 86 | pthread_mutex_t *pMutex; 87 | }; 88 | } 89 | #endif 90 | -------------------------------------------------------------------------------- /base/MyQueue.h: -------------------------------------------------------------------------------- 1 | #ifndef _MY_QUEUE_H_ 2 | #define _MY_QUEUE_H_ 3 | #include "mq.h" 4 | #include 5 | #include 6 | 7 | namespace MyNameSpace 8 | { 9 | class MyQueue 10 | { 11 | public: 12 | void pushMsg(int len, char *msg) 13 | { 14 | std::string str(msg, len); 15 | // std::cerr<<__FUNCTION__<<":"<<__LINE__<<"size:"<second, tmp->first); 25 | queue.pop(); 26 | ++i; 27 | } 28 | // std::cout << i << std::endl; 29 | } 30 | virtual bool cmdParse(const char *msg, int len) = 0; 31 | private: 32 | MessageQueue<1024> queue; 33 | }; 34 | 35 | /* 36 | class MyClass : public MyQueue 37 | { 38 | public: 39 | virtual void cmdParse(int len, char *msg) 40 | { 41 | string s; 42 | s.assign(msg, len); 43 | cout << "len:" << len << "msg:" << s << endl; 44 | } 45 | }; 46 | */ 47 | 48 | } 49 | #endif 50 | -------------------------------------------------------------------------------- /base/MySingleton.h: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: MySingleton.h 3 | > Author: huangyun 4 | > Mail: 895175589@qq.com 5 | > Created Time: Fri 26 Jun 2015 11:09:53 PM 6 | ************************************************************************/ 7 | #ifndef __MYSINGLETONH__ 8 | #define __MYSINGLETONH__ 9 | 10 | #include 11 | 12 | namespace MyNameSpace 13 | { 14 | template 15 | class MySingleton 16 | { 17 | public: 18 | static T & getInstance() 19 | { 20 | pthread_once(&ponce_, &MySingleton::init); 21 | return *instance; 22 | } 23 | 24 | protected: 25 | MySingleton() {} 26 | MySingleton(const MySingleton&) {} 27 | private: 28 | static void init() 29 | { 30 | instance = new T(); 31 | } 32 | private: 33 | static pthread_once_t ponce_; 34 | static T *instance; 35 | }; 36 | 37 | template 38 | pthread_once_t MySingleton::ponce_ = PTHREAD_ONCE_INIT; 39 | 40 | template 41 | T *MySingleton::instance = nullptr; 42 | } 43 | 44 | #endif 45 | 46 | 47 | -------------------------------------------------------------------------------- /base/MySockTask.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: MySockTask.cpp 3 | > Author: huangyun 4 | > Mail: 895175589@qq.com 5 | > Created Time: Fri 07 Aug 2015 08:22:12 AM 6 | ************************************************************************/ 7 | 8 | #include "MySockTask.h" 9 | #include "MySocket.h" 10 | #include 11 | #include 12 | 13 | namespace MyNameSpace 14 | { 15 | MySockTask::MySockTask(int sock, int id): mSock(sock), mId(id) 16 | { 17 | } 18 | 19 | int MySockTask::getMsg() 20 | { 21 | int msgCount = 0; 22 | while(true) 23 | { 24 | int len = 0; 25 | std::vector msg; 26 | len = mSock.readBuffer(msg); 27 | if (len <= 0) 28 | { 29 | if (len < 0) 30 | { 31 | msgCount = -1; 32 | } 33 | break; 34 | } 35 | ++msgCount; 36 | std::string str(&msg[0], len); 37 | //std::cerr<<__FUNCTION__<<"("<<__LINE__<<") size :"< File Name: MySockTask.h 3 | > Author: huangyun 4 | > Mail: 895175589@qq.com 5 | > Created Time: Fri 07 Aug 2015 08:16:14 AM 6 | ************************************************************************/ 7 | 8 | #ifndef _MYSOCK_TASK_H_ 9 | #define _MYSOCK_TASK_H_ 10 | #include "MyQueue.h" 11 | #include "MySocket.h" 12 | 13 | namespace MyNameSpace 14 | { 15 | class MySockTask : public MyQueue 16 | { 17 | public: 18 | MySockTask(int sock, int id); 19 | virtual ~MySockTask() {} 20 | int getMsg(); 21 | int rcvBuffer(); 22 | int sendDataWithBuffer(const char *buf, uint32_t len); 23 | int addEpollEvent(int epfd, epoll_event & ev); 24 | int delEpollEvent(int epfd, epoll_event & ev); 25 | int syncSendBuf(); 26 | int getId() 27 | { 28 | return mId; 29 | } 30 | private: 31 | MySocket mSock; 32 | int mId; 33 | }; 34 | } 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /base/MySockTaskManager.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: MySockTaskManager.cpp 3 | > Author: huangyun 4 | > Mail: 895175589@qq.com 5 | > Created Time: Sat 08 Aug 2015 07:43:35 PM 6 | ************************************************************************/ 7 | 8 | #include "MySockTaskManager.h" 9 | 10 | namespace MyNameSpace 11 | { 12 | void MySockTaskManager::addTask(MySockTask *task) 13 | { 14 | MyScopeLock lock(mLock); 15 | mTasks[task->getId()] = task; 16 | } 17 | 18 | void MySockTaskManager::removeTask(MySockTask *task) 19 | { 20 | MyScopeLock lock(mLock); 21 | mTasks.erase(task->getId()); 22 | } 23 | 24 | bool MySockTaskManager::doProcessMsg() 25 | { 26 | MyScopeLock lock(mLock); 27 | for (Container_IT it = mTasks.begin(); it != mTasks.end(); ++it) 28 | { 29 | it->second->processMsg(); 30 | } 31 | return true; 32 | } 33 | 34 | MySockTask* MySockTaskManager::getTaskByIdWithOutLock(int id) 35 | { 36 | Container_IT it; 37 | // MyScopeLock lock(mLock); 38 | it = mTasks.find(id); 39 | if (mTasks.end() == it) 40 | { 41 | return NULL; 42 | } 43 | return it->second; 44 | } 45 | 46 | MySockTask* MySockTaskManager::getTaskByIdWithLock(int id) 47 | { 48 | Container_IT it; 49 | MyScopeLock lock(mLock); 50 | it = mTasks.find(id); 51 | if (mTasks.end() == it) 52 | { 53 | return NULL; 54 | } 55 | return it->second; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /base/MySockTaskManager.h: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: MySockTaskManager.h 3 | > Author: huangyun 4 | > Mail: 895175589@qq.com 5 | > Created Time: Sat 08 Aug 2015 07:31:14 PM 6 | ************************************************************************/ 7 | #ifndef _MY_SOCK_TASK_MANAGER_H_ 8 | #define _MY_SOCK_TASK_MANAGER_H_ 9 | 10 | #include "MyTask.h" 11 | #include "MySingleton.h" 12 | #include 13 | #include "MyLock.h" 14 | 15 | namespace MyNameSpace 16 | { 17 | class MySockTaskManager : public MySingleton 18 | { 19 | private: 20 | friend class MySingleton; 21 | MySockTaskManager() 22 | { 23 | 24 | } 25 | ~MySockTaskManager() 26 | { 27 | 28 | } 29 | public: 30 | void addTask(MySockTask *task); 31 | void removeTask(MySockTask *task); 32 | bool doProcessMsg(); 33 | MySockTask* getTaskByIdWithOutLock(int id); 34 | MySockTask* getTaskByIdWithLock(int id); 35 | private: 36 | typedef std::map Container; 37 | typedef std::map::iterator Container_IT; 38 | Container mTasks; 39 | MyLock mLock; //暂时先共用一把锁,以后优化 40 | }; 41 | } 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /base/MySockTaskPool.h: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: MySockTaskPool.h 3 | > Author: huangyun 4 | > Mail: 895175589@qq.com 5 | > Created Time: Fri 07 Aug 2015 10:56:08 PM 6 | ************************************************************************/ 7 | #ifndef _MYSOCK_TASK_POOL_H 8 | #define _MYSOCK_TASK_POOL_H 9 | 10 | #include "MyThread.h" 11 | #include "MySockTask.h" 12 | namespace MyNameSpace 13 | { 14 | class MyRecycleThread; 15 | class MySockTaskPool 16 | { 17 | public: 18 | MySockTaskPool(int ioCount, int maxConnPerIo); 19 | ~MySockTaskPool(); 20 | bool addTask(MySockTask * task); 21 | int getMaxCoonPerIo() 22 | { 23 | return mMaxConnPerIo; 24 | } 25 | void addRecycleThread(MySockTask *task); 26 | bool init(); 27 | void fini(); 28 | private: 29 | int mIoCount; 30 | int mMaxConnPerIo; 31 | MyThreadPool mIoThreadPool; 32 | MyRecycleThread *mRecycleThread; 33 | }; 34 | } 35 | #endif 36 | -------------------------------------------------------------------------------- /base/MySocket.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: MySocket.cpp 3 | > Author: huangyun 4 | > Mail: 895175589@qq.com 5 | > Created Time: Thu 06 Aug 2015 11:54:45 PM 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | #include 11 | #include "MySocket.h" 12 | #include "errno.h" 13 | 14 | namespace MyNameSpace 15 | { 16 | MySocket::MySocket(int sock) : mSock(sock) 17 | { 18 | } 19 | 20 | int MySocket::rcvBuffer() 21 | { 22 | char buf[64 * 1024]; 23 | memset(buf, 0x0, sizeof(buf)); 24 | int len = ::recv(mSock, buf, sizeof(buf), 0); 25 | if (len < 0) 26 | { 27 | if (errno != EINTR) 28 | { 29 | return -1; 30 | } 31 | return 0; 32 | } 33 | if (len == 0) 34 | { 35 | return 0; 36 | } 37 | mRecvBuffer.writeBuffer(buf, len); 38 | return len; 39 | } 40 | 41 | int MySocket::addEpollEvent(int epfd, epoll_event & ev) 42 | { 43 | return epoll_ctl(epfd, EPOLL_CTL_ADD, mSock, &ev); 44 | } 45 | 46 | int MySocket::delEpollEvent(int epfd, epoll_event & ev) 47 | { 48 | return epoll_ctl(epfd, EPOLL_CTL_DEL, mSock, &ev); 49 | } 50 | 51 | int MySocket::sendDataWithBuffer(const char *buf, uint32_t len) 52 | { 53 | // std::cerr<<__FUNCTION__<<"("<<__LINE__<<") size :"< msg; 70 | int len = mSendBuffer.readBuffer(msg); 71 | if (0 == len) 72 | { 73 | return 0; 74 | } 75 | int l = len; 76 | while(l > 0) 77 | { 78 | // std::cout<<"send buf:"< &msg) 94 | { 95 | return mRecvBuffer.readBuffer(msg); 96 | } 97 | 98 | uint16_t MySocket::getRemotePort() 99 | { 100 | sockaddr_in s; 101 | socklen_t len; 102 | len = sizeof(s); 103 | uint16_t port = 0; 104 | if ( 0 == getpeername(mSock, (sockaddr*)&s, &len)) 105 | { 106 | port = ntohs(s.sin_port); 107 | } 108 | return port; 109 | } 110 | std::string MySocket::getRemoteIp() 111 | { 112 | sockaddr_in s; 113 | socklen_t len; 114 | std::string ip; 115 | len = sizeof(s); 116 | if ( 0 == getpeername(mSock, (sockaddr*)&s, &len)) 117 | { 118 | ip= ::inet_ntoa(s.sin_addr); 119 | } 120 | return ip; 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /base/MyTask.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: MyTask.cpp 3 | > Author: huangyun 4 | > Mail: 895175589@qq.com 5 | > Created Time: Sat 08 Aug 2015 05:38:55 PM 6 | ************************************************************************/ 7 | 8 | #include "MyTask.h" 9 | 10 | namespace MyNameSpace 11 | { 12 | bool MyTask::cmdParse(const char *msg, int len) 13 | { 14 | std::cout<<"len: "< File Name: MyTask.h 3 | > Author: huangyun 4 | > Mail: 895175589@qq.com 5 | > Created Time: Sat 08 Aug 2015 05:31:44 PM 6 | ************************************************************************/ 7 | #ifndef _MY_TASK_H_ 8 | #define _MY_TASK_H_ 9 | #include "MySockTask.h" 10 | 11 | namespace MyNameSpace 12 | { 13 | class MyTask : public MySockTask 14 | { 15 | public: 16 | virtual bool cmdParse(const char *msg, int len); 17 | MyTask(int sock, int id) : MySockTask(sock, id) 18 | { 19 | 20 | } 21 | }; 22 | } 23 | #endif 24 | -------------------------------------------------------------------------------- /base/MyThread.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: MyThread.cpp 3 | > Author: huangyun 4 | > Mail: 895175589@qq.com 5 | > Created Time: Fri 07 Aug 2015 12:25:55 AM 6 | ************************************************************************/ 7 | 8 | #include "MyThread.h" 9 | #include 10 | #include 11 | 12 | namespace MyNameSpace 13 | { 14 | MyThread::MyThread(bool j) : pid(0), state(0), mJoinable(j), complete(false) 15 | { 16 | 17 | } 18 | void *MyThread::threadFun(void *arg) 19 | { 20 | MyThread *p = static_cast(arg); 21 | p->setState(1); 22 | p->notifyStartSuccess(); 23 | std::cerr<<__FUNCTION__<<":"<<__LINE__<<" thread "<< "start!"<run(); 25 | return NULL; 26 | } 27 | bool MyThread::start() 28 | { 29 | if (isStart()) 30 | { 31 | return true; 32 | } 33 | if (!init()) 34 | { 35 | std::cerr<<__FUNCTION__<<":"<<__LINE__<<" MyThread init fail!"<terminate(); 74 | if ((*iter)->isJoinAble()) 75 | { 76 | (*iter)->join(); 77 | } 78 | } 79 | } 80 | 81 | namespace 82 | { 83 | class DelFunctor 84 | { 85 | public: 86 | void operator()(MyThread *thread) 87 | { 88 | if (NULL != thread) 89 | { 90 | delete thread; 91 | } 92 | } 93 | }; 94 | DelFunctor delfun; 95 | } 96 | 97 | void MyThreadPool::delAllThread() 98 | { 99 | std::for_each(mThreads.begin(), mThreads.end(), delfun); 100 | mThreads.clear(); 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /base/MyThread.h: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: MyThread.h 3 | > Author: huangyun 4 | > Mail: 895175589@qq.com 5 | > Created Time: Fri 07 Aug 2015 12:11:57 AM 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | #include "MyLock.h" 11 | #include 12 | #include 13 | 14 | #ifndef __MYTHREAD_H_ 15 | #define __MYTHREAD_H_ 16 | namespace MyNameSpace 17 | { 18 | class MyThread 19 | { 20 | public: 21 | MyThread(bool j = false); 22 | virtual ~MyThread() {} 23 | bool start(); 24 | static void* threadFun(void *); 25 | virtual void run() = 0; 26 | void notifyStartSuccess(); 27 | pthread_t getId() 28 | { 29 | return pthread_self(); 30 | } 31 | void setState(int s) 32 | { 33 | state = s; 34 | } 35 | bool isStart() 36 | { 37 | return state == 1; 38 | } 39 | void terminate() 40 | { 41 | complete = true; 42 | } 43 | bool isJoinAble() 44 | { 45 | return mJoinable; 46 | } 47 | int join(); 48 | bool isFini() 49 | { 50 | return complete; 51 | } 52 | virtual bool init() 53 | { 54 | mCond.init(mLock.getMutex()); 55 | return true; 56 | } 57 | private: 58 | pthread_t pid; 59 | volatile int state; //0未开始运行 1正在运行 2死亡 60 | bool mJoinable; 61 | MyLock mLock; 62 | MyCond mCond; 63 | int epfd; 64 | std::vector events; 65 | bool complete; 66 | }; 67 | 68 | class MyThreadPool 69 | { 70 | public: 71 | void putThread(MyThread *pThread); 72 | MyThread * getThreadByIndex(int index) 73 | { 74 | return mThreads[index]; 75 | } 76 | int getThreadCOunt() 77 | { 78 | return mThreads.size(); 79 | } 80 | void joinAll(); 81 | void delAllThread(); 82 | private: 83 | typedef std::vector Container; 84 | typedef std::vector::iterator ContainerIt; 85 | Container mThreads; 86 | }; 87 | } 88 | #endif 89 | -------------------------------------------------------------------------------- /base/MyTime.h: -------------------------------------------------------------------------------- 1 | #ifndef _MYTIMEH__ 2 | #define _MYTIMEH__ 3 | 4 | #include 5 | 6 | namespace MyNameSpace 7 | { 8 | class MyTime 9 | { 10 | public: 11 | MyTime() 12 | { 13 | time(&start); 14 | } 15 | time_t elapse() 16 | { 17 | time_t now; 18 | time(&now); 19 | return now - start; 20 | } 21 | void now() 22 | { 23 | time(&start); 24 | } 25 | private: 26 | time_t start; 27 | }; 28 | } 29 | #endif 30 | 31 | -------------------------------------------------------------------------------- /base/mq.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | namespace MyNameSpace 7 | { 8 | typedef std::pairMsgPair; 9 | typedef std::pairQueuePair; 10 | template 11 | class MessageQueue 12 | { 13 | public: 14 | MessageQueue() : readPos(0), writePos(0) 15 | { 16 | } 17 | bool pushMsg(int len, char *msg) 18 | { 19 | char *buf = NULL; 20 | buf = new char[len]; 21 | if (buf != NULL) 22 | { 23 | memcpy((void *)buf, (void *)msg, len); 24 | // string s; 25 | // s.assign(buf, len); 26 | // cout << "len:" << len << "msg:" << s << endl; 27 | processCache(); 28 | if (!msgQueue[writePos].first) 29 | { 30 | msgQueue[writePos].second.first = len; 31 | msgQueue[writePos].second.second = buf; 32 | msgQueue[writePos].first = true; 33 | // writePos = ++writePos % QUEUE_SIZE; 34 | ++writePos; 35 | writePos %= QUEUE_SIZE; 36 | } 37 | else 38 | { 39 | cacheQueue.push(std::make_pair(len, buf)); 40 | } 41 | return true; 42 | } 43 | return false; 44 | } 45 | MsgPair *getMsg() 46 | { 47 | MsgPair *ret = NULL; 48 | if (msgQueue[readPos].first) 49 | { 50 | ret = &msgQueue[readPos].second; 51 | } 52 | else 53 | { 54 | // processCache(); maybe multithread 55 | if (msgQueue[readPos].first) 56 | { 57 | ret = &msgQueue[readPos].second; 58 | } 59 | } 60 | return ret; 61 | } 62 | void pop() 63 | { 64 | if (msgQueue[readPos].first) 65 | { 66 | delete [] msgQueue[readPos].second.second; 67 | msgQueue[readPos].first = false; 68 | // readPos = ++readPos % QUEUE_SIZE; 69 | ++readPos; 70 | readPos %= QUEUE_SIZE; 71 | } 72 | else 73 | { 74 | processCache(); 75 | if (msgQueue[readPos].first) 76 | { 77 | delete[] msgQueue[readPos].second.second; 78 | msgQueue[readPos].first = false; 79 | // readPos = ++readPos % QUEUE_SIZE; 80 | ++readPos; 81 | readPos %= QUEUE_SIZE; 82 | } 83 | } 84 | } 85 | void processCache() 86 | { 87 | while (!cacheQueue.empty()) 88 | { 89 | if (!msgQueue[writePos].first) 90 | { 91 | MsgPair tmp = cacheQueue.front(); 92 | msgQueue[writePos].second = tmp; 93 | msgQueue[writePos].first = true; 94 | // writePos = ++writePos % QUEUE_SIZE; 95 | ++writePos; 96 | writePos %= QUEUE_SIZE; 97 | cacheQueue.pop(); 98 | } 99 | else 100 | { 101 | return; 102 | } 103 | } 104 | } 105 | void clear() 106 | { 107 | while (getMsg()) 108 | { 109 | pop(); 110 | } 111 | } 112 | private: 113 | unsigned int readPos; 114 | unsigned int writePos; 115 | QueuePair msgQueue[QUEUE_SIZE]; 116 | std::queue > cacheQueue; 117 | }; 118 | } 119 | -------------------------------------------------------------------------------- /base/mysqlConnectPool/DBDefine.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: DBDefine.h 5 | * 6 | * Description: define database operation struct 7 | * 8 | * Version: 1.0 9 | * Created: 10/09/2015 10:26:34 AM 10 | * Revision: none 11 | * Compiler: gcc 12 | * 13 | * Author: huangyun (hy), 895175589@qq.com 14 | * Organization: 15 | * 16 | * ===================================================================================== 17 | */ 18 | 19 | #ifndef __DBDEFINE_H__ 20 | #define __DBDEFINE_H__ 21 | 22 | #include "DBPool.h" 23 | #include 24 | 25 | using namespace MyNameSpace; 26 | 27 | /*example 28 | const dbCol test_db_define[] = 29 | { 30 | {"USERID", DB_DATA_TYPE::DB_ULONG, 8, NULL}, 31 | {"MYNAME", DB_DATA_TYPE::DB_STR, 32, NULL}, 32 | {"ISOK", DB_DATA_TYPE::DB_UCHAR, 1, NULL}, 33 | {NULL , DB_DATA_TYPE::DB_INVALID, 0, NULL} 34 | }; 35 | */ 36 | 37 | struct testDataStruct 38 | { 39 | uint64_t userId; 40 | char name[32]; 41 | unsigned char isOk; 42 | } __attribute__ ((packed)); 43 | 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /base/mysqlConnectPool/DBPool.h: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: DbPool.h 3 | > Author: huangyun 4 | > Mail: 895175589@qq.com 5 | > Created Time: Fri 26 Jun 2015 09:44:15 PM 6 | ************************************************************************/ 7 | 8 | #ifndef __DBPOOLH__ 9 | #define __DBPOOLH__ 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include "MyLock.h" 17 | 18 | namespace MyNameSpace 19 | { 20 | enum class DB_DATA_TYPE 21 | { 22 | DB_CHAR, 23 | DB_UCHAR, 24 | DB_SHORT, 25 | DB_USHORT, 26 | DB_INT, 27 | DB_UINT, 28 | DB_LONG, 29 | DB_ULONG, 30 | DB_STR, 31 | DB_BIN, 32 | //add new type before DB_INVALID 33 | 34 | DB_INVALID 35 | }; 36 | 37 | typedef struct 38 | { 39 | const char *name; 40 | DB_DATA_TYPE type; 41 | uint32_t size; 42 | unsigned char *data; 43 | } dbCol; 44 | 45 | class MyDBConn; 46 | class DBPool 47 | { 48 | public: 49 | struct DBConnInfo 50 | { 51 | std::string host; 52 | std::string user; 53 | std::string passwd; 54 | std::string dbName; 55 | int port; 56 | bool supportTransactions; 57 | }; 58 | 59 | public: 60 | DBPool(DBConnInfo & info) : connInfo(info), MAX_CONN_COUNT(100) 61 | { 62 | 63 | } 64 | 65 | void setConnInfo(DBConnInfo & info) 66 | { 67 | connInfo = info; 68 | } 69 | int getConn(); 70 | void releaseConn(int connId); 71 | uint32_t execInsert(uint32_t handleId, const char *tableName, const dbCol *column); 72 | uint32_t execDelete(uint32_t handleId, const char *tableName, const char * where); 73 | uint32_t execUpdate(uint32_t handleId, const char *tableName, const dbCol *column, const char * where); 74 | uint32_t execSelect(uint32_t handleId, const char *tableName, const dbCol *column, const char *where, const char *order, unsigned char **data); 75 | uint32_t execSelectLimit(uint32_t handleId, const char *tableName, const dbCol *column, const char *where, const char *order, uint32_t limit, unsigned char *data, uint32_t limitFrom = 0); 76 | bool commit(uint32_t handleId); 77 | bool rollback(uint32_t handleId); 78 | bool setTransactions(uint32_t handleId, bool bSupportTransactions); 79 | bool isSupportTransactions(uint32_t handleId); 80 | 81 | private: 82 | std::shared_ptr getHandleById(uint32_t handleId); 83 | private: 84 | using ConnPoolType = std::map >; 85 | ConnPoolType connPool; 86 | DBConnInfo connInfo; 87 | const uint32_t MAX_CONN_COUNT; 88 | MyLock myLock; 89 | }; 90 | } 91 | 92 | #endif 93 | -------------------------------------------------------------------------------- /base/mysqlConnectPool/Makefile: -------------------------------------------------------------------------------- 1 | CPPFLAGS = -std=c++11 -Wall -g -gstabs -I ../ 2 | .PHONY : all clean 3 | all : DBPool.o 4 | test : test.o DBPool.o 5 | g++ $(CPPFLAGS) -o test test.o DBPool.o -lmysqlclient -lpthread 6 | 7 | test.o : test.cpp DBDefine.h 8 | g++ $(CPPFLAGS) -c test.cpp 9 | 10 | DBPool.o : DBPool.cpp 11 | g++ $(CPPFLAGS) -c DBPool.cpp 12 | 13 | clean: 14 | rm -rf test 15 | rm -rf test.o 16 | rm -rf DBPool.o 17 | -------------------------------------------------------------------------------- /base/mysqlConnectPool/MySingleton.h: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: MySingleton.h 3 | > Author: huangyun 4 | > Mail: 895175589@qq.com 5 | > Created Time: Fri 26 Jun 2015 11:09:53 PM 6 | ************************************************************************/ 7 | #ifndef __MYSINGLETONH__ 8 | #define __MYSINGLETONH__ 9 | 10 | #include 11 | using namespace std; 12 | 13 | namespace MyNameSpace 14 | { 15 | template 16 | class MySingleton 17 | { 18 | public: 19 | static T & getInstance() 20 | { 21 | static T instance; 22 | return instance; 23 | } 24 | private: 25 | }; 26 | } 27 | 28 | #endif 29 | 30 | 31 | -------------------------------------------------------------------------------- /base/mysqlConnectPool/sql/import.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | user="root" 3 | pass="123123" 4 | db="test" 5 | mysql -u${user} -p${pass} $db < sql.sql 6 | -------------------------------------------------------------------------------- /base/mysqlConnectPool/sql/sql.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE IF EXISTS `USERINFO`; 2 | CREATE TABLE `USERINFO` ( 3 | `id` int(10) unsigned NOT NULL AUTO_INCREMENT, 4 | `USERID` int(8) unsigned NOT NULL, 5 | `MYNAME` varchar(32) NOT NULL, 6 | `ISOK` varchar(1) NOT NULL, 7 | PRIMARY KEY (`id`) 8 | ) ENGINE=MyISAM DEFAULT CHARSET=utf8; 9 | -------------------------------------------------------------------------------- /base/redisConnectPool/Makefile: -------------------------------------------------------------------------------- 1 | CPPFLAGS = -std=c++11 -Wall -g -gstabs -I ../../lib/hiredis -L../../lib/hiredis -lhiredis 2 | .PHONY : all clean 3 | all : test redisOpt.o 4 | test : test.o redisOpt.o 5 | g++ $(CPPFLAGS) -o test test.o redisOpt.o 6 | 7 | test.o : test.cpp redisOpt.h 8 | g++ $(CPPFLAGS) -c test.cpp 9 | 10 | redisOpt.o : redisOpt.cpp 11 | g++ $(CPPFLAGS) -c redisOpt.cpp 12 | 13 | clean: 14 | rm -rf test 15 | rm -rf test.o 16 | rm -rf redisOpt.o 17 | -------------------------------------------------------------------------------- /base/redisConnectPool/redisDataType.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: redisDataType.h 5 | * 6 | * Description: redis result Data Type 7 | * 8 | * Version: 1.0 9 | * Created: 10/27/2015 02:50:20 PM 10 | * Revision: none 11 | * Compiler: gcc 12 | * 13 | * Author: huangyun (hy), 895175589@qq.com 14 | * Organization: 15 | * 16 | * ===================================================================================== 17 | */ 18 | 19 | #ifndef __REDISDATATYPE_H_ 20 | #define __REDISDATATYPE_H_ 21 | 22 | #include 23 | #include 24 | 25 | class MyRedisDataType 26 | { 27 | MyRedisDataType() 28 | { 29 | public: 30 | MyRedisDataType() 31 | { 32 | memset(this, 0x0, sizeof(*this)); 33 | } 34 | 35 | const char *getBin() const 36 | { 37 | return &mBin[0]; 38 | } 39 | 40 | size_t getBinSize() const 41 | { 42 | return mBin.size(); 43 | } 44 | 45 | void setBool(bool b) const 46 | { 47 | mBool = b; 48 | } 49 | 50 | bool getBool() const 51 | { 52 | return mBool; 53 | } 54 | 55 | const char *getStr() const 56 | { 57 | return mStr; 58 | } 59 | 60 | std::vector mBin; 61 | uint64_t mUi64t; 62 | int64_t mI64t; 63 | const char *mStr; 64 | double mDouble; 65 | float mFloat; 66 | uint32_t mUi32t; 67 | int32_t mI32t; 68 | uint16_t mUi16t; 69 | int16_t mI16t; 70 | uint8_t mUi8t; 71 | int8_t mI8t; 72 | bool mBool; 73 | } 74 | }; 75 | 76 | #endif 77 | -------------------------------------------------------------------------------- /base/redisConnectPool/redisOpt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: redisOpt.h 5 | * 6 | * Description: redis opt 7 | * 8 | * Version: 1.0 9 | * Created: 10/26/2015 10:53:21 PM 10 | * Revision: none 11 | * Compiler: gcc 12 | * 13 | * Author: huangyun (hy), 895175589@qq.com 14 | * Organization: 15 | * 16 | * ===================================================================================== 17 | */ 18 | 19 | #ifndef __REDISOPT_H_ 20 | #define __REDISOPT_H_ 21 | 22 | #include 23 | #include "hiredis.h" 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | namespace MyNameSpace 30 | { 31 | class MyRedisOpt 32 | { 33 | public: 34 | enum class existRtn : char 35 | { 36 | EERROR, 37 | EKEYEXIST, 38 | EKEYNOEXIST, 39 | 40 | ESETMEM, 41 | ENOTSETMEM, 42 | }; 43 | MyRedisOpt(const std::string &host, int port, int dbNum, const std::string &password=""); 44 | ~MyRedisOpt(); 45 | 46 | bool init(); 47 | bool getInt(const char *key, int64_t &value); 48 | bool setInt(const char *key, const int64_t &value); 49 | bool setExpire(const char *key, unsigned long long expireTime); 50 | bool set(const char *key, const char* value); 51 | bool setBin(const char *key, const char* value, int len); 52 | bool getBin(const char *key, std::string &value); 53 | bool setSet(const char *key, const char *value); 54 | bool setSet(const char *key, unsigned long value); 55 | bool getSet(const char *key, std::set &valueSet, unsigned int count); 56 | bool getSet(const char *key, std::set &valueSet, unsigned int count); 57 | bool del(const char *key); 58 | bool delSet(const char *key, const char *value); 59 | bool delSet(const char *key, unsigned long value); 60 | existRtn isSet(const char *key, const char *value); 61 | existRtn isSet(const char *key, unsigned long value); 62 | existRtn existKey(const char *key); 63 | bool setZSet(const char *key, const unsigned long score, const char *value); 64 | bool delZSet(const char *key, const char *value); 65 | bool addZSetScore(const char *key, unsigned long score, const char *value); 66 | bool getZSetScore(const char *key, const char *value, unsigned long &score); 67 | bool getZSet(const char *key, std::set &valueSet, unsigned long start, unsigned long end); 68 | bool getZSetWithScore(const char *key, std::map &valueMap, const unsigned long start, const unsigned long end); 69 | bool hSet(const char *key, const char *field, const char *value); 70 | bool hMSet(const char *key, std::map fieldValue); 71 | bool hGet(const char *key, const char *field, std::string &value); 72 | bool hMGet(const char *key, const std::set &fieldSet, std::vector &valueVec); 73 | bool getKeys(std::set &keySet, const char *keyFilter = "*"); 74 | 75 | private: 76 | void fini(); 77 | const char *getStrByType(int type); 78 | private: 79 | redisContext *mContext; 80 | redisReply *mReply; 81 | std::string mHost; 82 | int mPort; 83 | int mDbNum; 84 | std::string mPassword; 85 | }; 86 | } 87 | 88 | #endif 89 | -------------------------------------------------------------------------------- /base/redisConnectPool/test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/p308945/MyNetWorkLib/e86d550bf837b08d3fe9b76341e802096c1f8f5c/base/redisConnectPool/test -------------------------------------------------------------------------------- /base/redisConnectPool/test.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: test.cpp 5 | * 6 | * Description: test 7 | * 8 | * Version: 1.0 9 | * Created: 10/28/2015 11:27:51 AM 10 | * Revision: none 11 | * Compiler: gcc 12 | * 13 | * Author: huangyun (hy), 895175589@qq.com 14 | * Organization: 15 | * 16 | * ===================================================================================== 17 | */ 18 | 19 | #include "redisOpt.h" 20 | #include 21 | 22 | int main() 23 | { 24 | MyNameSpace::MyRedisOpt redisConn("192.168.123.174", 6379, 0); 25 | redisConn.init(); 26 | std::map fieldValue; 27 | fieldValue.insert(std::make_pair("3", "3")); 28 | fieldValue.insert(std::make_pair("4", "4")); 29 | fieldValue.insert(std::make_pair("5", "5")); 30 | fieldValue.insert(std::make_pair("6", "6")); 31 | std::cout<<(int)redisConn.hMSet("test", fieldValue)< value; 34 | std::set field; 35 | field.insert("1"); 36 | field.insert("2"); 37 | field.insert("3"); 38 | std::cout<<(int)redisConn.hMGet("test", field, value)< File Name: MyClient.cpp 3 | > Author: huangyun 4 | > Mail: 895175589@qq.com 5 | > Created Time: Sun 09 Aug 2015 01:10:29 AM 6 | ************************************************************************/ 7 | 8 | #include "MyClient.h" 9 | #include "MyClientTaskManager.h" 10 | #include "MyClientTask.h" 11 | 12 | namespace MyNameSpace 13 | { 14 | bool MyClient::init(std::multimap &ipPortPair) 15 | { 16 | if (!mTaskPool.init()) 17 | { 18 | std::cerr<<__FUNCTION__<<"("<<__LINE__<<"): task pool init fail"<::iterator it = ipPortPair.begin(); it != ipPortPair.end(); ++it) 21 | { 22 | ++mUniqueId; 23 | MyClientTask *task = new MyClientTask(mUniqueId, it->first, it->second); 24 | if (NULL != task) 25 | { 26 | MyClientTaskManager::getInstance().addTask(task); 27 | mTaskPool.addTask(task); 28 | } 29 | } 30 | return true; 31 | } 32 | 33 | void MyClient::mainLoop() 34 | { 35 | while(!isFini()) 36 | { 37 | MyClientTaskManager::getInstance().doProcessMsg(); 38 | // MyClientTaskManager::getInstance().testSend(); 39 | usleep(3 * 1000); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /client/MyClient.h: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: MyClient.h 3 | > Author: huangyun 4 | > Mail: 895175589@qq.com 5 | > Created Time: Sat 08 Aug 2015 07:10:10 PM 6 | ************************************************************************/ 7 | #ifndef _MY_CLIENT_H_ 8 | #define _MY_CLIENT_H_ 9 | 10 | #include 11 | #include "MySockClientTaskPool.h" 12 | 13 | namespace MyNameSpace 14 | { 15 | class MyClient 16 | { 17 | public: 18 | MyClient() : mUniqueId(0), mComplete(false) 19 | { 20 | 21 | } 22 | ~MyClient() 23 | { 24 | 25 | } 26 | bool init(std::multimap &ipPortPair); 27 | void mainLoop(); 28 | bool isFini() 29 | { 30 | return mComplete; 31 | } 32 | private: 33 | uint32_t mUniqueId; 34 | MySockClientTaskPool mTaskPool; 35 | bool mComplete; 36 | }; 37 | } 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /client/MyClientTask.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: MyClientTask.cpp 3 | > Author: huangyun 4 | > Mail: 895175589@qq.com 5 | > Created Time: Sat 08 Aug 2015 11:22:40 PM 6 | ************************************************************************/ 7 | 8 | #include "MyClientTask.h" 9 | #include "../proto/BaseCmd.h" 10 | 11 | namespace MyNameSpace 12 | { 13 | bool MyClientTask::cmdParse(const char *msg, int len) 14 | { 15 | std::cout<<"len: "<(msg); 19 | switch(pCmd->mType) 20 | { 21 | case Command::COMMAND_TYPE::INNER: 22 | { 23 | mInnerDispatcher->dispatcher(pCmd, len, getId()); 24 | } 25 | break; 26 | case Command::COMMAND_TYPE::OUTTER: 27 | { 28 | mOutterDispatcher->dispatcher(pCmd, len, getId()); 29 | } 30 | break; 31 | default: 32 | { 33 | std::cerr<<__FUNCTION__<<": "<<__LINE__<<"type "<<(int)pCmd->mType<<" error"< File Name: MyClientTask.h 3 | > Author: huangyun 4 | > Mail: 895175589@qq.com 5 | > Created Time: Sat 08 Aug 2015 11:20:49 PM 6 | ************************************************************************/ 7 | #ifndef _MY_CLIENT_TASK_H_ 8 | #define _MY_CLIENT_TASK_H_ 9 | #include "MySockClientTask.h" 10 | #include "../base/CmdDispatcher.h" 11 | 12 | namespace MyNameSpace 13 | { 14 | class MyClientTask : public MySockClientTask 15 | { 16 | public: 17 | virtual bool cmdParse(const char *msg, int len); 18 | MyClientTask(int id, int ip, int port) : MySockClientTask(id, ip, port, 0, 0) 19 | { 20 | 21 | } 22 | MyClientTask(int id, int ip, int port, int serverId, int serverType, Dispatcher *iDispatcher, Dispatcher *oDispatcher) : MySockClientTask(id, ip, port, serverId, serverType), mInnerDispatcher(iDispatcher), mOutterDispatcher(oDispatcher) 23 | { 24 | 25 | } 26 | private: 27 | Dispatcher *mInnerDispatcher; 28 | Dispatcher *mOutterDispatcher; 29 | }; 30 | } 31 | #endif 32 | -------------------------------------------------------------------------------- /client/MyClientTaskManager.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: MyClientTaskManager.cpp 3 | > Author: huangyun 4 | > Mail: 895175589@qq.com 5 | > Created Time: Sun 09 Aug 2015 01:01:25 AM 6 | ************************************************************************/ 7 | 8 | #include "MyClientTaskManager.h" 9 | 10 | namespace MyNameSpace 11 | { 12 | void MyClientTaskManager::addTask(MySockClientTask *task) 13 | { 14 | MyScopeLock lock(mLock); 15 | mTasks[task->getId()] = task; 16 | } 17 | 18 | void MyClientTaskManager::removeTask(MySockClientTask *task) 19 | { 20 | MyScopeLock lock(mLock); 21 | mTasks.erase(task->getId()); 22 | } 23 | 24 | bool MyClientTaskManager::doProcessMsg() 25 | { 26 | MyScopeLock lock(mLock); 27 | for (Container_IT it = mTasks.begin(); it != mTasks.end(); ++it) 28 | { 29 | it->second->processMsg(); 30 | } 31 | return true; 32 | } 33 | 34 | MySockClientTask * MyClientTaskManager::getTaskById(int id) 35 | { 36 | Container_IT it; 37 | it = mTasks.find(id); 38 | if (mTasks.end() == it) 39 | { 40 | return NULL; 41 | } 42 | return it->second; 43 | } 44 | 45 | void MyClientTaskManager::testSend() 46 | { 47 | std::cout<<"test send"<second->isConn()) 53 | { 54 | it->second->sendDataWithBuffer(tmp, strlen(tmp)); 55 | } 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /client/MyClientTaskManager.h: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: MyClientTaskManager.h 3 | > Author: huangyun 4 | > Mail: 895175589@qq.com 5 | > Created Time: Sun 09 Aug 2015 12:58:02 AM 6 | ************************************************************************/ 7 | #ifndef __MY_CLIENT_TASK_MANAGER_H_ 8 | #define __MY_CLIENT_TASK_MANAGER_H_ 9 | 10 | #include "../base/MySingleton.h" 11 | #include "../base/MyLock.h" 12 | #include "MySockClientTask.h" 13 | #include 14 | 15 | namespace MyNameSpace 16 | { 17 | class MyClientTaskManager : public MySingleton 18 | { 19 | private: 20 | friend class MySingleton; 21 | MyClientTaskManager() 22 | { 23 | 24 | } 25 | ~MyClientTaskManager() 26 | { 27 | 28 | } 29 | public: 30 | void addTask(MySockClientTask *task); 31 | void removeTask(MySockClientTask *task); 32 | bool doProcessMsg(); 33 | void testSend(); 34 | MySockClientTask *getTaskById(int); 35 | private: 36 | typedef std::map Container; 37 | typedef std::map::iterator Container_IT; 38 | Container mTasks; 39 | MyLock mLock; 40 | }; 41 | } 42 | #endif 43 | -------------------------------------------------------------------------------- /client/MySockClientTask.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: MySockClientTask.cpp 3 | > Author: huangyun 4 | > Mail: 895175589@qq.com 5 | > Created Time: Sat 08 Aug 2015 11:13:13 PM 6 | ************************************************************************/ 7 | #include "MySockClientTask.h" 8 | 9 | namespace MyNameSpace 10 | { 11 | bool MySockClientTask::getMsg() 12 | { 13 | bool hasMsg = false; 14 | while(true) 15 | { 16 | std::vector msg; 17 | int len = mSock.readBuffer(msg); 18 | if (len == 0) 19 | { 20 | break; 21 | } 22 | hasMsg = true; 23 | pushMsg(len, &msg[0]); 24 | } 25 | return hasMsg; 26 | } 27 | 28 | int MySockClientTask::rcvBuffer() 29 | { 30 | return mSock.rcvBuffer(); 31 | } 32 | int MySockClientTask::addEpollEvent(int epfd, epoll_event & ev) 33 | { 34 | return mSock.addEpollEvent(epfd, ev); 35 | } 36 | int MySockClientTask::delEpollEvent(int epfd, epoll_event & ev) 37 | { 38 | return mSock.delEpollEvent(epfd, ev); 39 | } 40 | 41 | int MySockClientTask::syncSendBuf() 42 | { 43 | return mSock.syncSendBuf(); 44 | } 45 | 46 | int MySockClientTask::sendDataWithBuffer(const char *buf, uint32_t len) 47 | { 48 | return mSock.sendDataWithBuffer(buf, len); 49 | } 50 | 51 | int MySockClientTask::conn() 52 | { 53 | mSockfd = ::socket(AF_INET, SOCK_STREAM, 0); 54 | if (mSockfd < 0) 55 | { 56 | std::cerr<<__FUNCTION__<<"("<<__LINE__<<"): socket fail"< File Name: MySockClientTask.h 3 | > Author: huangyun 4 | > Mail: 895175589@qq.com 5 | > Created Time: Sat 08 Aug 2015 07:12:03 PM 6 | ************************************************************************/ 7 | #ifndef _MYSOCK_CLIENT_TASK_H_ 8 | #define _MYSOCK_CLIENT_TASK_H_ 9 | #include "../base/MyQueue.h" 10 | #include "../base/MySocket.h" 11 | 12 | namespace MyNameSpace 13 | { 14 | class MySockClientTask : public MyQueue 15 | { 16 | public: 17 | MySockClientTask(int id, int ip, int port, int serverId, int serverType) :mId(id), mIp(ip), mPort(port), mSockfd(-1), mConn(false), mServerId(serverId), mServerType(serverType) 18 | { 19 | 20 | } 21 | bool getMsg(); 22 | int rcvBuffer(); 23 | int sendDataWithBuffer(const char *buf, uint32_t len); 24 | int addEpollEvent(int epfd, epoll_event & ev); 25 | int delEpollEvent(int epfd, epoll_event & ev); 26 | int syncSendBuf(); 27 | int conn(); 28 | int getId() 29 | { 30 | return mId; 31 | } 32 | bool isConn() 33 | { 34 | return mConn; 35 | } 36 | void disConn() 37 | { 38 | mConn = false; 39 | } 40 | int getType() 41 | { 42 | return mServerType; 43 | } 44 | private: 45 | MySocket mSock; 46 | int mId; 47 | int mIp; 48 | int mPort; 49 | int mSockfd; 50 | bool mConn; 51 | int mServerId; 52 | int mServerType; 53 | }; 54 | } 55 | #endif 56 | -------------------------------------------------------------------------------- /client/MySockClientTaskPool.h: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: MySockClientTaskPool.h 3 | > Author: huangyun 4 | > Mail: 895175589@qq.com 5 | > Created Time: Fri 07 Aug 2015 10:56:08 PM 6 | ************************************************************************/ 7 | #ifndef _MYSOCK_CLIENT_TASK_POOL_H 8 | #define _MYSOCK_CLIENT_TASK_POOL_H 9 | 10 | #include "../base/MyThread.h" 11 | #include "MySockClientTask.h" 12 | namespace MyNameSpace 13 | { 14 | class MyClientRecycleThread; 15 | class MyClientIoThread; 16 | class MySockClientTaskPool 17 | { 18 | public: 19 | MySockClientTaskPool() 20 | { 21 | 22 | } 23 | ~MySockClientTaskPool(); 24 | bool addTask(MySockClientTask * task); 25 | void addIoThread(MySockClientTask *task); 26 | void addRecycleThread(MySockClientTask *task); 27 | bool init(); 28 | void fini(); 29 | private: 30 | MyClientIoThread *mIoThread; 31 | MyClientRecycleThread *mRecycleThread; 32 | }; 33 | } 34 | #endif 35 | -------------------------------------------------------------------------------- /client/MyTcpClient.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: MyClient.cpp 3 | > Author: huangyun 4 | > Mail: 895175589@qq.com 5 | > Created Time: Sat 08 Aug 2015 10:50:08 PM 6 | ************************************************************************/ 7 | 8 | #include 9 | #include "MyTcpClient.h" 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | namespace MyNameSpace 16 | { 17 | int MyTcpClient::conn() 18 | { 19 | mSock = ::socket(AF_INET, SOCK_STREAM, 0); 20 | if (mSock < 0) 21 | { 22 | std::cerr<<__FUNCTION__<<"("<<__LINE__<<"): socket fail"< File Name: MyTcpClient.h 3 | > Author: huangyun 4 | > Mail: 895175589@qq.com 5 | > Created Time: Sat 08 Aug 2015 10:43:38 PM 6 | ************************************************************************/ 7 | #ifndef __MY_TCP_CLIENT_H_ 8 | #define __MY_TCP_CLIENT_H_ 9 | 10 | namespace MyNameSpace 11 | { 12 | class MyTcpClient 13 | { 14 | public: 15 | MyTcpClient(int ip, int port, int reconn) : 16 | mIp(ip), mPort(port), mSock(-1), mReconn(reconn) 17 | { 18 | } 19 | ~MyTcpClient(); 20 | int conn(); 21 | int close(); 22 | private: 23 | int mIp; 24 | int mPort; 25 | int mSock; 26 | bool mReconn; 27 | }; 28 | } 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /client/test.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: tese.cpp 3 | > Author: huangyun 4 | > Mail: 895175589@qq.com 5 | > Created Time: Sat 08 Aug 2015 11:01:42 PM 6 | ************************************************************************/ 7 | 8 | #include "MyClient.h" 9 | 10 | int main() 11 | { 12 | MyNameSpace::MyClient client; 13 | std::multimap ipPortPair; 14 | ipPortPair.insert(std::pair(INADDR_ANY, 50002)); 15 | if (!client.init(ipPortPair)) 16 | { 17 | std::cerr<<__FUNCTION__<<"("<<__LINE__<<"): client init fail"<= 1024). 94 | 95 | ### 0.10.1 96 | 97 | * Makefile overhaul. Important to check out if you override one or more 98 | variables using environment variables or via arguments to the "make" tool. 99 | 100 | * Issue #45: Fix potential memory leak for a multi bulk reply with 0 elements 101 | being created by the default reply object functions. 102 | 103 | * Issue #43: Don't crash in an asynchronous context when Redis returns an error 104 | reply after the connection has been made (this happens when the maximum 105 | number of connections is reached). 106 | 107 | ### 0.10.0 108 | 109 | * See commit log. 110 | 111 | -------------------------------------------------------------------------------- /lib/hiredis/COPYING: -------------------------------------------------------------------------------- 1 | Copyright (c) 2009-2011, Salvatore Sanfilippo 2 | Copyright (c) 2010-2011, Pieter Noordhuis 3 | 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | * Redistributions of source code must retain the above copyright notice, 10 | this list of conditions and the following disclaimer. 11 | 12 | * Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | * Neither the name of Redis nor the names of its contributors may be used 17 | to endorse or promote products derived from this software without specific 18 | prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 21 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 22 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 24 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 25 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 26 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 27 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | -------------------------------------------------------------------------------- /lib/hiredis/adapters/ivykis.h: -------------------------------------------------------------------------------- 1 | #ifndef __HIREDIS_IVYKIS_H__ 2 | #define __HIREDIS_IVYKIS_H__ 3 | #include 4 | #include "../hiredis.h" 5 | #include "../async.h" 6 | 7 | typedef struct redisIvykisEvents { 8 | redisAsyncContext *context; 9 | struct iv_fd fd; 10 | } redisIvykisEvents; 11 | 12 | static void redisIvykisReadEvent(void *arg) { 13 | redisAsyncContext *context = (redisAsyncContext *)arg; 14 | redisAsyncHandleRead(context); 15 | } 16 | 17 | static void redisIvykisWriteEvent(void *arg) { 18 | redisAsyncContext *context = (redisAsyncContext *)arg; 19 | redisAsyncHandleWrite(context); 20 | } 21 | 22 | static void redisIvykisAddRead(void *privdata) { 23 | redisIvykisEvents *e = (redisIvykisEvents*)privdata; 24 | iv_fd_set_handler_in(&e->fd, redisIvykisReadEvent); 25 | } 26 | 27 | static void redisIvykisDelRead(void *privdata) { 28 | redisIvykisEvents *e = (redisIvykisEvents*)privdata; 29 | iv_fd_set_handler_in(&e->fd, NULL); 30 | } 31 | 32 | static void redisIvykisAddWrite(void *privdata) { 33 | redisIvykisEvents *e = (redisIvykisEvents*)privdata; 34 | iv_fd_set_handler_out(&e->fd, redisIvykisWriteEvent); 35 | } 36 | 37 | static void redisIvykisDelWrite(void *privdata) { 38 | redisIvykisEvents *e = (redisIvykisEvents*)privdata; 39 | iv_fd_set_handler_out(&e->fd, NULL); 40 | } 41 | 42 | static void redisIvykisCleanup(void *privdata) { 43 | redisIvykisEvents *e = (redisIvykisEvents*)privdata; 44 | 45 | iv_fd_unregister(&e->fd); 46 | free(e); 47 | } 48 | 49 | static int redisIvykisAttach(redisAsyncContext *ac) { 50 | redisContext *c = &(ac->c); 51 | redisIvykisEvents *e; 52 | 53 | /* Nothing should be attached when something is already attached */ 54 | if (ac->ev.data != NULL) 55 | return REDIS_ERR; 56 | 57 | /* Create container for context and r/w events */ 58 | e = (redisIvykisEvents*)malloc(sizeof(*e)); 59 | e->context = ac; 60 | 61 | /* Register functions to start/stop listening for events */ 62 | ac->ev.addRead = redisIvykisAddRead; 63 | ac->ev.delRead = redisIvykisDelRead; 64 | ac->ev.addWrite = redisIvykisAddWrite; 65 | ac->ev.delWrite = redisIvykisDelWrite; 66 | ac->ev.cleanup = redisIvykisCleanup; 67 | ac->ev.data = e; 68 | 69 | /* Initialize and install read/write events */ 70 | IV_FD_INIT(&e->fd); 71 | e->fd.fd = c->fd; 72 | e->fd.handler_in = redisIvykisReadEvent; 73 | e->fd.handler_out = redisIvykisWriteEvent; 74 | e->fd.handler_err = NULL; 75 | e->fd.cookie = e->context; 76 | 77 | iv_fd_register(&e->fd); 78 | 79 | return REDIS_OK; 80 | } 81 | #endif 82 | -------------------------------------------------------------------------------- /lib/hiredis/adapters/libuv.h: -------------------------------------------------------------------------------- 1 | #ifndef __HIREDIS_LIBUV_H__ 2 | #define __HIREDIS_LIBUV_H__ 3 | #include 4 | #include 5 | #include "../hiredis.h" 6 | #include "../async.h" 7 | #include 8 | 9 | typedef struct redisLibuvEvents { 10 | redisAsyncContext* context; 11 | uv_poll_t handle; 12 | int events; 13 | } redisLibuvEvents; 14 | 15 | 16 | static void redisLibuvPoll(uv_poll_t* handle, int status, int events) { 17 | redisLibuvEvents* p = (redisLibuvEvents*)handle->data; 18 | 19 | if (status != 0) { 20 | return; 21 | } 22 | 23 | if (events & UV_READABLE) { 24 | redisAsyncHandleRead(p->context); 25 | } 26 | if (events & UV_WRITABLE) { 27 | redisAsyncHandleWrite(p->context); 28 | } 29 | } 30 | 31 | 32 | static void redisLibuvAddRead(void *privdata) { 33 | redisLibuvEvents* p = (redisLibuvEvents*)privdata; 34 | 35 | p->events |= UV_READABLE; 36 | 37 | uv_poll_start(&p->handle, p->events, redisLibuvPoll); 38 | } 39 | 40 | 41 | static void redisLibuvDelRead(void *privdata) { 42 | redisLibuvEvents* p = (redisLibuvEvents*)privdata; 43 | 44 | p->events &= ~UV_READABLE; 45 | 46 | if (p->events) { 47 | uv_poll_start(&p->handle, p->events, redisLibuvPoll); 48 | } else { 49 | uv_poll_stop(&p->handle); 50 | } 51 | } 52 | 53 | 54 | static void redisLibuvAddWrite(void *privdata) { 55 | redisLibuvEvents* p = (redisLibuvEvents*)privdata; 56 | 57 | p->events |= UV_WRITABLE; 58 | 59 | uv_poll_start(&p->handle, p->events, redisLibuvPoll); 60 | } 61 | 62 | 63 | static void redisLibuvDelWrite(void *privdata) { 64 | redisLibuvEvents* p = (redisLibuvEvents*)privdata; 65 | 66 | p->events &= ~UV_WRITABLE; 67 | 68 | if (p->events) { 69 | uv_poll_start(&p->handle, p->events, redisLibuvPoll); 70 | } else { 71 | uv_poll_stop(&p->handle); 72 | } 73 | } 74 | 75 | 76 | static void on_close(uv_handle_t* handle) { 77 | redisLibuvEvents* p = (redisLibuvEvents*)handle->data; 78 | 79 | free(p); 80 | } 81 | 82 | 83 | static void redisLibuvCleanup(void *privdata) { 84 | redisLibuvEvents* p = (redisLibuvEvents*)privdata; 85 | 86 | uv_close((uv_handle_t*)&p->handle, on_close); 87 | } 88 | 89 | 90 | static int redisLibuvAttach(redisAsyncContext* ac, uv_loop_t* loop) { 91 | redisContext *c = &(ac->c); 92 | 93 | if (ac->ev.data != NULL) { 94 | return REDIS_ERR; 95 | } 96 | 97 | ac->ev.addRead = redisLibuvAddRead; 98 | ac->ev.delRead = redisLibuvDelRead; 99 | ac->ev.addWrite = redisLibuvAddWrite; 100 | ac->ev.delWrite = redisLibuvDelWrite; 101 | ac->ev.cleanup = redisLibuvCleanup; 102 | 103 | redisLibuvEvents* p = (redisLibuvEvents*)malloc(sizeof(*p)); 104 | 105 | if (!p) { 106 | return REDIS_ERR; 107 | } 108 | 109 | memset(p, 0, sizeof(*p)); 110 | 111 | if (uv_poll_init(loop, &p->handle, c->fd) != 0) { 112 | return REDIS_ERR; 113 | } 114 | 115 | ac->ev.data = p; 116 | p->handle.data = p; 117 | p->context = ac; 118 | 119 | return REDIS_OK; 120 | } 121 | #endif 122 | -------------------------------------------------------------------------------- /lib/hiredis/examples/example-ae.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | /* Put event loop in the global scope, so it can be explicitly stopped */ 11 | static aeEventLoop *loop; 12 | 13 | void getCallback(redisAsyncContext *c, void *r, void *privdata) { 14 | redisReply *reply = r; 15 | if (reply == NULL) return; 16 | printf("argv[%s]: %s\n", (char*)privdata, reply->str); 17 | 18 | /* Disconnect after receiving the reply to GET */ 19 | redisAsyncDisconnect(c); 20 | } 21 | 22 | void connectCallback(const redisAsyncContext *c, int status) { 23 | if (status != REDIS_OK) { 24 | printf("Error: %s\n", c->errstr); 25 | aeStop(loop); 26 | return; 27 | } 28 | 29 | printf("Connected...\n"); 30 | } 31 | 32 | void disconnectCallback(const redisAsyncContext *c, int status) { 33 | if (status != REDIS_OK) { 34 | printf("Error: %s\n", c->errstr); 35 | aeStop(loop); 36 | return; 37 | } 38 | 39 | printf("Disconnected...\n"); 40 | aeStop(loop); 41 | } 42 | 43 | int main (int argc, char **argv) { 44 | signal(SIGPIPE, SIG_IGN); 45 | 46 | redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379); 47 | if (c->err) { 48 | /* Let *c leak for now... */ 49 | printf("Error: %s\n", c->errstr); 50 | return 1; 51 | } 52 | 53 | loop = aeCreateEventLoop(64); 54 | redisAeAttach(loop, c); 55 | redisAsyncSetConnectCallback(c,connectCallback); 56 | redisAsyncSetDisconnectCallback(c,disconnectCallback); 57 | redisAsyncCommand(c, NULL, NULL, "SET key %b", argv[argc-1], strlen(argv[argc-1])); 58 | redisAsyncCommand(c, getCallback, (char*)"end-1", "GET key"); 59 | aeMain(loop); 60 | return 0; 61 | } 62 | 63 | -------------------------------------------------------------------------------- /lib/hiredis/examples/example-glib.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | static GMainLoop *mainloop; 8 | 9 | static void 10 | connect_cb (const redisAsyncContext *ac G_GNUC_UNUSED, 11 | int status) 12 | { 13 | if (status != REDIS_OK) { 14 | g_printerr("Failed to connect: %s\n", ac->errstr); 15 | g_main_loop_quit(mainloop); 16 | } else { 17 | g_printerr("Connected...\n"); 18 | } 19 | } 20 | 21 | static void 22 | disconnect_cb (const redisAsyncContext *ac G_GNUC_UNUSED, 23 | int status) 24 | { 25 | if (status != REDIS_OK) { 26 | g_error("Failed to disconnect: %s", ac->errstr); 27 | } else { 28 | g_printerr("Disconnected...\n"); 29 | g_main_loop_quit(mainloop); 30 | } 31 | } 32 | 33 | static void 34 | command_cb(redisAsyncContext *ac, 35 | gpointer r, 36 | gpointer user_data G_GNUC_UNUSED) 37 | { 38 | redisReply *reply = r; 39 | 40 | if (reply) { 41 | g_print("REPLY: %s\n", reply->str); 42 | } 43 | 44 | redisAsyncDisconnect(ac); 45 | } 46 | 47 | gint 48 | main (gint argc G_GNUC_UNUSED, 49 | gchar *argv[] G_GNUC_UNUSED) 50 | { 51 | redisAsyncContext *ac; 52 | GMainContext *context = NULL; 53 | GSource *source; 54 | 55 | ac = redisAsyncConnect("127.0.0.1", 6379); 56 | if (ac->err) { 57 | g_printerr("%s\n", ac->errstr); 58 | exit(EXIT_FAILURE); 59 | } 60 | 61 | source = redis_source_new(ac); 62 | mainloop = g_main_loop_new(context, FALSE); 63 | g_source_attach(source, context); 64 | 65 | redisAsyncSetConnectCallback(ac, connect_cb); 66 | redisAsyncSetDisconnectCallback(ac, disconnect_cb); 67 | redisAsyncCommand(ac, command_cb, NULL, "SET key 1234"); 68 | redisAsyncCommand(ac, command_cb, NULL, "GET key"); 69 | 70 | g_main_loop_run(mainloop); 71 | 72 | return EXIT_SUCCESS; 73 | } 74 | -------------------------------------------------------------------------------- /lib/hiredis/examples/example-ivykis.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | void getCallback(redisAsyncContext *c, void *r, void *privdata) { 11 | redisReply *reply = r; 12 | if (reply == NULL) return; 13 | printf("argv[%s]: %s\n", (char*)privdata, reply->str); 14 | 15 | /* Disconnect after receiving the reply to GET */ 16 | redisAsyncDisconnect(c); 17 | } 18 | 19 | void connectCallback(const redisAsyncContext *c, int status) { 20 | if (status != REDIS_OK) { 21 | printf("Error: %s\n", c->errstr); 22 | return; 23 | } 24 | printf("Connected...\n"); 25 | } 26 | 27 | void disconnectCallback(const redisAsyncContext *c, int status) { 28 | if (status != REDIS_OK) { 29 | printf("Error: %s\n", c->errstr); 30 | return; 31 | } 32 | printf("Disconnected...\n"); 33 | } 34 | 35 | int main (int argc, char **argv) { 36 | signal(SIGPIPE, SIG_IGN); 37 | 38 | iv_init(); 39 | 40 | redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379); 41 | if (c->err) { 42 | /* Let *c leak for now... */ 43 | printf("Error: %s\n", c->errstr); 44 | return 1; 45 | } 46 | 47 | redisIvykisAttach(c); 48 | redisAsyncSetConnectCallback(c,connectCallback); 49 | redisAsyncSetDisconnectCallback(c,disconnectCallback); 50 | redisAsyncCommand(c, NULL, NULL, "SET key %b", argv[argc-1], strlen(argv[argc-1])); 51 | redisAsyncCommand(c, getCallback, (char*)"end-1", "GET key"); 52 | 53 | iv_main(); 54 | 55 | iv_deinit(); 56 | 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /lib/hiredis/examples/example-libev.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | void getCallback(redisAsyncContext *c, void *r, void *privdata) { 11 | redisReply *reply = r; 12 | if (reply == NULL) return; 13 | printf("argv[%s]: %s\n", (char*)privdata, reply->str); 14 | 15 | /* Disconnect after receiving the reply to GET */ 16 | redisAsyncDisconnect(c); 17 | } 18 | 19 | void connectCallback(const redisAsyncContext *c, int status) { 20 | if (status != REDIS_OK) { 21 | printf("Error: %s\n", c->errstr); 22 | return; 23 | } 24 | printf("Connected...\n"); 25 | } 26 | 27 | void disconnectCallback(const redisAsyncContext *c, int status) { 28 | if (status != REDIS_OK) { 29 | printf("Error: %s\n", c->errstr); 30 | return; 31 | } 32 | printf("Disconnected...\n"); 33 | } 34 | 35 | int main (int argc, char **argv) { 36 | signal(SIGPIPE, SIG_IGN); 37 | 38 | redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379); 39 | if (c->err) { 40 | /* Let *c leak for now... */ 41 | printf("Error: %s\n", c->errstr); 42 | return 1; 43 | } 44 | 45 | redisLibevAttach(EV_DEFAULT_ c); 46 | redisAsyncSetConnectCallback(c,connectCallback); 47 | redisAsyncSetDisconnectCallback(c,disconnectCallback); 48 | redisAsyncCommand(c, NULL, NULL, "SET key %b", argv[argc-1], strlen(argv[argc-1])); 49 | redisAsyncCommand(c, getCallback, (char*)"end-1", "GET key"); 50 | ev_loop(EV_DEFAULT_ 0); 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /lib/hiredis/examples/example-libevent.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | void getCallback(redisAsyncContext *c, void *r, void *privdata) { 11 | redisReply *reply = r; 12 | if (reply == NULL) return; 13 | printf("argv[%s]: %s\n", (char*)privdata, reply->str); 14 | 15 | /* Disconnect after receiving the reply to GET */ 16 | redisAsyncDisconnect(c); 17 | } 18 | 19 | void connectCallback(const redisAsyncContext *c, int status) { 20 | if (status != REDIS_OK) { 21 | printf("Error: %s\n", c->errstr); 22 | return; 23 | } 24 | printf("Connected...\n"); 25 | } 26 | 27 | void disconnectCallback(const redisAsyncContext *c, int status) { 28 | if (status != REDIS_OK) { 29 | printf("Error: %s\n", c->errstr); 30 | return; 31 | } 32 | printf("Disconnected...\n"); 33 | } 34 | 35 | int main (int argc, char **argv) { 36 | signal(SIGPIPE, SIG_IGN); 37 | struct event_base *base = event_base_new(); 38 | 39 | redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379); 40 | if (c->err) { 41 | /* Let *c leak for now... */ 42 | printf("Error: %s\n", c->errstr); 43 | return 1; 44 | } 45 | 46 | redisLibeventAttach(c,base); 47 | redisAsyncSetConnectCallback(c,connectCallback); 48 | redisAsyncSetDisconnectCallback(c,disconnectCallback); 49 | redisAsyncCommand(c, NULL, NULL, "SET key %b", argv[argc-1], strlen(argv[argc-1])); 50 | redisAsyncCommand(c, getCallback, (char*)"end-1", "GET key"); 51 | event_base_dispatch(base); 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /lib/hiredis/examples/example-libuv.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | void getCallback(redisAsyncContext *c, void *r, void *privdata) { 11 | redisReply *reply = r; 12 | if (reply == NULL) return; 13 | printf("argv[%s]: %s\n", (char*)privdata, reply->str); 14 | 15 | /* Disconnect after receiving the reply to GET */ 16 | redisAsyncDisconnect(c); 17 | } 18 | 19 | void connectCallback(const redisAsyncContext *c, int status) { 20 | if (status != REDIS_OK) { 21 | printf("Error: %s\n", c->errstr); 22 | return; 23 | } 24 | printf("Connected...\n"); 25 | } 26 | 27 | void disconnectCallback(const redisAsyncContext *c, int status) { 28 | if (status != REDIS_OK) { 29 | printf("Error: %s\n", c->errstr); 30 | return; 31 | } 32 | printf("Disconnected...\n"); 33 | } 34 | 35 | int main (int argc, char **argv) { 36 | signal(SIGPIPE, SIG_IGN); 37 | uv_loop_t* loop = uv_default_loop(); 38 | 39 | redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379); 40 | if (c->err) { 41 | /* Let *c leak for now... */ 42 | printf("Error: %s\n", c->errstr); 43 | return 1; 44 | } 45 | 46 | redisLibuvAttach(c,loop); 47 | redisAsyncSetConnectCallback(c,connectCallback); 48 | redisAsyncSetDisconnectCallback(c,disconnectCallback); 49 | redisAsyncCommand(c, NULL, NULL, "SET key %b", argv[argc-1], strlen(argv[argc-1])); 50 | redisAsyncCommand(c, getCallback, (char*)"end-1", "GET key"); 51 | uv_run(loop, UV_RUN_DEFAULT); 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /lib/hiredis/examples/example-macosx.c: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Дмитрий Бахвалов on 13.07.15. 3 | // Copyright (c) 2015 Dmitry Bakhvalov. All rights reserved. 4 | // 5 | 6 | #include 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | void getCallback(redisAsyncContext *c, void *r, void *privdata) { 13 | redisReply *reply = r; 14 | if (reply == NULL) return; 15 | printf("argv[%s]: %s\n", (char*)privdata, reply->str); 16 | 17 | /* Disconnect after receiving the reply to GET */ 18 | redisAsyncDisconnect(c); 19 | } 20 | 21 | void connectCallback(const redisAsyncContext *c, int status) { 22 | if (status != REDIS_OK) { 23 | printf("Error: %s\n", c->errstr); 24 | return; 25 | } 26 | printf("Connected...\n"); 27 | } 28 | 29 | void disconnectCallback(const redisAsyncContext *c, int status) { 30 | if (status != REDIS_OK) { 31 | printf("Error: %s\n", c->errstr); 32 | return; 33 | } 34 | CFRunLoopStop(CFRunLoopGetCurrent()); 35 | printf("Disconnected...\n"); 36 | } 37 | 38 | int main (int argc, char **argv) { 39 | signal(SIGPIPE, SIG_IGN); 40 | 41 | CFRunLoopRef loop = CFRunLoopGetCurrent(); 42 | if( !loop ) { 43 | printf("Error: Cannot get current run loop\n"); 44 | return 1; 45 | } 46 | 47 | redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379); 48 | if (c->err) { 49 | /* Let *c leak for now... */ 50 | printf("Error: %s\n", c->errstr); 51 | return 1; 52 | } 53 | 54 | redisMacOSAttach(c, loop); 55 | 56 | redisAsyncSetConnectCallback(c,connectCallback); 57 | redisAsyncSetDisconnectCallback(c,disconnectCallback); 58 | 59 | redisAsyncCommand(c, NULL, NULL, "SET key %b", argv[argc-1], strlen(argv[argc-1])); 60 | redisAsyncCommand(c, getCallback, (char*)"end-1", "GET key"); 61 | 62 | CFRunLoopRun(); 63 | 64 | return 0; 65 | } 66 | 67 | -------------------------------------------------------------------------------- /lib/hiredis/examples/example-qt.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | 7 | #include "example-qt.h" 8 | 9 | void getCallback(redisAsyncContext *, void * r, void * privdata) { 10 | 11 | redisReply * reply = static_cast(r); 12 | ExampleQt * ex = static_cast(privdata); 13 | if (reply == nullptr || ex == nullptr) return; 14 | 15 | cout << "key: " << reply->str << endl; 16 | 17 | ex->finish(); 18 | } 19 | 20 | void ExampleQt::run() { 21 | 22 | m_ctx = redisAsyncConnect("localhost", 6379); 23 | 24 | if (m_ctx->err) { 25 | cerr << "Error: " << m_ctx->errstr << endl; 26 | redisAsyncFree(m_ctx); 27 | emit finished(); 28 | } 29 | 30 | m_adapter.setContext(m_ctx); 31 | 32 | redisAsyncCommand(m_ctx, NULL, NULL, "SET key %s", m_value); 33 | redisAsyncCommand(m_ctx, getCallback, this, "GET key"); 34 | } 35 | 36 | int main (int argc, char **argv) { 37 | 38 | QCoreApplication app(argc, argv); 39 | 40 | ExampleQt example(argv[argc-1]); 41 | 42 | QObject::connect(&example, SIGNAL(finished()), &app, SLOT(quit())); 43 | QTimer::singleShot(0, &example, SLOT(run())); 44 | 45 | return app.exec(); 46 | } 47 | -------------------------------------------------------------------------------- /lib/hiredis/examples/example-qt.h: -------------------------------------------------------------------------------- 1 | #ifndef __HIREDIS_EXAMPLE_QT_H 2 | #define __HIREDIS_EXAMPLE_QT_H 3 | 4 | #include 5 | 6 | class ExampleQt : public QObject { 7 | 8 | Q_OBJECT 9 | 10 | public: 11 | ExampleQt(const char * value, QObject * parent = 0) 12 | : QObject(parent), m_value(value) {} 13 | 14 | signals: 15 | void finished(); 16 | 17 | public slots: 18 | void run(); 19 | 20 | private: 21 | void finish() { emit finished(); } 22 | 23 | private: 24 | const char * m_value; 25 | redisAsyncContext * m_ctx; 26 | RedisQtAdapter m_adapter; 27 | 28 | friend 29 | void getCallback(redisAsyncContext *, void *, void *); 30 | }; 31 | 32 | #endif /* !__HIREDIS_EXAMPLE_QT_H */ 33 | -------------------------------------------------------------------------------- /lib/hiredis/examples/example.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | 7 | int main(int argc, char **argv) { 8 | unsigned int j; 9 | redisContext *c; 10 | redisReply *reply; 11 | const char *hostname = (argc > 1) ? argv[1] : "127.0.0.1"; 12 | int port = (argc > 2) ? atoi(argv[2]) : 6379; 13 | 14 | struct timeval timeout = { 1, 500000 }; // 1.5 seconds 15 | c = redisConnectWithTimeout(hostname, port, timeout); 16 | if (c == NULL || c->err) { 17 | if (c) { 18 | printf("Connection error: %s\n", c->errstr); 19 | redisFree(c); 20 | } else { 21 | printf("Connection error: can't allocate redis context\n"); 22 | } 23 | exit(1); 24 | } 25 | 26 | /* PING server */ 27 | reply = redisCommand(c,"PING"); 28 | printf("PING: %s\n", reply->str); 29 | freeReplyObject(reply); 30 | 31 | /* Set a key */ 32 | reply = redisCommand(c,"SET %s %s", "foo", "hello world"); 33 | printf("SET: %s\n", reply->str); 34 | freeReplyObject(reply); 35 | 36 | /* Set a key using binary safe API */ 37 | reply = redisCommand(c,"SET %b %b", "bar", (size_t) 3, "hello", (size_t) 5); 38 | printf("SET (binary API): %s\n", reply->str); 39 | freeReplyObject(reply); 40 | 41 | /* Try a GET and two INCR */ 42 | reply = redisCommand(c,"GET foo"); 43 | printf("GET foo: %s\n", reply->str); 44 | freeReplyObject(reply); 45 | 46 | reply = redisCommand(c,"INCR counter"); 47 | printf("INCR counter: %lld\n", reply->integer); 48 | freeReplyObject(reply); 49 | /* again ... */ 50 | reply = redisCommand(c,"INCR counter"); 51 | printf("INCR counter: %lld\n", reply->integer); 52 | freeReplyObject(reply); 53 | 54 | /* Create a list of numbers, from 0 to 9 */ 55 | reply = redisCommand(c,"DEL mylist"); 56 | freeReplyObject(reply); 57 | for (j = 0; j < 10; j++) { 58 | char buf[64]; 59 | 60 | snprintf(buf,64,"%d",j); 61 | reply = redisCommand(c,"LPUSH mylist element-%s", buf); 62 | freeReplyObject(reply); 63 | } 64 | 65 | /* Let's check what we have inside the list */ 66 | reply = redisCommand(c,"LRANGE mylist 0 -1"); 67 | if (reply->type == REDIS_REPLY_ARRAY) { 68 | for (j = 0; j < reply->elements; j++) { 69 | printf("%u) %s\n", j, reply->element[j]->str); 70 | } 71 | } 72 | freeReplyObject(reply); 73 | 74 | /* Disconnects and frees the context */ 75 | redisFree(c); 76 | 77 | return 0; 78 | } 79 | -------------------------------------------------------------------------------- /lib/hiredis/fmacros.h: -------------------------------------------------------------------------------- 1 | #ifndef __HIREDIS_FMACRO_H 2 | #define __HIREDIS_FMACRO_H 3 | 4 | #if defined(__linux__) 5 | #define _BSD_SOURCE 6 | #define _DEFAULT_SOURCE 7 | #endif 8 | 9 | #if defined(__sun__) 10 | #define _POSIX_C_SOURCE 200112L 11 | #elif defined(__linux__) || defined(__OpenBSD__) || defined(__NetBSD__) 12 | #define _XOPEN_SOURCE 600 13 | #else 14 | #define _XOPEN_SOURCE 15 | #endif 16 | 17 | #if __APPLE__ && __MACH__ 18 | #define _OSX 19 | #endif 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /lib/hiredis/net.h: -------------------------------------------------------------------------------- 1 | /* Extracted from anet.c to work properly with Hiredis error reporting. 2 | * 3 | * Copyright (c) 2009-2011, Salvatore Sanfilippo 4 | * Copyright (c) 2010-2014, Pieter Noordhuis 5 | * Copyright (c) 2015, Matt Stancliff , 6 | * Jan-Erik Rediger 7 | * 8 | * All rights reserved. 9 | * 10 | * Redistribution and use in source and binary forms, with or without 11 | * modification, are permitted provided that the following conditions are met: 12 | * 13 | * * Redistributions of source code must retain the above copyright notice, 14 | * this list of conditions and the following disclaimer. 15 | * * Redistributions in binary form must reproduce the above copyright 16 | * notice, this list of conditions and the following disclaimer in the 17 | * documentation and/or other materials provided with the distribution. 18 | * * Neither the name of Redis nor the names of its contributors may be used 19 | * to endorse or promote products derived from this software without 20 | * specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 23 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 26 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | */ 34 | 35 | #ifndef __NET_H 36 | #define __NET_H 37 | 38 | #include "hiredis.h" 39 | 40 | #if defined(__sun) 41 | #define AF_LOCAL AF_UNIX 42 | #endif 43 | 44 | int redisCheckSocketError(redisContext *c); 45 | int redisContextSetTimeout(redisContext *c, const struct timeval tv); 46 | int redisContextConnectTcp(redisContext *c, const char *addr, int port, const struct timeval *timeout); 47 | int redisContextConnectBindTcp(redisContext *c, const char *addr, int port, 48 | const struct timeval *timeout, 49 | const char *source_addr); 50 | int redisContextConnectUnix(redisContext *c, const char *path, const struct timeval *timeout); 51 | int redisKeepAlive(redisContext *c, int interval); 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /lib/hiredis/win32.h: -------------------------------------------------------------------------------- 1 | #ifndef _WIN32_HELPER_INCLUDE 2 | #define _WIN32_HELPER_INCLUDE 3 | #ifdef _MSC_VER 4 | 5 | #ifndef inline 6 | #define inline __inline 7 | #endif 8 | 9 | #ifndef va_copy 10 | #define va_copy(d,s) ((d) = (s)) 11 | #endif 12 | 13 | #ifndef snprintf 14 | #define snprintf c99_snprintf 15 | 16 | __inline int c99_vsnprintf(char* str, size_t size, const char* format, va_list ap) 17 | { 18 | int count = -1; 19 | 20 | if (size != 0) 21 | count = _vsnprintf_s(str, size, _TRUNCATE, format, ap); 22 | if (count == -1) 23 | count = _vscprintf(format, ap); 24 | 25 | return count; 26 | } 27 | 28 | __inline int c99_snprintf(char* str, size_t size, const char* format, ...) 29 | { 30 | int count; 31 | va_list ap; 32 | 33 | va_start(ap, format); 34 | count = c99_vsnprintf(str, size, format, ap); 35 | va_end(ap); 36 | 37 | return count; 38 | } 39 | #endif 40 | 41 | #endif 42 | #endif -------------------------------------------------------------------------------- /lib/tinyxml/changes.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/p308945/MyNetWorkLib/e86d550bf837b08d3fe9b76341e802096c1f8f5c/lib/tinyxml/changes.txt -------------------------------------------------------------------------------- /lib/tinyxml/docs/annotated.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | TinyXml: Class List 6 | 7 | 8 | 9 | 10 | 11 | 28 |
29 |

Class List

Here are the classes, structs, unions and interfaces with brief descriptions: 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 |
TiXmlAttributeAn attribute is a name-value pair
TiXmlBaseTiXmlBase is a base class for every class in TinyXml
TiXmlCommentAn XML comment
TiXmlDeclarationIn correct XML the declaration is the first entry in the file
TiXmlDocumentAlways the top level node
TiXmlElementThe element is a container class
TiXmlHandleA TiXmlHandle is a class that wraps a node pointer with null checks; this is an incredibly useful thing
TiXmlNodeThe parent class for everything in the Document Object Model
TiXmlPrinterPrint to memory functionality
TiXmlTextXML text
TiXmlUnknownAny tag that tinyXml doesn't recognize is saved as an unknown
TiXmlVisitorImplements the interface to the "Visitor pattern" (see the Accept() method
43 |
44 |
Generated by  45 | 46 | doxygen 1.6.2
47 | 48 | 49 | -------------------------------------------------------------------------------- /lib/tinyxml/docs/classTiXmlAttribute.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/p308945/MyNetWorkLib/e86d550bf837b08d3fe9b76341e802096c1f8f5c/lib/tinyxml/docs/classTiXmlAttribute.png -------------------------------------------------------------------------------- /lib/tinyxml/docs/classTiXmlBase-members.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | TinyXml: Member List 6 | 7 | 8 | 9 | 10 | 11 | 28 |
29 |

TiXmlBase Member List

This is the complete list of members for TiXmlBase, including all inherited members. 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 |
Column() const TiXmlBase [inline]
EncodeString(const TIXML_STRING &str, TIXML_STRING *out)TiXmlBase [static]
GetUserData()TiXmlBase [inline]
GetUserData() const TiXmlBase [inline]
IsWhiteSpaceCondensed()TiXmlBase [inline, static]
Print(FILE *cfile, int depth) const =0TiXmlBase [pure virtual]
Row() const TiXmlBase [inline]
SetCondenseWhiteSpace(bool condense)TiXmlBase [inline, static]
SetUserData(void *user)TiXmlBase [inline]
userDataTiXmlBase [protected]
41 |
Generated by  42 | 43 | doxygen 1.6.2
44 | 45 | 46 | -------------------------------------------------------------------------------- /lib/tinyxml/docs/classTiXmlBase.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/p308945/MyNetWorkLib/e86d550bf837b08d3fe9b76341e802096c1f8f5c/lib/tinyxml/docs/classTiXmlBase.png -------------------------------------------------------------------------------- /lib/tinyxml/docs/classTiXmlComment.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/p308945/MyNetWorkLib/e86d550bf837b08d3fe9b76341e802096c1f8f5c/lib/tinyxml/docs/classTiXmlComment.png -------------------------------------------------------------------------------- /lib/tinyxml/docs/classTiXmlDeclaration.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/p308945/MyNetWorkLib/e86d550bf837b08d3fe9b76341e802096c1f8f5c/lib/tinyxml/docs/classTiXmlDeclaration.png -------------------------------------------------------------------------------- /lib/tinyxml/docs/classTiXmlDocument.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/p308945/MyNetWorkLib/e86d550bf837b08d3fe9b76341e802096c1f8f5c/lib/tinyxml/docs/classTiXmlDocument.png -------------------------------------------------------------------------------- /lib/tinyxml/docs/classTiXmlElement.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/p308945/MyNetWorkLib/e86d550bf837b08d3fe9b76341e802096c1f8f5c/lib/tinyxml/docs/classTiXmlElement.png -------------------------------------------------------------------------------- /lib/tinyxml/docs/classTiXmlNode.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/p308945/MyNetWorkLib/e86d550bf837b08d3fe9b76341e802096c1f8f5c/lib/tinyxml/docs/classTiXmlNode.png -------------------------------------------------------------------------------- /lib/tinyxml/docs/classTiXmlPrinter.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/p308945/MyNetWorkLib/e86d550bf837b08d3fe9b76341e802096c1f8f5c/lib/tinyxml/docs/classTiXmlPrinter.png -------------------------------------------------------------------------------- /lib/tinyxml/docs/classTiXmlText.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/p308945/MyNetWorkLib/e86d550bf837b08d3fe9b76341e802096c1f8f5c/lib/tinyxml/docs/classTiXmlText.png -------------------------------------------------------------------------------- /lib/tinyxml/docs/classTiXmlUnknown.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/p308945/MyNetWorkLib/e86d550bf837b08d3fe9b76341e802096c1f8f5c/lib/tinyxml/docs/classTiXmlUnknown.png -------------------------------------------------------------------------------- /lib/tinyxml/docs/classTiXmlVisitor-members.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | TinyXml: Member List 6 | 7 | 8 | 9 | 10 | 11 | 28 |
29 |

TiXmlVisitor Member List

This is the complete list of members for TiXmlVisitor, including all inherited members. 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 |
Visit(const TiXmlDeclaration &)TiXmlVisitor [inline, virtual]
Visit(const TiXmlText &)TiXmlVisitor [inline, virtual]
Visit(const TiXmlComment &)TiXmlVisitor [inline, virtual]
Visit(const TiXmlUnknown &)TiXmlVisitor [inline, virtual]
VisitEnter(const TiXmlDocument &)TiXmlVisitor [inline, virtual]
VisitEnter(const TiXmlElement &, const TiXmlAttribute *)TiXmlVisitor [inline, virtual]
VisitExit(const TiXmlDocument &)TiXmlVisitor [inline, virtual]
VisitExit(const TiXmlElement &)TiXmlVisitor [inline, virtual]
39 |
Generated by  40 | 41 | doxygen 1.6.2
42 | 43 | 44 | -------------------------------------------------------------------------------- /lib/tinyxml/docs/classTiXmlVisitor.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/p308945/MyNetWorkLib/e86d550bf837b08d3fe9b76341e802096c1f8f5c/lib/tinyxml/docs/classTiXmlVisitor.png -------------------------------------------------------------------------------- /lib/tinyxml/docs/classes.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | TinyXml: Alphabetical List 6 | 7 | 8 | 9 | 10 | 11 | 28 |
29 |

Class Index

30 | 31 |
  T  
32 |
TiXmlComment   TiXmlElement   TiXmlPrinter   TiXmlUnknown   
TiXmlAttribute   TiXmlDeclaration   TiXmlHandle   TiXmlText   TiXmlVisitor   
TiXmlBase   TiXmlDocument   TiXmlNode   
33 |
34 |
Generated by  35 | 36 | doxygen 1.6.2
37 | 38 | 39 | -------------------------------------------------------------------------------- /lib/tinyxml/docs/deprecated.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | TinyXml: Deprecated List 6 | 7 | 8 | 9 | 10 | 11 | 21 |
22 | 23 | 24 |

Deprecated List

25 |
26 |
Member TiXmlHandle::Element () const
27 |
use ToElement. Return the handle as a TiXmlElement. This may return null.
28 |
29 |

30 |
31 |
Member TiXmlHandle::Node () const
32 |
use ToNode. Return the handle as a TiXmlNode. This may return null.
33 |
34 |

35 |
36 |
Member TiXmlHandle::Text () const
37 |
use ToText() Return the handle as a TiXmlText. This may return null.
38 |
39 |

40 |
41 |
Member TiXmlHandle::Unknown () const
42 |
use ToUnknown() Return the handle as a TiXmlUnknown. This may return null.
43 |
44 |
45 |
Generated by  46 | 47 | doxygen 1.6.2
48 | 49 | 50 | -------------------------------------------------------------------------------- /lib/tinyxml/docs/doxygen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/p308945/MyNetWorkLib/e86d550bf837b08d3fe9b76341e802096c1f8f5c/lib/tinyxml/docs/doxygen.png -------------------------------------------------------------------------------- /lib/tinyxml/docs/files.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | TinyXml: File Index 6 | 7 | 8 | 9 | 10 | 11 | 26 |
27 |

File List

Here is a list of all documented files with brief descriptions: 28 | 29 | 30 |
tinystr.h [code]
tinyxml.h [code]
31 |
32 |
Generated by  33 | 34 | doxygen 1.6.2
35 | 36 | 37 | -------------------------------------------------------------------------------- /lib/tinyxml/docs/functions_0x64.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | TinyXml: Class Members 6 | 7 | 8 | 9 | 10 | 11 | 58 |
59 | Here is a list of all documented class members with links to the class documentation for each member: 60 | 61 |

- d -

66 |
67 |
Generated by  68 | 69 | doxygen 1.6.2
70 | 71 | 72 | -------------------------------------------------------------------------------- /lib/tinyxml/docs/functions_0x67.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | TinyXml: Class Members 6 | 7 | 8 | 9 | 10 | 11 | 58 |
59 | Here is a list of all documented class members with links to the class documentation for each member: 60 | 61 |

- g -

72 |
73 |
Generated by  74 | 75 | doxygen 1.6.2
76 | 77 | 78 | -------------------------------------------------------------------------------- /lib/tinyxml/docs/functions_0x6c.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | TinyXml: Class Members 6 | 7 | 8 | 9 | 10 | 11 | 58 |
59 | Here is a list of all documented class members with links to the class documentation for each member: 60 | 61 |

- l -

78 |
79 |
Generated by  80 | 81 | doxygen 1.6.2
82 | 83 | 84 | -------------------------------------------------------------------------------- /lib/tinyxml/docs/functions_0x6f.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | TinyXml: Class Members 6 | 7 | 8 | 9 | 10 | 11 | 58 |
59 | Here is a list of all documented class members with links to the class documentation for each member: 60 | 61 |

- o -

69 |
70 |
Generated by  71 | 72 | doxygen 1.6.2
73 | 74 | 75 | -------------------------------------------------------------------------------- /lib/tinyxml/docs/functions_0x72.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | TinyXml: Class Members 6 | 7 | 8 | 9 | 10 | 11 | 58 |
59 | Here is a list of all documented class members with links to the class documentation for each member: 60 | 61 |

- r -

78 |
79 |
Generated by  80 | 81 | doxygen 1.6.2
82 | 83 | 84 | -------------------------------------------------------------------------------- /lib/tinyxml/docs/functions_0x75.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | TinyXml: Class Members 6 | 7 | 8 | 9 | 10 | 11 | 58 |
59 | Here is a list of all documented class members with links to the class documentation for each member: 60 | 61 |

- u -

69 |
70 |
Generated by  71 | 72 | doxygen 1.6.2
73 | 74 | 75 | -------------------------------------------------------------------------------- /lib/tinyxml/docs/functions_enum.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | TinyXml: Class Members - Enumerations 6 | 7 | 8 | 9 | 10 | 11 | 37 |
38 |   43 |
44 |
Generated by  45 | 46 | doxygen 1.6.2
47 | 48 | 49 | -------------------------------------------------------------------------------- /lib/tinyxml/docs/functions_func_0x64.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | TinyXml: Class Members - Functions 6 | 7 | 8 | 9 | 10 | 11 | 57 |
58 |   59 | 60 |

- d -

65 |
66 |
Generated by  67 | 68 | doxygen 1.6.2
69 | 70 | 71 | -------------------------------------------------------------------------------- /lib/tinyxml/docs/functions_func_0x67.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | TinyXml: Class Members - Functions 6 | 7 | 8 | 9 | 10 | 11 | 57 |
58 |   59 | 60 |

- g -

71 |
72 |
Generated by  73 | 74 | doxygen 1.6.2
75 | 76 | 77 | -------------------------------------------------------------------------------- /lib/tinyxml/docs/functions_func_0x6c.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | TinyXml: Class Members - Functions 6 | 7 | 8 | 9 | 10 | 11 | 57 |
58 |   59 | 60 |

- l -

77 |
78 |
Generated by  79 | 80 | doxygen 1.6.2
81 | 82 | 83 | -------------------------------------------------------------------------------- /lib/tinyxml/docs/functions_func_0x72.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | TinyXml: Class Members - Functions 6 | 7 | 8 | 9 | 10 | 11 | 57 |
58 |   59 | 60 |

- r -

77 |
78 |
Generated by  79 | 80 | doxygen 1.6.2
81 | 82 | 83 | -------------------------------------------------------------------------------- /lib/tinyxml/docs/functions_func_0x75.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | TinyXml: Class Members - Functions 6 | 7 | 8 | 9 | 10 | 11 | 57 |
58 |   59 | 60 |

- u -

65 |
66 |
Generated by  67 | 68 | doxygen 1.6.2
69 | 70 | 71 | -------------------------------------------------------------------------------- /lib/tinyxml/docs/functions_rela.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | TinyXml: Class Members - Related Functions 6 | 7 | 8 | 9 | 10 | 11 | 37 |
38 |   46 |
47 |
Generated by  48 | 49 | doxygen 1.6.2
50 | 51 | 52 | -------------------------------------------------------------------------------- /lib/tinyxml/docs/functions_vars.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | TinyXml: Class Members - Variables 6 | 7 | 8 | 9 | 10 | 11 | 37 |
38 |   43 |
44 |
Generated by  45 | 46 | doxygen 1.6.2
47 | 48 | 49 | -------------------------------------------------------------------------------- /lib/tinyxml/docs/hierarchy.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | TinyXml: Hierarchical Index 6 | 7 | 8 | 9 | 10 | 11 | 28 |
29 |

Class Hierarchy

This inheritance list is sorted roughly, but not completely, alphabetically: 49 |
50 |
Generated by  51 | 52 | doxygen 1.6.2
53 | 54 | 55 | -------------------------------------------------------------------------------- /lib/tinyxml/docs/pages.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | TinyXml: Page Index 6 | 7 | 8 | 9 | 10 | 11 | 21 |
22 |

Related Pages

Here is a list of all related documentation pages: 26 |
27 |
Generated by  28 | 29 | doxygen 1.6.2
30 | 31 | 32 | -------------------------------------------------------------------------------- /lib/tinyxml/docs/tab_b.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/p308945/MyNetWorkLib/e86d550bf837b08d3fe9b76341e802096c1f8f5c/lib/tinyxml/docs/tab_b.gif -------------------------------------------------------------------------------- /lib/tinyxml/docs/tab_l.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/p308945/MyNetWorkLib/e86d550bf837b08d3fe9b76341e802096c1f8f5c/lib/tinyxml/docs/tab_l.gif -------------------------------------------------------------------------------- /lib/tinyxml/docs/tab_r.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/p308945/MyNetWorkLib/e86d550bf837b08d3fe9b76341e802096c1f8f5c/lib/tinyxml/docs/tab_r.gif -------------------------------------------------------------------------------- /lib/tinyxml/docs/tabs.css: -------------------------------------------------------------------------------- 1 | /* tabs styles, based on http://www.alistapart.com/articles/slidingdoors */ 2 | 3 | DIV.tabs 4 | { 5 | float : left; 6 | width : 100%; 7 | background : url("tab_b.gif") repeat-x bottom; 8 | margin-bottom : 4px; 9 | } 10 | 11 | DIV.tabs UL 12 | { 13 | margin : 0px; 14 | padding-left : 10px; 15 | list-style : none; 16 | } 17 | 18 | DIV.tabs LI, DIV.tabs FORM 19 | { 20 | display : inline; 21 | margin : 0px; 22 | padding : 0px; 23 | } 24 | 25 | DIV.tabs FORM 26 | { 27 | float : right; 28 | } 29 | 30 | DIV.tabs A 31 | { 32 | float : left; 33 | background : url("tab_r.gif") no-repeat right top; 34 | border-bottom : 1px solid #84B0C7; 35 | font-size : 80%; 36 | font-weight : bold; 37 | text-decoration : none; 38 | } 39 | 40 | DIV.tabs A:hover 41 | { 42 | background-position: 100% -150px; 43 | } 44 | 45 | DIV.tabs A:link, DIV.tabs A:visited, 46 | DIV.tabs A:active, DIV.tabs A:hover 47 | { 48 | color: #1A419D; 49 | } 50 | 51 | DIV.tabs SPAN 52 | { 53 | float : left; 54 | display : block; 55 | background : url("tab_l.gif") no-repeat left top; 56 | padding : 5px 9px; 57 | white-space : nowrap; 58 | } 59 | 60 | DIV.tabs #MSearchBox 61 | { 62 | float : right; 63 | display : inline; 64 | font-size : 1em; 65 | } 66 | 67 | DIV.tabs TD 68 | { 69 | font-size : 80%; 70 | font-weight : bold; 71 | text-decoration : none; 72 | } 73 | 74 | 75 | 76 | /* Commented Backslash Hack hides rule from IE5-Mac \*/ 77 | DIV.tabs SPAN {float : none;} 78 | /* End IE5-Mac hack */ 79 | 80 | DIV.tabs A:hover SPAN 81 | { 82 | background-position: 0% -150px; 83 | } 84 | 85 | DIV.tabs LI.current A 86 | { 87 | background-position: 100% -150px; 88 | border-width : 0px; 89 | } 90 | 91 | DIV.tabs LI.current SPAN 92 | { 93 | background-position: 0% -150px; 94 | padding-bottom : 6px; 95 | } 96 | 97 | DIV.navpath 98 | { 99 | background : none; 100 | border : none; 101 | border-bottom : 1px solid #84B0C7; 102 | text-align : center; 103 | margin : 2px; 104 | padding : 2px; 105 | } 106 | -------------------------------------------------------------------------------- /lib/tinyxml/libtinyxml.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/p308945/MyNetWorkLib/e86d550bf837b08d3fe9b76341e802096c1f8f5c/lib/tinyxml/libtinyxml.a -------------------------------------------------------------------------------- /lib/tinyxml/tinystr.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | www.sourceforge.net/projects/tinyxml 3 | 4 | This software is provided 'as-is', without any express or implied 5 | warranty. In no event will the authors be held liable for any 6 | damages arising from the use of this software. 7 | 8 | Permission is granted to anyone to use this software for any 9 | purpose, including commercial applications, and to alter it and 10 | redistribute it freely, subject to the following restrictions: 11 | 12 | 1. The origin of this software must not be misrepresented; you must 13 | not claim that you wrote the original software. If you use this 14 | software in a product, an acknowledgment in the product documentation 15 | would be appreciated but is not required. 16 | 17 | 2. Altered source versions must be plainly marked as such, and 18 | must not be misrepresented as being the original software. 19 | 20 | 3. This notice may not be removed or altered from any source 21 | distribution. 22 | */ 23 | 24 | 25 | #ifndef TIXML_USE_STL 26 | 27 | #include "tinystr.h" 28 | 29 | // Error value for find primitive 30 | const TiXmlString::size_type TiXmlString::npos = static_cast< TiXmlString::size_type >(-1); 31 | 32 | 33 | // Null rep. 34 | TiXmlString::Rep TiXmlString::nullrep_ = { 0, 0, { '\0' } }; 35 | 36 | 37 | void TiXmlString::reserve (size_type cap) 38 | { 39 | if (cap > capacity()) 40 | { 41 | TiXmlString tmp; 42 | tmp.init(length(), cap); 43 | memcpy(tmp.start(), data(), length()); 44 | swap(tmp); 45 | } 46 | } 47 | 48 | 49 | TiXmlString& TiXmlString::assign(const char* str, size_type len) 50 | { 51 | size_type cap = capacity(); 52 | if (len > cap || cap > 3*(len + 8)) 53 | { 54 | TiXmlString tmp; 55 | tmp.init(len); 56 | memcpy(tmp.start(), str, len); 57 | swap(tmp); 58 | } 59 | else 60 | { 61 | memmove(start(), str, len); 62 | set_size(len); 63 | } 64 | return *this; 65 | } 66 | 67 | 68 | TiXmlString& TiXmlString::append(const char* str, size_type len) 69 | { 70 | size_type newsize = length() + len; 71 | if (newsize > capacity()) 72 | { 73 | reserve (newsize + capacity()); 74 | } 75 | memmove(finish(), str, len); 76 | set_size(newsize); 77 | return *this; 78 | } 79 | 80 | 81 | TiXmlString operator + (const TiXmlString & a, const TiXmlString & b) 82 | { 83 | TiXmlString tmp; 84 | tmp.reserve(a.length() + b.length()); 85 | tmp += a; 86 | tmp += b; 87 | return tmp; 88 | } 89 | 90 | TiXmlString operator + (const TiXmlString & a, const char* b) 91 | { 92 | TiXmlString tmp; 93 | TiXmlString::size_type b_len = static_cast( strlen(b) ); 94 | tmp.reserve(a.length() + b_len); 95 | tmp += a; 96 | tmp.append(b, b_len); 97 | return tmp; 98 | } 99 | 100 | TiXmlString operator + (const char* a, const TiXmlString & b) 101 | { 102 | TiXmlString tmp; 103 | TiXmlString::size_type a_len = static_cast( strlen(a) ); 104 | tmp.reserve(a_len + b.length()); 105 | tmp.append(a, a_len); 106 | tmp += b; 107 | return tmp; 108 | } 109 | 110 | 111 | #endif // TIXML_USE_STL 112 | -------------------------------------------------------------------------------- /lib/tinyxml/tinyxmlerror.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | www.sourceforge.net/projects/tinyxml 3 | Original code (2.0 and earlier )copyright (c) 2000-2006 Lee Thomason (www.grinninglizard.com) 4 | 5 | This software is provided 'as-is', without any express or implied 6 | warranty. In no event will the authors be held liable for any 7 | damages arising from the use of this software. 8 | 9 | Permission is granted to anyone to use this software for any 10 | purpose, including commercial applications, and to alter it and 11 | redistribute it freely, subject to the following restrictions: 12 | 13 | 1. The origin of this software must not be misrepresented; you must 14 | not claim that you wrote the original software. If you use this 15 | software in a product, an acknowledgment in the product documentation 16 | would be appreciated but is not required. 17 | 18 | 2. Altered source versions must be plainly marked as such, and 19 | must not be misrepresented as being the original software. 20 | 21 | 3. This notice may not be removed or altered from any source 22 | distribution. 23 | */ 24 | 25 | #include "tinyxml.h" 26 | 27 | // The goal of the seperate error file is to make the first 28 | // step towards localization. tinyxml (currently) only supports 29 | // english error messages, but the could now be translated. 30 | // 31 | // It also cleans up the code a bit. 32 | // 33 | 34 | const char* TiXmlBase::errorString[ TiXmlBase::TIXML_ERROR_STRING_COUNT ] = 35 | { 36 | "No error", 37 | "Error", 38 | "Failed to open file", 39 | "Error parsing Element.", 40 | "Failed to read Element name", 41 | "Error reading Element value.", 42 | "Error reading Attributes.", 43 | "Error: empty tag.", 44 | "Error reading end tag.", 45 | "Error parsing Unknown.", 46 | "Error parsing Comment.", 47 | "Error parsing Declaration.", 48 | "Error document empty.", 49 | "Error null (0) or unexpected EOF found in input stream.", 50 | "Error parsing CDATA.", 51 | "Error when TiXmlDocument added to document, because TiXmlDocument can only be at the root.", 52 | }; 53 | -------------------------------------------------------------------------------- /lib/tinyxml/utf8test.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/p308945/MyNetWorkLib/e86d550bf837b08d3fe9b76341e802096c1f8f5c/lib/tinyxml/utf8test.gif -------------------------------------------------------------------------------- /lib/tinyxml/utf8test.xml: -------------------------------------------------------------------------------- 1 |  2 | 3 | The world has many languages 4 | Мир имеет много языков 5 | el mundo tiene muchos idiomas 6 | 世界有很多语言 7 | <Русский название="name" ценность="value"><имеет> 8 | <汉语 名字="name" 价值="value">世界有很多语言 9 | "Mëtæl!" 10 | <ä>Umlaut Element 11 | 12 | -------------------------------------------------------------------------------- /lib/tinyxml/utf8testverify.xml: -------------------------------------------------------------------------------- 1 |  2 | 3 | The world has many languages 4 | Мир имеет много языков 5 | el mundo tiene muchos idiomas 6 | 世界有很多语言 7 | <Русский название="name" ценность="value"><имеет> 8 | <汉语 名字="name" 价值="value">世界有很多语言 9 | "Mëtæl!" 10 | <ä>Umlaut Element 11 | 12 | -------------------------------------------------------------------------------- /lib/tinyxml/xmltest.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/p308945/MyNetWorkLib/e86d550bf837b08d3fe9b76341e802096c1f8f5c/lib/tinyxml/xmltest.cpp -------------------------------------------------------------------------------- /proto/BaseCmd.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: baseCmd.h 5 | * 6 | * Description: base command 7 | * 8 | * Version: 1.0 9 | * Created: 09/28/2015 02:25:50 PM 10 | * Revision: none 11 | * Compiler: gcc 12 | * 13 | * Author: huangyun (hy), 895175589@qq.com 14 | * Organization: 15 | * 16 | * ===================================================================================== 17 | */ 18 | 19 | #ifndef __BASECMD_H_ 20 | #define __BASECMD_H_ 21 | 22 | #include 23 | #include 24 | 25 | //#pragma pack(1) 26 | 27 | namespace MyNameSpace 28 | { 29 | namespace Command 30 | { 31 | const uint32_t MAX_PROTOBUF_SIZE = 100 * 1024; 32 | enum class COMMAND_TYPE : char 33 | { 34 | INNER, 35 | OUTTER 36 | }; 37 | struct BaseCommand 38 | { 39 | BaseCommand(uint32_t cmdId, COMMAND_TYPE type) : mCmdId(cmdId), mType(type) 40 | { 41 | } 42 | uint32_t mCmdId; 43 | COMMAND_TYPE mType; //0 InnerMessage, 1 OutterMessage,也就是服务器内部之间的消息是0,服务器和客户端之间的消息是1 44 | }__attribute__ ((packed)); 45 | 46 | struct ProtobufBaseCommand : BaseCommand 47 | { 48 | ProtobufBaseCommand(uint32_t cmdId, COMMAND_TYPE type) : BaseCommand(cmdId, type) 49 | { 50 | memset(protobuf, 0x0, sizeof(protobuf)); 51 | } 52 | unsigned char protobuf[MAX_PROTOBUF_SIZE]; 53 | }__attribute__ ((packed)); 54 | } 55 | } 56 | 57 | #pragma pack() 58 | #endif 59 | -------------------------------------------------------------------------------- /proto/CmdNumber.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: LoadBalanceCmd.h 5 | * 6 | * Description: command Number file 7 | * 8 | * Version: 1.0 9 | * Created: 09/28/2015 11:42:07 AM 10 | * Revision: none 11 | * Compiler: gcc 12 | * 13 | * Author: huangyun (hy), 895175589@qq.com 14 | * Organization: 15 | * 16 | * ===================================================================================== 17 | */ 18 | 19 | #ifndef __CMDNUMBER_H_ 20 | #define __CMDNUMBER_H_ 21 | 22 | #include 23 | 24 | namespace MyNameSpace 25 | { 26 | namespace Command 27 | { 28 | namespace OutterCommand 29 | { 30 | const uint32_t REQ_LOGIN_CMD = 1; 31 | const uint32_t RTN_LOGIN_CMD = 2; 32 | const uint32_t TEST_PROTO_BUF = 3; 33 | const uint32_t TEST_PROTO_BUF_RTN = 4; 34 | } 35 | 36 | namespace InnerCommand 37 | { 38 | const uint32_t REQ_LOADBALANCE_CMD = 1; 39 | const uint32_t RTN_LOADBALANCE_CMD = 2; 40 | } 41 | } 42 | } 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /proto/LoadBalanceCmd.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: LoadBalanceCmd.h 5 | * 6 | * Description: loadbalance command file 7 | * 8 | * Version: 1.0 9 | * Created: 09/28/2015 02:14:19 PM 10 | * Revision: none 11 | * Compiler: gcc 12 | * 13 | * Author: huangyun (hy), 895175589@qq.com 14 | * Organization: 15 | * 16 | * ===================================================================================== 17 | */ 18 | 19 | #ifndef __LOADBALANCECMD_H_ 20 | #define __LOADBALANCECMD_H_ 21 | 22 | #include "CmdNumber.h" 23 | #include "BaseCmd.h" 24 | 25 | namespace MyNameSpace 26 | { 27 | namespace Command 28 | { 29 | struct ReqLoadBalance : BaseCommand(REQ_LOADBALANCE_CMD, COMMAND_TYPE::OUTTER) 30 | { 31 | 32 | }; 33 | struct RtnLoadBalance : BaseCommand(RTN_LOADBALANCE_CMD, COMMAND_TYPE::INNER) 34 | { 35 | RtnLoadBalance() 36 | { 37 | memset(this, 0x0, sizeof(*this)); 38 | } 39 | unsigned char mIp[32]; 40 | unsigned short mPort; 41 | }; 42 | } 43 | } 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /proto/LoginProcessCmd.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: LoginProcessCmd.h 5 | * 6 | * Description: Login Command 7 | * 8 | * Version: 1.0 9 | * Created: 10/20/2015 11:46:56 AM 10 | * Revision: none 11 | * Compiler: gcc 12 | * 13 | * Author: huangyun (hy), 895175589@qq.com 14 | * Organization: 15 | * 16 | * ===================================================================================== 17 | */ 18 | 19 | #ifndef __LOGINPROCESSCMD_H_ 20 | #define __LOGINPROCESSCMD_H_ 21 | #include "CmdNumber.h" 22 | #include "BaseCmd.h" 23 | #include 24 | 25 | namespace MyNameSpace 26 | { 27 | namespace Command 28 | { 29 | const int MAX_ACCOUNT_LEN = 48; 30 | struct ReqLogin : BaseCommand 31 | { 32 | ReqLogin() : BaseCommand(OutterCommand::REQ_LOGIN_CMD, COMMAND_TYPE::OUTTER) 33 | { 34 | memset(account, 0x0, sizeof(account)); 35 | } 36 | char account[MAX_ACCOUNT_LEN]; 37 | }__attribute__ ((packed)); 38 | 39 | struct RtnLogin : BaseCommand 40 | { 41 | RtnLogin() : BaseCommand(OutterCommand::RTN_LOGIN_CMD, COMMAND_TYPE::OUTTER) 42 | { 43 | res = 0; 44 | memset(account, 0x0, sizeof(account)); 45 | } 46 | int res; 47 | char account[MAX_ACCOUNT_LEN]; 48 | }__attribute__ ((packed)); 49 | } 50 | } 51 | #endif 52 | -------------------------------------------------------------------------------- /proto/protobuf/gen_protobuf: -------------------------------------------------------------------------------- 1 | for i in `ls *.proto` 2 | do 3 | protoc $i --cpp_out=./ 4 | done 5 | -------------------------------------------------------------------------------- /proto/protobuf/test.proto: -------------------------------------------------------------------------------- 1 | package MyProtoBuf; 2 | 3 | message CMsgLoginReq 4 | { 5 | required string account = 1; 6 | } 7 | 8 | message CMsgLoginRtn 9 | { 10 | required int32 ret = 1; 11 | } 12 | -------------------------------------------------------------------------------- /server/ConnectionLoadBalance.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: ConnectionLoadBalance.cpp 5 | * 6 | * Description: Connection Load Balance 7 | * 8 | * Version: 1.0 9 | * Created: 09/28/2015 09:36:09 PM 10 | * Revision: none 11 | * Compiler: gcc 12 | * 13 | * Author: huangyun (hy), 895175589@qq.com 14 | * Organization: 15 | * 16 | * ===================================================================================== 17 | */ 18 | 19 | #include "ConnectionLoadBalance.h" 20 | 21 | namespace MyNameSpace 22 | { 23 | bool MyConnectionLoadBalance::addServer(const std::string & ip, short port) 24 | { 25 | std::shared_ptr pInfo(new ServerInfo(ip, port)); 26 | if (!pInfo) 27 | { 28 | std::cerr<<"addServer new shared ptr return NULL"<<"ip: "<GetTags(), MyConstHash::VIRTUAL_NODE_COUNT, pInfo); 31 | } 32 | 33 | void MyConnectionLoadBalance::removeServer(const std::string & ip, short port) 34 | { 35 | std::string tags = ServerInfo(ip, port).GetTags(); 36 | return mConnHash.removeNode(tags); 37 | } 38 | 39 | std::shared_ptr MyConnectionLoadBalance::SearchServer(const std::string & ip, short port) 40 | { 41 | std::string tags = ServerInfo(ip, port).GetTags(); 42 | return mConnHash.searchNode(tags); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /server/ConnectionLoadBalance.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: LoadBalance.h 5 | * 6 | * Description: load balance 7 | * 8 | * Version: 1.0 9 | * Created: 09/28/2015 03:33:02 PM 10 | * Revision: none 11 | * Compiler: gcc 12 | * 13 | * Author: huangyun (hy), 895175589@qq.com 14 | * Organization: 15 | * 16 | * ===================================================================================== 17 | */ 18 | 19 | #ifndef __CONNECTIONLOADBALANCE_H_ 20 | #define __CONNECTIONLOADBALANCE_H_ 21 | 22 | #include "../base/MyConstHash.h" 23 | #include 24 | #include 25 | 26 | namespace MyNameSpace 27 | { 28 | class MyConnectionLoadBalance 29 | { 30 | public: 31 | class ServerInfo 32 | { 33 | public: 34 | ServerInfo(const std::string & ip, short port): mIp(ip), mPort(port) 35 | { 36 | std::ostringstream os; 37 | os<<"ServerLoadBalance ip:"< SearchServer(const std::string & ip, short port); 49 | void SetHashFunc(HashFunction fun) 50 | { 51 | mConnHash.SetHashFunc(fun); 52 | } 53 | private: 54 | MyConstHash mConnHash; 55 | }; 56 | } 57 | 58 | #endif 59 | -------------------------------------------------------------------------------- /server/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS=-Wall -std=c++11 -g -Wparentheses -Wsequence-point 2 | LIBFLAGS=-fPIC -shared 3 | .PHONY : all 4 | all : test libhyServer.so 5 | test : test.o MySocket.o MySockTaskManager.o MySockTask.o MyThread.o MySockTaskPool.o MyTcpServer.o MyBaseServer.o MySockTask.o MyServer.o MyTask.o MyServerMsgProcess.o MyServerTask.o MyHashFunction.o ConnectionLoadBalance.o 6 | g++ $(CXXFLAGS) -o $@ MySocket.o MySockTask.o MyThread.o MySockTaskManager.o MySockTaskPool.o MyTcpServer.o MyBaseServer.o MyServer.o MyTask.o test.o MyServerMsgProcess.o MyServerTask.o MyHashFunction.o ConnectionLoadBalance.o -lmysqlclient -lpthread 7 | 8 | libhyServer.so : MySocket.o MySockTaskManager.o MySockTask.o MyThread.o MySockTaskPool.o MyTcpServer.o MyBaseServer.o MySockTask.o MyTask.o MyServerMsgProcess.o MyServerTask.o MyHashFunction.o ConnectionLoadBalance.o 9 | g++ $(CXXFLAGS) $(LIBFLAGS) -o $@ MySocket.o MySockTask.o MyThread.o MySockTaskManager.o MySockTaskPool.o MyTcpServer.o MyBaseServer.o MyServer.o MyTask.o test.o MyServerMsgProcess.o MyServerTask.o MyHashFunction.o ConnectionLoadBalance.o -lmysqlclient -lpthread 10 | 11 | MySocket.o : ../base/MySocket.cpp ../base/MySocket.h ../base/MyLock.h 12 | g++ $(CXXFLAGS) -c ../base/MySocket.cpp 13 | 14 | MySockTaskManager.o : ../base/MySockTaskManager.h ../base/MySockTaskManager.cpp 15 | g++ $(CXXFLAGS) -c ../base/MySockTaskManager.cpp 16 | 17 | MySockTask.o : ../base/MySockTask.cpp ../base/MySockTask.h ../base/MyQueue.h ../base/MySocket.h 18 | g++ $(CXXFLAGS) -c ../base/MySockTask.cpp 19 | 20 | MyThread.o : ../base/MyThread.cpp ../base/MyThread.h ../base/MyLock.h 21 | g++ $(CXXFLAGS) -c ../base/MyThread.cpp -lpthread 22 | 23 | MySockTaskPool.o : ../base/MySockTaskPool.cpp ../base/MySockTaskPool.h ../base/MySockTaskManager.h 24 | g++ $(CXXFLAGS) -c ../base/MySockTaskPool.cpp 25 | 26 | MyTcpServer.o : MyTcpServer.cpp MyTcpServer.h 27 | g++ $(CXXFLAGS) -c MyTcpServer.cpp 28 | 29 | MyBaseServer.o : MyBaseServer.cpp MyBaseServer.h 30 | g++ $(CXXFLAGS) -c MyBaseServer.cpp 31 | 32 | MyServer.o : MyServer.cpp MyServer.h ../base/CmdDispatcher.h 33 | g++ $(CXXFLAGS) -c MyServer.cpp 34 | 35 | MyServerTask.o : ../base/MyTask.cpp ../base/MyTask.h MyServerTask.h MyServerTask.cpp 36 | g++ $(CXXFLAGS) -c MyServerTask.cpp 37 | MyTask.o : ../base/MyTask.cpp ../base/MyTask.h 38 | g++ $(CXXFLAGS) -c ../base/MyTask.cpp 39 | MyHashFunction.o : ../base/MyHashFunction.h ../base/MyHashFunction.cpp 40 | g++ $(CXXFLAGS) -c ../base/MyHashFunction.cpp 41 | test.o : test.cpp 42 | g++ $(CXXFLAGS) -c test.cpp 43 | MyServerMsgProcess.o : MyServerMsgProcess.cpp 44 | g++ $(CXXFLAGS) -c MyServerMsgProcess.cpp 45 | ConnectionLoadBalance.o : ConnectionLoadBalance.h ConnectionLoadBalance.cpp 46 | g++ $(CXXFLAGS) -c ConnectionLoadBalance.cpp ../base/MyConstHash.h 47 | clean: 48 | rm -rf test test.o MySockTaskPool.o MySocket.o MySockTask.o MyThread.o MyTcpServer.o MyBaseServer.o MyServerTask.o MyServer.o MyTask.o MySockTaskManager.o MyServerMsgProcess.o libhyServer.so MyHashFunction.o ConnectionLoadBalance.o MyHashFunction.o 49 | rm -rf ../lib/libhyServer.so 50 | install: 51 | cp libhyServer.so ../lib 52 | -------------------------------------------------------------------------------- /server/MyBaseServer.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: MyBaseServer.cpp 3 | > Author: huangyun 4 | > Mail: 895175589@qq.com 5 | > Created Time: Sat 08 Aug 2015 02:41:36 PM 6 | ************************************************************************/ 7 | #include "MyBaseServer.h" 8 | #include 9 | #include 10 | #include 11 | 12 | namespace MyNameSpace 13 | { 14 | namespace 15 | { 16 | void handlerHup(int sig) 17 | { 18 | std::cerr<<__FUNCTION__<<"("<<__LINE__<<"): get hup signal"<reload(); 23 | } 24 | } 25 | void handlerCtrlC(int sig) 26 | { 27 | std::cerr<<__FUNCTION__<<"("<<__LINE__<<"): get ctrl c signal"<fini(); 32 | } 33 | } 34 | } 35 | MyBaseServer::Container MyBaseServer::mServerContainer; 36 | 37 | bool MyBaseServer::reload() 38 | { 39 | return true; 40 | } 41 | 42 | void MyBaseServer::regInnerCallBack(uint32_t cmdId, CallBackFunT fun) 43 | { 44 | mInnerDispatcher.regCallback(cmdId, fun); 45 | } 46 | 47 | void MyBaseServer::regOutterCallBack(uint32_t cmdId, CallBackFunT fun) 48 | { 49 | mOutterDispatcher.regCallback(cmdId, fun); 50 | } 51 | 52 | bool MyBaseServer::init(int port) 53 | { 54 | mServerContainer.push_back(this); 55 | bool ret = mTcpServer.bindPort(port); 56 | if (!ret) 57 | { 58 | std::cerr<<__FUNCTION__<<"("<<__LINE__<<"): bind fail"<= 0) 75 | { 76 | newTask(ret); 77 | } 78 | return ret; 79 | } 80 | void MyBaseServer::mainLoop() 81 | { 82 | while(!isFini()) 83 | { 84 | serverProcess(); 85 | usleep(3*1000); 86 | } 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /server/MyBaseServer.h: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: MyBaseServer.h 3 | > Author: huangyun 4 | > Mail: 895175589@qq.com 5 | > Created Time: Sat 08 Aug 2015 01:10:00 PM 6 | ************************************************************************/ 7 | 8 | #ifndef _MY_BASE_SERVER_H_ 9 | #define _MY_BASE_SERVER_H_ 10 | 11 | #include "MyTcpServer.h" 12 | #include 13 | #include "../base/CmdDispatcher.h" 14 | namespace MyNameSpace 15 | { 16 | class MyBaseServer 17 | { 18 | public: 19 | MyBaseServer() : mComplete(false), mInnerDispatcher("InnerDispatcher"), mOutterDispatcher("OutterDispatcher") 20 | { 21 | } 22 | virtual bool reload(); //for hup signal 23 | virtual bool init(int port); 24 | virtual bool newTask(int sock) = 0; 25 | virtual void fini() 26 | { 27 | mComplete = true; 28 | } 29 | void mainLoop(); 30 | protected: 31 | void regInnerCallBack(uint32_t cmdId, CallBackFunT fun); //内部消息分发器,也就是服务器之间的消息 32 | void regOutterCallBack(uint32_t cmdId, CallBackFunT fun); //外部消息分发器,也就是服务端客户端之间的消息 33 | private: 34 | int serverProcess(); 35 | bool isFini() 36 | { 37 | return mComplete; 38 | } 39 | public: 40 | typedef std::vector Container; 41 | typedef std::vector::iterator Container_IT; 42 | static Container mServerContainer; 43 | private: 44 | bool mComplete; 45 | MyTcpServer mTcpServer; 46 | protected: 47 | Dispatcher mInnerDispatcher; 48 | Dispatcher mOutterDispatcher; 49 | }; 50 | } 51 | #endif 52 | -------------------------------------------------------------------------------- /server/MyServer.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: MyServer.cpp 3 | > Author: huangyun 4 | > Mail: 895175589@qq.com 5 | > Created Time: Sat 08 Aug 2015 04:56:34 PM 6 | ************************************************************************/ 7 | 8 | #include "MyServer.h" 9 | #include "../base/MySockTaskManager.h" 10 | #include "MyServerMsgProcess.h" 11 | #include "MyServerTask.h" 12 | 13 | namespace MyNameSpace 14 | { 15 | bool MyServer::init(int port) 16 | { 17 | //TODO inti load config... and so on 18 | if (!mTaskPool.init()) 19 | { 20 | std::cerr<<__FUNCTION__<<"("<<__LINE__<<"): MySockTaskPool init fail"< File Name: MyServer.h 3 | > Author: huangyun 4 | > Mail: 895175589@qq.com 5 | > Created Time: Sat 08 Aug 2015 04:49:19 PM 6 | ************************************************************************/ 7 | #ifndef _MY_SERVER_H 8 | #define _MY_SERVER_H 9 | 10 | #include "MyBaseServer.h" 11 | #include "../base/MySockTaskPool.h" 12 | 13 | namespace MyNameSpace 14 | { 15 | class MyServer : public MyBaseServer 16 | { 17 | public: 18 | MyServer() : mTaskPool(4, 32), mUniqueId(0) 19 | { 20 | } 21 | virtual bool init(int port); 22 | virtual bool reload(); 23 | virtual bool newTask(int sock); 24 | void fini(); 25 | void initCallBack(); 26 | private: 27 | MySockTaskPool mTaskPool; 28 | uint32_t mUniqueId; 29 | }; 30 | } 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /server/MyServerMsgProcess.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: MyServerMsgProcess.cpp 3 | > Author: huangyun 4 | > Mail: 895175589@qq.com 5 | > Created Time: Sat 08 Aug 2015 07:25:05 PM 6 | ************************************************************************/ 7 | 8 | #include "MyServerMsgProcess.h" 9 | #include "../base/MySockTaskManager.h" 10 | 11 | namespace MyNameSpace 12 | { 13 | void MyServerMsgProcess::run() 14 | { 15 | while (!isFini()) 16 | { 17 | MySockTaskManager::getInstance().doProcessMsg(); 18 | usleep(5*1000); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /server/MyServerMsgProcess.h: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: MyServerTimeTick.h 3 | > Author: huangyun 4 | > Mail: 895175589@qq.com 5 | > Created Time: Sat 08 Aug 2015 07:16:23 PM 6 | ************************************************************************/ 7 | #ifndef _MY_SERVER_MSG_PROCESS_H_ 8 | #define _MY_SERVER_MSG_PROCESS_H_ 9 | 10 | #include "../base/MySingleton.h" 11 | #include "../base/MyThread.h" 12 | 13 | namespace MyNameSpace 14 | { 15 | class MyServer; 16 | class MyServerMsgProcess : public MyThread, public MySingleton 17 | { 18 | private: 19 | friend class MySingleton; 20 | MyServerMsgProcess() 21 | { 22 | 23 | } 24 | ~MyServerMsgProcess() 25 | { 26 | 27 | } 28 | public: 29 | void run(); 30 | }; 31 | } 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /server/MyServerTask.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: MyServerTask.cpp 5 | * 6 | * Description: server tcp connection 7 | * 8 | * Version: 1.0 9 | * Created: 09/28/2015 10:53:59 AM 10 | * Revision: none 11 | * Compiler: gcc 12 | * 13 | * Author: huangyun (hy), 895175589@qq.com 14 | * Organization: 15 | * 16 | * ===================================================================================== 17 | */ 18 | 19 | #include "MyServerTask.h" 20 | #include "../proto/BaseCmd.h" 21 | 22 | namespace MyNameSpace 23 | { 24 | bool MyServerTask::cmdParse(const char *msg, int len) 25 | { 26 | // mDispatcher.dispatch(msg, len); 27 | if (mInnerDispatcher && mOutterDispatcher) 28 | { 29 | const Command::BaseCommand *pCmd = reinterpret_cast(msg); 30 | // std::cout<<"msg: "<mCmdId<<" type: "<<(char)pCmd->mType<<" data: "<data<<" len: "<mCmdId<<" type: "<<(char)pCmd->mType<<" len: "<mType) 33 | { 34 | case Command::COMMAND_TYPE::INNER: 35 | { 36 | mInnerDispatcher->dispatcher(pCmd, len, getId()); 37 | } 38 | break; 39 | case Command::COMMAND_TYPE::OUTTER: 40 | { 41 | mOutterDispatcher->dispatcher(pCmd, len, getId()); 42 | } 43 | break; 44 | default: 45 | { 46 | std::cerr<<"file: "<<__FILE__<<" function: "<<__FUNCTION__<<" line: "<<__LINE__<<" Id: "<mCmdId<<" error"< File Name: TcpServer.h 3 | > Author: huangyun 4 | > Mail: 895175589@qq.com 5 | > Created Time: Sat 08 Aug 2015 01:14:20 PM 6 | ************************************************************************/ 7 | 8 | #ifndef _MY_TCP_SERVER_H_ 9 | #define _MY_TCP_SERVER_H_ 10 | 11 | #include 12 | 13 | namespace MyNameSpace 14 | { 15 | class MyTcpServer 16 | { 17 | public: 18 | MyTcpServer() : mEpfd(-1), mSock(-1), mListenCount(1024) 19 | { 20 | } 21 | ~MyTcpServer(); 22 | bool bindPort(int port); 23 | int acceptCallBack(); 24 | bool setNonBlock(bool b); 25 | private: 26 | int mEpfd; 27 | int mSock; 28 | int mListenCount; 29 | }; 30 | } 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /server/test.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: test.cpp 3 | > Author: huangyun 4 | > Mail: 895175589@qq.com 5 | > Created Time: Fri 07 Aug 2015 09:58:04 PM 6 | ************************************************************************/ 7 | #include "MyServer.h" 8 | #include "../base/MySockTaskManager.h" 9 | //#include "../base/MyHashFunction.h" 10 | #include "../base/MyConstHash.h" 11 | 12 | int main() 13 | { 14 | MyNameSpace::MyServer server; 15 | if (!server.init(50002)) 16 | { 17 | std::cerr<<__FUNCTION__<<"("<<__LINE__<<"): server init fail"<mCmdId<(cmd); 35 | std::cout<<"login account: "<account<account, sizeof(rtn.account)); 39 | std::cout<<"return message! len:"<sendDataWithBuffer(reinterpret_cast(&rtn), sizeof(rtn)); 41 | } 42 | return true; 43 | } 44 | 45 | bool LoginProcess::testProtobuf(const Command::BaseCommand *cmd, uint32_t len, int taskId) 46 | { 47 | MySockTask* task = MySockTaskManager::getInstance().getTaskByIdWithOutLock(taskId); 48 | std::cout<<" taskId: "<mCmdId<sendDataWithBuffer(reinterpret_cast(&rtn), sizeof(Command::BaseCommand) + protoSize); 64 | } 65 | return true; 66 | } 67 | 68 | } 69 | -------------------------------------------------------------------------------- /test/LoginProcess.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: LoginProcess.h 5 | * 6 | * Description: Login Process 7 | * 8 | * Version: 1.0 9 | * Created: 10/20/2015 10:46:40 AM 10 | * Revision: none 11 | * Compiler: gcc 12 | * 13 | * Author: huangyun (hy), 895175589@qq.com 14 | * Organization: 15 | * 16 | * ===================================================================================== 17 | */ 18 | 19 | 20 | #ifndef __LOGINPROCESS_H_ 21 | #define __LOGINPROCESS_H_ 22 | 23 | #include "BaseCmd.h" 24 | 25 | namespace MyNameSpace 26 | { 27 | class LoginProcess 28 | { 29 | public: 30 | LoginProcess() {} 31 | ~LoginProcess() {} 32 | public: 33 | bool ReqLogin(const Command::BaseCommand *cmd, uint32_t len, int taskId); 34 | bool testProtobuf(const Command::BaseCommand *cmd, uint32_t len, int taskId); 35 | 36 | }; 37 | #endif 38 | } 39 | -------------------------------------------------------------------------------- /test/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS=-Wall -std=c++11 -g -Wparentheses -Wl,-rpath,../lib 2 | #CXXFLAGS=-Wall -std=c++11 -g -gstabs+ 3 | INCLUDE= -I../base -I../server -I../client -I../proto -I../lib/tinyxml -I../include 4 | LDFLAGS= -L../lib -lhyServer -L../lib -lhyClient -L../lib/tinyxml -ltinyxml -lprotobuf -lpthread 5 | .PHONY : all 6 | all : server 7 | server : main.o Server.o XmlconfigParse.o LoginProcess.o test.pb.o MyCsvParse.o 8 | g++ $(CXXFLAGS) $(INCLUDE) main.o Server.o test.pb.o LoginProcess.o XmlconfigParse.o MyCsvParse.o -o $@ $(LDFLAGS) 9 | main.o : main.cpp 10 | g++ $(CXXFLAGS) $(INCLUDE) -c $< -o $@ 11 | Server.o : Server.cpp Server.h 12 | g++ $(CXXFLAGS) $(INCLUDE) -c $< -o $@ 13 | 14 | XmlconfigParse.o : XmlconfigParse.cpp XmlconfigParse.h 15 | g++ $(CXXFLAGS) $(INCLUDE) -c $< -o $@ 16 | LoginProcess.o : LoginProcess.cpp LoginProcess.h 17 | g++ $(CXXFLAGS) $(INCLUDE) -c $< -o $@ 18 | 19 | test.pb.o : ../proto/protobuf/test.pb.cc 20 | g++ $(CXXFLAGS) -c $< -o $@ 21 | 22 | MyCsvParse.o : ../base/MyCsvParse.cpp 23 | g++ $(CXXFLAGS) -c $< -o $@ 24 | 25 | clean: 26 | rm -rf server 27 | rm -rf Server.o 28 | rm -rf main.o 29 | rm -rf XmlconfigParse.o 30 | rm -rf LoginProcess.o 31 | rm -rf MyCsvParse.o 32 | -------------------------------------------------------------------------------- /test/Server.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: Server.h 5 | * 6 | * Description: Server 7 | * 8 | * Version: 1.0 9 | * Created: 10/11/2015 02:05:35 PM 10 | * Revision: none 11 | * Compiler: gcc 12 | * 13 | * Author: huangyun (hy), 895175589@qq.com 14 | * Organization: 15 | * 16 | * ===================================================================================== 17 | */ 18 | 19 | #ifndef _HY_SERVER_H 20 | #define _HY_SERVER_H 21 | 22 | #include "MyBaseServer.h" 23 | #include "MySockTaskPool.h" 24 | #include "CmdDispatcher.h" 25 | #include "MySockClientTaskPool.h" 26 | #include "types.h" 27 | #include "LoginProcess.h" 28 | #include "MyCsvParse.h" 29 | 30 | namespace MyNameSpace 31 | { 32 | 33 | class Server : public MyBaseServer 34 | { 35 | public: 36 | Server() : mServerTaskPool(4, 4096), mServerUniqueId(0) 37 | { 38 | } 39 | virtual bool init(unsigned short port); 40 | virtual bool reload(); 41 | virtual bool newTask(int sock); //connection from remote 42 | void fini(); 43 | bool newClient(const char *ip, unsigned short port, int serverId, int serverType); //connect to remote 44 | virtual void initCallBack(); 45 | private: 46 | MySockTaskPool mServerTaskPool; 47 | MySockClientTaskPool mClientTaskPool; 48 | uint32_t mServerUniqueId; 49 | uint32_t mClientUniqueId; 50 | ClientInfoList clientInfoList; 51 | LoginProcess loginProcess; 52 | }; 53 | } 54 | #endif 55 | -------------------------------------------------------------------------------- /test/XmlconfigParse.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: XmlconfigParse.cpp 5 | * 6 | * Description: XmlConfigure 7 | * 8 | * Version: 1.0 9 | * Created: 10/11/2015 08:59:10 PM 10 | * Revision: none 11 | * Compiler: gcc 12 | * 13 | * Author: huangyun (hy), 895175589@qq.com 14 | * Organization: 15 | * 16 | * ===================================================================================== 17 | */ 18 | 19 | 20 | #include "tinyxml.h" 21 | #include "types.h" 22 | #include "XmlconfigParse.h" 23 | #include 24 | 25 | namespace MyNameSpace 26 | { 27 | namespace XmlConfig 28 | { 29 | bool loadClientConfig(const char *configFile, ClientInfoList &clientInfoList) 30 | { 31 | TiXmlDocument stDoc; 32 | stDoc.LoadFile(configFile); 33 | TiXmlElement *pElement = NULL; 34 | TiXmlElement *pChildElm = NULL; 35 | TiXmlElement *pRoot = stDoc.FirstChildElement(); 36 | 37 | // 读取需要连接的IP地址 38 | pElement = pRoot->FirstChildElement("EXTRANET"); 39 | if( pElement != NULL ) 40 | { 41 | for( pChildElm = pElement->FirstChildElement(); pChildElm != NULL ; pChildElm = pChildElm->NextSiblingElement() ) 42 | { 43 | const char *szIPAddr = pChildElm->Attribute("IPADDRESS"); 44 | const char *szId = pChildElm->Attribute("ID"); 45 | const char *szPort = pChildElm->Attribute("PORT"); 46 | const char *szType = pChildElm->Attribute("TYPE"); 47 | if (NULL == szIPAddr || NULL == szId || NULL == szPort || NULL == szType) 48 | { 49 | std::cerr<<__FUNCTION__<<": "<<__LINE__<<" clientConfig file error"< 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /test/csv/111.csv: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/p308945/MyNetWorkLib/e86d550bf837b08d3fe9b76341e802096c1f8f5c/test/csv/111.csv -------------------------------------------------------------------------------- /test/csv/csvConfig.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /test/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: main.cpp 5 | * 6 | * Description: main 7 | * 8 | * Version: 1.0 9 | * Created: 10/11/2015 04:49:58 PM 10 | * Revision: none 11 | * Compiler: gcc 12 | * 13 | * Author: huangyun (hy), 895175589@qq.com 14 | * Organization: 15 | * 16 | * ===================================================================================== 17 | */ 18 | 19 | #include "Server.h" 20 | 21 | int main() 22 | { 23 | MyNameSpace::Server s; 24 | if (!s.init(20000)) 25 | { 26 | std::cerr<<__FUNCTION__<<"("<<__LINE__<<"): server init fail"< 23 | #include 24 | 25 | namespace MyNameSpace 26 | { 27 | struct ClientInfo 28 | { 29 | std::string ip; 30 | int id; 31 | int type; 32 | unsigned short port; 33 | }; 34 | using ClientInfoList = std::vector; 35 | } 36 | 37 | #endif 38 | --------------------------------------------------------------------------------