├── CenterServer ├── CenSerSink.cpp ├── CenSerSink.h ├── CenterServer.cpp └── CenterServer.h ├── ConntionServer ├── ConnCliSink.cpp ├── ConnCliSink.h ├── ConnSerSink.cpp ├── ConnSerSink.h ├── ConnectServer.cpp └── ConnectServer.h ├── DataBaseEnginer ├── DataBaseEnginer.cpp ├── DataBaseEnginer.h ├── DataBaseService.cpp ├── DataBaseService.h ├── DataBaseSink.cpp ├── DataBaseSink.h ├── MySql.cpp └── MySql.h ├── DataServer ├── DataBaseDef.h ├── DataCliSink.cpp ├── DataCliSink.h ├── DataMySql.cpp ├── DataMySql.h ├── DataRedis.cpp ├── DataRedis.h ├── DataSerSink.cpp ├── DataSerSink.h ├── DataServer.cpp └── DataServer.h ├── Defines.h ├── Game ├── 11001 │ ├── Logic.cpp │ ├── Logic.h │ ├── UpGradeLogic.cpp │ ├── UpGradeLogic.h │ └── UpGradeMessage.h └── BaseLogic.h ├── GameServer ├── GameCliSink.cpp ├── GameCliSink.h ├── GameSerSink.cpp ├── GameSerSink.h ├── GameServer.cpp ├── GameServer.h ├── GameUser.cpp ├── GameUser.h ├── GameUserEnginer.cpp ├── GameUserEnginer.h ├── GameUserManager.cpp ├── GameUserManager.h ├── Robot.cpp ├── Robot.h ├── RobotManager.cpp ├── RobotManager.h ├── Room.cpp ├── Room.h ├── RoomManager.cpp └── RoomManager.h ├── Makefile ├── Makefile_old ├── MemDataBaseEnginer ├── MemDataBaseEnger.cpp ├── MemDataBaseEnger.h ├── MemDataBaseService.cpp ├── MemDataBaseService.h ├── MemSink.cpp ├── MemSink.h ├── MyRedis.cpp └── MyRedis.h ├── NetHandSink.h ├── NetSinkObj.cpp ├── NetSinkObj.h ├── PacketParse.cpp ├── PacketParse.h ├── Readme.txt ├── Server.cpp ├── Server.h ├── UserInfo.cpp ├── UserInfo.h ├── UserServer ├── MemDataDef.h ├── UserCliSink.cpp ├── UserCliSink.h ├── UserRedis.cpp ├── UserRedis.h ├── UserSerSink.cpp ├── UserSerSink.h ├── UserServer.cpp └── UserServer.h ├── commproto.h ├── config └── config.ini ├── include ├── core │ ├── Core.h │ ├── IniFile.h │ ├── MemPool.h │ ├── NetSink.h │ ├── ObjectManager.h │ ├── Poller.h │ ├── Services.h │ ├── SingleObject.h │ ├── SmartPoint.h │ ├── TimerNode.h │ ├── ToolLock.h │ └── types.h ├── hiredis │ ├── adapters │ │ ├── ae.h │ │ ├── glib.h │ │ ├── ivykis.h │ │ ├── libev.h │ │ ├── libevent.h │ │ ├── libuv.h │ │ ├── macosx.h │ │ └── qt.h │ ├── async.h │ ├── hiredis.h │ ├── read.h │ └── sds.h ├── interface.h ├── json │ ├── autolink.h │ ├── config.h │ ├── features.h │ ├── forwards.h │ ├── json.h │ ├── reader.h │ ├── value.h │ └── writer.h └── mysql │ ├── decimal.h │ ├── errmsg.h │ ├── keycache.h │ ├── m_ctype.h │ ├── m_string.h │ ├── my_alloc.h │ ├── my_attribute.h │ ├── my_compiler.h │ ├── my_config.h │ ├── my_config_x86_64.h │ ├── my_dbug.h │ ├── my_dir.h │ ├── my_getopt.h │ ├── my_global.h │ ├── my_list.h │ ├── my_net.h │ ├── my_no_pthread.h │ ├── my_pthread.h │ ├── my_sys.h │ ├── my_xml.h │ ├── mysql.h │ ├── mysql_com.h │ ├── mysql_embed.h │ ├── mysql_time.h │ ├── mysql_version.h │ ├── mysqld_ername.h │ ├── mysqld_error.h │ ├── plugin.h │ ├── sql_common.h │ ├── sql_state.h │ ├── sslopt-case.h │ ├── sslopt-longopts.h │ ├── sslopt-vars.h │ └── typelib.h ├── lib └── libcore.a ├── main.cpp ├── run.sh ├── test ├── RedisTest.cpp ├── SyncService.cpp ├── SyncService.h └── test.cpp └── 服务器架构图.docx /CenterServer/CenSerSink.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "../NetHandSink.h" 4 | #include 5 | #include 6 | #include "../Defines.h" 7 | #include "../include/core/ToolLock.h" 8 | #include "../include/core/TimerNode.h" 9 | #include "../commproto.h" 10 | #include "CenterServer.h" 11 | #include "../PacketParse.h" 12 | 13 | class CCenSerSink : public CNetHandSink 14 | { 15 | public: 16 | CCenSerSink(CServices* pServices); 17 | ~CCenSerSink(); 18 | public: 19 | virtual bool HandTimeMsg(uint16 uTimeID); 20 | virtual bool HandNetData(uint16, uint16, CInputPacket& pInPack); 21 | virtual void Connect(); 22 | virtual void Close(); 23 | private: 24 | bool HandMainMsgFromUserSrv(uint16, CInputPacket& inPacket); 25 | bool HandMainMsgFromDataSrv(uint16, CInputPacket& inPacket); 26 | bool HandMainMsgFromGameSrv(uint16, CInputPacket& inPacket); 27 | bool HandMainMsgFromConnSrv(uint16, CInputPacket& inPacket); 28 | 29 | bool HandTestNetConn(); 30 | 31 | void BroadCastGameSerInfo(const GameInfo& gameInfo); 32 | void SendAllGameSerInfo(); 33 | private: 34 | CTimer m_timer_Link; 35 | uint16 m_nTestNum; 36 | 37 | }; -------------------------------------------------------------------------------- /CenterServer/CenterServer.cpp: -------------------------------------------------------------------------------- 1 | #include "CenterServer.h" 2 | #include "../include/core/IniFile.h" 3 | #include 4 | #include "CenSerSink.h" 5 | 6 | 7 | CCenterServer* g_pCenterServer = NULL; 8 | 9 | CCenterServer::CCenterServer() 10 | { 11 | g_pCenterServer = this; 12 | 13 | m_mapGameInfo.clear(); 14 | 15 | memset(s_szConnSer,0,sizeof(s_szConnSer)); 16 | memset(s_szGameSer,0,sizeof(s_szGameSer)); 17 | memset(s_szUserSer,0,sizeof(s_szUserSer)); 18 | memset(s_szDataSer,0,sizeof(s_szDataSer)); 19 | 20 | m_mapLinkInfo.clear(); 21 | } 22 | 23 | CCenterServer::~CCenterServer() 24 | { 25 | m_mapGameInfo.clear(); 26 | m_mapLinkInfo.clear(); 27 | } 28 | 29 | int CCenterServer::ReadConfig(const char* szConfigFile) 30 | { 31 | IniFile iniFile; 32 | iniFile.OpenFile(szConfigFile); 33 | 34 | m_szIp = iniFile.ReadString("centerserver", "Host", ""); 35 | m_nPort = iniFile.ReadInt("centerserver", "Port", 0); 36 | 37 | //iniFile.CloseFile(); 38 | return 0; 39 | } 40 | 41 | int CCenterServer::Initialize() 42 | { 43 | if(0 != ReadConfig("./config/config.ini")) 44 | { 45 | printf("readconfig failer!\n"); 46 | return -1; 47 | } 48 | 49 | 50 | if(0 == m_pCore->AddTcpNetSer(m_szIp.c_str(), m_nPort, CreateNetSink, false)) 51 | { 52 | printf("AddTcpNetSer Failer ip = %s, m_nPort = %d!\n",m_szIp.c_str(), m_nPort); 53 | return -1; 54 | } 55 | 56 | 57 | InitLogFile("CenterServer"); 58 | return 0; 59 | } 60 | 61 | -------------------------------------------------------------------------------- /CenterServer/CenterServer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../Defines.h" 3 | #include "../Server.h" 4 | #include 5 | #include 6 | 7 | 8 | class CCenterServer : public CServer 9 | { 10 | public: 11 | CCenterServer(); 12 | ~CCenterServer(); 13 | public: 14 | int Initialize(); 15 | private: 16 | int ReadConfig(const char* szConfigFile); 17 | private: 18 | std::string m_szIp; 19 | unsigned short m_nPort; 20 | public: 21 | std::map m_mapGameInfo; //游戏服务器号和游戏信息的对应 22 | 23 | uint16 s_szConnSer[MAX_CONN_SRV_NUM]; 24 | uint16 s_szGameSer[MAX_GAME_SRV_NUM]; 25 | uint16 s_szUserSer[MAX_USER_DATA_SRV_NUM]; 26 | uint16 s_szDataSer[MAX_USER_DATA_SRV_NUM]; 27 | 28 | std::map m_mapLinkInfo; //服务号和游戏服务器的对应 29 | }; -------------------------------------------------------------------------------- /ConntionServer/ConnCliSink.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../Defines.h" 3 | #include "../NetHandSink.h" 4 | #include "../include/core/TimerNode.h" 5 | #include "../PacketParse.h" 6 | 7 | class CServices; 8 | class ConnSucess; 9 | 10 | class CConnCliSink : public CNetHandSink 11 | { 12 | public: 13 | CConnCliSink(CServices* pNet); 14 | ~CConnCliSink(); 15 | public: 16 | void Init(const char* szIp); 17 | bool DisConnect(); 18 | void Close(); 19 | bool HandNetData(uint16, uint16, CInputPacket& inPacket); 20 | bool HandTimeMsg(uint16 uTimeID); 21 | private: 22 | bool HandMsgFromCenterSrv(uint16, CInputPacket& inPacket); 23 | bool HandMsgFromGameSrv(uint16, CInputPacket& inPacket); 24 | bool HandMsgFromUserSrv(uint16, CInputPacket& inPacket); 25 | 26 | void SendData(uint16, COutputPacket& outPacket); 27 | void RegConnSrv(); 28 | void ConnectSucess(CInputPacket& inPacket); 29 | void UploadSrvInfo(); 30 | private: 31 | uint16 m_nPeerSerType; 32 | uint16 m_nPeerSerNo; 33 | 34 | CTimer m_timer_Link; 35 | uint16 m_nTestNum; 36 | 37 | uint16 m_nReConnectCount; 38 | CTimer m_timer_reconnect; 39 | }; 40 | 41 | -------------------------------------------------------------------------------- /ConntionServer/ConnSerSink.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | 4 | #include "../NetHandSink.h" 5 | #include "../UserInfo.h" 6 | #include "../include/core/TimerNode.h" 7 | #include "../PacketParse.h" 8 | 9 | class CServices; 10 | 11 | enum SERVICE_TYPE 12 | { 13 | TYPE_GAME, 14 | TYPE_USER, 15 | }; 16 | 17 | typedef struct tagGameSerInfo 18 | { 19 | uint32 nSerNo; 20 | uint32 nGameId; 21 | }GameSerInfo; 22 | 23 | union KEY 24 | { 25 | CUserInfo* pUserInfo; 26 | GameSerInfo* pSerInfo; 27 | }; 28 | 29 | class CConnSerSink : public CNetHandSink 30 | { 31 | public: 32 | CConnSerSink(CServices* m_pNetSer); 33 | ~CConnSerSink(); 34 | public: 35 | bool HandNetData(uint16, uint16, CInputPacket& inPacket); 36 | bool HandTimeMsg(uint16 uTimeId); 37 | 38 | bool HandDataBaseRet(uint32 uType, CInputPacket& inPacket); 39 | bool HandMemDataRet(uint32 uType, CInputPacket& inPacket); 40 | 41 | bool HandUserMsg(int nEvent, void * pData, DATASIZE uDataSize); 42 | void Close(); 43 | void Init(const char* szIp); 44 | void Connect(); 45 | bool DisConnect(); 46 | private: 47 | bool TestNetLink(); 48 | 49 | bool SendToMySelf(COutputPacket& out); 50 | bool SendToGameSer(uint16, COutputPacket& out); 51 | bool SendToCenterSer(COutputPacket& out); 52 | bool SendToUserSer(uint32, COutputPacket& out); 53 | bool SendToConnectSer(COutputPacket& out); 54 | 55 | bool HandMainMsgNet(uint16, CInputPacket& inPacket); 56 | bool HandMainMsgToRoom(uint16, CInputPacket& inPacket); 57 | bool HandMainMsgToGame(uint16, uint16, CInputPacket& inPacket); 58 | bool HandMainMsgFromConnect(uint16 nSub, CInputPacket& inPacket); 59 | bool HandMainMsgToHall(uint16, uint16, CInputPacket& inPacket); 60 | private: 61 | int m_nTestLink; 62 | CTimer m_timerConnTest; 63 | const char* m_szIp; 64 | 65 | CTimer m_timer_Login; 66 | 67 | UID m_nUserId; 68 | uint16 m_nGameSrvNo; 69 | uint16 m_nGameSrvIndex; 70 | }; 71 | 72 | -------------------------------------------------------------------------------- /ConntionServer/ConnectServer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "../Server.h" 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | class CConnectServer : public CServer 10 | { 11 | public: 12 | CConnectServer(); 13 | ~CConnectServer(); 14 | public: 15 | int Initialize(); 16 | int ConnectToGameServer(const GameInfo& info); 17 | int DisconnectToServer(uint16 nSerType, uint16 nSerNo, uint16 nIndex); 18 | 19 | uint16 GetGameSerIndexByNo(uint16 nGameSrvNo,uint32 nRand); 20 | uint16 GetGameSerNoByGameId(int nGameId); 21 | uint16 GetARandGameSer(); 22 | uint16 GetCenterServerIndex() const; 23 | uint16 GetUserServerIndex(uint32 nRand) const; 24 | private: 25 | int ReadConfig(const char* szConfigFile); 26 | private: 27 | //std::vector m_vecConfigGameInfo; //存储游戏服相关配置 28 | std::vector m_vecGameConnIndex[MAX_GAME_SRV_NUM]; //存储游戏服连接按游戏服serid 29 | std::map > m_mapGameSerInfo; //GameId 与 游戏服对应 30 | std::map m_mapGameNoToGameId; //游戏服与GameId对应 31 | 32 | //对客户端开放的地址 33 | std::string m_szIp; 34 | uint16 m_nPort; 35 | 36 | //中心服地址 37 | std::string m_szCenterIp; 38 | unsigned short m_nCenterPort; 39 | uint16 m_nCenterIndex; 40 | 41 | //用户服务器数据保存 42 | std::vector m_vecUserSerConfig; 43 | std::vector m_vecUserSerIndex; 44 | }; -------------------------------------------------------------------------------- /DataBaseEnginer/DataBaseEnginer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "DataBaseEnginer.h" 3 | #include "../include/core/Services.h" 4 | #include "DataBaseService.h" 5 | #include "../include/core/Core.h" 6 | #include "../include/core/SingleObject.h" 7 | #include 8 | #include 9 | #include "../NetSinkObj.h" 10 | 11 | CDataBaseEnginer::CDataBaseEnginer() : m_nServiceNum(0), 12 | m_pIndex(NULL) 13 | { 14 | 15 | } 16 | 17 | CDataBaseEnginer::~CDataBaseEnginer() 18 | { 19 | SAFE_DELTEARRAY(m_pIndex); 20 | } 21 | 22 | void CDataBaseEnginer::SetServiceNum(int nNum) 23 | { 24 | if(nNum <= 0) 25 | return; 26 | 27 | m_nServiceNum = nNum; 28 | m_pIndex = new SERVICEINDEX[m_nServiceNum]; 29 | 30 | for(int i = 0; i < m_nServiceNum; i++) 31 | { 32 | CDataBaseService* pService = new CDataBaseService; 33 | if (!pService) 34 | { 35 | exit(0); 36 | } 37 | 38 | if(!CSingleObject::Instance()->AddService(pService)) 39 | { 40 | exit(0); 41 | } 42 | 43 | if(!pService->Init()) 44 | { 45 | printf("DataBaseService Init Failer\n"); 46 | exit(0); 47 | } 48 | m_pIndex[i] = pService->GetServiceIndex(); 49 | } 50 | } 51 | 52 | SERVICEINDEX CDataBaseEnginer::GetIndex(int nRand) 53 | { 54 | if(m_nServiceNum == 0) 55 | return 0; 56 | 57 | return m_pIndex[nRand%m_nServiceNum]; 58 | } 59 | 60 | -------------------------------------------------------------------------------- /DataBaseEnginer/DataBaseEnginer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../Defines.h" 3 | #include 4 | 5 | class CServices; 6 | 7 | class CDataBaseEnginer 8 | { 9 | public: 10 | CDataBaseEnginer(); 11 | ~CDataBaseEnginer(); 12 | public: 13 | void SetServiceNum(int nNum); 14 | SERVICEINDEX GetIndex(int nRand); 15 | private: 16 | int m_nServiceNum; 17 | SERVICEINDEX* m_pIndex; 18 | }; 19 | 20 | -------------------------------------------------------------------------------- /DataBaseEnginer/DataBaseService.cpp: -------------------------------------------------------------------------------- 1 | #include "DataBaseService.h" 2 | #include "DataBaseSink.h" 3 | #include 4 | CDataBaseService::CDataBaseService() : m_pSink(NULL) 5 | { 6 | m_pSink = new CDataBaseSink(this); 7 | } 8 | CDataBaseService::~CDataBaseService() 9 | { 10 | SAFE_DELTE(m_pSink) 11 | } 12 | 13 | bool CDataBaseService::Init() 14 | { 15 | return m_pSink->Init(); 16 | } 17 | 18 | 19 | bool CDataBaseService::HandData(int e, SERVICEINDEX uFromSerId, void *pData, DATASIZE size) 20 | { 21 | switch (e) 22 | { 23 | case TIME_MSG: 24 | { 25 | uint32 nTimeId = *(uint32*)pData; 26 | return m_pSink->HandTimeMsg(nTimeId); 27 | } 28 | case DATA_BASE_REQ: 29 | { 30 | DataCenter* pCenter = (DataCenter*)pData; 31 | uint32* pType = (uint32*)(pCenter+1); 32 | return m_pSink->HandDataBaseReq(uFromSerId,pCenter->nCsid,*pType,pType+1,size-sizeof(uint32)-sizeof(DataCenter)); 33 | } 34 | default: 35 | break; 36 | } 37 | return true; 38 | } 39 | -------------------------------------------------------------------------------- /DataBaseEnginer/DataBaseService.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../include/core/Services.h" 3 | 4 | class CDataBaseSink; 5 | 6 | class CDataBaseService : 7 | public CServices 8 | { 9 | friend class CDataBaseEnginer; 10 | CDataBaseSink* m_pSink; 11 | public: 12 | bool Init(); 13 | bool HandData(int e, SERVICEINDEX uFromSerId, void *pData, DATASIZE size); 14 | private: 15 | CDataBaseService(); 16 | ~CDataBaseService(); 17 | }; -------------------------------------------------------------------------------- /DataBaseEnginer/DataBaseSink.cpp: -------------------------------------------------------------------------------- 1 | #include "DataBaseSink.h" 2 | #include 3 | #include "../commproto.h" 4 | #include "../include/core/Services.h" 5 | #include "DataBaseEnginer.h" 6 | #include "../DataServer/DataMySql.h" 7 | 8 | 9 | CDataBaseSink::CDataBaseSink(CServices * pService) 10 | { 11 | m_pDataBase = new CDataMySql(pService); 12 | } 13 | 14 | bool CDataBaseSink::Init() 15 | { 16 | if(!m_pDataBase->InitConnection()) 17 | { 18 | printf("m_pDataBase->Init() false\n"); 19 | return false; 20 | } 21 | return true; 22 | } 23 | 24 | CDataBaseSink::~CDataBaseSink() 25 | { 26 | SAFE_DELTE(m_pDataBase); 27 | } 28 | 29 | bool CDataBaseSink::HandDataBaseReq(SERVICEINDEX nFromIndex,SERVICEINDEX nCsid, uint32 nType,void* pData,DATASIZE nDataSize) 30 | { 31 | return m_pDataBase->Exec(nFromIndex, nCsid, nType, pData, nDataSize); 32 | } 33 | 34 | bool CDataBaseSink::HandTimeMsg(TIMEERID nTimeId) 35 | { 36 | return true; 37 | } 38 | -------------------------------------------------------------------------------- /DataBaseEnginer/DataBaseSink.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../Defines.h" 3 | 4 | class CMySql; 5 | class CServices; 6 | 7 | class CDataBaseSink 8 | { 9 | public: 10 | CDataBaseSink(CServices* pDataBaseService); 11 | ~CDataBaseSink(); 12 | public: 13 | bool Init(); 14 | bool HandDataBaseReq(SERVICEINDEX nFromIndex,SERVICEINDEX nCsid,uint32 nType,void* pData,DATASIZE nDataSize); 15 | bool HandTimeMsg(TIMEERID nTimeId); 16 | private: 17 | CMySql* m_pDataBase; 18 | }; -------------------------------------------------------------------------------- /DataBaseEnginer/MySql.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "../include/mysql/mysql.h" 4 | #include "../Defines.h" 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | 11 | class CServices; 12 | 13 | struct DBConfig 14 | { 15 | std::string szHost; 16 | unsigned short nPort; 17 | std::string szDbName; 18 | std::string szDbUser; 19 | std::string szDbPass; 20 | 21 | DBConfig() 22 | { 23 | szHost = ""; 24 | nPort = 0; 25 | szDbName = ""; 26 | szDbUser = ""; 27 | szDbPass = ""; 28 | } 29 | }; 30 | 31 | using namespace std; 32 | 33 | class CMySql 34 | { 35 | public: 36 | CMySql(CServices* pService); 37 | virtual ~CMySql(); 38 | public: 39 | bool InitConnection(); 40 | virtual int Exec(SERVICEINDEX nIndex, SERVICEINDEX nCsid ,uint32 nType,void* pData,uint32 nDataSize)=0; 41 | protected: 42 | virtual int GetDBConfig()=0; 43 | virtual bool Connected()=0; 44 | protected: 45 | bool OpenConnect(); 46 | void CloseConnect(); 47 | 48 | void SetSpName(const char* szSpName); 49 | bool ExecPro(); 50 | bool ExecSql(const char* szSql); 51 | 52 | bool HaveNext(); 53 | void MoveNext(); 54 | 55 | long long GetNumValue(const char* szFild,long long nDef = 0); 56 | const char* GetStrValue(const char* szFild,const char* szDef = NULL); 57 | long long GetOutNumValue(const char* szStrFild,long long nDef = 0); 58 | const char* GetOutStrValue(const char* szFild,const char* szDef = NULL); 59 | 60 | void AddNumParam(long long llValue); 61 | void AddStrParam(const char* szValue); 62 | void AddOutParam(const char* szOutParam); 63 | void Clean(); 64 | 65 | void AddStr(const string& szParam); 66 | bool ExecQuery(const string& szSql1); 67 | bool ExecSingleQuery(string& szSql); 68 | bool GetSetResult(); 69 | bool GetSingleResult(); 70 | const char* GetValue(const char* szFild); 71 | const char* GetOutValue(const char* szFild); 72 | protected: 73 | DBConfig m_dbConfig; 74 | CServices* const m_pService; 75 | MYSQL *m_pMySql; 76 | 77 | string m_szSql; 78 | string m_szOutSql; 79 | int m_nInParam; 80 | int m_nOutParam; 81 | 82 | map m_mapFildname; 83 | vector > m_vecRes; 84 | map m_mapRes; 85 | vector >::iterator m_it; 86 | }; 87 | 88 | -------------------------------------------------------------------------------- /DataServer/DataBaseDef.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | namespace DataBase 3 | { 4 | enum TYPE_DATABASE_REQ 5 | { 6 | USER_LOGIN_REQ = 1, 7 | }; 8 | 9 | enum TYPE_DATABASE_RET 10 | { 11 | USER_LOGIN_RET = 1 12 | }; 13 | 14 | struct UserLoginReq 15 | { 16 | int nUserId; 17 | char szPass[16]; 18 | UserLoginReq() 19 | { 20 | memset(this, 0, sizeof(UserLoginReq)); 21 | } 22 | }; 23 | 24 | struct UserLoginRet 25 | { 26 | int ret; 27 | int nUserId; 28 | char nSex; 29 | char szName[64]; 30 | char szHeadUrl[128]; 31 | UserLoginRet() 32 | { 33 | memset(this, 0, sizeof(UserLoginRet)); 34 | } 35 | }; 36 | } -------------------------------------------------------------------------------- /DataServer/DataCliSink.cpp: -------------------------------------------------------------------------------- 1 | #include "DataCliSink.h" 2 | #include "../Defines.h" 3 | #include "../NetSinkObj.h" 4 | #include "../include/core/Services.h" 5 | #include "../commproto.h" 6 | 7 | extern CDataServer* g_pDataServer; 8 | 9 | enum TIME_ID 10 | { 11 | TIME_TEST_CONN=1, 12 | TIME_RECONNECT, 13 | }; 14 | 15 | 16 | CDataCliSink::CDataCliSink(CServices* pNetSer):CNetHandSink(pNetSer) 17 | { 18 | m_timer_Link.InitTimerObj(m_pNet, TIME_TEST_CONN); 19 | m_timer_reconnect.InitTimerObj(m_pNet, TIME_RECONNECT); 20 | } 21 | 22 | CDataCliSink::~CDataCliSink() 23 | { 24 | 25 | } 26 | 27 | 28 | bool CDataCliSink::HandTimeMsg(uint16 nTimeID) 29 | { 30 | switch(nTimeID) 31 | { 32 | case TIME_TEST_CONN: 33 | { 34 | ++m_nTestNum; 35 | if(m_nTestNum > 1) 36 | return false; 37 | 38 | COutputPacket out; 39 | out.Begin(MAIN_MSG_DATASER, DS_SUB_MSG_TEST); 40 | out.End(); 41 | CNetSinkObj::SendData(m_pNet, m_pNet->GetServiceIndex(), out); 42 | m_timer_Link.StartTimerSec(CLIENT_TEST_TIME); 43 | return true; 44 | } 45 | case TIME_RECONNECT: 46 | { 47 | ++m_nReConnectCount; 48 | if(m_nReConnectCount >= CLIENT_RECONN_NUMS) 49 | { 50 | m_pNet->Log("m_nReConnectCount = %d", m_nReConnectCount); 51 | return false; 52 | } 53 | m_pNet->PostData(m_pNet->GetServiceIndex(), NET_RECONNECT); 54 | return true; 55 | } 56 | } 57 | return false; 58 | } 59 | 60 | void CDataCliSink::SendMsgToCenterSrv(COutputPacket& out) 61 | { 62 | CNetSinkObj::SendData(m_pNet, g_pDataServer->GetCenterIndex(), out); 63 | } 64 | 65 | 66 | bool CDataCliSink::HandNetData(uint16 nMain, uint16 nSub, CInputPacket& inPacket) 67 | { 68 | m_pNet->Log("CLient Recv cmd %d, %d", nMain, nSub); 69 | switch(nMain) 70 | { 71 | case MAIN_MSG_CENTERSER: 72 | { 73 | return HandMainMsgFromCenterSrv(nSub, inPacket); 74 | } 75 | default: 76 | break; 77 | } 78 | return false; 79 | } 80 | 81 | bool CDataCliSink::DisConnect() 82 | { 83 | m_timer_Link.StopTimer(); 84 | m_timer_reconnect.StartTimerSec(CLIENT_RECONN_TIME); 85 | return true; 86 | } 87 | 88 | void CDataCliSink::RegDataSrv() 89 | { 90 | COutputPacket out; 91 | out.Begin(MAIN_MSG_DATASER, DS_SUB_MSG_REG_DATASRV); 92 | out.WriteInt16(g_pDataServer->GetSerNo()); 93 | out.End(); 94 | SendMsgToCenterSrv(out); 95 | } 96 | 97 | 98 | bool CDataCliSink::HandMainMsgFromCenterSrv(uint16 nSub, CInputPacket& inPacket) 99 | { 100 | switch(nSub) 101 | { 102 | case CT_SUB_MSG_TEST: 103 | { 104 | m_nTestNum = 0; 105 | return true; 106 | } 107 | case CT_SUB_MSG_CONN_SUCSS: 108 | { 109 | m_nTestNum = 0; 110 | m_nReConnectCount = 0; 111 | m_timer_reconnect.StopTimer(); 112 | m_timer_Link.StartTimerSec(CLIENT_TEST_TIME); 113 | RegDataSrv(); 114 | return true; 115 | } 116 | default: 117 | break; 118 | } 119 | return false; 120 | } 121 | 122 | -------------------------------------------------------------------------------- /DataServer/DataCliSink.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../NetHandSink.h" 3 | #include "DataServer.h" 4 | #include "../include/core/TimerNode.h" 5 | #include "../PacketParse.h" 6 | 7 | class CServices; 8 | 9 | class CDataCliSink : public CNetHandSink 10 | { 11 | public: 12 | CDataCliSink(CServices* m_pNetSer); 13 | ~CDataCliSink(); 14 | public: 15 | virtual bool HandTimeMsg(uint16 nTimeID); 16 | virtual bool HandNetData(uint16, uint16, CInputPacket& inPacket); 17 | virtual bool DisConnect(); 18 | private: 19 | void SendMsgToCenterSrv(COutputPacket& out); 20 | bool HandMainMsgFromCenterSrv( uint16, CInputPacket& inPacket); 21 | void RegDataSrv(); 22 | private: 23 | CTimer m_timer_Link; 24 | uint16 m_nTestNum; 25 | 26 | uint16 m_nReConnectCount; 27 | CTimer m_timer_reconnect; 28 | }; 29 | 30 | 31 | -------------------------------------------------------------------------------- /DataServer/DataMySql.cpp: -------------------------------------------------------------------------------- 1 | #include "DataMySql.h" 2 | #include "DataServer.h" 3 | #include "DataBaseDef.h" 4 | 5 | extern CDataServer* g_pDataServer; 6 | 7 | using namespace DataBase; 8 | 9 | CDataMySql::CDataMySql(CServices* pService):CMySql(pService) 10 | { 11 | 12 | } 13 | 14 | CDataMySql::~CDataMySql() 15 | { 16 | 17 | } 18 | 19 | int CDataMySql::GetDBConfig() 20 | { 21 | m_dbConfig.szHost = g_pDataServer->GetDbHost(); 22 | m_dbConfig.szDbName = g_pDataServer->GetDbName(); 23 | m_dbConfig.szDbUser = g_pDataServer->GetDbUserName(); 24 | m_dbConfig.szDbPass = g_pDataServer->GetDbPass(); 25 | m_dbConfig.nPort = g_pDataServer->GetDbPort(); 26 | return 0; 27 | 28 | /*ifstream in("./config/mysql.config"); 29 | if(in.is_open()) 30 | { 31 | char szConfig[64] = { 0 }; 32 | while (in.getline(szConfig, 64)) 33 | { 34 | char *szKey = strtok(szConfig, ":"); 35 | char *szValue = strtok(NULL, ":"); 36 | 37 | if(!szKey) 38 | continue; 39 | if(!strcmp(szKey,"Host")) 40 | strcpy(m_dbConfig.szHost,szValue); 41 | else if(!strcmp(szKey,"Port")) 42 | m_dbConfig.nPort = atoi(szValue); 43 | else if(!strcmp(szKey,"DbName")) 44 | strcpy((char*)m_dbConfig.szDbName,szValue); 45 | else if(!strcmp(szKey,"User")) 46 | strcpy((char*)m_dbConfig.szUser,szValue); 47 | else if(!strcmp(szKey,"Pass")) 48 | strcpy((char*)m_dbConfig.szPass,szValue); 49 | } 50 | in.close(); 51 | } 52 | else 53 | { 54 | printf("mysql.config not found\n"); 55 | return false; 56 | } 57 | return true;*/ 58 | } 59 | 60 | bool CDataMySql::Connected() 61 | { 62 | return true; 63 | } 64 | 65 | 66 | int CDataMySql::Exec(SERVICEINDEX nIndex, SERVICEINDEX nCsid ,uint32 nType,void* pData,uint32 nDataSize) 67 | { 68 | switch(nType) 69 | { 70 | case DataBase::USER_LOGIN_REQ: 71 | { 72 | UserLogin(nIndex,nCsid,pData,nDataSize); 73 | break; 74 | } 75 | default: 76 | break; 77 | } 78 | return true; 79 | } 80 | 81 | 82 | void CDataMySql::UserLogin(uint16 nIndex,SERVICEINDEX nCsid,void* pData,uint16 nDataSize) 83 | { 84 | if(nDataSize != sizeof(DataBase::UserLoginReq)) 85 | return; 86 | 87 | DataBase::UserLoginReq* pLogin = (DataBase::UserLoginReq*)pData; 88 | SetSpName("UserLogin"); 89 | AddNumParam(pLogin->nUserId); 90 | AddStrParam(pLogin->szPass); 91 | AddOutParam("@ret"); 92 | ExecPro(); 93 | 94 | int ret = (int)GetOutNumValue("@ret"); 95 | DataBase::UserLoginRet LoginRet; 96 | LoginRet.ret = ret; 97 | LoginRet.nUserId = pLogin->nUserId; 98 | LoginRet.nSex = 1; 99 | strcpy(LoginRet.szName,"LiLei"); 100 | strcpy(LoginRet.szHeadUrl,"szHeadUrl"); 101 | g_pDataServer->PostDataBaseRet(m_pService, nIndex, nCsid, DataBase::USER_LOGIN_RET, &LoginRet,sizeof(DataBase::UserLoginRet)); 102 | } 103 | 104 | -------------------------------------------------------------------------------- /DataServer/DataMySql.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../DataBaseEnginer/MySql.h" 3 | 4 | using namespace std; 5 | class CServices; 6 | 7 | class CDataMySql : public CMySql 8 | { 9 | public: 10 | CDataMySql(CServices* pService); 11 | ~CDataMySql(); 12 | public: 13 | virtual int Exec(SERVICEINDEX nIndex, SERVICEINDEX nCsid ,uint32 nType,void* pData,uint32 nDataSize); 14 | protected: 15 | virtual int GetDBConfig(); 16 | virtual bool Connected(); 17 | private: 18 | void UserLogin(uint16 nIndex,SERVICEINDEX nCsid,void* pData,uint16 nDataSize); 19 | }; 20 | -------------------------------------------------------------------------------- /DataServer/DataRedis.cpp: -------------------------------------------------------------------------------- 1 | #include "DataRedis.h" 2 | #include "DataServer.h" 3 | 4 | extern CDataServer* g_pDataServer; 5 | 6 | CDataRedis::CDataRedis(CServices* pService):CRedis(pService) 7 | { 8 | 9 | } 10 | 11 | CDataRedis::~CDataRedis() 12 | { 13 | 14 | } 15 | 16 | int CDataRedis::GetRedisConfig() 17 | { 18 | m_dbConfig.szHost = g_pDataServer->GetRedisHost(); 19 | m_dbConfig.nPort = g_pDataServer->GetRedisPort(); 20 | m_dbConfig.szAuth = g_pDataServer->GetRedisAuth(); 21 | return 0; 22 | } 23 | 24 | void CDataRedis::RegRedisScript(int nType,const char* szStr) 25 | { 26 | redisReply* reply = (redisReply*)redisCommand(m_pConn, "SCRIPT LOAD %s", szStr); 27 | m_RedisPro[nType] = new char[reply->len+1]; 28 | memcpy(m_RedisPro[nType], reply->str, reply->len); 29 | m_RedisPro[nType][reply->len] = 0; 30 | freeReplyObject(reply); 31 | } 32 | 33 | bool CDataRedis::Connected() 34 | { 35 | return true; 36 | } 37 | 38 | int CDataRedis::Exec(SERVICEINDEX nSrcIndex,SERVICEINDEX nCsid,uint32 nType,void* pData,DATASIZE nDataSize) 39 | { 40 | return 0; 41 | } 42 | 43 | -------------------------------------------------------------------------------- /DataServer/DataRedis.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "../MemDataBaseEnginer/MyRedis.h" 4 | #include "../Defines.h" 5 | 6 | class CDataRedis : public CRedis 7 | { 8 | public: 9 | CDataRedis(CServices* pService); 10 | ~CDataRedis(); 11 | protected: 12 | virtual int GetRedisConfig(); 13 | virtual bool Connected(); 14 | virtual int Exec(SERVICEINDEX nSrcIndex,SERVICEINDEX nCsid,uint32 nType,void* pData,DATASIZE nDataSize); 15 | private: 16 | void RegRedisScript(int nType,const char* szStr); 17 | private: 18 | char* m_RedisPro[5]; 19 | }; -------------------------------------------------------------------------------- /DataServer/DataSerSink.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "DataSerSink.h" 3 | #include "../include/core/Core.h" 4 | #include "../include/core/ToolLock.h" 5 | #include "../commproto.h" 6 | #include "../include/core/Services.h" 7 | #include "DataBaseDef.h" 8 | #include "../NetSinkObj.h" 9 | #include 10 | #include 11 | #include 12 | #include "../DataBaseEnginer/DataBaseEnginer.h" 13 | #include "../MemDataBaseEnginer/MemDataBaseEnger.h" 14 | #include "DataServer.h" 15 | 16 | extern CDataServer* g_pDataServer; 17 | 18 | enum TIME_ID 19 | { 20 | TIME_TEST_LINK = 1, 21 | }; 22 | 23 | CDataSerSink::CDataSerSink(CServices* pNet) :CNetHandSink(pNet),m_nTestNum(0) 24 | { 25 | m_timer_Link.InitTimerObj(m_pNet, TIME_TEST_LINK); 26 | } 27 | 28 | 29 | CDataSerSink::~CDataSerSink() 30 | { 31 | 32 | } 33 | 34 | void CDataSerSink::Connect() 35 | { 36 | COutputPacket out; 37 | out.Begin(MAIN_MSG_DATASER, DS_SUB_MSG_CONN_SUCSS); 38 | out.WriteInt16(g_pDataServer->GetSerType()); 39 | out.WriteInt16(g_pDataServer->GetSerNo()); 40 | out.End(); 41 | CNetSinkObj::SendData(m_pNet, m_pNet->GetServiceIndex(), out); 42 | } 43 | 44 | bool CDataSerSink::HandNetData(uint16 nMain, uint16 nSub, CInputPacket& inPacket) 45 | { 46 | switch (nMain) 47 | { 48 | case MAIN_MSG_GAMESER: 49 | { 50 | return HandMainMsgFromGameSer(nSub,inPacket); 51 | } 52 | default: 53 | break; 54 | } 55 | return false; 56 | } 57 | 58 | bool CDataSerSink::HandTimeMsg(uint16 uTimeId) 59 | { 60 | switch (uTimeId) 61 | { 62 | case TIME_TEST_LINK: 63 | { 64 | return TestNetLink(); 65 | } 66 | default: 67 | break; 68 | } 69 | return true; 70 | } 71 | 72 | bool CDataSerSink::TestNetLink() 73 | { 74 | ++m_nTestNum; 75 | if(m_nTestNum > 1) 76 | { 77 | m_pNet->Log("test link timeout!"); 78 | return false; 79 | } 80 | m_timer_Link.StartTimerSec(SERVER_TEST_TIME); 81 | return true; 82 | } 83 | 84 | bool CDataSerSink::HandMainMsgFromGameSer(uint16 nSub, CInputPacket& inPacket) 85 | { 86 | switch (nSub) 87 | { 88 | case GS_SUB_MSG_TEST: 89 | { 90 | HandTestNetConn(); 91 | return true; 92 | } 93 | case GS_SUB_MSG_REG_GAMESRV: 94 | { 95 | HandTestNetConn(); 96 | m_timer_Link.StartTimerSec(SERVER_TEST_TIME); 97 | return true; 98 | } 99 | default: 100 | return false; 101 | } 102 | return true; 103 | } 104 | 105 | bool CDataSerSink::HandTestNetConn() 106 | { 107 | m_nTestNum = 0; 108 | COutputPacket out; 109 | out.Begin(MAIN_MSG_DATASER,DS_SUB_MSG_TEST); 110 | out.End(); 111 | CNetSinkObj::SendData(m_pNet, m_pNet->GetServiceIndex(), out); 112 | return true; 113 | } 114 | 115 | 116 | -------------------------------------------------------------------------------- /DataServer/DataSerSink.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../NetHandSink.h" 3 | #include "../MemDataBaseEnginer/MemDataBaseEnger.h" 4 | #include "DataServer.h" 5 | #include "../include/core/TimerNode.h" 6 | #include "../PacketParse.h" 7 | 8 | class CServices; 9 | 10 | class CDataSerSink : public CNetHandSink 11 | { 12 | public: 13 | CDataSerSink(CServices* m_pNetSer); 14 | ~CDataSerSink(); 15 | public: 16 | void Connect(); 17 | bool HandNetData(uint16, uint16, CInputPacket& inPacket); 18 | bool HandTimeMsg(uint16 uTimeId); 19 | private: 20 | bool TestNetLink(); 21 | bool HandMainMsgFromGameSer(uint16,CInputPacket& inPacket); 22 | private: 23 | bool HandTestNetConn(); 24 | private: 25 | CTimer m_timer_Link; 26 | uint16 m_nTestNum; 27 | }; -------------------------------------------------------------------------------- /DataServer/DataServer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "../Server.h" 4 | #include 5 | 6 | class CMemDataBaseEnginer; 7 | class CDataBaseEnginer; 8 | class CServices; 9 | 10 | class CDataServer : public CServer 11 | { 12 | public: 13 | CDataServer(); 14 | ~CDataServer(); 15 | public: 16 | int Initialize(); 17 | public: 18 | const char* GetRedisHost() const; 19 | const unsigned short GetRedisPort() const; 20 | const char* GetRedisAuth() const; 21 | 22 | const char* GetDbHost() const; 23 | const unsigned short GetDbPort() const; 24 | const char* GetDbName() const; 25 | const char* GetDbUserName() const; 26 | const char* GetDbPass() const; 27 | 28 | SERVICEINDEX GetCenterIndex() const; 29 | 30 | bool PostMemDataBaseReq(CServices* pServices,void* pData, DATASIZE uDataSize); 31 | bool PostMemDataBaseRet(CServices* pServices,SERVICEINDEX nToSerId,SERVICEINDEX nCsid, uint32 nTypeId, void* pData, DATASIZE nDataSize); 32 | 33 | bool PostDataBaseReq(CServices* pServices,void* pData, DATASIZE uDataSize); 34 | bool PostDataBaseRet(CServices* pServices,SERVICEINDEX nToSerId,SERVICEINDEX nCsid, uint32 uTypeId, void* pData, DATASIZE uDataSize); 35 | private: 36 | int ReadConfig(const char* szConfigFile); 37 | 38 | private: 39 | std::string m_szIp; 40 | unsigned short m_nPort; 41 | 42 | std::string m_szCenterIp; 43 | unsigned short m_nCenterPort; 44 | SERVICEINDEX m_nCenterIndex; 45 | 46 | std::string m_szRedisHost; 47 | unsigned short m_nRedisPort; 48 | std::string m_szAuth; 49 | 50 | std::string m_szDbHost; 51 | uint16 m_nDbPort; 52 | std::string m_szDbName; 53 | std::string m_szDbUserName; 54 | std::string m_szDbPass; 55 | 56 | CMemDataBaseEnginer* m_pMem; 57 | CDataBaseEnginer* m_pData; 58 | }; -------------------------------------------------------------------------------- /Defines.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "./include/core/types.h" 4 | #include 5 | #include 6 | 7 | 8 | #define MAX_MSG_SIZE 65536 9 | 10 | #define CLIENT_TEST_TIME 60 11 | #define SERVER_TEST_TIME 65 12 | 13 | #define CLIENT_RECONN_TIME 30 14 | #define CLIENT_RECONN_NUMS 5 15 | 16 | #define MAX_USER_DATA_SRV_NUM 16 17 | #define MAX_CONN_SRV_NUM 600 18 | #define MAX_GAME_SRV_NUM 600 19 | #define MAX_CONN_TO_GAME_LINK 23 20 | 21 | enum USER_EVENT 22 | { 23 | USER_MSG = MAX_SYS_MSG+1, //用户消息 24 | USER_NET_MSG, //玩家网络消息 25 | DATA_BASE_REQ, //数据库请求 26 | DATA_BASE_RET, //数据库结果 27 | MEM_DATA_BASE_REQ, //缓存请求 28 | MEM_DATA_BASE_RET, //缓存结果 29 | EN_DOUBLE_LOGIN, 30 | EN_SYNC_GAME_SER_INFO, 31 | EN_RECONNECT_FAIL, 32 | 33 | EV_NEW_ROOM, 34 | EV_JOIN_ROOM, 35 | 36 | MAX_MSG 37 | }; 38 | 39 | 40 | 41 | enum SERVER_TYPE 42 | { 43 | SRV_TYPE_CENTER = 1, 44 | SRV_TYPE_USER, 45 | SRV_TYPE_DATA, 46 | SRV_TYPE_GAME, 47 | SRV_TYPE_CONN, 48 | }; 49 | 50 | enum MAIN_MSG 51 | { 52 | MAIN_MSG_CENTERSER = 1, 53 | MAIN_MSG_USERSER, 54 | MAIN_MSG_DATASER, 55 | MAIN_MSG_GAMESER, 56 | MAIN_MSG_CONNSER, 57 | MAIN_MSG_HALL, 58 | MAIN_MSG_ROOM, 59 | MAIN_MSG_GAME, 60 | MAIN_MSG_MAX, 61 | }; 62 | 63 | enum Sub_Msg_CenterSer 64 | { 65 | CT_SUB_MSG_CONN_SUCSS = 1, 66 | CT_SUB_MSG_TEST, 67 | CT_SUB_MSG_NEWGAMESER, 68 | }; 69 | 70 | enum Sub_Msg_UserSer 71 | { 72 | US_SUB_MSG_CONN_SUCSS = 1, 73 | US_SUB_MSG_TEST, 74 | US_SUB_MSG_REGUSERSRV, 75 | US_SUB_MSG_MEM_BASE_RET, 76 | }; 77 | 78 | enum Sub_Msg_DataSer 79 | { 80 | DS_SUB_MSG_CONN_SUCSS = 1, 81 | DS_SUB_MSG_TEST, 82 | DS_SUB_MSG_REG_DATASRV, 83 | DS_SUB_MSG_MEM_BASE_RET, 84 | DS_SUB_MSG_DATA_BASE_RET, 85 | }; 86 | 87 | enum Sub_Msg_GameSer 88 | { 89 | GS_SUB_MSG_CONN_SUCSS = 1, 90 | GS_SUB_MSG_TEST, 91 | GS_SUB_MSG_REG_GAMESRV, 92 | GS_SUB_MSG_GAME2USER, 93 | GS_SUB_MSG_GAME2CONN, 94 | }; 95 | 96 | enum Sub_Msg_ConnSer 97 | { 98 | CS_SUB_MSG_CONN_SUCSS = 1, 99 | CS_SUB_MSG_TEST, 100 | CS_SUB_MSG_REG_CONN, 101 | CS_SUB_MSG_USER_LOGIN_HALL, 102 | CS_SUB_MSG_USER_CLOSE, 103 | CS_SUB_MSG_USERLOGIN, 104 | CS_SUB_MSG_USER4HALL, 105 | CS_SUB_MSG_USER4ROOM, 106 | CS_SUB_MSG_USER2ROOM, 107 | CS_SUB_MSG_USER2GAME, 108 | CS_SUB_MSG_UPLOADINFO, 109 | CS_SUB_MSG_USER, 110 | }; 111 | 112 | enum Sub_Msg_Room 113 | { 114 | SUB_MSG_CREATE = 1, 115 | SUB_MSG_JOIN, 116 | SUB_MSG_CREATE_FAIL, 117 | SUB_MSG_ENTER, 118 | SUB_MSG_JOIN_FAIL, 119 | SUB_MSG_GAME4USER, 120 | SUB_MSG_USER_RECONNECT, 121 | SUB_MSG_USER_DISCONNECT, 122 | SUB_MSG_USER_READY, 123 | SUB_MSG_ROOMINFO, 124 | SUB_MSG_USER4GAME, 125 | SUB_MSG_LOGIN, 126 | SUB_MSG_USER_DOUBLELOGIN, 127 | SUB_MSG_USER_RELOGIN, 128 | SUB_MSG_DOUBLE_LOGIN, 129 | SUB_MSG_USER4ROOM, 130 | SUB_MSG_TEST, 131 | }; 132 | 133 | typedef int32 UID; 134 | 135 | typedef struct tagUser2Room 136 | { 137 | uint16 nIndex; 138 | uint16 nMain; 139 | uint16 nSub; 140 | }User2Room; 141 | 142 | typedef struct tagUser2Game 143 | { 144 | uint16 nIndex; 145 | uint16 nMain; 146 | uint16 nSub; 147 | int nSeatNo; 148 | }User2Game; 149 | 150 | typedef struct tagGame2User 151 | { 152 | uint16 nIndex; 153 | uint16 nMain; 154 | uint16 nSub; 155 | }Game2User; 156 | 157 | typedef struct tagDataCenter 158 | { 159 | SERVICEINDEX nCsid; 160 | }DataCenter; 161 | 162 | typedef struct tagUser 163 | { 164 | UID iUserId; 165 | char szName[32]; 166 | char bSex; 167 | char szHeadUrl[128]; 168 | unsigned int nDiamond; 169 | unsigned int nJinBi; 170 | unsigned int nSockIndex; 171 | int nConnSer; 172 | int nGameSer; 173 | int nRoomId; 174 | uint16 uSeatId; 175 | 176 | tagUser() 177 | { 178 | memset(this, 0, sizeof(tagUser)); 179 | } 180 | } Users; 181 | 182 | 183 | struct HostConfig 184 | { 185 | std::string szIp; 186 | uint16 nPort; 187 | }; 188 | 189 | struct GameInfo : public HostConfig 190 | { 191 | int nGameId; 192 | int nGameNo; 193 | }; 194 | -------------------------------------------------------------------------------- /Game/11001/Logic.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../BaseLogic.h" 3 | #include "../../Defines.h" 4 | #include "../../include/core/SmartPoint.h" 5 | #include "../../include/core/TimerNode.h" 6 | 7 | class CLogic : 8 | public CBaseLogic 9 | { 10 | CSmartArrayPoint m_pCards; 11 | CSmartArrayPoint m_bShowCards; 12 | CSmartArrayPoint m_pBank; 13 | CSmartArrayPoint m_pBets; 14 | CSmartArrayPoint m_pWin; 15 | CSmartArrayPoint m_pCardType; 16 | CSmartArrayPoint m_pMaxCard; 17 | 18 | int m_nBank; 19 | int m_nBet; 20 | int m_nGameState; 21 | int m_nGameStateTime; 22 | int m_nUserCount; 23 | 24 | CTimer m_timerReady; 25 | CTimer m_timerBanker; 26 | CTimer m_timerRestart; 27 | CTimer m_timerShowBanker; 28 | CTimer m_timerBet; 29 | CTimer m_timerDispatchCard; 30 | CTimer m_timerShowCard; 31 | CTimer m_timerCompare; 32 | public: 33 | CLogic(CRoom* pRoom); 34 | ~CLogic(); 35 | public: 36 | bool InitGameStation(void* pData, uint16 nDataSize); 37 | bool GetGameStation(uint16 uSeatNo); 38 | bool HandNetMsg(uint16 uSeatNo, uint16 uSub, CInputPacket& inPacket); 39 | bool HandTimeMsg(uint16 uTimeId); 40 | bool GameBegin(); 41 | void GameOver(); 42 | private: 43 | void RobBank(); 44 | void BeginBet(); 45 | void DispatchCard(); 46 | void CompareCard(); 47 | void UpdateGameState(int nState); 48 | 49 | void UserBet(uint16 nSeatNo,int nBet); 50 | void UserRobBank(uint16 nSeatNo,int nRob); 51 | void UserShowCard(uint16 nSeatNo); 52 | 53 | void ConfirmBank(); 54 | void EndReady(); 55 | void EndBanker(); 56 | void EndBet(); 57 | void EndShowCard(); 58 | void EndDispatchCard(); 59 | }; -------------------------------------------------------------------------------- /Game/11001/UpGradeLogic.cpp: -------------------------------------------------------------------------------- 1 | #include "UpGradeLogic.h" 2 | #include 3 | 4 | CUpGradeGameLogic::CUpGradeGameLogic() 5 | { 6 | } 7 | 8 | CUpGradeGameLogic::~CUpGradeGameLogic() 9 | { 10 | } 11 | 12 | int CUpGradeGameLogic::GetCardNum(uint8 nCard) 13 | { 14 | return (nCard&UG_VALUE_MASK)+1; 15 | } 16 | 17 | int CUpGradeGameLogic::GetCardKind(uint8 nCard) 18 | { 19 | return nCard&UG_HUA_MASK; 20 | } 21 | 22 | int CUpGradeGameLogic::SortCards(uint8 nCards[],int nCardCount) 23 | { 24 | for(int i = 0; i < nCardCount-1; i++) 25 | { 26 | for(int j = i+1; j < nCardCount; j++) 27 | { 28 | if(GetPoint(nCards[i]) < GetPoint(nCards[j])) 29 | { 30 | SwapCard(nCards[i], nCards[j]); 31 | continue; 32 | } 33 | 34 | if(GetCardNum(nCards[i]) == GetCardNum(nCards[j])) 35 | { 36 | if(GetCardKind(nCards[i]) > GetCardKind(nCards[j])) 37 | { 38 | SwapCard(nCards[i], nCards[j]); 39 | } 40 | } 41 | else 42 | { 43 | if(GetCardNum(nCards[i]) > GetCardNum(nCards[j])) 44 | { 45 | SwapCard(nCards[i], nCards[j]); 46 | } 47 | } 48 | } 49 | } 50 | return 0; 51 | } 52 | 53 | int CUpGradeGameLogic::GetCardType(uint8 nCards[],int nCardCount,uint8& nMaxCard) 54 | { 55 | if(nCardCount != 5) 56 | return 0; 57 | SortCards(nCards,nCardCount); 58 | nMaxCard = nCards[4]; 59 | 60 | if(IsFiveSmall(nCards)) 61 | return UG_FIVE_SMALL; 62 | 63 | if(IsBombBull(nCards)) 64 | return UG_BULL_BOMB; 65 | 66 | if(IsGoldBull(nCards)) 67 | return UG_BULL_GOLD; 68 | 69 | if(IsSilverBull(nCards)) 70 | return UG_BULL_SILVER; 71 | 72 | int nSum = GetPoint(nCards[0])+GetPoint(nCards[1])+GetPoint(nCards[2])+GetPoint(nCards[3])+GetPoint(nCards[4]); 73 | int nRest = nSum % 10; 74 | 75 | for(int i = 0; i < nCardCount - 1; i++) 76 | { 77 | for(int j = i+1; j < nCardCount; j++) 78 | { 79 | if((GetPoint(nCards[i])+GetPoint(nCards[j]))%10 == nRest) 80 | { 81 | SwapCard(nCards[j],nCards[4]); 82 | SwapCard(nCards[i],nCards[3]); 83 | if(nRest == 0) 84 | return UG_BULL_BULL; 85 | return nRest; 86 | } 87 | } 88 | } 89 | return UG_NO_POINT; 90 | } 91 | 92 | int CUpGradeGameLogic::GetPoint(uint8 nCard) 93 | { 94 | switch (nCard) 95 | { 96 | case 0x0D: 97 | return 1; 98 | case 0x0A: 99 | case 0x0B: 100 | case 0x0C: 101 | return 10; 102 | default: 103 | return GetCardNum(nCard); 104 | } 105 | } 106 | 107 | void CUpGradeGameLogic::SwapCard(uint8& nCard1,uint8& nCard2) 108 | { 109 | if(nCard1 == nCard2) 110 | return; 111 | 112 | uint8 bTemp = nCard1; 113 | nCard1 = nCard2; 114 | nCard2 = bTemp; 115 | } 116 | 117 | bool CUpGradeGameLogic::IsGoldBull(uint8 nCards[]) 118 | { 119 | if(GetCardNum(nCards[1]) > 0x09 && GetCardNum(nCards[4]) < 0x0D) 120 | return true; 121 | return false; 122 | } 123 | 124 | bool CUpGradeGameLogic::IsSilverBull(uint8 nCards[]) 125 | { 126 | if(GetCardNum(nCards[1]) > 0x08 && GetCardNum(nCards[4]) < 0x0D) 127 | return true; 128 | return false; 129 | } 130 | 131 | bool CUpGradeGameLogic::IsBombBull(uint8 nCards[]) 132 | { 133 | if(GetCardNum(nCards[0]) == GetCardNum(nCards[3]) || GetCardNum(nCards[1]) == GetCardNum(nCards[4])) 134 | return true; 135 | return false; 136 | } 137 | 138 | bool CUpGradeGameLogic::IsFiveSmall(uint8 nCards[]) 139 | { 140 | if(GetPoint(nCards[4]) > 4) 141 | return false; 142 | if(GetPoint(nCards[0])+GetPoint(nCards[1])+GetPoint(nCards[2])+GetPoint(nCards[3])+GetPoint(nCards[4]) > 9) 143 | return false; 144 | return true; 145 | } 146 | 147 | bool CUpGradeGameLogic::CompareCard(int nCardType1,uint8 nMaxCard1,int nCardType2,uint8 nMaxCard2) 148 | { 149 | if(nCardType1 == nCardType2) 150 | { 151 | if(GetCardNum(nMaxCard1) == GetCardNum(nMaxCard2)) 152 | return GetCardKind(nMaxCard1) > GetCardKind(nMaxCard1); 153 | return GetCardNum(nMaxCard1) > GetCardNum(nMaxCard2); 154 | } 155 | return nCardType1 > nCardType2; 156 | } 157 | 158 | void CUpGradeGameLogic::RandCards(uint8 nCards[],int nCardCount) 159 | { 160 | for(int i = 0,j = nCardCount-1; i < nCardCount; ++i,--j) 161 | { 162 | int nIndex = rand()%nCardCount; 163 | SwapCard(nCards[i],nCards[nIndex]); 164 | nIndex = rand()%nCardCount; 165 | SwapCard(nCards[j],nCards[nIndex]); 166 | } 167 | } 168 | 169 | int CUpGradeGameLogic::GetMulByCardType(uint8 nCardType) 170 | { 171 | switch (nCardType) 172 | { 173 | case UG_BULL_SEVEN: 174 | case UG_BULL_EIGHT: 175 | case UG_BULL_NINE: 176 | return 2; 177 | case UG_BULL_BULL: 178 | return 3; 179 | case UG_BULL_SILVER: 180 | return 4; 181 | case UG_BULL_GOLD: 182 | return 5; 183 | case UG_BULL_BOMB: 184 | return 6; 185 | case UG_FIVE_SMALL: 186 | return 7; 187 | default: 188 | return 1; 189 | } 190 | } 191 | 192 | -------------------------------------------------------------------------------- /Game/11001/UpGradeLogic.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "../../Defines.h" 4 | 5 | #define UG_HUA_MASK 0xF0 6 | #define UG_VALUE_MASK 0x0F 7 | 8 | 9 | enum UG_CARD_TYPE 10 | { 11 | UG_NO_POINT, 12 | UG_BULL_ONE, 13 | UG_BULL_TWO, 14 | UG_BULL_THREE, 15 | UG_BULL_FOUR, 16 | UG_BULL_FIVE, 17 | UG_BULL_SIX, 18 | UG_BULL_SEVEN, 19 | UG_BULL_EIGHT, 20 | UG_BULL_NINE, 21 | UG_BULL_BULL, 22 | UG_BULL_SILVER, 23 | UG_BULL_GOLD, 24 | UG_BULL_BOMB, 25 | UG_FIVE_SMALL 26 | }; 27 | 28 | class CUpGradeGameLogic 29 | { 30 | public: 31 | CUpGradeGameLogic(); 32 | ~CUpGradeGameLogic(); 33 | public: 34 | static int GetCardType(uint8 nCards[],int nCardCount,uint8& nMaxCard); 35 | static bool CompareCard(int nCardType1,uint8 nMaxCard1,int nCardType2,uint8 nMaxCard2); 36 | static void RandCards(uint8 nCards[],int nCardCount); 37 | static int GetMulByCardType(uint8 nCardType); 38 | private: 39 | static int GetCardNum(uint8 nCard); 40 | static int GetCardKind(uint8 nCard); 41 | static int SortCards(uint8 nCards[],int nCardCount); 42 | static int GetPoint(uint8 nCard); 43 | static void SwapCard(uint8& nCard1,uint8& nCard2); 44 | static bool IsGoldBull(uint8 nCards[]); 45 | static bool IsSilverBull(uint8 nCards[]); 46 | static bool IsBombBull(uint8 nCards[]); 47 | static bool IsFiveSmall(uint8 nCards[]); 48 | 49 | }; 50 | 51 | 52 | -------------------------------------------------------------------------------- /Game/11001/UpGradeMessage.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../../Defines.h" 3 | 4 | enum S_C_PROTOL 5 | { 6 | S_C_GAMEBEGIN = 1, 7 | S_C_BEGINROBBANK, 8 | S_C_CONFIRMBANK, 9 | S_C_BEGINBET, 10 | S_C_DISPATCHCARD, 11 | S_C_USERROBBANK, 12 | S_C_USERBET, 13 | S_C_USERSHOWCARD, 14 | S_C_GAMEFINISH, 15 | S_C_GAMEOVER 16 | }; 17 | 18 | enum C_S_PROTOL 19 | { 20 | C_S_USERROBBANK = 1, 21 | C_S_USERBET, 22 | C_S_USERSHOWCARD, 23 | }; 24 | 25 | enum Timer 26 | { 27 | TIMER_READY=1000, 28 | TIMER_BANKER, 29 | TIMER_RESTART, 30 | TIMER_SHOWBANKER, 31 | TIMER_BET, 32 | TIMER_DISPATCHCARD, 33 | TIMER_SHOWCARD, 34 | TIMER_COMPARE 35 | }; 36 | 37 | enum GameState 38 | { 39 | NONE, 40 | READY, 41 | BANK, 42 | BET, 43 | DISCARD, 44 | SHOWCARD, 45 | COMPARE, 46 | FINISH 47 | }; 48 | 49 | struct C_S_RobBank 50 | { 51 | int nOper; 52 | }; 53 | 54 | struct C_S_UserBet 55 | { 56 | int nMul; 57 | }; 58 | 59 | struct S_C_DisPatchCard 60 | { 61 | uint8 nCards[5]; 62 | }; 63 | 64 | struct S_C_UserBet 65 | { 66 | uint16 nSeatNo; 67 | int nMul; 68 | }; 69 | 70 | struct S_C_UserRobBank 71 | { 72 | uint16 nSeatNo; 73 | int nRob; 74 | }; 75 | 76 | struct S_C_ConfirmBank 77 | { 78 | uint16 nBankSeatNo; 79 | }; 80 | 81 | struct S_C_UserShowCard 82 | { 83 | uint16 nSeatNo; 84 | uint16 nCardType; 85 | int nMuls; 86 | uint8 nCards[5]; 87 | }; 88 | 89 | struct UserWinInfo 90 | { 91 | uint16 nSeatNo; 92 | int nWin; 93 | }; 94 | 95 | struct S_C_UserWininfo 96 | { 97 | uint16 nUserCount; 98 | UserWinInfo userWinInfo[1]; 99 | }; -------------------------------------------------------------------------------- /Game/BaseLogic.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include "../Defines.h" 4 | #include "../GameServer/Room.h" 5 | #include "../PacketParse.h" 6 | 7 | class CBaseLogic 8 | { 9 | public: 10 | CBaseLogic(CRoom* pRoom):m_pRoom(pRoom){}; 11 | virtual ~CBaseLogic(){}; 12 | public: 13 | virtual bool InitGameStation(void* pData,uint16 uDataSize) = 0; //解析房间规则 14 | virtual bool GetGameStation(uint16 uSeatNo) = 0; // 获取游戏状态 15 | virtual bool HandNetMsg(uint16 uSeatNo, uint16 uSub, CInputPacket& inPacket)=0; //处理网络消息 16 | virtual bool HandTimeMsg(uint16 uTimeId) = 0; //处理定时器消息 17 | virtual bool GameBegin() = 0; 18 | virtual void UserNetCut(uint16 nSeatNo) {} 19 | virtual void UserLeft(uint16 nSeatNo) {} 20 | virtual void GameOver() = 0; 21 | protected: 22 | CRoom* const m_pRoom; 23 | }; -------------------------------------------------------------------------------- /GameServer/GameCliSink.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../NetHandSink.h" 3 | #include "../include/core/TimerNode.h" 4 | #include "../PacketParse.h" 5 | 6 | class CServices; 7 | class ConnSucess; 8 | 9 | class CGameCliSink : public CNetHandSink 10 | { 11 | public: 12 | CGameCliSink(CServices* pNet); 13 | ~CGameCliSink(); 14 | public: 15 | void Init(const char* szIp); 16 | bool DisConnect(); 17 | bool HandNetData(uint16, uint16, CInputPacket& inPacket); 18 | bool HandTimeMsg(uint16 uTimeID); 19 | private: 20 | bool HandMainMsgRoom(uint16, CInputPacket& inPacket); 21 | bool HandMainMsgNet(uint16, CInputPacket& inPacket); 22 | 23 | bool HandMsgFromCenterSrv(uint16, CInputPacket& inPacket); 24 | bool HandMsgFromUserSrv(uint16, CInputPacket& inPacket); 25 | bool HandMsgFromDataSrv(uint16, CInputPacket& inPacket); 26 | private: 27 | bool HandMsgTestConn(); 28 | void ConnectSucess(CInputPacket& inPacket); 29 | void RegGameSrv(); 30 | private: 31 | uint16 m_nTestNum; 32 | uint16 m_nReConnectCount; 33 | 34 | uint16 m_nPeerSrvType; 35 | uint16 m_nPeerSrvNo; 36 | 37 | CTimerNode m_timer_Link; 38 | CTimerNode m_timer_reconnect; 39 | }; 40 | 41 | -------------------------------------------------------------------------------- /GameServer/GameSerSink.cpp: -------------------------------------------------------------------------------- 1 | #include "GameSerSink.h" 2 | #include "../commproto.h" 3 | #include "GameServer.h" 4 | #include "../include/core/Services.h" 5 | #include "../include/core/SingleObject.h" 6 | #include "../NetSinkObj.h" 7 | #include "GameUserEnginer.h" 8 | 9 | extern CGameServer* g_pGameServer; 10 | 11 | enum TIME_ID 12 | { 13 | TIME_TEST_LINK = 1, 14 | }; 15 | 16 | 17 | CGameSerSink::CGameSerSink(CServices* pServices):CNetHandSink(pServices) 18 | { 19 | pServices->SetPri(); 20 | m_timer_Link.InitTimerObj(m_pNet, TIME_TEST_LINK); 21 | } 22 | 23 | CGameSerSink::~CGameSerSink() 24 | { 25 | 26 | } 27 | 28 | void CGameSerSink::Connect() 29 | { 30 | COutputPacket out; 31 | out.Begin(MAIN_MSG_GAMESER, GS_SUB_MSG_CONN_SUCSS); 32 | out.WriteInt16(g_pGameServer->GetSerType()); 33 | out.WriteInt16(g_pGameServer->GetSerNo()); 34 | out.End(); 35 | CNetSinkObj::SendData(m_pNet, m_pNet->GetServiceIndex(), out); 36 | } 37 | 38 | void CGameSerSink::Close() 39 | { 40 | g_pGameServer->DelConnSrvIndex(m_nConnNo, m_pNet->GetServiceIndex()); 41 | } 42 | 43 | bool CGameSerSink::HandTimeMsg(uint16 nTimeID) 44 | { 45 | switch(nTimeID) 46 | { 47 | case TIME_TEST_LINK: 48 | { 49 | ++m_nTestNum; 50 | if(m_nTestNum > 1) 51 | { 52 | m_pNet->Log("test link timeout!"); 53 | return false; 54 | } 55 | m_timer_Link.StartTimerSec(SERVER_TEST_TIME); 56 | return true; 57 | } 58 | } 59 | return false; 60 | } 61 | 62 | 63 | bool CGameSerSink::HandNetData(uint16 nMain, uint16 nSub, CInputPacket& inPacket) 64 | { 65 | switch(nMain) 66 | { 67 | case MAIN_MSG_CONNSER: 68 | return HandMsgFromConnSrv(nSub, inPacket); 69 | default: 70 | break; 71 | } 72 | return false; 73 | } 74 | 75 | bool CGameSerSink::HandTestNetConn() 76 | { 77 | m_nTestNum = 0; 78 | COutputPacket out; 79 | out.Begin(MAIN_MSG_GAMESER, GS_SUB_MSG_TEST); 80 | out.End(); 81 | CNetSinkObj::SendData(m_pNet, m_pNet->GetServiceIndex(), out); 82 | return true; 83 | } 84 | 85 | 86 | bool CGameSerSink::HandMsgFromConnSrv(uint16 nSub, CInputPacket& inPacket) 87 | { 88 | switch(nSub) 89 | { 90 | case CS_SUB_MSG_REG_CONN: 91 | { 92 | uint16 nSrvNo = inPacket.ReadInt16(); 93 | g_pGameServer->AddConnInfo(nSrvNo, m_pNet->GetServiceIndex()); 94 | HandTestNetConn(); 95 | m_timer_Link.StartTimerSec(SERVER_TEST_TIME); 96 | break; 97 | } 98 | case CS_SUB_MSG_TEST: 99 | { 100 | HandTestNetConn(); 101 | break; 102 | } 103 | case CS_SUB_MSG_USER4HALL: 104 | { 105 | HandMsgFromUserForHall(inPacket); 106 | break; 107 | } 108 | case CS_SUB_MSG_USER4ROOM: 109 | { 110 | HandMsgFromUserForRoom(inPacket); 111 | break; 112 | } 113 | case CS_SUB_MSG_USER2ROOM: 114 | { 115 | HandMsgFromUserToGame(inPacket); 116 | break; 117 | } 118 | case CS_SUB_MSG_USER: 119 | { 120 | HandMsgFromUser(inPacket); 121 | break; 122 | } 123 | default: 124 | break; 125 | } 126 | return true; 127 | } 128 | 129 | 130 | void CGameSerSink::HandMsgFromUserToGame(CInputPacket& inPacket) 131 | { 132 | SERVICEINDEX nIndex = inPacket.ReadInt16(); 133 | m_pNet->PostData(nIndex, USER_NET_MSG, (void*)inPacket.RestPacket(), inPacket.Rest_Len()); 134 | } 135 | 136 | void CGameSerSink::HandMsgFromUserForRoom(CInputPacket& inPacket) 137 | { 138 | UID nUserId = inPacket.ReadInt32(); 139 | SERVICEINDEX nIndex = CSingleObject::Instance()->GetUserManagerIndex(nUserId); 140 | m_pNet->PostData(nIndex, USER_NET_MSG, inPacket.RestPacket(), inPacket.Rest_Len()); 141 | //m_pNet->PostData(nIndex, USER_NET_MSG, (void*)inPacket.RestPacket(), inPacket.Rest_Len()); 142 | } 143 | 144 | void CGameSerSink::HandMsgFromUserForHall(CInputPacket& inPacket) 145 | { 146 | UID nUserId = inPacket.ReadInt32(); 147 | 148 | SERVICEINDEX nIndex = CSingleObject::Instance()->GetUserManagerIndex(nUserId); 149 | m_pNet->PostData(nIndex, USER_NET_MSG, inPacket.RestPacket(), inPacket.Rest_Len()); 150 | } 151 | 152 | void CGameSerSink::HandMsgFromUser(CInputPacket& inPacket) 153 | { 154 | UID nUserId = inPacket.ReadInt32(); 155 | SERVICEINDEX nIndex = inPacket.ReadInt16(); 156 | 157 | if(INVALID_SERIVCE_INDEX == nIndex) 158 | nIndex = CSingleObject::Instance()->GetUserManagerIndex(nUserId); 159 | 160 | m_pNet->PostData(nIndex, USER_NET_MSG, inPacket.RestPacket(), inPacket.Rest_Len()); 161 | } 162 | 163 | -------------------------------------------------------------------------------- /GameServer/GameSerSink.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "../Defines.h" 4 | #include "../NetHandSink.h" 5 | #include "../include/core/TimerNode.h" 6 | #include "../PacketParse.h" 7 | 8 | class CGameSerSink : public CNetHandSink 9 | { 10 | public: 11 | CGameSerSink(CServices* pServices); 12 | ~CGameSerSink(); 13 | public: 14 | virtual bool HandTimeMsg(uint16 uTimeID); 15 | virtual bool HandNetData(uint16, uint16, CInputPacket& inPacket); 16 | virtual void Connect(); 17 | virtual void Close(); 18 | private: 19 | bool HandTestNetConn(); 20 | bool HandMsgFromConnSrv(uint16, CInputPacket& inPacket); 21 | private: 22 | void ConnectSucess(CInputPacket& inPacket); 23 | void HandMsgFromUserToGame(CInputPacket& inPacket); 24 | void HandMsgFromUserForRoom(CInputPacket& inPacket); 25 | void HandMsgFromUserForHall(CInputPacket& inPacket); 26 | void HandMsgFromUser(CInputPacket& inPacket); 27 | private: 28 | uint16 m_nConnNo; 29 | uint8 m_nTestNum; 30 | CTimer m_timer_Link; 31 | }; -------------------------------------------------------------------------------- /GameServer/GameServer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include "../Server.h" 6 | #include "../include/core/ToolLock.h" 7 | 8 | class CGameServer : public CServer 9 | { 10 | public: 11 | CGameServer(); 12 | ~CGameServer(); 13 | 14 | public: 15 | int Initialize(); 16 | 17 | uint16 GetDataSrvIndex(uint32 nRand) const; 18 | uint16 GetCenterServerIndex() const; 19 | uint16 GetUserServerIndex(uint32 nRand) const; 20 | 21 | 22 | const char* GetIp() const; 23 | uint16 GetPort() const; 24 | int GetGameId() const; 25 | 26 | void AddConnInfo(uint16 nSerNo,uint16 nIndex); 27 | uint16 GetConnSrvIndex(uint16 nSerNo,uint32 nRand); 28 | void DelConnSrvIndex(uint16 nSerNo, uint16 nIndex); 29 | 30 | void DisconnectToRemoteSrv(uint16 nSrvType, uint16 nSrvNo, uint16 nIndex); 31 | private: 32 | int ReadConfig(const char* szConfigFile); 33 | int ConnectToCenterSrv(const char* szIp, uint16 nPort); 34 | int ConnectToUserSrv(const char* szIp, uint16 nPort); 35 | int ConnectToDataSrv(const char* szIp, uint16 nPort); 36 | 37 | private: 38 | CRWLock m_rw_Lock; 39 | 40 | int m_nGameId; 41 | 42 | //对网关开放的地址 43 | std::string m_szIp; 44 | uint16 m_nPort; 45 | 46 | //中心服地址 47 | //std::string m_szCenterIp; 48 | //unsigned short m_nCenterPort; 49 | uint16 m_nCenterIndex; 50 | 51 | //用户服务器数据保存 52 | std::vector m_vecUserSerIndex; 53 | 54 | //数据服务器数址保存 55 | std::vector m_vecDataSerIndex; 56 | 57 | //存储连接服务器的相关信息 58 | std::vector m_szConnSerInfo[MAX_USER_DATA_SRV_NUM]; 59 | }; -------------------------------------------------------------------------------- /GameServer/GameUser.cpp: -------------------------------------------------------------------------------- 1 | #include "GameUser.h" 2 | 3 | CGameUser::CGameUser(UID nUid):m_nUserId(nUid) 4 | { 5 | 6 | } 7 | 8 | CGameUser::~CGameUser() 9 | { 10 | 11 | } 12 | 13 | UID CGameUser::GetUid() const 14 | { 15 | return m_nUserId; 16 | } 17 | 18 | uint32 CGameUser::GetOffLineTime() const 19 | { 20 | return m_nOffLineTime; 21 | } 22 | 23 | 24 | void CGameUser::UserOffLine(uint16 nCid, uint16 nCsid) 25 | { 26 | if(nCid == m_nCid && nCsid == m_nCsid) 27 | { 28 | m_nCsid = 0; 29 | //m_nLastUpdatTime = (uint32)time(NULL); 30 | m_nUserState = USER_STATE_NONE; 31 | } 32 | } 33 | 34 | -------------------------------------------------------------------------------- /GameServer/GameUser.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "../Defines.h" 4 | #include 5 | 6 | enum USER_STATE 7 | { 8 | USER_STATE_NONE=0, 9 | USER_STATE_LOGINING, 10 | USER_STATE_LOGINED, 11 | USER_STATE_CREATING, 12 | USER_STATE_JOINING, 13 | 14 | USER_STATE_WATCH, 15 | USER_STATE_SIT 16 | }; 17 | 18 | class CGameUser 19 | { 20 | public: 21 | CGameUser(UID nUid); 22 | ~CGameUser(); 23 | public: 24 | UID GetUid() const; 25 | void UserLoginOnLine(uint16 nCid, uint16 nCsid); 26 | void UserOffLine(uint16 nCid, uint16 nCsid); 27 | uint32 GetOffLineTime() const; 28 | private: 29 | UID m_nUserId; 30 | uint16 m_nCid; 31 | uint16 m_nCsid; 32 | uint16 m_nRoomIndex; 33 | uint16 m_nUserState; 34 | uint16 m_nGameState; 35 | std::string m_szUserInfo; 36 | uint32 m_nOffLineTime; 37 | }; -------------------------------------------------------------------------------- /GameServer/GameUserEnginer.cpp: -------------------------------------------------------------------------------- 1 | #include "GameUserEnginer.h" 2 | #include "GameUserManager.h" 3 | #include "../include/core/Core.h" 4 | #include "../include/core/SingleObject.h" 5 | 6 | 7 | //Single_Init(CGameUserEnginer) 8 | 9 | CGameUserEnginer::CGameUserEnginer() 10 | { 11 | memset(m_arrIndex,0,sizeof(m_arrIndex)); 12 | m_mapManagerInfo.clear(); 13 | } 14 | 15 | CGameUserEnginer::~CGameUserEnginer() 16 | { 17 | m_mapManagerInfo.clear(); 18 | } 19 | 20 | bool CGameUserEnginer::Init() 21 | { 22 | for(int i = 0; i < MAX_GAMEUSER_MANAGER; ++i) 23 | { 24 | CGameUserManager* pGameUser = new CGameUserManager; 25 | if(!CSingleObject::Instance()->AddService(pGameUser)) 26 | return false; 27 | m_arrIndex[i] = pGameUser->GetServiceIndex(); 28 | } 29 | return true; 30 | } 31 | 32 | SERVICEINDEX CGameUserEnginer::GetUserManagerIndex(UID nUserId) 33 | { 34 | return m_arrIndex[nUserId%MAX_GAMEUSER_MANAGER]; 35 | } 36 | 37 | bool CGameUserEnginer::HandData(int nType, SERVICEINDEX nSrcIndex, void *pData, DATASIZE nSize) 38 | { 39 | return true; 40 | } 41 | 42 | -------------------------------------------------------------------------------- /GameServer/GameUserEnginer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../include/core/Services.h" 3 | #include "../include/core/TimerNode.h" 4 | #include "../Defines.h" 5 | #include "../PacketParse.h" 6 | #include 7 | #include 8 | 9 | #define MAX_GAMEUSER_MANAGER 0xFF 10 | 11 | class CGameUserEnginer : public CServices 12 | { 13 | //Single(CGameUserEnginer) 14 | public: 15 | CGameUserEnginer(); 16 | ~CGameUserEnginer(); 17 | public: 18 | bool Init(); 19 | SERVICEINDEX GetUserManagerIndex(UID nUserId); 20 | protected: 21 | bool HandData(int nType, SERVICEINDEX nSrcIndex, void *pData, DATASIZE nSize); 22 | private: 23 | SERVICEINDEX m_arrIndex[MAX_GAMEUSER_MANAGER]; 24 | std::map m_mapManagerInfo; 25 | }; 26 | 27 | -------------------------------------------------------------------------------- /GameServer/GameUserManager.cpp: -------------------------------------------------------------------------------- 1 | #include "GameUserManager.h" 2 | #include "GameUser.h" 3 | 4 | #define SECSPERHOUR 3600 5 | 6 | enum TIME_ID 7 | { 8 | TIME_ID_CLEAR_OFFLINE_USER=1, 9 | }; 10 | 11 | CGameUserManager::CGameUserManager() 12 | { 13 | m_time_clear.InitTimerObj(this, TIME_ID_CLEAR_OFFLINE_USER); 14 | m_mapUsers.clear(); 15 | } 16 | 17 | CGameUserManager::~CGameUserManager() 18 | { 19 | std::map::iterator it = m_mapUsers.begin(); 20 | for(; it != m_mapUsers.end(); it++) 21 | { 22 | SAFE_DELTE(it->second); 23 | } 24 | m_mapUsers.clear(); 25 | } 26 | 27 | void CGameUserManager::Activated() 28 | { 29 | m_time_clear.StartTimerSec(SECSPERHOUR); 30 | } 31 | 32 | 33 | bool CGameUserManager::HandData(int nType, SERVICEINDEX nSrcIndex, void *pData, DATASIZE nSize) 34 | { 35 | switch(nType) 36 | { 37 | case USER_NET_MSG: 38 | { 39 | CInputPacket inPacket; 40 | inPacket.Copy((char*)pData, nSize); 41 | HandUserNetMsg(nSrcIndex,inPacket); 42 | break; 43 | } 44 | case TIME_MSG: 45 | { 46 | TIMEERID nTimeId = *(TIMEERID*)pData; 47 | HandTimeMsg(nTimeId); 48 | break; 49 | } 50 | } 51 | return true; 52 | } 53 | 54 | CGameUser* CGameUserManager::GetUser(UID nUid) 55 | { 56 | CGameUser* pUser = NULL; 57 | USERMAP::iterator it = m_mapUsers.find(nUid); 58 | if(it != m_mapUsers.end()) 59 | { 60 | pUser = it->second; 61 | } 62 | 63 | if(NULL == pUser) 64 | { 65 | pUser = new CGameUser(nUid); 66 | if(NULL != pUser) 67 | { 68 | m_mapUsers[nUid] = pUser; 69 | } 70 | } 71 | return pUser; 72 | } 73 | 74 | 75 | void CGameUserManager::HandUserNetMsg(SERVICEINDEX nSrcIndex,CInputPacket& inPacket) 76 | { 77 | uint16 nMain = inPacket.GetMainCmd(); 78 | uint16 nSub = inPacket.GetSubCmd(); 79 | 80 | switch(nMain) 81 | { 82 | case MAIN_MSG_CONNSER: 83 | { 84 | HandUserMsgFromConnSrv(nSrcIndex,nSub,inPacket); 85 | break; 86 | } 87 | case MAIN_MSG_DATASER: 88 | { 89 | break; 90 | } 91 | default: 92 | { 93 | break; 94 | } 95 | } 96 | return; 97 | } 98 | 99 | void CGameUserManager::ClearOffLineUser() 100 | { 101 | USERMAP::iterator it = m_mapUsers.begin(); 102 | uint32 nNow = (uint32)time(NULL); 103 | while(it != m_mapUsers.end()) 104 | { 105 | CGameUser* pUser = it->second; 106 | if(NULL == pUser) 107 | { 108 | m_mapUsers.erase(it++); 109 | continue; 110 | } 111 | 112 | uint32 nOffLimeTime = pUser->GetOffLineTime(); 113 | if(0 == nOffLimeTime) 114 | { 115 | ++it; 116 | continue; 117 | } 118 | 119 | if(nNow - nOffLimeTime > 18000) 120 | { 121 | m_mapUsers.erase(it++); 122 | continue; 123 | } 124 | 125 | ++it; 126 | } 127 | 128 | m_time_clear.StartTimerSec(3600); 129 | } 130 | 131 | 132 | void CGameUserManager::HandTimeMsg(TIMEERID nTimeID) 133 | { 134 | switch(nTimeID) 135 | { 136 | case TIME_ID_CLEAR_OFFLINE_USER: 137 | { 138 | ClearOffLineUser(); 139 | break; 140 | } 141 | default: 142 | { 143 | break; 144 | } 145 | } 146 | } 147 | 148 | 149 | void CGameUserManager::HandUserMsgFromConnSrv(SERVICEINDEX nSrcIndex, uint16 nSub, CInputPacket& inPacket) 150 | { 151 | switch(nSub) 152 | { 153 | case CS_SUB_MSG_USER_LOGIN_HALL: 154 | break; 155 | default: 156 | break; 157 | } 158 | 159 | UID nUid = inPacket.ReadInt32(); 160 | 161 | //CGameUser* 162 | } 163 | 164 | -------------------------------------------------------------------------------- /GameServer/GameUserManager.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../include/core/Services.h" 3 | #include "../include/core/TimerNode.h" 4 | #include "../Defines.h" 5 | #include "../PacketParse.h" 6 | #include 7 | 8 | class CGameUser; 9 | 10 | class CGameUserManager : public CServices 11 | { 12 | public: 13 | CGameUserManager(); 14 | ~CGameUserManager(); 15 | protected: 16 | bool HandData(int nType, SERVICEINDEX nSrcIndex, void *pData, DATASIZE nSize); 17 | void Activated(); 18 | private: 19 | void HandUserNetMsg(SERVICEINDEX nSrcIndex,CInputPacket& inPacket); 20 | void HandTimeMsg(TIMEERID nTimeID); 21 | void HandUserMsgFromConnSrv(SERVICEINDEX nSrcIndex,uint16 nSub,CInputPacket& inPacket); 22 | void ClearOffLineUser(); 23 | 24 | void JoinRoom(); 25 | void LeaveRoom(); 26 | private: 27 | CGameUser* GetUser(UID nUid); 28 | void UserLogin(); 29 | private: 30 | typedef std::map USERMAP; 31 | USERMAP m_mapUsers; 32 | CTimerNode m_time_clear; 33 | }; 34 | -------------------------------------------------------------------------------- /GameServer/Robot.cpp: -------------------------------------------------------------------------------- 1 | #include "Robot.h" 2 | 3 | 4 | CRobot::CRobot() 5 | { 6 | } 7 | 8 | 9 | CRobot::~CRobot() 10 | { 11 | } 12 | -------------------------------------------------------------------------------- /GameServer/Robot.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../include/core/Services.h" 3 | class CRobot : 4 | public CServices 5 | { 6 | friend class CRobotManager; 7 | private: 8 | CRobot(); 9 | ~CRobot(); 10 | }; 11 | 12 | -------------------------------------------------------------------------------- /GameServer/RobotManager.cpp: -------------------------------------------------------------------------------- 1 | #include "RobotManager.h" 2 | #include 3 | 4 | //Single_Init(CRobotManager) 5 | 6 | CRobotManager::CRobotManager() 7 | { 8 | } 9 | 10 | 11 | CRobotManager::~CRobotManager() 12 | { 13 | 14 | } 15 | -------------------------------------------------------------------------------- /GameServer/RobotManager.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../Defines.h" 3 | #include 4 | #include "Robot.h" 5 | #include 6 | #include "../include/core/ToolLock.h" 7 | 8 | class CRobotManager : public CServices 9 | { 10 | //Single(CRobotManager) 11 | public: 12 | CRobotManager(); 13 | ~CRobotManager(); 14 | //void ActiveRobot(int num); 15 | //void RetrieveRobot(); 16 | public: 17 | //void Init(int num); 18 | bool HandData(int nType, SERVICEINDEX nSrcIndex, void *pData, DATASIZE nSize) { return true;} 19 | private: 20 | CMutexLock m_metux; 21 | }; 22 | -------------------------------------------------------------------------------- /GameServer/Room.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include "../include/core/Services.h" 4 | //#include "BaseLogic.h" 5 | #include "../commproto.h" 6 | #include "../include/core/SmartPoint.h" 7 | #include "../include/core/TimerNode.h" 8 | #include "../PacketParse.h" 9 | 10 | class CUserInfo; 11 | class CBaseLogic; 12 | 13 | /* 14 | enum EN_USER_MSG_ROOM 15 | { 16 | NET_MSG=1, 17 | };*/ 18 | 19 | class CRoom : 20 | public CServices 21 | { 22 | CTimerNode m_timerGameOver; 23 | CTimerNode m_timerGameMustBegin; 24 | CTimerNode m_timerGameBegin; 25 | CTimerNode m_timerGameFinish; 26 | 27 | int m_nRoomId; 28 | int m_nGameId; 29 | uint16 m_nUserCount; 30 | UID m_dwOwerId; 31 | uint16 m_nAllRound; 32 | uint16 m_nCurRound; 33 | bool m_bStarted; 34 | CSmartArrayPoint m_pUsers; 35 | uint16 *m_pUserState; 36 | CBaseLogic* m_pGameLogic; 37 | 38 | static void* m_pHander; 39 | static CBaseLogic* (*m_sFun)(CRoom*); 40 | public: 41 | CRoom(); 42 | ~CRoom(); 43 | CBaseLogic* CreateLogic(); 44 | bool InitRoomData(UID uid,int,CreateRoom* pData,uint16 uDataSize); 45 | static bool LoadGameLogic(int nGameId); 46 | static void ReleaseGameLogic(); 47 | public: 48 | bool GameStart(); 49 | void GameFinish(); 50 | void SendDataToUser(uint16, COutputPacket&); 51 | void SendDataToAll(COutputPacket&,uint16 nSeatNo = -1); 52 | uint16 GetUserCount() const; 53 | CUserInfo* GetUsers(uint16 nSeatNo); 54 | 55 | private: 56 | virtual bool HandData(int e, SERVICEINDEX uFromSerId, void *pData, DATASIZE size); 57 | void PreExitSelf(); 58 | 59 | private: 60 | bool HandNetMsg(uint16 nSeatNo,uint16 nIndex,uint16 uMain,uint16 uSub,CInputPacket& inPacket); 61 | bool HandTimeMsg(uint16 uTimeId); 62 | bool HandDataBaseRet(); 63 | bool HandMemDataRet(uint32 uType, void* pData, uint16 uDataSize); 64 | bool HandUserMsg(uint32 nType, void* pData, uint16 nDataSize); 65 | bool UserJoin(uint16 nCsid, uint16 nIndex, CInputPacket& in); 66 | bool UserQuit(uint16); 67 | void GameOver(); 68 | void SendRoomInfoToUser(uint16 nSeatNo); 69 | void SendAllUsersInfoToUser(uint16); 70 | void SyncGameSerInfo(uint16 nSeatNo); 71 | void UserReady(uint16 nSeatNo); 72 | }; 73 | 74 | -------------------------------------------------------------------------------- /GameServer/RoomManager.cpp: -------------------------------------------------------------------------------- 1 | #include "RoomManager.h" 2 | #include "../Defines.h" 3 | #include 4 | #include 5 | //#include "Services.h" 6 | #include "Room.h" 7 | #include "../commproto.h" 8 | #include 9 | #include "../include/core/Core.h" 10 | 11 | #include "GameServer.h" 12 | 13 | extern CGameServer* g_pGameServer; 14 | 15 | //Single_Init(CRoomManager) 16 | CRoomManager::CRoomManager() 17 | { 18 | } 19 | 20 | 21 | CRoomManager::~CRoomManager() 22 | { 23 | } 24 | 25 | int CRoomManager::AddRoom(uint16 nIndex) 26 | { 27 | static int sCurRoomid = 0; 28 | int i = sCurRoomid; 29 | int nRoomPreId = g_pGameServer->GetSerNo() * 100000; 30 | int nRoomId = 0; 31 | do 32 | { 33 | ++sCurRoomid; 34 | if(sCurRoomid >= 100000) 35 | sCurRoomid = 1; 36 | 37 | if(i == sCurRoomid) 38 | return 0; 39 | 40 | nRoomId = nRoomPreId+sCurRoomid; 41 | if (m_room_manager.find(nRoomId) == m_room_manager.end()) 42 | { 43 | m_room_manager[nRoomId] = nIndex; 44 | break; 45 | } 46 | } while (1); 47 | return nRoomId; 48 | } 49 | 50 | void CRoomManager::DestroyRoom(int nRoomId) 51 | { 52 | if(nRoomId < 100000) 53 | return; 54 | map::iterator it; 55 | 56 | it = m_room_manager.find(nRoomId); 57 | if (it != m_room_manager.end()) 58 | { 59 | m_room_manager.erase(it); 60 | } 61 | } 62 | 63 | uint16 CRoomManager::GetRoom(int nRoomId) 64 | { 65 | map::iterator it; 66 | 67 | it = m_room_manager.find(nRoomId); 68 | if (it != m_room_manager.end()) 69 | { 70 | return it->second; 71 | } 72 | return 0; 73 | } 74 | 75 | bool CRoomManager::HandData(int nType, SERVICEINDEX nSrcIndex, void *pData, DATASIZE nSize) 76 | { 77 | 78 | } -------------------------------------------------------------------------------- /GameServer/RoomManager.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include "../include/core/ToolLock.h" 5 | #include 6 | #include "../Defines.h" 7 | #include "../include/core/Services.h" 8 | using namespace std; 9 | 10 | 11 | class CRoomManager : public CServices 12 | { 13 | //Single(CRoomManager) 14 | public: 15 | CRoomManager(); 16 | ~CRoomManager(); 17 | protected: 18 | bool HandData(int nType, SERVICEINDEX nSrcIndex, void *pData, DATASIZE nSize); 19 | public: 20 | int AddRoom(uint16 nIndex); 21 | void DestroyRoom(int nRoomId); 22 | uint16 GetRoom(int); 23 | private: 24 | map m_room_manager; 25 | }; -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | CXX = g++ 2 | 3 | VPATH = ./MemDataBaseEnginer:./DataBaseEnginer:./CenterServer: \ 4 | ./UserServer:./DataServer:./GameServer:./ConntionServer 5 | 6 | CFLAGS = -g -Werror -O0 7 | 8 | LIBS = -L./lib -lcore \ 9 | -L/usr/lib64/mysql -lmysqlclient \ 10 | -lhiredis -lpthread -ldl 11 | 12 | LDFLAGS = -Wl,--export-dynamic \ 13 | 14 | SOFLAGS = -fPIC -shared 15 | 16 | CURR_SRC_FILES = $(wildcard ./*.cpp) 17 | MDE_SRC_FILES = $(wildcard ./MemDataBaseEnginer/*.cpp) 18 | DBE_SRC_FILES = $(wildcard ./DataBaseEnginer/*.cpp) 19 | CT_SRC_FILES = $(wildcard ./CenterServer/*.cpp) 20 | US_SRC_FILES = $(wildcard ./UserServer/*.cpp) 21 | DS_SRC_FILES = $(wildcard ./DataServer/*.cpp) 22 | GS_SRC_FILES = $(wildcard ./GameServer/*.cpp) 23 | CS_SRC_FILES = $(wildcard ./ConntionServer/*.cpp) 24 | 25 | SEROBJ = \ 26 | $(CURR_SRC_FILES:./%.cpp=./build/%.o) \ 27 | $(MDE_SRC_FILES:./MemDataBaseEnginer/%.cpp=./build/%.o) \ 28 | $(DBE_SRC_FILES:./DataBaseEnginer/%.cpp=./build/%.o) \ 29 | $(CT_SRC_FILES:./CenterServer/%.cpp=./build/%.o) \ 30 | $(US_SRC_FILES:./UserServer/%.cpp=./build/%.o) \ 31 | $(DS_SRC_FILES:./DataServer/%.cpp=./build/%.o) \ 32 | $(GS_SRC_FILES:./GameServer/%.cpp=./build/%.o) \ 33 | $(CS_SRC_FILES:./ConntionServer/%.cpp=./build/%.o) \ 34 | 35 | ./build/%.d:%.cpp 36 | @set -e; rm -f $@; $(CXX) -MM $< -o $@; \ 37 | sed -i 's,\($*\).o[ :]*,build/\1.o : ,g' $@; \ 38 | echo ' $(CXX) -c $(CFLAGS) $$< -o $$@' >> $@; 39 | 40 | server:$(SEROBJ) 41 | $(CXX) $(CFLAGS) $^ -o $@ $(LDFLAGS) $(LIBS) 42 | 43 | %.so:./Game/%/*.cpp 44 | $(CXX) $(SOFLAGS) $(CFLAGS) $^ -o $@ 45 | 46 | -include $(SEROBJ:.o=.d) 47 | 48 | .PHONY:all clean 49 | 50 | all:clean server 51 | 52 | clean: 53 | rm -f ./build/*.o ./core.* ./log/* 54 | -------------------------------------------------------------------------------- /Makefile_old: -------------------------------------------------------------------------------- 1 | #此Makefile不带依赖文件自动推导功能 2 | 3 | CXX = g++ 4 | 5 | VPATH = ./MemDataBaseEnginer:./DataBaseEnginer:./CenterServer: \ 6 | ./UserServer:./DataServer:./GameServer:./ConntionServer 7 | 8 | CFLAGS = -g -Werror -O0 9 | 10 | SOFLAGS = -fPIC -shared 11 | 12 | LIBS = -L./lib -lcore \ 13 | -L/usr/lib64/mysql -lmysqlclient \ 14 | -lpthread -lhiredis -ldl 15 | 16 | LDFLAGS = -Wl,--export-dynamic \ 17 | 18 | CURR_SRC_FILES = $(wildcard ./*.cpp) 19 | MDE_SRC_FILES = $(wildcard ./MemDataBaseEnginer/*.cpp) 20 | DBE_SRC_FILES = $(wildcard ./DataBaseEnginer/*.cpp) 21 | CT_SRC_FILES = $(wildcard ./CenterServer/*.cpp) 22 | US_SRC_FILES = $(wildcard ./UserServer/*.cpp) 23 | DS_SRC_FILES = $(wildcard ./DataServer/*.cpp) 24 | GS_SRC_FILES = $(wildcard ./GameServer/*.cpp) 25 | CS_SRC_FILES = $(wildcard ./ConntionServer/*.cpp) 26 | 27 | 28 | 29 | SEROBJ = $(CURR_SRC_FILES:./%.cpp=./debug/%.o) $(MDE_SRC_FILES:./MemDataBaseEnginer/%.cpp=./debug/%.o) \ 30 | $(DBE_SRC_FILES:./DataBaseEnginer/%.cpp=./debug/%.o) $(CT_SRC_FILES:./CenterServer/%.cpp=./debug/%.o) \ 31 | $(US_SRC_FILES:./UserServer/%.cpp=./debug/%.o) $(DS_SRC_FILES:./DataServer/%.cpp=./debug/%.o) \ 32 | $(GS_SRC_FILES:./GameServer/%.cpp=./debug/%.o) $(CS_SRC_FILES:./ConntionServer/%.cpp=./debug/%.o) \ 33 | 34 | 35 | ./debug/%.o:%.cpp 36 | $(CXX) -c $(CFLAGS) $< -o $@ 37 | 38 | server:$(SEROBJ) 39 | $(CXX) $(CFLAGS) $^ -o $@ $(LDFLAGS) $(LIBS) 40 | 41 | %.so:./Game/%/*.cpp 42 | $(CXX) $(SOFLAGS) $(CFLAGS) $^ -o $@ 43 | 44 | clean: 45 | rm -f ./debug/*.o core.* ./log/* 46 | 47 | all:clean server 48 | 49 | .PHONY:all clean 50 | -------------------------------------------------------------------------------- /MemDataBaseEnginer/MemDataBaseEnger.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "MemDataBaseEnger.h" 5 | #include "MemSink.h" 6 | #include "MemDataBaseService.h" 7 | #include 8 | #include "../include/core/Core.h" 9 | #include "../include/core/SingleObject.h" 10 | #include "../NetSinkObj.h" 11 | 12 | CMemDataBaseEnginer::CMemDataBaseEnginer() : m_nServiceNum(0), m_pIndex(NULL) 13 | { 14 | 15 | } 16 | 17 | CMemDataBaseEnginer::~CMemDataBaseEnginer() 18 | { 19 | SAFE_DELTEARRAY(m_pIndex) 20 | } 21 | 22 | void CMemDataBaseEnginer::SetServiceNum(int nNum) 23 | { 24 | if(nNum <= 0) 25 | return; 26 | 27 | m_nServiceNum = nNum; 28 | m_pIndex = new SERVICEINDEX[m_nServiceNum]; 29 | 30 | for (int i = 0; i < m_nServiceNum; i++) 31 | { 32 | CMemDataBaseService* pService = new CMemDataBaseService; 33 | assert(pService != NULL); 34 | if (!CSingleObject::Instance()->AddService(pService)) 35 | { 36 | printf("MemDataBaseService Create Failer\n"); 37 | exit(0); 38 | } 39 | 40 | if(!pService->Init()) 41 | { 42 | printf("MemDataBaseService Init Failer\n"); 43 | exit(0); 44 | } 45 | 46 | m_pIndex[i] = pService->GetServiceIndex(); 47 | } 48 | } 49 | 50 | SERVICEINDEX CMemDataBaseEnginer::GetIndex(SERVICEINDEX nRand) 51 | { 52 | if(m_nServiceNum == 0) 53 | return 0; 54 | 55 | return m_pIndex[nRand%m_nServiceNum]; 56 | } 57 | -------------------------------------------------------------------------------- /MemDataBaseEnginer/MemDataBaseEnger.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../Defines.h" 3 | #include 4 | 5 | 6 | class CServices; 7 | class CMemDataBaseEnginer 8 | { 9 | public: 10 | CMemDataBaseEnginer(); 11 | ~CMemDataBaseEnginer(); 12 | public: 13 | void SetServiceNum(int nNum); 14 | SERVICEINDEX GetIndex(SERVICEINDEX nRand); 15 | private: 16 | int m_nServiceNum; 17 | SERVICEINDEX* m_pIndex; 18 | }; 19 | -------------------------------------------------------------------------------- /MemDataBaseEnginer/MemDataBaseService.cpp: -------------------------------------------------------------------------------- 1 | #include "MemDataBaseService.h" 2 | #include "MemDataBaseEnger.h" 3 | #include 4 | #include "MemSink.h" 5 | 6 | CMemDataBaseService::CMemDataBaseService() :m_Sink(NULL) 7 | { 8 | m_Sink = new CMemSink(this); 9 | assert(m_Sink != NULL); 10 | } 11 | 12 | CMemDataBaseService::~CMemDataBaseService() 13 | { 14 | SAFE_DELTE(m_Sink); 15 | } 16 | 17 | bool CMemDataBaseService::Init() 18 | { 19 | return m_Sink->Init(); 20 | } 21 | 22 | bool CMemDataBaseService::HandData(int e, SERVICEINDEX uFromSerId, void *pData, DATASIZE nDataSize) 23 | { 24 | switch (e) 25 | { 26 | case TIME_MSG: 27 | { 28 | TIMEERID nTimeId = *(TIMEERID*)pData; 29 | return m_Sink->HandTimeMsg(nTimeId); 30 | } 31 | case MEM_DATA_BASE_REQ: 32 | { 33 | //static DATASIZE nHeadSize = sizeof(DataCenter) + sizeof(uint32); 34 | //if(nDataSize < nHeadSize) 35 | // break; 36 | 37 | //DataCenter* pCsid = (DataCenter*)pData; 38 | //uint32* pType = (uint32*)(pCsid+1); 39 | //m_Sink->HandMemDataReq(uFromSerId,pCsid->nCsid,*pType ,pType+1, nDataSize-nHeadSize); 40 | m_Sink->HandMemDataReq(uFromSerId, pData, nDataSize); 41 | break; 42 | } 43 | default: 44 | break; 45 | } 46 | return true; 47 | } 48 | 49 | -------------------------------------------------------------------------------- /MemDataBaseEnginer/MemDataBaseService.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../include/core/Services.h" 3 | #include "../Defines.h" 4 | //#include "interface.h" 5 | 6 | class CMemSink; 7 | 8 | class CMemDataBaseService : public CServices 9 | { 10 | friend class CMemDataBaseEnginer; 11 | CMemSink* m_Sink; 12 | public: 13 | CMemDataBaseService(); 14 | ~CMemDataBaseService(); 15 | bool Init(); 16 | protected: 17 | virtual bool HandData(int e, SERVICEINDEX uFromSerId, void *pData, DATASIZE size); 18 | }; 19 | 20 | -------------------------------------------------------------------------------- /MemDataBaseEnginer/MemSink.cpp: -------------------------------------------------------------------------------- 1 | #include "MemSink.h" 2 | #include 3 | #include 4 | #include "../include/hiredis/hiredis.h" 5 | #include "MyRedis.h" 6 | #include "MemDataBaseEnger.h" 7 | #include "../UserServer/UserRedis.h" 8 | #include "../DataServer/DataRedis.h" 9 | #include "../Server.h" 10 | 11 | extern CServer* g_pSer; 12 | 13 | CMemSink::CMemSink(CServices* pMemData) : m_pMemData(NULL) 14 | { 15 | if(SRV_TYPE_USER == g_pSer->GetSerType()) 16 | m_pMemData = new CUserRedis(pMemData); 17 | else if(SRV_TYPE_DATA == g_pSer->GetSerType()) 18 | m_pMemData = new CDataRedis(pMemData); 19 | } 20 | 21 | CMemSink::~CMemSink() 22 | { 23 | SAFE_DELTE(m_pMemData); 24 | } 25 | 26 | bool CMemSink::Init() 27 | { 28 | return m_pMemData->InitConnection(); 29 | } 30 | 31 | bool CMemSink::HandMemDataReq(SERVICEINDEX nFromSerId,void *pData, DATASIZE nDataSize) 32 | { 33 | static DATASIZE nHeadSize = sizeof(DataCenter) + sizeof(uint32); 34 | if(nDataSize < nHeadSize) 35 | return false; 36 | 37 | DataCenter* pCsid = (DataCenter*)pData; 38 | uint32* pType = (uint32*)(pCsid+1); 39 | 40 | return m_pMemData->Exec(nFromSerId, pCsid->nCsid, *pType, pType+1, nDataSize - nHeadSize); 41 | /*switch (uTypeId) 42 | { 43 | case Mem::USER_LOGIN_REQ: 44 | { 45 | UserLoginMemRet ret; 46 | m_pMemData->Exec(Mem::USER_LOGIN_REQ,pData,nDataSize,&ret,sizeof(UserLoginMemRet)); 47 | //CMemDataBaseEnginer::PostMemDataBaseRet(m_pService,uFromSerId,nCsid,Mem::USER_LOGIN_RET,&ret,sizeof(ret)); 48 | break; 49 | } 50 | case Mem::USER_LOGOUT_REQ: 51 | { 52 | if(nDataSize != sizeof(Mem::UserLogOutMemReq)) 53 | break; 54 | m_pMemData->Exec(Mem::USER_LOGOUT_REQ,pData,nDataSize); 55 | break; 56 | } 57 | case Mem::USER_JOIN_GAME_REQ: 58 | { 59 | if(nDataSize != sizeof(Mem::UserJoinGameReq)) 60 | break; 61 | 62 | Mem::UserJoinGameRet ret; 63 | m_pMemData->Exec(Mem::USER_JOIN_GAME_REQ, pData, nDataSize, &ret, sizeof(Mem::UserJoinGameRet)); 64 | //CMemDataBaseEnginer::PostMemDataBaseRet(m_pService,uFromSerId,nCsid,Mem::USER_JOIN_GAME_RET,&ret,sizeof(ret)); 65 | break; 66 | } 67 | case Mem::USER_QUIT_GAME_REQ: 68 | { 69 | if(nDataSize != sizeof(Mem::UserQuitGameReq)) 70 | break; 71 | m_pMemData->Exec(Mem::USER_QUIT_GAME_REQ, pData, nDataSize); 72 | break; 73 | } 74 | default: 75 | break; 76 | } 77 | return true;*/ 78 | } 79 | 80 | bool CMemSink::HandTimeMsg(TIMEERID nTimeId) 81 | { 82 | switch(nTimeId) 83 | { 84 | default: 85 | break; 86 | } 87 | return true; 88 | } -------------------------------------------------------------------------------- /MemDataBaseEnginer/MemSink.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../include/core/Services.h" 3 | 4 | class CRedis; 5 | 6 | class CMemSink 7 | { 8 | public: 9 | CMemSink(CServices* pMemData); 10 | ~CMemSink(); 11 | public: 12 | bool Init(); 13 | bool HandMemDataReq(SERVICEINDEX uFromSerId, void *pData, DATASIZE size); 14 | bool HandTimeMsg(TIMEERID nTimeId); 15 | private: 16 | CRedis* m_pMemData; 17 | }; 18 | 19 | -------------------------------------------------------------------------------- /MemDataBaseEnginer/MyRedis.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "MyRedis.h" 6 | #include "../Defines.h" 7 | 8 | using namespace std; 9 | 10 | CRedis::CRedis(CServices* pService) : m_pConn(NULL),m_pReply(NULL),m_pService(pService) 11 | { 12 | 13 | } 14 | 15 | CRedis::~CRedis() 16 | { 17 | CloseConnect(); 18 | } 19 | 20 | bool CRedis::InitConnection() 21 | { 22 | if(0 != GetRedisConfig()) 23 | return false; 24 | 25 | if(!OpenConnect()) 26 | return false; 27 | 28 | if(!Connected()) 29 | return false; 30 | 31 | return true; 32 | } 33 | 34 | bool CRedis::OpenConnect() 35 | { 36 | m_pConn = redisConnect(m_dbConfig.szHost.c_str(), m_dbConfig.nPort); 37 | 38 | if(!m_pConn) 39 | { 40 | printf("redis connect refuse\n"); 41 | return false; 42 | } 43 | 44 | if(m_pConn->err != 0) 45 | { 46 | printf("redis connect failer:%s\n",m_pConn->errstr); 47 | return false; 48 | } 49 | 50 | if(m_dbConfig.szAuth.length() > 0) 51 | { 52 | redisReply* reply = (redisReply*)redisCommand(m_pConn, "AUTH %s", m_dbConfig.szAuth.c_str()); 53 | if(reply->type == 6) 54 | { 55 | printf("auth failer\n"); 56 | freeReplyObject(reply); 57 | return false; 58 | } 59 | freeReplyObject(reply); 60 | } 61 | return true; 62 | } 63 | 64 | void CRedis::CloseConnect() 65 | { 66 | if(m_pConn) 67 | { 68 | redisFree(m_pConn); 69 | m_pConn = NULL; 70 | } 71 | } 72 | 73 | void CRedis::CloseReply() 74 | { 75 | if(m_pReply) 76 | { 77 | freeReplyObject(m_pReply); 78 | m_pReply = NULL; 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /MemDataBaseEnginer/MyRedis.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include "../include/hiredis/hiredis.h" 4 | #include "../Defines.h" 5 | #include 6 | 7 | struct RedisConfig 8 | { 9 | std::string szHost; 10 | unsigned short nPort; 11 | std::string szAuth; 12 | 13 | RedisConfig() 14 | { 15 | nPort = 0; 16 | szHost = ""; 17 | szAuth = ""; 18 | } 19 | }; 20 | 21 | class CServices; 22 | 23 | class CRedis 24 | { 25 | public: 26 | CRedis(CServices* pService); 27 | virtual ~CRedis(); 28 | public: 29 | bool InitConnection(); 30 | virtual int Exec(SERVICEINDEX nSrcIndex,SERVICEINDEX nCsid,uint32 nType,void* pData,DATASIZE nDataSize)=0; 31 | protected: 32 | virtual int GetRedisConfig()=0; 33 | virtual bool Connected()=0; 34 | bool OpenConnect(); 35 | void CloseConnect(); 36 | void CloseReply(); 37 | protected: 38 | redisContext* m_pConn; 39 | redisReply* m_pReply; 40 | RedisConfig m_dbConfig; 41 | CServices* const m_pService; 42 | }; 43 | -------------------------------------------------------------------------------- /NetHandSink.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "./Defines.h" 3 | #include "PacketParse.h" 4 | 5 | class CServices; 6 | 7 | class CNetHandSink 8 | { 9 | protected: 10 | CServices* const m_pNet; 11 | public: 12 | CNetHandSink(CServices* pServices):m_pNet(pServices){}; 13 | virtual ~CNetHandSink(){}; 14 | public: 15 | virtual bool HandTimeMsg(uint16 uTimeID) {return true;} 16 | virtual bool HandDataBaseRet(uint32 uType,void* pData,uint16 uDataSize){return true;} 17 | virtual bool HandMemDataRet(uint32 uType, void* pData, uint16 uDataSize){return true;} 18 | virtual void Init(const char* szIp){} 19 | virtual void Close(){} 20 | virtual void Connect(){} 21 | virtual bool DisConnect(){ return false;} 22 | virtual bool HandUserMsg(int nEvent, void* pData, uint16 uDataSize) {return false;} 23 | virtual bool HandNetData(uint16, uint16, uint16, void*, uint32){return true;} 24 | virtual bool HandNetData(uint16,uint16, CInputPacket& pInPack) {return true;}; 25 | }; 26 | 27 | 28 | -------------------------------------------------------------------------------- /NetSinkObj.cpp: -------------------------------------------------------------------------------- 1 | #include "NetSinkObj.h" 2 | #include "./include/core/Services.h" 3 | #include 4 | #include 5 | #include 6 | #include "Server.h" 7 | #include "NetHandSink.h" 8 | 9 | 10 | extern CServer* g_pSer; 11 | 12 | CNetSinkObj::CNetSinkObj(CNetHandSink* pSink): 13 | m_pSink(pSink) 14 | { 15 | 16 | } 17 | 18 | CNetSinkObj::~CNetSinkObj() 19 | { 20 | SAFE_DELTE(m_pSink) 21 | } 22 | 23 | void CNetSinkObj::SendData(CServices* pService,SERVICEINDEX nIndex, void* pData,DATASIZE nDataSize) 24 | { 25 | if(nIndex == INVALID_SERIVCE_INDEX) 26 | return; 27 | pService->PostData(nIndex, SEND_DATA_REQ, pData, nDataSize); 28 | 29 | } 30 | 31 | void CNetSinkObj::SendData(CServices* pService,SERVICEINDEX nIndex, COutputPacket& out) 32 | { 33 | if(nIndex == INVALID_SERIVCE_INDEX) 34 | return; 35 | 36 | if(out.Packet_Len() > MAX_MSG_SIZE) 37 | return; 38 | 39 | pService->PostData(nIndex, SEND_DATA_REQ, out.Get_Packet(), out.Packet_Len()); 40 | } 41 | 42 | 43 | /* 44 | -1 出错 45 | 0 没有处理继续接收 46 | int 处理了多少字节 47 | */ 48 | int CNetSinkObj::HandNetMsg(SERVICEINDEX nIndex, const char* pData, unsigned nDataCount) 49 | { 50 | int32 nPacketLen = CInputPacket::GetPacketLen(pData, nDataCount); 51 | 52 | if(nPacketLen > 0) 53 | { 54 | CInputPacket in; 55 | in.Copy(pData, nPacketLen); 56 | if(!m_pSink->HandNetData(in.GetMainCmd(), in.GetSubCmd(), in)) 57 | { 58 | return -1; 59 | } 60 | } 61 | return nPacketLen; 62 | } 63 | 64 | 65 | bool CNetSinkObj::HandTimeMsg(TIMEERID uTimeID) 66 | { 67 | return m_pSink->HandTimeMsg(uTimeID); 68 | } 69 | 70 | bool CNetSinkObj::DisConnect() 71 | { 72 | return m_pSink->DisConnect(); 73 | } 74 | 75 | void CNetSinkObj::Init(const char* szIp) 76 | { 77 | return m_pSink->Init(szIp); 78 | } 79 | 80 | void CNetSinkObj::Close() 81 | { 82 | m_pSink->Close(); 83 | } 84 | 85 | void CNetSinkObj::Connect() 86 | { 87 | m_pSink->Connect(); 88 | } 89 | 90 | bool CNetSinkObj::HandUserMsg(int nType, void* pData, DATASIZE uDataSize) 91 | { 92 | return m_pSink->HandUserMsg(nType, pData, uDataSize); 93 | } 94 | 95 | 96 | -------------------------------------------------------------------------------- /NetSinkObj.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "./include/core/NetSink.h" 4 | #include "PacketParse.h" 5 | 6 | class CServices; 7 | class CNetHandSink; 8 | 9 | class CNetSinkObj : public CNetSink 10 | { 11 | public: 12 | CNetSinkObj(CNetHandSink* pSink); 13 | ~CNetSinkObj(); 14 | static void SendData(CServices* pService,SERVICEINDEX nIndex, void* pData,DATASIZE nDataSize); 15 | static void SendData(CServices* pService,SERVICEINDEX nIndex, COutputPacket& out); 16 | public: 17 | virtual void Init(const char* szIp); 18 | virtual void Connect(); 19 | virtual bool DisConnect(); 20 | virtual int HandNetMsg(SERVICEINDEX nIndex, const char* pData, unsigned uDataSize); 21 | virtual bool HandTimeMsg(TIMEERID uTimeID); 22 | virtual bool HandUserMsg(int nType, void* pData, DATASIZE uDataSize); 23 | virtual void Close(); 24 | private: 25 | CNetHandSink* m_pSink; 26 | }; -------------------------------------------------------------------------------- /PacketParse.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Defines.h" 3 | #include 4 | 5 | 6 | class CPacketBase 7 | { 8 | public: 9 | CPacketBase(); 10 | ~CPacketBase(); 11 | public: 12 | char* Get_Packet(); 13 | uint32 Packet_Len(); 14 | 15 | char* RestPacket(); 16 | uint32 Rest_Len(); 17 | 18 | char* Data(); 19 | uint32 Data_Len(); 20 | protected: 21 | bool _Copy(const void* pBuf, uint32 nLen, bool bHead); 22 | void _Begin(uint16 nMain, uint16 nSub); 23 | void _End(); 24 | void _Reset(); 25 | bool _Read(void* pBuf, uint32 nLen); 26 | char* _ReadPoint(uint32 nLen); 27 | char* _ReadString(); 28 | bool _Write(const void* pBuf, uint32 nLen); 29 | bool _ReadHeader(void* pBuf, uint32 nLen, uint32 nPos); 30 | bool _WriteHeader(void* pBuf, uint32 nLen, uint32 nPos); 31 | protected: 32 | char* m_pData; 33 | uint32 m_nPacketSize; 34 | uint32 m_nCurrPos; 35 | }; 36 | 37 | 38 | class CInputPacket : public CPacketBase 39 | { 40 | public: 41 | CInputPacket(); 42 | ~CInputPacket(); 43 | public: 44 | static int32 GetPacketLen(const char* pData, uint32 nDataLen); 45 | public: 46 | bool Copy(const void* pBuf, uint32 nLen, bool bHaed = true); 47 | uint16 GetMainCmd(); 48 | uint16 GetSubCmd(); 49 | int8 ReadInt8(); 50 | int16 ReadInt16(); 51 | int32 ReadInt32(); 52 | int64 ReadInt64(); 53 | float ReadFloat(); 54 | double ReadDouble(); 55 | const char* ReadString(); 56 | const char* ReadBinary(); 57 | const char* ReadBinary(uint32 nLen); 58 | }; 59 | 60 | 61 | 62 | class COutputPacket : public CPacketBase 63 | { 64 | public: 65 | COutputPacket(); 66 | ~COutputPacket(); 67 | public: 68 | void Begin(uint16 nMain, uint16 nSub); 69 | void End(); 70 | bool WriteInt8(uint8 nValue); 71 | bool WriteInt16(uint16 nValue); 72 | bool WriteInt32(uint32 nValue); 73 | bool WriteInt64(uint64 nValue); 74 | bool WriteFloat(float nValue); 75 | bool WriteDouble(double nValue); 76 | bool WriteString(const char* szValue); 77 | bool WriteString(const std::string& szValue); 78 | bool WriteBinary(const void* pData, uint32 nLen); 79 | private: 80 | char m_szBuf[MAX_MSG_SIZE]; 81 | }; -------------------------------------------------------------------------------- /Readme.txt: -------------------------------------------------------------------------------- 1 | 编译运行注意事项(目前只支持64位Linux系统) 2 | 3 | 系统版本:CentOS release 6.10 4 | gcc版本:4.4.7 5 | 6 | 1、安装mysql开发客户端mysqlclient: yum install -y mysql-devel 7 | 2、安装reddis开发库 hiredis:下载源代码 make && make install 8 | 3、设置动态库路径:ldconfig ldconfig /usr/local/bin/ 或 /etc/ld.so.conf.d/ 下新建localuserlib文件 写入/usr/local/bin/ 9 | 4、设置打开文件限制: ulimit -n 65535 10 | 5、新建编译中间文件存放目录: mkdir build 11 | 6、编译: make 12 | 7、新建日志存放目录: mkdir log 13 | 8、运行: sh run.sh 14 | 15 | 2022/3/6 16 | 取消通过服务器类型和连接类型创建业务处理模块改用创建相应底层模块时加入创建业务模块回调函数 17 | 目前全部可编译通过,但因数据库没有环境,未实际测试运行 18 | -------------------------------------------------------------------------------- /Server.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "./Defines.h" 3 | #include "Server.h" 4 | #include "./include/core/Core.h" 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include "./include/core/SingleObject.h" 16 | 17 | CServer::CServer():m_nDeamon(0) 18 | { 19 | //m_pCore = CCore::CreateSingle(); 20 | } 21 | 22 | int CServer::Init(unsigned short nSerType,unsigned short nSerNo) 23 | { 24 | if(nSerNo >= 500 || nSerNo < 0) 25 | return -1; 26 | m_nSerType = nSerType; 27 | m_nSerNo = nSerNo; 28 | 29 | int nCpuNum = sysconf(_SC_NPROCESSORS_ONLN); 30 | CSingleObject::Instance()->SetThreadNum(nCpuNum*2); 31 | 32 | return Initialize(); 33 | } 34 | 35 | void CServer::InitLogFile(const char* pLogFile) 36 | { 37 | if(strcmp(pLogFile,"0")) 38 | { 39 | char szFileName[128] = {0}; 40 | char* szDir = get_current_dir_name(); 41 | printf("%s/log/%s\n",szDir,pLogFile); 42 | sprintf(szFileName,"%s/log/%s",szDir,pLogFile); 43 | free(szDir); 44 | //m_pCore->InitLogFileName(szFileName); 45 | CSingleObject::Instance()->InitLogFileName(szFileName); 46 | m_nDeamon = 1; 47 | } 48 | } 49 | 50 | void CServer::SetDaemon() 51 | { 52 | int pid, null; 53 | char nullbuf[] = {0x2f,0x64,0x65,0x76,0x2f,0x6e,0x75,0x6c,0x6c,0x00}; 54 | 55 | pid = fork(); 56 | if(pid < 0) 57 | { 58 | printf("Create service failer!\n"); 59 | exit(0); 60 | } 61 | 62 | if(pid > 0) 63 | { 64 | exit(0); 65 | } 66 | 67 | setsid(); 68 | //chdir ("/"); 69 | null = open (nullbuf, O_RDWR); 70 | dup2 (null, 0); 71 | dup2 (null, 1); 72 | dup2 (null, 2); 73 | close (null); 74 | } 75 | 76 | CServer::~CServer() 77 | { 78 | //CSingleObject::Instance()->Destroy(); 79 | } 80 | void CServer::Run() 81 | { 82 | if(1 == m_nDeamon) 83 | SetDaemon(); 84 | 85 | srand(time(NULL)); 86 | CSingleObject::Instance()->Run(); 87 | } 88 | 89 | uint16 CServer::GetSerType() const 90 | { 91 | return m_nSerType; 92 | } 93 | 94 | uint16 CServer::GetSerNo() const 95 | { 96 | return m_nSerNo; 97 | } -------------------------------------------------------------------------------- /Server.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include "./include/core/Core.h" 4 | #include "./include/core/MemPool.h" 5 | #include "Defines.h" 6 | #include "NetSinkObj.h" 7 | 8 | template CNetSink* CreateNetSink(CServices* pServices) 9 | { 10 | try 11 | { 12 | CNetHandSink* pNetHandSink = new T(pServices); 13 | return new CNetSinkObj(pNetHandSink); 14 | } 15 | catch(...) 16 | { 17 | return NULL; 18 | } 19 | } 20 | 21 | 22 | 23 | class CServer 24 | { 25 | public: 26 | CServer(); 27 | virtual ~CServer(); 28 | int Init(unsigned short nSerType,unsigned short nSerNo); 29 | void Run(); 30 | public: 31 | const char* GetIP() const; 32 | unsigned short GetPort() const; 33 | uint16 GetSerType() const; 34 | uint16 GetSerNo() const; 35 | protected: 36 | void InitLogFile(const char* pLogFile); 37 | virtual int Initialize() = 0; 38 | private: 39 | void SetDaemon(); 40 | protected: 41 | CCore* m_pCore; 42 | int m_nDeamon; 43 | unsigned short m_nSerType; 44 | unsigned short m_nSerNo; 45 | }; 46 | -------------------------------------------------------------------------------- /UserInfo.cpp: -------------------------------------------------------------------------------- 1 | #include "UserInfo.h" 2 | CUserInfo::CUserInfo() 3 | { 4 | 5 | } 6 | 7 | CUserInfo::~CUserInfo() 8 | { 9 | 10 | } 11 | 12 | UID CUserInfo::GetUserId() 13 | { 14 | return m_tagBaseInfo.nUserId; 15 | } 16 | 17 | void CUserInfo::UpdateConnInfo(uint16 nCid,uint16 nCsid) 18 | { 19 | m_tagSerInfo.nCid = nCid; 20 | m_tagSerInfo.nCsid = nCsid; 21 | } 22 | 23 | void CUserInfo::UpdateGameInfo(uint16 nGid,uint16 nGsid, uint16 nGsno) 24 | { 25 | m_tagSerInfo.nGid = nGid; 26 | m_tagSerInfo.nGsid = nGsid; 27 | m_tagSerInfo.nGsno = nGsno; 28 | } 29 | 30 | void CUserInfo::GetConnInfo(uint16& nCid,uint16& nCsid) 31 | { 32 | nCid = m_tagSerInfo.nCid; 33 | nCsid = m_tagSerInfo.nCsid; 34 | } 35 | 36 | void CUserInfo::GetGameInfo(uint16& nGid,uint16& nGsid, uint16& nGsno) 37 | { 38 | nGid = m_tagSerInfo.nGid; 39 | nGsid = m_tagSerInfo.nGsid; 40 | nGsno = m_tagSerInfo.nGsno; 41 | } 42 | 43 | void CUserInfo::SetUserBaseInfo(UID nUserId,char bSex, char const *szName, char const *szHeadUrl) 44 | { 45 | m_tagBaseInfo.nUserId = nUserId; 46 | m_tagBaseInfo.bSex = bSex; 47 | if(szName) 48 | strcpy(m_tagBaseInfo.szName,szName); 49 | if(szHeadUrl) 50 | strcpy(m_tagBaseInfo.szHeadUrl,szHeadUrl); 51 | } 52 | 53 | UserBaseInfo* CUserInfo::GetUserBaseInfo() 54 | { 55 | return &m_tagBaseInfo; 56 | } 57 | 58 | -------------------------------------------------------------------------------- /UserInfo.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "./Defines.h" 3 | struct UserBaseInfo 4 | { 5 | UID nUserId; 6 | char szName[32]; 7 | char bSex; 8 | char szHeadUrl[128]; 9 | 10 | UserBaseInfo() 11 | { 12 | memset(this,0,sizeof(UserBaseInfo)); 13 | } 14 | }; 15 | 16 | struct UserCoinInfo 17 | { 18 | uint32 nDiamond; 19 | uint64 nJinBi; 20 | UserCoinInfo() 21 | { 22 | memset(this,0,sizeof(UserCoinInfo)); 23 | } 24 | }; 25 | 26 | struct UserSerInfo 27 | { 28 | int nCid; 29 | int nCsid; 30 | int nGid; 31 | int nGsid; 32 | int nGsno; 33 | 34 | UserSerInfo() 35 | { 36 | memset(this,0,sizeof(UserSerInfo)); 37 | } 38 | }; 39 | 40 | 41 | class CUserInfo 42 | { 43 | public: 44 | CUserInfo(); 45 | ~CUserInfo(); 46 | public: 47 | UID GetUserId(); 48 | 49 | void UpdateConnInfo(uint16 nCid,uint16 nCsid); 50 | void UpdateGameInfo(uint16 nGid,uint16 nGsid, uint16 nGsno); 51 | 52 | void GetConnInfo(uint16& nCid,uint16& nCsid); 53 | void GetGameInfo(uint16& nGid,uint16& nGsid, uint16& nGsno); 54 | 55 | void SetUserBaseInfo(UID nUserId,char bSex=0,char const *szName=NULL,char const *szHeadUrl=NULL); 56 | UserBaseInfo* GetUserBaseInfo(); 57 | private: 58 | UserBaseInfo m_tagBaseInfo; 59 | UserCoinInfo m_tagCoinInfo; 60 | UserSerInfo m_tagSerInfo; 61 | }; 62 | -------------------------------------------------------------------------------- /UserServer/MemDataDef.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | namespace Mem 4 | { 5 | enum TYPE_MEMDATABASE_REQ 6 | { 7 | USER_LOGIN_REQ = 1, 8 | USER_LOGOUT_REQ, 9 | USER_JOIN_GAME_REQ, 10 | USER_QUIT_GAME_REQ, 11 | }; 12 | 13 | enum TYPE_MEMDATABASE_RET 14 | { 15 | USER_LOGIN_RET = 1, 16 | USER_JOIN_GAME_RET, 17 | }; 18 | 19 | struct UserLoginMemReq 20 | { 21 | int nUserId; 22 | uint16 nSerNo; 23 | uint16 nSockNo; 24 | 25 | UserLoginMemReq() 26 | { 27 | memset(this,0,sizeof(UserLoginMemReq)); 28 | } 29 | }; 30 | 31 | struct UserLoginMemRet 32 | { 33 | uint16 nCid; 34 | uint16 nGid; 35 | SERVICEINDEX nCSid; 36 | SERVICEINDEX nGSid; 37 | uint16 nGsno; 38 | 39 | UserLoginMemRet() 40 | { 41 | memset(this,0,sizeof(UserLoginMemReq)); 42 | } 43 | }; 44 | 45 | struct UserLogOutMemReq 46 | { 47 | int nUserId; 48 | uint16 nSerNo; 49 | uint16 nSockNo; 50 | 51 | UserLogOutMemReq() 52 | { 53 | memset(this,0,sizeof(UserLogOutMemReq)); 54 | } 55 | }; 56 | 57 | struct UserJoinGameReq 58 | { 59 | int nUserId; 60 | int nSeatNo; 61 | uint16 nGid; 62 | SERVICEINDEX nGsid; 63 | 64 | UserJoinGameReq() 65 | { 66 | memset(this,0,sizeof(UserJoinGameReq)); 67 | nSeatNo = -1; 68 | } 69 | }; 70 | 71 | struct UserJoinGameRet 72 | { 73 | int nSeatNo; 74 | uint16 nCid; 75 | SERVICEINDEX nCsid; 76 | UserJoinGameRet() 77 | { 78 | memset(this,0,sizeof(UserJoinGameRet)); 79 | } 80 | }; 81 | 82 | struct UserQuitGameReq 83 | { 84 | int nUserId; 85 | UserQuitGameReq() 86 | { 87 | memset(this,0,sizeof(UserQuitGameReq)); 88 | } 89 | }; 90 | } 91 | -------------------------------------------------------------------------------- /UserServer/UserCliSink.cpp: -------------------------------------------------------------------------------- 1 | #include "UserCliSink.h" 2 | #include "../NetSinkObj.h" 3 | #include "../include/core/Services.h" 4 | #include "../commproto.h" 5 | 6 | extern CUserServer* g_pUserServer; 7 | 8 | 9 | enum TIME_ID 10 | { 11 | TIME_TEST_CONN=1, 12 | TIME_RECONNECT, 13 | }; 14 | 15 | CUserCliSink::CUserCliSink(CServices* pServices):CNetHandSink(pServices) 16 | ,m_nTestNum(0) 17 | ,m_nReConnectCount(0) 18 | { 19 | m_timer_Link.InitTimerObj(m_pNet, TIME_TEST_CONN); 20 | m_timer_reconnect.InitTimerObj(m_pNet, TIME_RECONNECT); 21 | } 22 | 23 | CUserCliSink::~CUserCliSink() 24 | { 25 | 26 | } 27 | 28 | bool CUserCliSink::HandTimeMsg(uint16 nTimeID) 29 | { 30 | switch(nTimeID) 31 | { 32 | case TIME_TEST_CONN: 33 | { 34 | ++m_nTestNum; 35 | if(m_nTestNum > 1) 36 | { 37 | m_pNet->Log("test link times = %d", m_nTestNum); 38 | return false; 39 | } 40 | 41 | COutputPacket out; 42 | out.Begin(MAIN_MSG_USERSER, US_SUB_MSG_TEST); 43 | out.End(); 44 | CNetSinkObj::SendData(m_pNet, m_pNet->GetServiceIndex(), out); 45 | m_timer_Link.StartTimerSec(CLIENT_TEST_TIME); 46 | return true; 47 | } 48 | case TIME_RECONNECT: 49 | { 50 | ++m_nReConnectCount; 51 | if(m_nReConnectCount >= CLIENT_RECONN_NUMS) 52 | { 53 | m_pNet->Log("m_nReConnectCount = %d", m_nReConnectCount); 54 | return false; 55 | } 56 | m_pNet->PostData(m_pNet->GetServiceIndex(), NET_RECONNECT); 57 | return true; 58 | } 59 | } 60 | return true; 61 | } 62 | 63 | bool CUserCliSink::HandNetData(uint16 nMain, uint16 nSub, CInputPacket& inPacket) 64 | { 65 | m_pNet->Log("Recv cmd %d, %d", nMain, nSub); 66 | switch(nMain) 67 | { 68 | case MAIN_MSG_CENTERSER: 69 | { 70 | return HandMainMsgFromCenter(nSub, inPacket); 71 | } 72 | default: 73 | m_pNet->Log("invalid cmd main=%d,sub=%d", nMain,nSub); 74 | } 75 | return true; 76 | } 77 | 78 | 79 | bool CUserCliSink::DisConnect() 80 | { 81 | m_timer_Link.StopTimer(); 82 | m_timer_reconnect.StopTimer(); 83 | m_timer_reconnect.StartTimerSec(CLIENT_RECONN_TIME); 84 | return true; 85 | } 86 | 87 | bool CUserCliSink::HandMainMsgFromCenter( uint16 nSub, CInputPacket& inPacket) 88 | { 89 | switch(nSub) 90 | { 91 | case CT_SUB_MSG_CONN_SUCSS: 92 | { 93 | m_nTestNum = 0; 94 | m_nReConnectCount = 0; 95 | m_timer_Link.StartTimerSec(CLIENT_TEST_TIME); 96 | 97 | COutputPacket out; 98 | out.Begin(MAIN_MSG_USERSER, US_SUB_MSG_REGUSERSRV); 99 | out.WriteInt16(g_pUserServer->GetSerNo()); 100 | out.End(); 101 | CNetSinkObj::SendData(m_pNet, m_pNet->GetServiceIndex(), out); 102 | return true; 103 | } 104 | case CT_SUB_MSG_TEST: 105 | { 106 | m_nTestNum = 0; 107 | return true; 108 | } 109 | } 110 | return true; 111 | } 112 | 113 | 114 | -------------------------------------------------------------------------------- /UserServer/UserCliSink.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "UserServer.h" 3 | #include "../include/core/TimerNode.h" 4 | #include "../Defines.h" 5 | #include "../NetHandSink.h" 6 | #include "../PacketParse.h" 7 | 8 | class CUserCliSink : public CNetHandSink 9 | { 10 | public: 11 | CUserCliSink(CServices* pServices); 12 | ~CUserCliSink(); 13 | public: 14 | virtual bool HandTimeMsg(uint16 nTimeID); 15 | virtual bool HandNetData(uint16, uint16, CInputPacket& inPacket); 16 | virtual bool DisConnect(); 17 | private: 18 | bool HandMainMsgFromCenter( uint16, CInputPacket& inPacket); 19 | private: 20 | CTimer m_timer_Link; 21 | uint16 m_nTestNum; 22 | 23 | uint16 m_nReConnectCount; 24 | CTimer m_timer_reconnect; 25 | }; 26 | 27 | 28 | -------------------------------------------------------------------------------- /UserServer/UserRedis.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "../MemDataBaseEnginer/MyRedis.h" 4 | 5 | enum Redis_Pro 6 | { 7 | PRO_LOGIN_IN, 8 | PRO_LOGIN_OUT, 9 | PRO_JOIN_GAME, 10 | PRO_QUIT_GAME, 11 | PRO_MAX, 12 | }; 13 | 14 | class CUserRedis : public CRedis 15 | { 16 | public: 17 | CUserRedis(CServices* pService); 18 | ~CUserRedis(); 19 | protected: 20 | virtual int GetRedisConfig(); 21 | virtual bool Connected(); 22 | virtual int Exec(SERVICEINDEX nSrcIndex,SERVICEINDEX nCsid,uint32 nType,void* pData,DATASIZE nDataSize); 23 | private: 24 | void RegRedisScript(int nType,const char* szStr); 25 | 26 | void UserLoginReq(SERVICEINDEX nSrcIndex,SERVICEINDEX nCsid,int nUserId,uint16 nSerNo,SERVICEINDEX nSevNo); 27 | void UserJoinGame(SERVICEINDEX nSrcIndex,SERVICEINDEX nCsid,int nUserId,uint16 nSerNo,SERVICEINDEX nSevNo,int nSeatNo); 28 | private: 29 | char* m_RedisPro[PRO_MAX]; 30 | }; -------------------------------------------------------------------------------- /UserServer/UserSerSink.cpp: -------------------------------------------------------------------------------- 1 | #include "UserSerSink.h" 2 | #include "../NetSinkObj.h" 3 | #include "UserServer.h" 4 | #include "../include/core/Services.h" 5 | #include "../commproto.h" 6 | 7 | extern CUserServer* g_pUserServer; 8 | 9 | enum TIME_ID 10 | { 11 | TIME_TEST_LINK=1, 12 | }; 13 | 14 | 15 | CUserSerSink::CUserSerSink(CServices* pServices):CNetHandSink(pServices) 16 | { 17 | m_timer_Link.InitTimerObj(m_pNet, TIME_TEST_LINK); 18 | } 19 | 20 | CUserSerSink::~CUserSerSink() 21 | { 22 | 23 | } 24 | 25 | bool CUserSerSink::HandTimeMsg(uint16 nTimeID) 26 | { 27 | switch(nTimeID) 28 | { 29 | case TIME_TEST_LINK: 30 | { 31 | ++m_nTestNum; 32 | if(m_nTestNum > 1) 33 | { 34 | m_pNet->Log("test link timeout!"); 35 | return false; 36 | } 37 | m_timer_Link.StartTimerSec(SERVER_TEST_TIME); 38 | return true; 39 | } 40 | } 41 | return false; 42 | } 43 | 44 | bool CUserSerSink::HandNetData(uint16 nMain, uint16 nSub, CInputPacket& inPacket) 45 | { 46 | m_pNet->Log("Recv cmd %d, %d", nMain, nSub); 47 | switch(nMain) 48 | { 49 | case MAIN_MSG_GAMESER: 50 | return HandMainMSgGameSer(nSub, inPacket); 51 | case MAIN_MSG_CONNSER: 52 | return HandMainMsgConnSer(nSub, inPacket); 53 | default: 54 | break; 55 | } 56 | return false; 57 | } 58 | 59 | 60 | bool CUserSerSink::HandTestNetConn() 61 | { 62 | m_nTestNum = 0; 63 | COutputPacket out; 64 | out.Begin(MAIN_MSG_USERSER, US_SUB_MSG_TEST); 65 | out.End(); 66 | CNetSinkObj::SendData(m_pNet, m_pNet->GetServiceIndex(), out); 67 | return true; 68 | } 69 | 70 | 71 | void CUserSerSink::Connect() 72 | { 73 | COutputPacket out; 74 | out.Begin(MAIN_MSG_USERSER, US_SUB_MSG_CONN_SUCSS); 75 | out.WriteInt16( g_pUserServer->GetSerType()); 76 | out.WriteInt16( g_pUserServer->GetSerNo()); 77 | out.End(); 78 | CNetSinkObj::SendData(m_pNet, m_pNet->GetServiceIndex(), out); 79 | } 80 | 81 | bool CUserSerSink::HandMainMSgGameSer(uint16 nSub, CInputPacket& inPacket) 82 | { 83 | switch(nSub) 84 | { 85 | case GS_SUB_MSG_TEST: 86 | { 87 | return HandTestNetConn(); 88 | } 89 | case GS_SUB_MSG_REG_GAMESRV: 90 | { 91 | HandTestNetConn(); 92 | m_timer_Link.StartTimerSec(SERVER_TEST_TIME); 93 | return true; 94 | } 95 | } 96 | return true; 97 | } 98 | 99 | bool CUserSerSink::HandMainMsgConnSer(uint16 nSub, CInputPacket& inPacket) 100 | { 101 | switch(nSub) 102 | { 103 | case CS_SUB_MSG_TEST: 104 | { 105 | return HandTestNetConn(); 106 | } 107 | case CS_SUB_MSG_REG_CONN: 108 | { 109 | HandTestNetConn(); 110 | m_timer_Link.StartTimerSec(SERVER_TEST_TIME); 111 | return true; 112 | } 113 | case CS_SUB_MSG_USER_LOGIN_HALL: 114 | { 115 | 116 | //g_pUserServer->PostMemDataBaseReq(m_pNet, void * pData, DATASIZE uDataSize) 117 | break; 118 | } 119 | } 120 | return true; 121 | } 122 | 123 | -------------------------------------------------------------------------------- /UserServer/UserSerSink.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "UserServer.h" 3 | #include "../include/core/TimerNode.h" 4 | #include "../NetHandSink.h" 5 | #include "../PacketParse.h" 6 | 7 | class CUserSerSink : public CNetHandSink 8 | { 9 | public: 10 | CUserSerSink(CServices* pServices); 11 | ~CUserSerSink(); 12 | public: 13 | virtual bool HandTimeMsg(uint16 uTimeID); 14 | virtual bool HandNetData(uint16, uint16, CInputPacket& inPacket); 15 | virtual void Connect(); 16 | private: 17 | bool HandTestNetConn(); 18 | bool HandMainMSgGameSer(uint16, CInputPacket& inPacket); 19 | bool HandMainMsgConnSer(uint16, CInputPacket& inPacket); 20 | private: 21 | CTimer m_timer_Link; 22 | uint16 m_nTestNum; 23 | }; -------------------------------------------------------------------------------- /UserServer/UserServer.cpp: -------------------------------------------------------------------------------- 1 | #include "UserServer.h" 2 | #include "../include/core/IniFile.h" 3 | #include "../include/core/Services.h" 4 | #include "../NetSinkObj.h" 5 | #include "../MemDataBaseEnginer/MemDataBaseEnger.h" 6 | #include 7 | #include "UserCliSink.h" 8 | #include "UserSerSink.h" 9 | 10 | CUserServer* g_pUserServer = NULL; 11 | 12 | 13 | CUserServer::CUserServer():m_pMem(NULL) 14 | { 15 | g_pUserServer = this; 16 | } 17 | 18 | CUserServer::~CUserServer() 19 | { 20 | SAFE_DELTE(m_pMem); 21 | } 22 | 23 | int CUserServer::Initialize() 24 | { 25 | char szLogFile[128] = {0}; 26 | sprintf(szLogFile,"UserServer_%d", GetSerNo()); 27 | InitLogFile(szLogFile); 28 | 29 | if(0 != ReadConfig("./config/config.ini")) 30 | { 31 | printf("readconfig failer!\n"); 32 | return -1; 33 | } 34 | 35 | m_pMem = new CMemDataBaseEnginer; 36 | if(NULL == m_pMem) 37 | { 38 | printf("CMemDataBaseEnginer create failer!\n"); 39 | return -1; 40 | } 41 | m_pMem->SetServiceNum(8); 42 | 43 | if(0 == m_pCore->AddTcpNetCli(m_szCenterIp.c_str(),m_nCenterPort, CreateNetSink, false)) 44 | { 45 | printf("connect to center Failer!\n"); 46 | return -1; 47 | } 48 | 49 | 50 | if(0 == m_pCore->AddTcpNetSer(m_szIp.c_str(), m_nPort, CreateNetSink, false)) 51 | { 52 | printf("AddTcpNetSer Failer!\n"); 53 | return -1; 54 | } 55 | 56 | return 0; 57 | } 58 | 59 | int CUserServer::ReadConfig(const char* szConfigFile) 60 | { 61 | IniFile iniFile; 62 | iniFile.OpenFile(szConfigFile); 63 | 64 | m_szCenterIp = iniFile.ReadString("centerserver", "Host", ""); 65 | m_nCenterPort = iniFile.ReadInt("centerserver", "Port", 0); 66 | 67 | char szFild[64] = {0}; 68 | sprintf(szFild,"userserver_%d",GetSerNo()); 69 | m_szIp = iniFile.ReadString(szFild, "Host", ""); 70 | m_nPort = iniFile.ReadInt(szFild, "Port", 0); 71 | 72 | m_szRedisHost = iniFile.ReadString(szFild, "redisHost", ""); 73 | m_nRedisPort = iniFile.ReadInt(szFild, "redisPort", 0); 74 | m_szAuth = iniFile.ReadString(szFild, "redisAuth", ""); 75 | 76 | return 0; 77 | } 78 | 79 | 80 | const char* CUserServer::GetRedisHost() const 81 | { 82 | return m_szRedisHost.c_str(); 83 | } 84 | 85 | const unsigned short CUserServer::GetRedisPort() const 86 | { 87 | return m_nRedisPort; 88 | } 89 | 90 | const char* CUserServer::GetRedisAuth() const 91 | { 92 | return m_szAuth.c_str(); 93 | } 94 | 95 | bool CUserServer::PostMemDataBaseReq(CServices* pServices,void* pData, DATASIZE uDataSize) 96 | { 97 | SERVICEINDEX nIndex = m_pMem->GetIndex(pServices->GetServiceIndex()); 98 | if(nIndex == INVALID_SERIVCE_INDEX) 99 | return false; 100 | return pServices->PostData(nIndex, MEM_DATA_BASE_REQ, pData, uDataSize); 101 | } 102 | 103 | bool CUserServer::PostMemDataBaseRet(CServices* pServices,SERVICEINDEX nToSerId,SERVICEINDEX nCsid, uint32 nTypeId, void* pData, DATASIZE nDataSize) 104 | { 105 | COutputPacket out; 106 | out.Begin(MAIN_MSG_USERSER,US_SUB_MSG_MEM_BASE_RET); 107 | out.WriteInt16(nCsid); 108 | out.WriteInt32(nTypeId); 109 | out.WriteBinary(pData, nDataSize); 110 | out.End(); 111 | CNetSinkObj::SendData(pServices, nToSerId, out); 112 | return true; 113 | } 114 | 115 | -------------------------------------------------------------------------------- /UserServer/UserServer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../Server.h" 3 | #include 4 | 5 | class CMemDataBaseEnginer; 6 | 7 | class CUserServer : public CServer 8 | { 9 | public: 10 | CUserServer(); 11 | ~CUserServer(); 12 | public: 13 | int Initialize(); 14 | public: 15 | const char* GetRedisHost() const; 16 | const unsigned short GetRedisPort() const; 17 | const char* GetRedisAuth() const; 18 | bool PostMemDataBaseReq(CServices* pServices,void* pData, DATASIZE uDataSize); 19 | bool PostMemDataBaseRet(CServices* pServices,SERVICEINDEX nToSerId,SERVICEINDEX nCsid, uint32 nTypeId, void* pData, DATASIZE nDataSize); 20 | private: 21 | int ReadConfig(const char* szConfigFile); 22 | private: 23 | //本进程需要监听的ip、端口 24 | std::string m_szIp; 25 | unsigned short m_nPort; 26 | //中心服地址 27 | std::string m_szCenterIp; 28 | unsigned short m_nCenterPort; 29 | //redis 地址 30 | std::string m_szRedisHost; 31 | unsigned short m_nRedisPort; 32 | std::string m_szAuth; 33 | 34 | CMemDataBaseEnginer* m_pMem; 35 | }; -------------------------------------------------------------------------------- /commproto.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "./Defines.h" 3 | 4 | struct ConnSucess 5 | { 6 | uint16 nSrvType; 7 | uint16 nSrvNo; 8 | }; 9 | 10 | struct RegGameSer 11 | { 12 | uint16 nSerNo; 13 | uint32 nGameID; 14 | char szIp[16]; 15 | uint16 nPort; 16 | RegGameSer() 17 | { 18 | memset(this,0,sizeof(RegGameSer)); 19 | } 20 | }; 21 | 22 | struct RegConnSer 23 | { 24 | uint16 nSerNo; 25 | }; 26 | 27 | 28 | 29 | 30 | 31 | typedef struct tagCreateRoomInfo 32 | { 33 | uint32 nGameId; 34 | uint16 uPlayerCount; 35 | uint16 uRound; 36 | }CreateRoomInfo; 37 | 38 | typedef struct tagLogicInfo 39 | { 40 | uint16 nNum; 41 | uint16 nRules[1]; 42 | }LogicInfo; 43 | 44 | typedef struct tagCreateRoom 45 | { 46 | CreateRoomInfo roominfo; 47 | LogicInfo logicinfo; 48 | }CreateRoom; 49 | 50 | 51 | typedef struct tagJoinRoom 52 | { 53 | uint32 nRoomId; 54 | }JoinRoom; 55 | 56 | 57 | typedef struct tagRoomInfo 58 | { 59 | uint32 nGameId; 60 | uint32 nRoomId; 61 | uint16 nRound; 62 | uint16 nUserCount; 63 | uint16 nCurRound; 64 | }RoomInfo; 65 | 66 | typedef struct tagUserDoubleLogin 67 | { 68 | UID nUserId; 69 | uint16 nCid; 70 | uint16 nCsid; 71 | }UserDoubleLogin; 72 | 73 | typedef struct tagUserGameSerInfo 74 | { 75 | UID nUserId; 76 | uint16 nGid; 77 | uint16 nGsid; 78 | uint16 nSeatNo; 79 | }UserGameSerInfo; 80 | 81 | typedef struct tagInnerSync 82 | { 83 | uint16 nIndex; 84 | uint32 nType; 85 | }InnserSync; 86 | 87 | typedef struct tagUserConnSerInfo 88 | { 89 | uint16 nCid; 90 | uint16 nCsid; 91 | }UserConnSerInfo; 92 | 93 | typedef struct tagUserReConnInfo 94 | { 95 | UID nUserId; 96 | uint16 nCid; 97 | uint16 nCsid; 98 | }UserReConnInfo; 99 | 100 | typedef struct tagSyncInfoToDataBase 101 | { 102 | uint32 nEvType; 103 | UID nUserId; 104 | }SyncInfoToDataBase; -------------------------------------------------------------------------------- /config/config.ini: -------------------------------------------------------------------------------- 1 | [centerserver] 2 | Host=127.0.0.1 3 | Port=8000 4 | WebPort=8080 5 | 6 | [userserver_count] 7 | num=1 8 | 9 | [userserver_0] 10 | Host=127.0.0.1 11 | Port=9000 12 | 13 | redisHost=127.0.0.1 14 | redisPort=6379 15 | redisAuth=123 16 | 17 | [dataserver_count] 18 | num=1 19 | 20 | [dataserver_0] 21 | Host=127.0.0.1 22 | Port=10001 23 | 24 | redisHost=127.0.0.1 25 | redisPort=6379 26 | redisAuth=123 27 | 28 | DBHost=172.16.10.51 29 | DBPort=3306 30 | DBName=th_game 31 | DBUserName=dbuser 32 | DBPassword=!wt8P6IS7%eh 33 | 34 | [conn_0] 35 | Host=127.0.0.1 36 | Port=20001 37 | 38 | [game_0] 39 | Host=127.0.0.1 40 | Port=30000 41 | GameID=10011 42 | 43 | -------------------------------------------------------------------------------- /include/core/Core.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "types.h" 3 | #include 4 | #include "Poller.h" 5 | 6 | 7 | class CCore 8 | { 9 | public: 10 | CCore(); 11 | ~CCore(); 12 | //运行服务 13 | void Run(); 14 | 15 | //程序中止 16 | void Stop(); 17 | public: 18 | //设置服务线程数 默认为1 19 | void SetThreadNum(int nNum = 1); 20 | 21 | //添加一个服务 服务必须继承自 CServices 22 | bool AddService(CServices* pServices); 23 | 24 | //获取当前服务数量 25 | uint32 GetServiceNum(); 26 | 27 | //添加一个listen 套接字接收连接 28 | SERVICEINDEX AddTcpNetSer(const char* szIp, unsigned short nPort, FucCreateNetSink pCreateNetSink, bool IsIpV6 = false); 29 | 30 | //添加普通连接套接字 31 | SERVICEINDEX AddTcpNetCli(const char* szIp, unsigned short nPort, FucCreateNetSink pCreateNetSink, bool IsIpV6 = false); 32 | 33 | //添加websocket服务器 34 | SERVICEINDEX AddWebSockSer(const char* szIp, unsigned short nPort, FucCreateNetSink pCreateNetSink, bool IsIpV6 = false); 35 | 36 | //添加一个UDP套接字 37 | SERVICEINDEX AddUdpNet(const char* szIp, unsigned short nPort, FucCreateNetSink pCreateNetSink, bool IsIpV6 = false); 38 | 39 | //网络引擎添加自定义套接字 自定义套接字必须继承自CSocketService 40 | void AddPoller(CPoller* pPollerService,uint32 events); 41 | 42 | //网络引擎修改自定义套接字 43 | void ModPoller(CPoller* pPollerService, uint32 events); 44 | 45 | //网络引擎删除自定义套接字 46 | void DelPoller(CPoller* pPollerService); 47 | 48 | //设置日志输入文件,如果不设置则默认输出到标准输出 49 | void InitLogFileName(char* const pFileName); 50 | 51 | // 日志输出 52 | void SysLog(const char* szFormat,...); 53 | 54 | //数据传递 55 | bool PostSysData(SERVICEINDEX nDstIndex,int nType, void* pData, DATASIZE size); 56 | }; 57 | 58 | -------------------------------------------------------------------------------- /include/core/IniFile.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | class IniFile 5 | { 6 | public: 7 | IniFile(); 8 | IniFile(std::string FileName); 9 | ~IniFile(); 10 | 11 | int OpenFile(const char *pszFilename); 12 | std::string ReadString(const char *pszSectionName, 13 | const char *pszKeyName, 14 | const char *pszDefaultValue); 15 | 16 | int ReadInt(const char *pszSectionName, 17 | const char *pszKeyName, 18 | const int pszDefaultValue); 19 | private: 20 | unsigned int GetItemValue(const char *pszSectionName, 21 | const char *pszKeyName, 22 | char *pszReturnedString, 23 | unsigned int nSize); 24 | 25 | unsigned int GetItemValue(const char *pszSectionName, 26 | const char *pszKeyName, 27 | int &lReturnedValue, 28 | int lDefaultValue); 29 | 30 | unsigned int GetItemValue(const char *pszSectionName, 31 | const char *pszKeyName, 32 | int &lReturnedValue); 33 | 34 | unsigned int LocateSection(const char *pszSectionName, 35 | char * &pszSectionBegin, 36 | char * &pszSectionEnd); 37 | 38 | unsigned int LocateKeyRange(const char *pszKeyName, 39 | const char *pszSectionBegin, 40 | const char *pszSectionEnd, 41 | char * &pszKeyBegin, 42 | char * &pszKeyEnd); 43 | 44 | unsigned int LocateKeyValue(const char *pszKeyName, 45 | const char *pszSectionBegin, 46 | const char *pszSectionEnd, 47 | char * &pszValueBegin, 48 | char * &pszValueEnd); 49 | 50 | char *LocateStr(const char *pszCharSet, 51 | const char *pszBegin, 52 | const char *pszEnd); 53 | 54 | char *SearchMarchStr(const char *pszBegin, const char *pszCharSet); 55 | char *MapToContent(const char *p); 56 | char *MapToShadow(const char *p); 57 | 58 | void ToLower(char * pszSrc, size_t len); 59 | char* strdup(const char* szStr); 60 | void CloseFile(); 61 | private: 62 | char *m_pszContent; /* 配置文件的原始内容 */ 63 | char *m_pszShadow; /* 配置文件的内容全部转换成小写 */ 64 | size_t m_nSize; /* 配置文件内容的长度,不包括最后的NULL */ 65 | short m_bIsOpen; /* 配置文件是否打开成功的标志 */ 66 | char *m_pszFilename; /* 存放需要读取的配置文件名 */ 67 | }; 68 | 69 | -------------------------------------------------------------------------------- /include/core/MemPool.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "types.h" 3 | struct CMemPool 4 | { 5 | static void* Allocate(uint32 nSize); 6 | static void DeAllocate(void* pPtr); 7 | }; -------------------------------------------------------------------------------- /include/core/NetSink.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "types.h" 3 | 4 | class CServices; 5 | 6 | class CNetSink 7 | { 8 | public: 9 | CNetSink(); 10 | virtual void Destroy(); 11 | protected: 12 | virtual ~CNetSink(); 13 | public: 14 | virtual void Init(const char* szIp) = 0; 15 | virtual void Connect() = 0; 16 | virtual bool DisConnect() = 0; 17 | virtual int HandNetMsg(SERVICEINDEX nIndex, const char* pData, unsigned uDataSize) = 0; 18 | virtual bool HandTimeMsg(TIMEERID uTimeID) = 0; 19 | virtual bool HandUserMsg(int nType, void* pData, DATASIZE uDataSize) = 0; 20 | virtual void Close() = 0; 21 | }; 22 | 23 | -------------------------------------------------------------------------------- /include/core/ObjectManager.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "MemPool.h" 3 | 4 | template struct CObjctManager 5 | { 6 | static T* AllocateIntance() 7 | { 8 | void* ptr = CMemPool::Allocate(sizeof(T)); 9 | if(ptr) 10 | return new (ptr) T; 11 | return NULL; 12 | } 13 | 14 | static void DeAllocateIntance(T* p) 15 | { 16 | if(!p) 17 | return; 18 | p->~T(); 19 | CMemPool::DeAllocate(p); 20 | } 21 | 22 | static T* CreateIntance() 23 | { 24 | try 25 | { 26 | return new T; 27 | } 28 | catch(...) 29 | { 30 | return NULL; 31 | } 32 | } 33 | 34 | static void DeleteIntance(T* p) 35 | { 36 | if(p) 37 | delete p; 38 | } 39 | }; -------------------------------------------------------------------------------- /include/core/Poller.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include /* See NOTES */ 3 | #include 4 | #include "Services.h" 5 | #include "ToolLock.h" 6 | 7 | class CNetSink; 8 | typedef CNetSink* (*FucCreateNetSink)(CServices* pServices); 9 | 10 | class CPoller: 11 | public CServices 12 | { 13 | public: 14 | CPoller(); 15 | ~CPoller(); 16 | public: 17 | bool SetNoBlock(); 18 | int GetFd() const; 19 | protected: 20 | int m_nPollId; 21 | }; 22 | -------------------------------------------------------------------------------- /include/core/Services.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "types.h" 3 | 4 | class CMsgQueue; 5 | 6 | class CServices 7 | { 8 | friend class CMsgQueue; 9 | friend class CServiceManager; 10 | public: 11 | CServices(); 12 | virtual ~CServices(); 13 | protected: 14 | /* 15 | Initialized 代表该服务成为一个合法服务 16 | 注意所有与 GetServiceIndex() 相关的数据设置初始化请在 Initialized 函数中进行 17 | 定时器不能在构造函数中启动,请在 Initialized 函数中启动 18 | 如有需要请在子类重写 19 | */ 20 | virtual void Activated(); 21 | 22 | /* 23 | 返回值:false 服务会退出 true 正常处理 24 | 函数参数: 25 | nType : 消息类型 26 | nSrcIndex : 此消息由哪个服务投递 27 | pData : 消息体数据指针 28 | nSize : 消息体大小 29 | */ 30 | virtual bool HandData(int nType, SERVICEINDEX nSrcIndex, void *pData, DATASIZE nSize) = 0; 31 | 32 | /* 33 | 服务退出处理,默认是释放该服务内存 34 | 如果改用内存池请在子类重写此函数 35 | */ 36 | virtual void Destroy(); 37 | public: 38 | //获取当前服务索引函数 39 | SERVICEINDEX GetServiceIndex() const; 40 | //消息投递函数 41 | bool PostData(SERVICEINDEX nDstIndex,int nType, void* pData = 0, DATASIZE size = 0); 42 | //日志输出 43 | void Log(const char* szFormat,...); 44 | //设置服务优先 45 | void SetPri(); 46 | bool GetPri() const; 47 | private: 48 | CMsgQueue* m_pMsg; 49 | bool m_bPri; 50 | }; -------------------------------------------------------------------------------- /include/core/SingleObject.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "ToolLock.h" 3 | #include "ObjectManager.h" 4 | 5 | template class CSingleObject 6 | { 7 | CSingleObject(){} 8 | CSingleObject(const CSingleObject& obj){} 9 | CSingleObject& operator=(const CSingleObject& obj){} 10 | static T* _instance; 11 | static CMutexLock _mutex; 12 | public: 13 | static T* Instance() 14 | { 15 | if(NULL == _instance) 16 | { 17 | CToolLock lock(&_mutex); 18 | if(NULL == _instance) 19 | { 20 | if(usePool) 21 | { 22 | _instance = CObjctManager::AllocateIntance(); 23 | } 24 | else 25 | { 26 | _instance = CObjctManager::CreateIntance(); 27 | } 28 | } 29 | } 30 | return _instance; 31 | } 32 | 33 | static void Destroy() 34 | { 35 | if(NULL != _instance) 36 | { 37 | CToolLock lock(&_mutex); 38 | if(NULL != _instance) 39 | { 40 | if(usePool) 41 | { 42 | CObjctManager::DeAllocateIntance(_instance); 43 | } 44 | else 45 | { 46 | CObjctManager::DeleteIntance(_instance); 47 | } 48 | _instance = NULL; 49 | } 50 | } 51 | } 52 | 53 | }; 54 | template T* CSingleObject::_instance = NULL; 55 | template CMutexLock CSingleObject::_mutex; -------------------------------------------------------------------------------- /include/core/SmartPoint.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "types.h" 3 | #include 4 | #include 5 | #include 6 | 7 | template class CSmartPoint 8 | { 9 | T* m_pData; 10 | public: 11 | CSmartPoint() :m_pData(new T) 12 | { 13 | 14 | } 15 | CSmartPoint(T* pData) :m_pData(pData) 16 | { 17 | 18 | } 19 | ~CSmartPoint() 20 | { 21 | SAFE_DELTE(m_pData); 22 | } 23 | public: 24 | T& operator* () 25 | { 26 | return *m_pData; 27 | } 28 | 29 | T* operator->() 30 | { 31 | return m_pData; 32 | } 33 | }; 34 | 35 | templateclass CSmartArrayPoint 36 | { 37 | unsigned int m_uNum; 38 | T* m_pData; 39 | public: 40 | CSmartArrayPoint() :m_uNum(0), m_pData(NULL) 41 | { 42 | 43 | } 44 | 45 | CSmartArrayPoint(unsigned int uNum) :m_uNum(uNum), m_pData(NULL) 46 | { 47 | m_pData = new T[m_uNum]; 48 | } 49 | 50 | ~CSmartArrayPoint() 51 | { 52 | SAFE_DELTEARRAY(m_pData); 53 | } 54 | 55 | void Init(unsigned int uNum) 56 | { 57 | if (m_uNum != 0) 58 | { 59 | if(m_pData) 60 | { 61 | delete[] m_pData; 62 | } 63 | m_uNum = 0; 64 | m_pData = NULL; 65 | } 66 | m_uNum = uNum; 67 | m_pData = new T[m_uNum]; 68 | } 69 | 70 | void Reset(char nData) 71 | { 72 | if(m_uNum == 0) 73 | return; 74 | 75 | memset(m_pData,nData,sizeof(T)*m_uNum); 76 | } 77 | 78 | T& operator[] (unsigned int nIndex) 79 | { 80 | assert(nIndex < m_uNum); 81 | return m_pData[nIndex]; 82 | } 83 | 84 | const unsigned int size() const 85 | { 86 | return m_uNum; 87 | } 88 | }; -------------------------------------------------------------------------------- /include/core/TimerNode.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | /* 3 | * 4 | * 如果定时器是判断玩家超时操做,那么在执行定时器函数之前最好校验一下定时器的合法性 5 | * 也就是说用当前时间减去定时器开始时间,避免玩家和定时器一起触发操做 6 | * 另外在服务构造函数中不要调用StartTimer()函数 7 | */ 8 | #include "types.h" 9 | 10 | class CServices; 11 | 12 | class CTimerNode 13 | { 14 | friend class CTimerEnginer; 15 | public: 16 | CTimerNode(); 17 | ~CTimerNode(); 18 | public: 19 | //设置定时器绑定的Service 20 | void InitTimerObj(CServices* pService,uint32 nTimerId); 21 | //单位是10毫秒 22 | void StartTimer(uint32 nInterval); 23 | //单位是秒 24 | void StartTimerSec(uint32 nSec); 25 | //终止定时器 26 | void StopTimer(); 27 | private: 28 | bool IsEmpty(); 29 | void ClearNode(); 30 | void RemoveNode(); 31 | void AddTail(CTimerNode* const pNode); 32 | private: 33 | uint32 m_nExpire; 34 | uint32 m_nTimerId; 35 | CTimerNode* m_pPrev; 36 | CTimerNode* m_pNext; 37 | CServices* m_pService; 38 | }; 39 | 40 | typedef CTimerNode CTimer; -------------------------------------------------------------------------------- /include/core/ToolLock.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | struct ILock 10 | { 11 | virtual void Lock(int nType=0) = 0; 12 | virtual void UnLock() = 0; 13 | }; 14 | 15 | 16 | using namespace std; 17 | class CMutexLock : public ILock 18 | { 19 | pthread_mutex_t m_metux; 20 | public: 21 | CMutexLock(); 22 | ~CMutexLock(); 23 | public: 24 | virtual void Lock(int nType=1); 25 | virtual void UnLock(); 26 | }; 27 | 28 | class CRWLock : public ILock 29 | { 30 | pthread_rwlock_t m_rwlock; 31 | public: 32 | CRWLock(); 33 | ~CRWLock(); 34 | public: 35 | virtual void Lock(int nType = 0); 36 | virtual void UnLock(); 37 | }; 38 | 39 | class CToolLock 40 | { 41 | ILock* m_pLock; 42 | const bool m_bAutoLock; 43 | const int m_nLock_Type; 44 | public: 45 | CToolLock(ILock* pLock, int nLock_Type = 0, bool bAutoLock = true) :m_pLock(pLock),m_bAutoLock(bAutoLock),m_nLock_Type(nLock_Type) 46 | { 47 | if (m_pLock && m_bAutoLock) 48 | m_pLock->Lock(m_nLock_Type); 49 | } 50 | ~CToolLock() 51 | { 52 | if (m_pLock && m_bAutoLock) 53 | m_pLock->UnLock(); 54 | } 55 | public: 56 | void Lock(int nLock_Type=0); 57 | void UnLock(); 58 | }; 59 | -------------------------------------------------------------------------------- /include/core/types.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | typedef unsigned long long uint64; 4 | typedef long long int64; 5 | 6 | typedef unsigned int uint32; 7 | typedef int int32; 8 | 9 | typedef unsigned short uint16; 10 | typedef short int16; 11 | 12 | typedef char int8; 13 | typedef unsigned char uint8; 14 | 15 | #define MAX_SERVICE_NUM 65536 16 | #define INVALID_SERIVCE_INDEX 0 17 | 18 | #define MAX_LOG_SIZE 512 19 | 20 | typedef uint16 TIMEERID; 21 | typedef uint16 SERVICEINDEX; 22 | typedef uint32 DATASIZE; 23 | 24 | #define ARRAYSIZE(a) (sizeof(a)/sizeof(a[0])) 25 | #define SAFE_DELTE(p) if(p){delete p;} 26 | #define SAFE_DELTEARRAY(p) if(p){delete[] p;} 27 | 28 | enum SYS_EVENTS 29 | { 30 | EXIT_MSG = 0, //退出消息 31 | LOG_MSG, //写日志 32 | NET_MSG, //网络消息 33 | NET_CONNECT, //连接成功 34 | TIME_MSG, //时间消息 35 | NET_RECONNECT, //重连消息 36 | SEND_DATA_REQ, //发送数据请求 37 | MAX_SYS_MSG, //用户消息 38 | }; 39 | -------------------------------------------------------------------------------- /include/hiredis/adapters/ae.h: -------------------------------------------------------------------------------- 1 | /* 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 | * * Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in the 13 | * documentation and/or other materials provided with the distribution. 14 | * * Neither the name of Redis nor the names of its contributors may be used 15 | * to endorse or promote products derived from this software without 16 | * specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 | * POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | 31 | #ifndef __HIREDIS_AE_H__ 32 | #define __HIREDIS_AE_H__ 33 | #include 34 | #include 35 | #include "../hiredis.h" 36 | #include "../async.h" 37 | 38 | typedef struct redisAeEvents { 39 | redisAsyncContext *context; 40 | aeEventLoop *loop; 41 | int fd; 42 | int reading, writing; 43 | } redisAeEvents; 44 | 45 | static void redisAeReadEvent(aeEventLoop *el, int fd, void *privdata, int mask) { 46 | ((void)el); ((void)fd); ((void)mask); 47 | 48 | redisAeEvents *e = (redisAeEvents*)privdata; 49 | redisAsyncHandleRead(e->context); 50 | } 51 | 52 | static void redisAeWriteEvent(aeEventLoop *el, int fd, void *privdata, int mask) { 53 | ((void)el); ((void)fd); ((void)mask); 54 | 55 | redisAeEvents *e = (redisAeEvents*)privdata; 56 | redisAsyncHandleWrite(e->context); 57 | } 58 | 59 | static void redisAeAddRead(void *privdata) { 60 | redisAeEvents *e = (redisAeEvents*)privdata; 61 | aeEventLoop *loop = e->loop; 62 | if (!e->reading) { 63 | e->reading = 1; 64 | aeCreateFileEvent(loop,e->fd,AE_READABLE,redisAeReadEvent,e); 65 | } 66 | } 67 | 68 | static void redisAeDelRead(void *privdata) { 69 | redisAeEvents *e = (redisAeEvents*)privdata; 70 | aeEventLoop *loop = e->loop; 71 | if (e->reading) { 72 | e->reading = 0; 73 | aeDeleteFileEvent(loop,e->fd,AE_READABLE); 74 | } 75 | } 76 | 77 | static void redisAeAddWrite(void *privdata) { 78 | redisAeEvents *e = (redisAeEvents*)privdata; 79 | aeEventLoop *loop = e->loop; 80 | if (!e->writing) { 81 | e->writing = 1; 82 | aeCreateFileEvent(loop,e->fd,AE_WRITABLE,redisAeWriteEvent,e); 83 | } 84 | } 85 | 86 | static void redisAeDelWrite(void *privdata) { 87 | redisAeEvents *e = (redisAeEvents*)privdata; 88 | aeEventLoop *loop = e->loop; 89 | if (e->writing) { 90 | e->writing = 0; 91 | aeDeleteFileEvent(loop,e->fd,AE_WRITABLE); 92 | } 93 | } 94 | 95 | static void redisAeCleanup(void *privdata) { 96 | redisAeEvents *e = (redisAeEvents*)privdata; 97 | redisAeDelRead(privdata); 98 | redisAeDelWrite(privdata); 99 | free(e); 100 | } 101 | 102 | static int redisAeAttach(aeEventLoop *loop, redisAsyncContext *ac) { 103 | redisContext *c = &(ac->c); 104 | redisAeEvents *e; 105 | 106 | /* Nothing should be attached when something is already attached */ 107 | if (ac->ev.data != NULL) 108 | return REDIS_ERR; 109 | 110 | /* Create container for context and r/w events */ 111 | e = (redisAeEvents*)malloc(sizeof(*e)); 112 | e->context = ac; 113 | e->loop = loop; 114 | e->fd = c->fd; 115 | e->reading = e->writing = 0; 116 | 117 | /* Register functions to start/stop listening for events */ 118 | ac->ev.addRead = redisAeAddRead; 119 | ac->ev.delRead = redisAeDelRead; 120 | ac->ev.addWrite = redisAeAddWrite; 121 | ac->ev.delWrite = redisAeDelWrite; 122 | ac->ev.cleanup = redisAeCleanup; 123 | ac->ev.data = e; 124 | 125 | return REDIS_OK; 126 | } 127 | #endif 128 | -------------------------------------------------------------------------------- /include/hiredis/adapters/glib.h: -------------------------------------------------------------------------------- 1 | #ifndef __HIREDIS_GLIB_H__ 2 | #define __HIREDIS_GLIB_H__ 3 | 4 | #include 5 | 6 | #include "../hiredis.h" 7 | #include "../async.h" 8 | 9 | typedef struct 10 | { 11 | GSource source; 12 | redisAsyncContext *ac; 13 | GPollFD poll_fd; 14 | } RedisSource; 15 | 16 | static void 17 | redis_source_add_read (gpointer data) 18 | { 19 | RedisSource *source = (RedisSource *)data; 20 | g_return_if_fail(source); 21 | source->poll_fd.events |= G_IO_IN; 22 | g_main_context_wakeup(g_source_get_context((GSource *)data)); 23 | } 24 | 25 | static void 26 | redis_source_del_read (gpointer data) 27 | { 28 | RedisSource *source = (RedisSource *)data; 29 | g_return_if_fail(source); 30 | source->poll_fd.events &= ~G_IO_IN; 31 | g_main_context_wakeup(g_source_get_context((GSource *)data)); 32 | } 33 | 34 | static void 35 | redis_source_add_write (gpointer data) 36 | { 37 | RedisSource *source = (RedisSource *)data; 38 | g_return_if_fail(source); 39 | source->poll_fd.events |= G_IO_OUT; 40 | g_main_context_wakeup(g_source_get_context((GSource *)data)); 41 | } 42 | 43 | static void 44 | redis_source_del_write (gpointer data) 45 | { 46 | RedisSource *source = (RedisSource *)data; 47 | g_return_if_fail(source); 48 | source->poll_fd.events &= ~G_IO_OUT; 49 | g_main_context_wakeup(g_source_get_context((GSource *)data)); 50 | } 51 | 52 | static void 53 | redis_source_cleanup (gpointer data) 54 | { 55 | RedisSource *source = (RedisSource *)data; 56 | 57 | g_return_if_fail(source); 58 | 59 | redis_source_del_read(source); 60 | redis_source_del_write(source); 61 | /* 62 | * It is not our responsibility to remove ourself from the 63 | * current main loop. However, we will remove the GPollFD. 64 | */ 65 | if (source->poll_fd.fd >= 0) { 66 | g_source_remove_poll((GSource *)data, &source->poll_fd); 67 | source->poll_fd.fd = -1; 68 | } 69 | } 70 | 71 | static gboolean 72 | redis_source_prepare (GSource *source, 73 | gint *timeout_) 74 | { 75 | RedisSource *redis = (RedisSource *)source; 76 | *timeout_ = -1; 77 | return !!(redis->poll_fd.events & redis->poll_fd.revents); 78 | } 79 | 80 | static gboolean 81 | redis_source_check (GSource *source) 82 | { 83 | RedisSource *redis = (RedisSource *)source; 84 | return !!(redis->poll_fd.events & redis->poll_fd.revents); 85 | } 86 | 87 | static gboolean 88 | redis_source_dispatch (GSource *source, 89 | GSourceFunc callback, 90 | gpointer user_data) 91 | { 92 | RedisSource *redis = (RedisSource *)source; 93 | 94 | if ((redis->poll_fd.revents & G_IO_OUT)) { 95 | redisAsyncHandleWrite(redis->ac); 96 | redis->poll_fd.revents &= ~G_IO_OUT; 97 | } 98 | 99 | if ((redis->poll_fd.revents & G_IO_IN)) { 100 | redisAsyncHandleRead(redis->ac); 101 | redis->poll_fd.revents &= ~G_IO_IN; 102 | } 103 | 104 | if (callback) { 105 | return callback(user_data); 106 | } 107 | 108 | return TRUE; 109 | } 110 | 111 | static void 112 | redis_source_finalize (GSource *source) 113 | { 114 | RedisSource *redis = (RedisSource *)source; 115 | 116 | if (redis->poll_fd.fd >= 0) { 117 | g_source_remove_poll(source, &redis->poll_fd); 118 | redis->poll_fd.fd = -1; 119 | } 120 | } 121 | 122 | static GSource * 123 | redis_source_new (redisAsyncContext *ac) 124 | { 125 | static GSourceFuncs source_funcs = { 126 | .prepare = redis_source_prepare, 127 | .check = redis_source_check, 128 | .dispatch = redis_source_dispatch, 129 | .finalize = redis_source_finalize, 130 | }; 131 | redisContext *c = &ac->c; 132 | RedisSource *source; 133 | 134 | g_return_val_if_fail(ac != NULL, NULL); 135 | 136 | source = (RedisSource *)g_source_new(&source_funcs, sizeof *source); 137 | source->ac = ac; 138 | source->poll_fd.fd = c->fd; 139 | source->poll_fd.events = 0; 140 | source->poll_fd.revents = 0; 141 | g_source_add_poll((GSource *)source, &source->poll_fd); 142 | 143 | ac->ev.addRead = redis_source_add_read; 144 | ac->ev.delRead = redis_source_del_read; 145 | ac->ev.addWrite = redis_source_add_write; 146 | ac->ev.delWrite = redis_source_del_write; 147 | ac->ev.cleanup = redis_source_cleanup; 148 | ac->ev.data = source; 149 | 150 | return (GSource *)source; 151 | } 152 | 153 | #endif /* __HIREDIS_GLIB_H__ */ 154 | -------------------------------------------------------------------------------- /include/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 | -------------------------------------------------------------------------------- /include/hiredis/adapters/libev.h: -------------------------------------------------------------------------------- 1 | /* 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 | * * Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in the 13 | * documentation and/or other materials provided with the distribution. 14 | * * Neither the name of Redis nor the names of its contributors may be used 15 | * to endorse or promote products derived from this software without 16 | * specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 | * POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | 31 | #ifndef __HIREDIS_LIBEV_H__ 32 | #define __HIREDIS_LIBEV_H__ 33 | #include 34 | #include 35 | #include 36 | #include "../hiredis.h" 37 | #include "../async.h" 38 | 39 | typedef struct redisLibevEvents { 40 | redisAsyncContext *context; 41 | struct ev_loop *loop; 42 | int reading, writing; 43 | ev_io rev, wev; 44 | } redisLibevEvents; 45 | 46 | static void redisLibevReadEvent(EV_P_ ev_io *watcher, int revents) { 47 | #if EV_MULTIPLICITY 48 | ((void)loop); 49 | #endif 50 | ((void)revents); 51 | 52 | redisLibevEvents *e = (redisLibevEvents*)watcher->data; 53 | redisAsyncHandleRead(e->context); 54 | } 55 | 56 | static void redisLibevWriteEvent(EV_P_ ev_io *watcher, int revents) { 57 | #if EV_MULTIPLICITY 58 | ((void)loop); 59 | #endif 60 | ((void)revents); 61 | 62 | redisLibevEvents *e = (redisLibevEvents*)watcher->data; 63 | redisAsyncHandleWrite(e->context); 64 | } 65 | 66 | static void redisLibevAddRead(void *privdata) { 67 | redisLibevEvents *e = (redisLibevEvents*)privdata; 68 | struct ev_loop *loop = e->loop; 69 | ((void)loop); 70 | if (!e->reading) { 71 | e->reading = 1; 72 | ev_io_start(EV_A_ &e->rev); 73 | } 74 | } 75 | 76 | static void redisLibevDelRead(void *privdata) { 77 | redisLibevEvents *e = (redisLibevEvents*)privdata; 78 | struct ev_loop *loop = e->loop; 79 | ((void)loop); 80 | if (e->reading) { 81 | e->reading = 0; 82 | ev_io_stop(EV_A_ &e->rev); 83 | } 84 | } 85 | 86 | static void redisLibevAddWrite(void *privdata) { 87 | redisLibevEvents *e = (redisLibevEvents*)privdata; 88 | struct ev_loop *loop = e->loop; 89 | ((void)loop); 90 | if (!e->writing) { 91 | e->writing = 1; 92 | ev_io_start(EV_A_ &e->wev); 93 | } 94 | } 95 | 96 | static void redisLibevDelWrite(void *privdata) { 97 | redisLibevEvents *e = (redisLibevEvents*)privdata; 98 | struct ev_loop *loop = e->loop; 99 | ((void)loop); 100 | if (e->writing) { 101 | e->writing = 0; 102 | ev_io_stop(EV_A_ &e->wev); 103 | } 104 | } 105 | 106 | static void redisLibevCleanup(void *privdata) { 107 | redisLibevEvents *e = (redisLibevEvents*)privdata; 108 | redisLibevDelRead(privdata); 109 | redisLibevDelWrite(privdata); 110 | free(e); 111 | } 112 | 113 | static int redisLibevAttach(EV_P_ redisAsyncContext *ac) { 114 | redisContext *c = &(ac->c); 115 | redisLibevEvents *e; 116 | 117 | /* Nothing should be attached when something is already attached */ 118 | if (ac->ev.data != NULL) 119 | return REDIS_ERR; 120 | 121 | /* Create container for context and r/w events */ 122 | e = (redisLibevEvents*)malloc(sizeof(*e)); 123 | e->context = ac; 124 | #if EV_MULTIPLICITY 125 | e->loop = loop; 126 | #else 127 | e->loop = NULL; 128 | #endif 129 | e->reading = e->writing = 0; 130 | e->rev.data = e; 131 | e->wev.data = e; 132 | 133 | /* Register functions to start/stop listening for events */ 134 | ac->ev.addRead = redisLibevAddRead; 135 | ac->ev.delRead = redisLibevDelRead; 136 | ac->ev.addWrite = redisLibevAddWrite; 137 | ac->ev.delWrite = redisLibevDelWrite; 138 | ac->ev.cleanup = redisLibevCleanup; 139 | ac->ev.data = e; 140 | 141 | /* Initialize read/write events */ 142 | ev_io_init(&e->rev,redisLibevReadEvent,c->fd,EV_READ); 143 | ev_io_init(&e->wev,redisLibevWriteEvent,c->fd,EV_WRITE); 144 | return REDIS_OK; 145 | } 146 | 147 | #endif 148 | -------------------------------------------------------------------------------- /include/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 | int ev = (status ? p->events : events); 19 | 20 | if (p->context != NULL && (ev & UV_READABLE)) { 21 | redisAsyncHandleRead(p->context); 22 | } 23 | if (p->context != NULL && (ev & UV_WRITABLE)) { 24 | redisAsyncHandleWrite(p->context); 25 | } 26 | } 27 | 28 | 29 | static void redisLibuvAddRead(void *privdata) { 30 | redisLibuvEvents* p = (redisLibuvEvents*)privdata; 31 | 32 | p->events |= UV_READABLE; 33 | 34 | uv_poll_start(&p->handle, p->events, redisLibuvPoll); 35 | } 36 | 37 | 38 | static void redisLibuvDelRead(void *privdata) { 39 | redisLibuvEvents* p = (redisLibuvEvents*)privdata; 40 | 41 | p->events &= ~UV_READABLE; 42 | 43 | if (p->events) { 44 | uv_poll_start(&p->handle, p->events, redisLibuvPoll); 45 | } else { 46 | uv_poll_stop(&p->handle); 47 | } 48 | } 49 | 50 | 51 | static void redisLibuvAddWrite(void *privdata) { 52 | redisLibuvEvents* p = (redisLibuvEvents*)privdata; 53 | 54 | p->events |= UV_WRITABLE; 55 | 56 | uv_poll_start(&p->handle, p->events, redisLibuvPoll); 57 | } 58 | 59 | 60 | static void redisLibuvDelWrite(void *privdata) { 61 | redisLibuvEvents* p = (redisLibuvEvents*)privdata; 62 | 63 | p->events &= ~UV_WRITABLE; 64 | 65 | if (p->events) { 66 | uv_poll_start(&p->handle, p->events, redisLibuvPoll); 67 | } else { 68 | uv_poll_stop(&p->handle); 69 | } 70 | } 71 | 72 | 73 | static void on_close(uv_handle_t* handle) { 74 | redisLibuvEvents* p = (redisLibuvEvents*)handle->data; 75 | 76 | free(p); 77 | } 78 | 79 | 80 | static void redisLibuvCleanup(void *privdata) { 81 | redisLibuvEvents* p = (redisLibuvEvents*)privdata; 82 | 83 | p->context = NULL; // indicate that context might no longer exist 84 | uv_close((uv_handle_t*)&p->handle, on_close); 85 | } 86 | 87 | 88 | static int redisLibuvAttach(redisAsyncContext* ac, uv_loop_t* loop) { 89 | redisContext *c = &(ac->c); 90 | 91 | if (ac->ev.data != NULL) { 92 | return REDIS_ERR; 93 | } 94 | 95 | ac->ev.addRead = redisLibuvAddRead; 96 | ac->ev.delRead = redisLibuvDelRead; 97 | ac->ev.addWrite = redisLibuvAddWrite; 98 | ac->ev.delWrite = redisLibuvDelWrite; 99 | ac->ev.cleanup = redisLibuvCleanup; 100 | 101 | redisLibuvEvents* p = (redisLibuvEvents*)malloc(sizeof(*p)); 102 | 103 | if (!p) { 104 | return REDIS_ERR; 105 | } 106 | 107 | memset(p, 0, sizeof(*p)); 108 | 109 | if (uv_poll_init(loop, &p->handle, c->fd) != 0) { 110 | return REDIS_ERR; 111 | } 112 | 113 | ac->ev.data = p; 114 | p->handle.data = p; 115 | p->context = ac; 116 | 117 | return REDIS_OK; 118 | } 119 | #endif 120 | -------------------------------------------------------------------------------- /include/hiredis/adapters/macosx.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Дмитрий Бахвалов on 13.07.15. 3 | // Copyright (c) 2015 Dmitry Bakhvalov. All rights reserved. 4 | // 5 | 6 | #ifndef __HIREDIS_MACOSX_H__ 7 | #define __HIREDIS_MACOSX_H__ 8 | 9 | #include 10 | 11 | #include "../hiredis.h" 12 | #include "../async.h" 13 | 14 | typedef struct { 15 | redisAsyncContext *context; 16 | CFSocketRef socketRef; 17 | CFRunLoopSourceRef sourceRef; 18 | } RedisRunLoop; 19 | 20 | static int freeRedisRunLoop(RedisRunLoop* redisRunLoop) { 21 | if( redisRunLoop != NULL ) { 22 | if( redisRunLoop->sourceRef != NULL ) { 23 | CFRunLoopSourceInvalidate(redisRunLoop->sourceRef); 24 | CFRelease(redisRunLoop->sourceRef); 25 | } 26 | if( redisRunLoop->socketRef != NULL ) { 27 | CFSocketInvalidate(redisRunLoop->socketRef); 28 | CFRelease(redisRunLoop->socketRef); 29 | } 30 | free(redisRunLoop); 31 | } 32 | return REDIS_ERR; 33 | } 34 | 35 | static void redisMacOSAddRead(void *privdata) { 36 | RedisRunLoop *redisRunLoop = (RedisRunLoop*)privdata; 37 | CFSocketEnableCallBacks(redisRunLoop->socketRef, kCFSocketReadCallBack); 38 | } 39 | 40 | static void redisMacOSDelRead(void *privdata) { 41 | RedisRunLoop *redisRunLoop = (RedisRunLoop*)privdata; 42 | CFSocketDisableCallBacks(redisRunLoop->socketRef, kCFSocketReadCallBack); 43 | } 44 | 45 | static void redisMacOSAddWrite(void *privdata) { 46 | RedisRunLoop *redisRunLoop = (RedisRunLoop*)privdata; 47 | CFSocketEnableCallBacks(redisRunLoop->socketRef, kCFSocketWriteCallBack); 48 | } 49 | 50 | static void redisMacOSDelWrite(void *privdata) { 51 | RedisRunLoop *redisRunLoop = (RedisRunLoop*)privdata; 52 | CFSocketDisableCallBacks(redisRunLoop->socketRef, kCFSocketWriteCallBack); 53 | } 54 | 55 | static void redisMacOSCleanup(void *privdata) { 56 | RedisRunLoop *redisRunLoop = (RedisRunLoop*)privdata; 57 | freeRedisRunLoop(redisRunLoop); 58 | } 59 | 60 | static void redisMacOSAsyncCallback(CFSocketRef __unused s, CFSocketCallBackType callbackType, CFDataRef __unused address, const void __unused *data, void *info) { 61 | redisAsyncContext* context = (redisAsyncContext*) info; 62 | 63 | switch (callbackType) { 64 | case kCFSocketReadCallBack: 65 | redisAsyncHandleRead(context); 66 | break; 67 | 68 | case kCFSocketWriteCallBack: 69 | redisAsyncHandleWrite(context); 70 | break; 71 | 72 | default: 73 | break; 74 | } 75 | } 76 | 77 | static int redisMacOSAttach(redisAsyncContext *redisAsyncCtx, CFRunLoopRef runLoop) { 78 | redisContext *redisCtx = &(redisAsyncCtx->c); 79 | 80 | /* Nothing should be attached when something is already attached */ 81 | if( redisAsyncCtx->ev.data != NULL ) return REDIS_ERR; 82 | 83 | RedisRunLoop* redisRunLoop = (RedisRunLoop*) calloc(1, sizeof(RedisRunLoop)); 84 | if( !redisRunLoop ) return REDIS_ERR; 85 | 86 | /* Setup redis stuff */ 87 | redisRunLoop->context = redisAsyncCtx; 88 | 89 | redisAsyncCtx->ev.addRead = redisMacOSAddRead; 90 | redisAsyncCtx->ev.delRead = redisMacOSDelRead; 91 | redisAsyncCtx->ev.addWrite = redisMacOSAddWrite; 92 | redisAsyncCtx->ev.delWrite = redisMacOSDelWrite; 93 | redisAsyncCtx->ev.cleanup = redisMacOSCleanup; 94 | redisAsyncCtx->ev.data = redisRunLoop; 95 | 96 | /* Initialize and install read/write events */ 97 | CFSocketContext socketCtx = { 0, redisAsyncCtx, NULL, NULL, NULL }; 98 | 99 | redisRunLoop->socketRef = CFSocketCreateWithNative(NULL, redisCtx->fd, 100 | kCFSocketReadCallBack | kCFSocketWriteCallBack, 101 | redisMacOSAsyncCallback, 102 | &socketCtx); 103 | if( !redisRunLoop->socketRef ) return freeRedisRunLoop(redisRunLoop); 104 | 105 | redisRunLoop->sourceRef = CFSocketCreateRunLoopSource(NULL, redisRunLoop->socketRef, 0); 106 | if( !redisRunLoop->sourceRef ) return freeRedisRunLoop(redisRunLoop); 107 | 108 | CFRunLoopAddSource(runLoop, redisRunLoop->sourceRef, kCFRunLoopDefaultMode); 109 | 110 | return REDIS_OK; 111 | } 112 | 113 | #endif 114 | 115 | -------------------------------------------------------------------------------- /include/hiredis/adapters/qt.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (C) 2014 Pietro Cerutti 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 1. Redistributions of source code must retain the above copyright 8 | * notice, this list of conditions and the following disclaimer. 9 | * 2. Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * 13 | * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 | * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE 17 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 | * SUCH DAMAGE. 24 | */ 25 | 26 | #ifndef __HIREDIS_QT_H__ 27 | #define __HIREDIS_QT_H__ 28 | #include 29 | #include "../async.h" 30 | 31 | static void RedisQtAddRead(void *); 32 | static void RedisQtDelRead(void *); 33 | static void RedisQtAddWrite(void *); 34 | static void RedisQtDelWrite(void *); 35 | static void RedisQtCleanup(void *); 36 | 37 | class RedisQtAdapter : public QObject { 38 | 39 | Q_OBJECT 40 | 41 | friend 42 | void RedisQtAddRead(void * adapter) { 43 | RedisQtAdapter * a = static_cast(adapter); 44 | a->addRead(); 45 | } 46 | 47 | friend 48 | void RedisQtDelRead(void * adapter) { 49 | RedisQtAdapter * a = static_cast(adapter); 50 | a->delRead(); 51 | } 52 | 53 | friend 54 | void RedisQtAddWrite(void * adapter) { 55 | RedisQtAdapter * a = static_cast(adapter); 56 | a->addWrite(); 57 | } 58 | 59 | friend 60 | void RedisQtDelWrite(void * adapter) { 61 | RedisQtAdapter * a = static_cast(adapter); 62 | a->delWrite(); 63 | } 64 | 65 | friend 66 | void RedisQtCleanup(void * adapter) { 67 | RedisQtAdapter * a = static_cast(adapter); 68 | a->cleanup(); 69 | } 70 | 71 | public: 72 | RedisQtAdapter(QObject * parent = 0) 73 | : QObject(parent), m_ctx(0), m_read(0), m_write(0) { } 74 | 75 | ~RedisQtAdapter() { 76 | if (m_ctx != 0) { 77 | m_ctx->ev.data = NULL; 78 | } 79 | } 80 | 81 | int setContext(redisAsyncContext * ac) { 82 | if (ac->ev.data != NULL) { 83 | return REDIS_ERR; 84 | } 85 | m_ctx = ac; 86 | m_ctx->ev.data = this; 87 | m_ctx->ev.addRead = RedisQtAddRead; 88 | m_ctx->ev.delRead = RedisQtDelRead; 89 | m_ctx->ev.addWrite = RedisQtAddWrite; 90 | m_ctx->ev.delWrite = RedisQtDelWrite; 91 | m_ctx->ev.cleanup = RedisQtCleanup; 92 | return REDIS_OK; 93 | } 94 | 95 | private: 96 | void addRead() { 97 | if (m_read) return; 98 | m_read = new QSocketNotifier(m_ctx->c.fd, QSocketNotifier::Read, 0); 99 | connect(m_read, SIGNAL(activated(int)), this, SLOT(read())); 100 | } 101 | 102 | void delRead() { 103 | if (!m_read) return; 104 | delete m_read; 105 | m_read = 0; 106 | } 107 | 108 | void addWrite() { 109 | if (m_write) return; 110 | m_write = new QSocketNotifier(m_ctx->c.fd, QSocketNotifier::Write, 0); 111 | connect(m_write, SIGNAL(activated(int)), this, SLOT(write())); 112 | } 113 | 114 | void delWrite() { 115 | if (!m_write) return; 116 | delete m_write; 117 | m_write = 0; 118 | } 119 | 120 | void cleanup() { 121 | delRead(); 122 | delWrite(); 123 | } 124 | 125 | private slots: 126 | void read() { redisAsyncHandleRead(m_ctx); } 127 | void write() { redisAsyncHandleWrite(m_ctx); } 128 | 129 | private: 130 | redisAsyncContext * m_ctx; 131 | QSocketNotifier * m_read; 132 | QSocketNotifier * m_write; 133 | }; 134 | 135 | #endif /* !__HIREDIS_QT_H__ */ 136 | -------------------------------------------------------------------------------- /include/hiredis/read.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009-2011, Salvatore Sanfilippo 3 | * Copyright (c) 2010-2011, Pieter Noordhuis 4 | * 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * * Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * * Redistributions in binary form must reproduce the above copyright 13 | * notice, this list of conditions and the following disclaimer in the 14 | * documentation and/or other materials provided with the distribution. 15 | * * Neither the name of Redis nor the names of its contributors may be used 16 | * to endorse or promote products derived from this software without 17 | * specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | 33 | #ifndef __HIREDIS_READ_H 34 | #define __HIREDIS_READ_H 35 | #include /* for size_t */ 36 | 37 | #define REDIS_ERR -1 38 | #define REDIS_OK 0 39 | 40 | /* When an error occurs, the err flag in a context is set to hold the type of 41 | * error that occurred. REDIS_ERR_IO means there was an I/O error and you 42 | * should use the "errno" variable to find out what is wrong. 43 | * For other values, the "errstr" field will hold a description. */ 44 | #define REDIS_ERR_IO 1 /* Error in read or write */ 45 | #define REDIS_ERR_EOF 3 /* End of file */ 46 | #define REDIS_ERR_PROTOCOL 4 /* Protocol error */ 47 | #define REDIS_ERR_OOM 5 /* Out of memory */ 48 | #define REDIS_ERR_TIMEOUT 6 /* Timed out */ 49 | #define REDIS_ERR_OTHER 2 /* Everything else... */ 50 | 51 | #define REDIS_REPLY_STRING 1 52 | #define REDIS_REPLY_ARRAY 2 53 | #define REDIS_REPLY_INTEGER 3 54 | #define REDIS_REPLY_NIL 4 55 | #define REDIS_REPLY_STATUS 5 56 | #define REDIS_REPLY_ERROR 6 57 | #define REDIS_REPLY_DOUBLE 7 58 | #define REDIS_REPLY_BOOL 8 59 | #define REDIS_REPLY_VERB 9 60 | #define REDIS_REPLY_MAP 9 61 | #define REDIS_REPLY_SET 10 62 | #define REDIS_REPLY_ATTR 11 63 | #define REDIS_REPLY_PUSH 12 64 | #define REDIS_REPLY_BIGNUM 13 65 | 66 | #define REDIS_READER_MAX_BUF (1024*16) /* Default max unused reader buffer. */ 67 | 68 | #ifdef __cplusplus 69 | extern "C" { 70 | #endif 71 | 72 | typedef struct redisReadTask { 73 | int type; 74 | int elements; /* number of elements in multibulk container */ 75 | int idx; /* index in parent (array) object */ 76 | void *obj; /* holds user-generated value for a read task */ 77 | struct redisReadTask *parent; /* parent task */ 78 | void *privdata; /* user-settable arbitrary field */ 79 | } redisReadTask; 80 | 81 | typedef struct redisReplyObjectFunctions { 82 | void *(*createString)(const redisReadTask*, char*, size_t); 83 | void *(*createArray)(const redisReadTask*, size_t); 84 | void *(*createInteger)(const redisReadTask*, long long); 85 | void *(*createDouble)(const redisReadTask*, double, char*, size_t); 86 | void *(*createNil)(const redisReadTask*); 87 | void *(*createBool)(const redisReadTask*, int); 88 | void (*freeObject)(void*); 89 | } redisReplyObjectFunctions; 90 | 91 | typedef struct redisReader { 92 | int err; /* Error flags, 0 when there is no error */ 93 | char errstr[128]; /* String representation of error when applicable */ 94 | 95 | char *buf; /* Read buffer */ 96 | size_t pos; /* Buffer cursor */ 97 | size_t len; /* Buffer length */ 98 | size_t maxbuf; /* Max length of unused buffer */ 99 | 100 | redisReadTask rstack[9]; 101 | int ridx; /* Index of current read task */ 102 | void *reply; /* Temporary reply pointer */ 103 | 104 | redisReplyObjectFunctions *fn; 105 | void *privdata; 106 | } redisReader; 107 | 108 | /* Public API for the protocol parser. */ 109 | redisReader *redisReaderCreateWithFunctions(redisReplyObjectFunctions *fn); 110 | void redisReaderFree(redisReader *r); 111 | int redisReaderFeed(redisReader *r, const char *buf, size_t len); 112 | int redisReaderGetReply(redisReader *r, void **reply); 113 | 114 | #define redisReaderSetPrivdata(_r, _p) (int)(((redisReader*)(_r))->privdata = (_p)) 115 | #define redisReaderGetObject(_r) (((redisReader*)(_r))->reply) 116 | #define redisReaderGetError(_r) (((redisReader*)(_r))->errstr) 117 | 118 | #ifdef __cplusplus 119 | } 120 | #endif 121 | 122 | #endif 123 | -------------------------------------------------------------------------------- /include/interface.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "types.h" 3 | 4 | struct IDataBase 5 | { 6 | virtual ~IDataBase(){} 7 | virtual bool Init() = 0; 8 | virtual bool OpenConnection() = 0; 9 | virtual void CloseConnection() = 0; 10 | 11 | virtual void SetSpName(const char* szSpName) = 0; 12 | virtual bool ExecPro() = 0; 13 | virtual bool ExecSql(const char* szSql) = 0; 14 | 15 | virtual bool HaveNext() = 0; 16 | virtual void MoveNext() = 0; 17 | 18 | virtual long long GetNumValue(const char* szFild,long long nDef = 0) = 0; 19 | virtual const char* GetStrValue(const char* szFild,const char* szDef = NULL) = 0; 20 | virtual long long GetOutNumValue(const char* szStrFild,long long nDef = 0) = 0; 21 | virtual const char* GetOutStrValue(const char* szFild,const char* szDef = NULL) = 0; 22 | 23 | virtual void AddNumParam(long long llValue) = 0; 24 | virtual void AddStrParam(const char* szValue) = 0; 25 | virtual void AddOutParam(const char* szOutParam) = 0; 26 | }; 27 | -------------------------------------------------------------------------------- /include/json/autolink.h: -------------------------------------------------------------------------------- 1 | #ifndef JSON_AUTOLINK_H_INCLUDED 2 | # define JSON_AUTOLINK_H_INCLUDED 3 | 4 | # include "config.h" 5 | 6 | # ifdef JSON_IN_CPPTL 7 | # include 8 | # endif 9 | 10 | # if !defined(JSON_NO_AUTOLINK) && !defined(JSON_DLL_BUILD) && !defined(JSON_IN_CPPTL) 11 | # define CPPTL_AUTOLINK_NAME "json" 12 | # undef CPPTL_AUTOLINK_DLL 13 | # ifdef JSON_DLL 14 | # define CPPTL_AUTOLINK_DLL 15 | # endif 16 | # include "autolink.h" 17 | # endif 18 | 19 | #endif // JSON_AUTOLINK_H_INCLUDED 20 | -------------------------------------------------------------------------------- /include/json/config.h: -------------------------------------------------------------------------------- 1 | #ifndef JSON_CONFIG_H_INCLUDED 2 | # define JSON_CONFIG_H_INCLUDED 3 | 4 | /// If defined, indicates that json library is embedded in CppTL library. 5 | //# define JSON_IN_CPPTL 1 6 | 7 | /// If defined, indicates that json may leverage CppTL library 8 | //# define JSON_USE_CPPTL 1 9 | /// If defined, indicates that cpptl vector based map should be used instead of std::map 10 | /// as Value container. 11 | //# define JSON_USE_CPPTL_SMALLMAP 1 12 | /// If defined, indicates that Json specific container should be used 13 | /// (hash table & simple deque container with customizable allocator). 14 | /// THIS FEATURE IS STILL EXPERIMENTAL! 15 | //# define JSON_VALUE_USE_INTERNAL_MAP 1 16 | /// Force usage of standard new/malloc based allocator instead of memory pool based allocator. 17 | /// The memory pools allocator used optimization (initializing Value and ValueInternalLink 18 | /// as if it was a POD) that may cause some validation tool to report errors. 19 | /// Only has effects if JSON_VALUE_USE_INTERNAL_MAP is defined. 20 | //# define JSON_USE_SIMPLE_INTERNAL_ALLOCATOR 1 21 | 22 | /// If defined, indicates that Json use exception to report invalid type manipulation 23 | /// instead of C assert macro. 24 | # define JSON_USE_EXCEPTION 1 25 | 26 | # ifdef JSON_IN_CPPTL 27 | # include 28 | # ifndef JSON_USE_CPPTL 29 | # define JSON_USE_CPPTL 1 30 | # endif 31 | # endif 32 | 33 | # ifdef JSON_IN_CPPTL 34 | # define JSON_API CPPTL_API 35 | # elif defined(JSON_DLL_BUILD) 36 | # define JSON_API __declspec(dllexport) 37 | # elif defined(JSON_DLL) 38 | # define JSON_API __declspec(dllimport) 39 | # else 40 | # define JSON_API 41 | # endif 42 | 43 | #endif // JSON_CONFIG_H_INCLUDED 44 | -------------------------------------------------------------------------------- /include/json/features.h: -------------------------------------------------------------------------------- 1 | #ifndef CPPTL_JSON_FEATURES_H_INCLUDED 2 | # define CPPTL_JSON_FEATURES_H_INCLUDED 3 | 4 | # include "forwards.h" 5 | 6 | namespace Json { 7 | 8 | /** \brief Configuration passed to reader and writer. 9 | * This configuration object can be used to force the Reader or Writer 10 | * to behave in a standard conforming way. 11 | */ 12 | class JSON_API Features 13 | { 14 | public: 15 | /** \brief A configuration that allows all features and assumes all strings are UTF-8. 16 | * - C & C++ comments are allowed 17 | * - Root object can be any JSON value 18 | * - Assumes Value strings are encoded in UTF-8 19 | */ 20 | static Features all(); 21 | 22 | /** \brief A configuration that is strictly compatible with the JSON specification. 23 | * - Comments are forbidden. 24 | * - Root object must be either an array or an object value. 25 | * - Assumes Value strings are encoded in UTF-8 26 | */ 27 | static Features strictMode(); 28 | 29 | /** \brief Initialize the configuration like JsonConfig::allFeatures; 30 | */ 31 | Features(); 32 | 33 | /// \c true if comments are allowed. Default: \c true. 34 | bool allowComments_; 35 | 36 | /// \c true if root must be either an array or an object value. Default: \c false. 37 | bool strictRoot_; 38 | }; 39 | 40 | } // namespace Json 41 | 42 | #endif // CPPTL_JSON_FEATURES_H_INCLUDED 43 | -------------------------------------------------------------------------------- /include/json/forwards.h: -------------------------------------------------------------------------------- 1 | #ifndef JSON_FORWARDS_H_INCLUDED 2 | # define JSON_FORWARDS_H_INCLUDED 3 | 4 | # include "config.h" 5 | 6 | namespace Json { 7 | 8 | // writer.h 9 | class FastWriter; 10 | class StyledWriter; 11 | 12 | // reader.h 13 | class Reader; 14 | 15 | // features.h 16 | class Features; 17 | 18 | // value.h 19 | typedef int Int; 20 | typedef unsigned int UInt; 21 | class StaticString; 22 | class Path; 23 | class PathArgument; 24 | class Value; 25 | class ValueIteratorBase; 26 | class ValueIterator; 27 | class ValueConstIterator; 28 | #ifdef JSON_VALUE_USE_INTERNAL_MAP 29 | class ValueAllocator; 30 | class ValueMapAllocator; 31 | class ValueInternalLink; 32 | class ValueInternalArray; 33 | class ValueInternalMap; 34 | #endif // #ifdef JSON_VALUE_USE_INTERNAL_MAP 35 | 36 | } // namespace Json 37 | 38 | 39 | #endif // JSON_FORWARDS_H_INCLUDED 40 | -------------------------------------------------------------------------------- /include/json/json.h: -------------------------------------------------------------------------------- 1 | #ifndef JSON_JSON_H_INCLUDED 2 | # define JSON_JSON_H_INCLUDED 3 | 4 | # include "autolink.h" 5 | # include "value.h" 6 | # include "reader.h" 7 | # include "writer.h" 8 | # include "features.h" 9 | 10 | #endif // JSON_JSON_H_INCLUDED 11 | -------------------------------------------------------------------------------- /include/mysql/decimal.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved. 2 | 3 | This program is free software; you can redistribute it and/or modify 4 | it under the terms of the GNU General Public License as published by 5 | the Free Software Foundation; version 2 of the License. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program; if not, write to the Free Software 14 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ 15 | 16 | #ifndef _decimal_h 17 | #define _decimal_h 18 | 19 | typedef enum 20 | {TRUNCATE=0, HALF_EVEN, HALF_UP, CEILING, FLOOR} 21 | decimal_round_mode; 22 | typedef int32 decimal_digit_t; 23 | 24 | /** 25 | intg is the number of *decimal* digits (NOT number of decimal_digit_t's !) 26 | before the point 27 | frac is the number of decimal digits after the point 28 | len is the length of buf (length of allocated space) in decimal_digit_t's, 29 | not in bytes 30 | sign false means positive, true means negative 31 | buf is an array of decimal_digit_t's 32 | */ 33 | typedef struct st_decimal_t { 34 | int intg, frac, len; 35 | my_bool sign; 36 | decimal_digit_t *buf; 37 | } decimal_t; 38 | 39 | int internal_str2dec(const char *from, decimal_t *to, char **end, 40 | my_bool fixed); 41 | int decimal2string(decimal_t *from, char *to, int *to_len, 42 | int fixed_precision, int fixed_decimals, 43 | char filler); 44 | int decimal2ulonglong(decimal_t *from, ulonglong *to); 45 | int ulonglong2decimal(ulonglong from, decimal_t *to); 46 | int decimal2longlong(decimal_t *from, longlong *to); 47 | int longlong2decimal(longlong from, decimal_t *to); 48 | int decimal2double(decimal_t *from, double *to); 49 | int double2decimal(double from, decimal_t *to); 50 | int decimal_actual_fraction(decimal_t *from); 51 | int decimal2bin(decimal_t *from, uchar *to, int precision, int scale); 52 | int bin2decimal(const uchar *from, decimal_t *to, int precision, int scale); 53 | 54 | int decimal_size(int precision, int scale); 55 | int decimal_bin_size(int precision, int scale); 56 | int decimal_result_size(decimal_t *from1, decimal_t *from2, char op, 57 | int param); 58 | 59 | int decimal_intg(decimal_t *from); 60 | int decimal_add(decimal_t *from1, decimal_t *from2, decimal_t *to); 61 | int decimal_sub(decimal_t *from1, decimal_t *from2, decimal_t *to); 62 | int decimal_cmp(decimal_t *from1, decimal_t *from2); 63 | int decimal_mul(decimal_t *from1, decimal_t *from2, decimal_t *to); 64 | int decimal_div(decimal_t *from1, decimal_t *from2, decimal_t *to, 65 | int scale_incr); 66 | int decimal_mod(decimal_t *from1, decimal_t *from2, decimal_t *to); 67 | int decimal_round(decimal_t *from, decimal_t *to, int new_scale, 68 | decimal_round_mode mode); 69 | int decimal_is_zero(decimal_t *from); 70 | void max_decimal(int precision, int frac, decimal_t *to); 71 | 72 | #define string2decimal(A,B,C) internal_str2dec((A), (B), (C), 0) 73 | #define string2decimal_fixed(A,B,C) internal_str2dec((A), (B), (C), 1) 74 | 75 | /* set a decimal_t to zero */ 76 | 77 | #define decimal_make_zero(dec) do { \ 78 | (dec)->buf[0]=0; \ 79 | (dec)->intg=1; \ 80 | (dec)->frac=0; \ 81 | (dec)->sign=0; \ 82 | } while(0) 83 | 84 | /* 85 | returns the length of the buffer to hold string representation 86 | of the decimal (including decimal dot, possible sign and \0) 87 | */ 88 | 89 | #define decimal_string_size(dec) (((dec)->intg ? (dec)->intg : 1) + \ 90 | (dec)->frac + ((dec)->frac > 0) + 2) 91 | 92 | /* negate a decimal */ 93 | #define decimal_neg(dec) do { (dec)->sign^=1; } while(0) 94 | 95 | /* 96 | conventions: 97 | 98 | decimal_smth() == 0 -- everything's ok 99 | decimal_smth() <= 1 -- result is usable, but precision loss is possible 100 | decimal_smth() <= 2 -- result can be unusable, most significant digits 101 | could've been lost 102 | decimal_smth() > 2 -- no result was generated 103 | */ 104 | 105 | #define E_DEC_OK 0 106 | #define E_DEC_TRUNCATED 1 107 | #define E_DEC_OVERFLOW 2 108 | #define E_DEC_DIV_ZERO 4 109 | #define E_DEC_BAD_NUM 8 110 | #define E_DEC_OOM 16 111 | 112 | #define E_DEC_ERROR 31 113 | #define E_DEC_FATAL_ERROR 30 114 | 115 | #endif 116 | 117 | -------------------------------------------------------------------------------- /include/mysql/errmsg.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2000-2008 MySQL AB 2 | 3 | This program is free software; you can redistribute it and/or modify 4 | it under the terms of the GNU General Public License as published by 5 | the Free Software Foundation; version 2 of the License. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program; if not, write to the Free Software 14 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ 15 | 16 | /* Error messages for MySQL clients */ 17 | /* (Error messages for the daemon are in sql/share/errmsg.txt) */ 18 | 19 | #ifdef __cplusplus 20 | extern "C" { 21 | #endif 22 | void init_client_errs(void); 23 | void finish_client_errs(void); 24 | extern const char *client_errors[]; /* Error messages */ 25 | #ifdef __cplusplus 26 | } 27 | #endif 28 | 29 | #define CR_MIN_ERROR 2000 /* For easier client code */ 30 | #define CR_MAX_ERROR 2999 31 | #if !defined(ER) 32 | #define ER(X) client_errors[(X)-CR_MIN_ERROR] 33 | #endif 34 | #define CLIENT_ERRMAP 2 /* Errormap used by my_error() */ 35 | 36 | /* Do not add error numbers before CR_ERROR_FIRST. */ 37 | /* If necessary to add lower numbers, change CR_ERROR_FIRST accordingly. */ 38 | #define CR_ERROR_FIRST 2000 /*Copy first error nr.*/ 39 | #define CR_UNKNOWN_ERROR 2000 40 | #define CR_SOCKET_CREATE_ERROR 2001 41 | #define CR_CONNECTION_ERROR 2002 42 | #define CR_CONN_HOST_ERROR 2003 43 | #define CR_IPSOCK_ERROR 2004 44 | #define CR_UNKNOWN_HOST 2005 45 | #define CR_SERVER_GONE_ERROR 2006 46 | #define CR_VERSION_ERROR 2007 47 | #define CR_OUT_OF_MEMORY 2008 48 | #define CR_WRONG_HOST_INFO 2009 49 | #define CR_LOCALHOST_CONNECTION 2010 50 | #define CR_TCP_CONNECTION 2011 51 | #define CR_SERVER_HANDSHAKE_ERR 2012 52 | #define CR_SERVER_LOST 2013 53 | #define CR_COMMANDS_OUT_OF_SYNC 2014 54 | #define CR_NAMEDPIPE_CONNECTION 2015 55 | #define CR_NAMEDPIPEWAIT_ERROR 2016 56 | #define CR_NAMEDPIPEOPEN_ERROR 2017 57 | #define CR_NAMEDPIPESETSTATE_ERROR 2018 58 | #define CR_CANT_READ_CHARSET 2019 59 | #define CR_NET_PACKET_TOO_LARGE 2020 60 | #define CR_EMBEDDED_CONNECTION 2021 61 | #define CR_PROBE_SLAVE_STATUS 2022 62 | #define CR_PROBE_SLAVE_HOSTS 2023 63 | #define CR_PROBE_SLAVE_CONNECT 2024 64 | #define CR_PROBE_MASTER_CONNECT 2025 65 | #define CR_SSL_CONNECTION_ERROR 2026 66 | #define CR_MALFORMED_PACKET 2027 67 | #define CR_WRONG_LICENSE 2028 68 | 69 | /* new 4.1 error codes */ 70 | #define CR_NULL_POINTER 2029 71 | #define CR_NO_PREPARE_STMT 2030 72 | #define CR_PARAMS_NOT_BOUND 2031 73 | #define CR_DATA_TRUNCATED 2032 74 | #define CR_NO_PARAMETERS_EXISTS 2033 75 | #define CR_INVALID_PARAMETER_NO 2034 76 | #define CR_INVALID_BUFFER_USE 2035 77 | #define CR_UNSUPPORTED_PARAM_TYPE 2036 78 | 79 | #define CR_SHARED_MEMORY_CONNECTION 2037 80 | #define CR_SHARED_MEMORY_CONNECT_REQUEST_ERROR 2038 81 | #define CR_SHARED_MEMORY_CONNECT_ANSWER_ERROR 2039 82 | #define CR_SHARED_MEMORY_CONNECT_FILE_MAP_ERROR 2040 83 | #define CR_SHARED_MEMORY_CONNECT_MAP_ERROR 2041 84 | #define CR_SHARED_MEMORY_FILE_MAP_ERROR 2042 85 | #define CR_SHARED_MEMORY_MAP_ERROR 2043 86 | #define CR_SHARED_MEMORY_EVENT_ERROR 2044 87 | #define CR_SHARED_MEMORY_CONNECT_ABANDONED_ERROR 2045 88 | #define CR_SHARED_MEMORY_CONNECT_SET_ERROR 2046 89 | #define CR_CONN_UNKNOW_PROTOCOL 2047 90 | #define CR_INVALID_CONN_HANDLE 2048 91 | #define CR_SECURE_AUTH 2049 92 | #define CR_FETCH_CANCELED 2050 93 | #define CR_NO_DATA 2051 94 | #define CR_NO_STMT_METADATA 2052 95 | #define CR_NO_RESULT_SET 2053 96 | #define CR_NOT_IMPLEMENTED 2054 97 | #define CR_SERVER_LOST_EXTENDED 2055 98 | #define CR_STMT_CLOSED 2056 99 | #define CR_NEW_STMT_METADATA 2057 100 | #define CR_ERROR_LAST /*Copy last error nr:*/ 2057 101 | /* Add error numbers before CR_ERROR_LAST and change it accordingly. */ 102 | 103 | -------------------------------------------------------------------------------- /include/mysql/my_alloc.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2000, 2002, 2005, 2007 MySQL AB 2 | 3 | This program is free software; you can redistribute it and/or modify 4 | it under the terms of the GNU General Public License as published by 5 | the Free Software Foundation; version 2 of the License. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program; if not, write to the Free Software 14 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ 15 | 16 | /* 17 | Data structures for mysys/my_alloc.c (root memory allocator) 18 | */ 19 | 20 | #ifndef _my_alloc_h 21 | #define _my_alloc_h 22 | 23 | #define ALLOC_MAX_BLOCK_TO_DROP 4096 24 | #define ALLOC_MAX_BLOCK_USAGE_BEFORE_DROP 10 25 | 26 | typedef struct st_used_mem 27 | { /* struct for once_alloc (block) */ 28 | struct st_used_mem *next; /* Next block in use */ 29 | unsigned int left; /* memory left in block */ 30 | unsigned int size; /* size of block */ 31 | } USED_MEM; 32 | 33 | 34 | typedef struct st_mem_root 35 | { 36 | USED_MEM *free; /* blocks with free memory in it */ 37 | USED_MEM *used; /* blocks almost without free memory */ 38 | USED_MEM *pre_alloc; /* preallocated block */ 39 | /* if block have less memory it will be put in 'used' list */ 40 | size_t min_malloc; 41 | size_t block_size; /* initial block size */ 42 | unsigned int block_num; /* allocated blocks counter */ 43 | /* 44 | first free block in queue test counter (if it exceed 45 | MAX_BLOCK_USAGE_BEFORE_DROP block will be dropped in 'used' list) 46 | */ 47 | unsigned int first_block_usage; 48 | 49 | void (*error_handler)(void); 50 | } MEM_ROOT; 51 | #endif 52 | -------------------------------------------------------------------------------- /include/mysql/my_attribute.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2000-2003, 2007 MySQL AB 2 | 3 | This program is free software; you can redistribute it and/or modify 4 | it under the terms of the GNU General Public License as published by 5 | the Free Software Foundation; version 2 of the License. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program; if not, write to the Free Software 14 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ 15 | 16 | /* 17 | Helper macros used for setting different __attributes__ 18 | on functions in a portable fashion 19 | */ 20 | 21 | #ifndef _my_attribute_h 22 | #define _my_attribute_h 23 | 24 | /* 25 | Disable __attribute__() on gcc < 2.7, g++ < 3.4, and non-gcc compilers. 26 | Some forms of __attribute__ are actually supported in earlier versions of 27 | g++, but we just disable them all because we only use them to generate 28 | compilation warnings. 29 | */ 30 | #ifndef __attribute__ 31 | # if !defined(__GNUC__) 32 | # define __attribute__(A) 33 | # elif GCC_VERSION < 2008 34 | # define __attribute__(A) 35 | # elif defined(__cplusplus) && GCC_VERSION < 3004 36 | # define __attribute__(A) 37 | # endif 38 | #endif 39 | 40 | /* 41 | __attribute__((format(...))) is only supported in gcc >= 2.8 and g++ >= 3.4 42 | But that's already covered by the __attribute__ tests above, so this is 43 | just a convenience macro. 44 | */ 45 | #ifndef ATTRIBUTE_FORMAT 46 | # define ATTRIBUTE_FORMAT(style, m, n) __attribute__((format(style, m, n))) 47 | #endif 48 | 49 | /* 50 | 51 | __attribute__((format(...))) on a function pointer is not supported 52 | until gcc 3.1 53 | */ 54 | #ifndef ATTRIBUTE_FORMAT_FPTR 55 | # if (GCC_VERSION >= 3001) 56 | # define ATTRIBUTE_FORMAT_FPTR(style, m, n) ATTRIBUTE_FORMAT(style, m, n) 57 | # else 58 | # define ATTRIBUTE_FORMAT_FPTR(style, m, n) 59 | # endif /* GNUC >= 3.1 */ 60 | #endif 61 | 62 | 63 | #endif 64 | -------------------------------------------------------------------------------- /include/mysql/my_compiler.h: -------------------------------------------------------------------------------- 1 | #ifndef MY_COMPILER_INCLUDED 2 | #define MY_COMPILER_INCLUDED 3 | 4 | /* Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved. 5 | 6 | This program is free software; you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation; version 2 of the License. 9 | 10 | This program is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with this program; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ 18 | 19 | /** 20 | Header for compiler-dependent features. 21 | 22 | Intended to contain a set of reusable wrappers for preprocessor 23 | macros, attributes, pragmas, and any other features that are 24 | specific to a target compiler. 25 | */ 26 | 27 | #include /* stddef.h offsetof */ 28 | 29 | /** 30 | Compiler-dependent internal convenience macros. 31 | */ 32 | 33 | /* GNU C/C++ */ 34 | #if defined __GNUC__ 35 | /* Convenience macro to test the minimum required GCC version. */ 36 | # define MY_GNUC_PREREQ(maj, min) \ 37 | ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min)) 38 | /* Any after 2.95... */ 39 | # define MY_ALIGN_EXT 40 | /* Comunicate to the compiler the unreachability of the code. */ 41 | # if MY_GNUC_PREREQ(4,5) 42 | # define MY_ASSERT_UNREACHABLE() __builtin_unreachable() 43 | # endif 44 | 45 | /* Microsoft Visual C++ */ 46 | #elif defined _MSC_VER 47 | # define MY_ALIGNOF(type) __alignof(type) 48 | # define MY_ALIGNED(n) __declspec(align(n)) 49 | 50 | /* Oracle Solaris Studio */ 51 | #elif defined(__SUNPRO_C) || defined(__SUNPRO_CC) 52 | # if __SUNPRO_C >= 0x590 53 | # define MY_ALIGN_EXT 54 | # endif 55 | 56 | /* IBM XL C/C++ */ 57 | #elif defined __xlC__ 58 | # if __xlC__ >= 0x0600 59 | # define MY_ALIGN_EXT 60 | # endif 61 | 62 | /* HP aCC */ 63 | #elif defined(__HP_aCC) || defined(__HP_cc) 64 | # if (__HP_aCC >= 60000) || (__HP_cc >= 60000) 65 | # define MY_ALIGN_EXT 66 | # endif 67 | #endif 68 | 69 | #ifdef MY_ALIGN_EXT 70 | /** Specifies the minimum alignment of a type. */ 71 | # define MY_ALIGNOF(type) __alignof__(type) 72 | /** Determine the alignment requirement of a type. */ 73 | # define MY_ALIGNED(n) __attribute__((__aligned__((n)))) 74 | #endif 75 | 76 | /** 77 | Generic (compiler-independent) features. 78 | */ 79 | 80 | #ifndef MY_GNUC_PREREQ 81 | # define MY_GNUC_PREREQ(maj, min) (0) 82 | #endif 83 | 84 | #ifndef MY_ALIGNOF 85 | # ifdef __cplusplus 86 | template struct my_alignof_helper { char m1; type m2; }; 87 | /* Invalid for non-POD types, but most compilers give the right answer. */ 88 | # define MY_ALIGNOF(type) offsetof(my_alignof_helper, m2) 89 | # else 90 | # define MY_ALIGNOF(type) offsetof(struct { char m1; type m2; }, m2) 91 | # endif 92 | #endif 93 | 94 | #ifndef MY_ASSERT_UNREACHABLE 95 | # define MY_ASSERT_UNREACHABLE() do { assert(0); } while (0) 96 | #endif 97 | 98 | /** 99 | C++ Type Traits 100 | */ 101 | 102 | #ifdef __cplusplus 103 | 104 | /** 105 | Opaque storage with a particular alignment. 106 | */ 107 | # if defined(MY_ALIGNED) 108 | /* Partial specialization used due to MSVC++. */ 109 | template struct my_alignment_imp; 110 | template<> struct MY_ALIGNED(1) my_alignment_imp<1> {}; 111 | template<> struct MY_ALIGNED(2) my_alignment_imp<2> {}; 112 | template<> struct MY_ALIGNED(4) my_alignment_imp<4> {}; 113 | template<> struct MY_ALIGNED(8) my_alignment_imp<8> {}; 114 | template<> struct MY_ALIGNED(16) my_alignment_imp<16> {}; 115 | /* ... expand as necessary. */ 116 | # else 117 | template 118 | struct my_alignment_imp { double m1; }; 119 | # endif 120 | 121 | /** 122 | A POD type with a given size and alignment. 123 | 124 | @remark If the compiler does not support a alignment attribute 125 | (MY_ALIGN macro), the default alignment of a double is 126 | used instead. 127 | 128 | @tparam size The minimum size. 129 | @tparam alignment The desired alignment: 1, 2, 4, 8 or 16. 130 | */ 131 | template 132 | struct my_aligned_storage 133 | { 134 | union 135 | { 136 | char data[size]; 137 | my_alignment_imp align; 138 | }; 139 | }; 140 | 141 | #endif /* __cplusplus */ 142 | 143 | #include 144 | 145 | #endif /* MY_COMPILER_INCLUDED */ 146 | -------------------------------------------------------------------------------- /include/mysql/my_config.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Kluge to support multilib installation of both 32- and 64-bit RPMS: 3 | * we need to arrange that header files that appear in both RPMs are 4 | * identical. Hence, this file is architecture-independent and calls 5 | * in an arch-dependent file that will appear in just one RPM. 6 | * 7 | * To avoid breaking arches not explicitly supported by Red Hat, we 8 | * use this indirection file *only* on known multilib arches. 9 | * 10 | * Note: this may well fail if user tries to use gcc's -I- option. 11 | * But that option is deprecated anyway. 12 | */ 13 | #if defined(__x86_64__) 14 | #include "my_config_x86_64.h" 15 | #elif defined(__i386__) 16 | #include "my_config_i386.h" 17 | #elif defined(__ppc64__) || defined(__powerpc64__) 18 | #include "my_config_ppc64.h" 19 | #elif defined(__ppc__) || defined(__powerpc__) 20 | #include "my_config_ppc.h" 21 | #elif defined(__s390x__) 22 | #include "my_config_s390x.h" 23 | #elif defined(__s390__) 24 | #include "my_config_s390.h" 25 | #elif defined(__sparc__) && defined(__arch64__) 26 | #include "my_config_sparc64.h" 27 | #elif defined(__sparc__) 28 | #include "my_config_sparc.h" 29 | #endif 30 | -------------------------------------------------------------------------------- /include/mysql/my_dir.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2000, 2003 MySQL AB 2 | 3 | This program is free software; you can redistribute it and/or modify 4 | it under the terms of the GNU General Public License as published by 5 | the Free Software Foundation; version 2 of the License. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program; if not, write to the Free Software 14 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ 15 | 16 | #ifndef _my_dir_h 17 | #define _my_dir_h 18 | #ifdef __cplusplus 19 | extern "C" { 20 | #endif 21 | 22 | #ifndef MY_DIR_H 23 | #define MY_DIR_H 24 | 25 | #include 26 | 27 | /* Defines for my_dir and my_stat */ 28 | 29 | #define MY_S_IFMT S_IFMT /* type of file */ 30 | #define MY_S_IFDIR S_IFDIR /* directory */ 31 | #define MY_S_IFCHR S_IFCHR /* character special */ 32 | #define MY_S_IFBLK S_IFBLK /* block special */ 33 | #define MY_S_IFREG S_IFREG /* regular */ 34 | #define MY_S_IFIFO S_IFIFO /* fifo */ 35 | #define MY_S_ISUID S_ISUID /* set user id on execution */ 36 | #define MY_S_ISGID S_ISGID /* set group id on execution */ 37 | #define MY_S_ISVTX S_ISVTX /* save swapped text even after use */ 38 | #define MY_S_IREAD S_IREAD /* read permission, owner */ 39 | #define MY_S_IWRITE S_IWRITE /* write permission, owner */ 40 | #define MY_S_IEXEC S_IEXEC /* execute/search permission, owner */ 41 | 42 | #define MY_S_ISDIR(m) (((m) & MY_S_IFMT) == MY_S_IFDIR) 43 | #define MY_S_ISCHR(m) (((m) & MY_S_IFMT) == MY_S_IFCHR) 44 | #define MY_S_ISBLK(m) (((m) & MY_S_IFMT) == MY_S_IFBLK) 45 | #define MY_S_ISREG(m) (((m) & MY_S_IFMT) == MY_S_IFREG) 46 | #define MY_S_ISFIFO(m) (((m) & MY_S_IFMT) == MY_S_IFIFO) 47 | 48 | #define MY_DONT_SORT 512 /* my_lib; Don't sort files */ 49 | #define MY_WANT_STAT 1024 /* my_lib; stat files */ 50 | 51 | /* typedefs for my_dir & my_stat */ 52 | 53 | #ifdef USE_MY_STAT_STRUCT 54 | 55 | typedef struct my_stat 56 | { 57 | dev_t st_dev; /* major & minor device numbers */ 58 | ino_t st_ino; /* inode number */ 59 | ushort st_mode; /* file permissons (& suid sgid .. bits) */ 60 | short st_nlink; /* number of links to file */ 61 | ushort st_uid; /* user id */ 62 | ushort st_gid; /* group id */ 63 | dev_t st_rdev; /* more major & minor device numbers (???) */ 64 | off_t st_size; /* size of file */ 65 | time_t st_atime; /* time for last read */ 66 | time_t st_mtime; /* time for last contens modify */ 67 | time_t st_ctime; /* time for last inode or contents modify */ 68 | } MY_STAT; 69 | 70 | #else 71 | 72 | #define MY_STAT struct stat /* Orginal struct have what we need */ 73 | 74 | #endif /* USE_MY_STAT_STRUCT */ 75 | 76 | /* Struct describing one file returned from my_dir */ 77 | typedef struct fileinfo 78 | { 79 | char *name; 80 | MY_STAT *mystat; 81 | } FILEINFO; 82 | 83 | typedef struct st_my_dir /* Struct returned from my_dir */ 84 | { 85 | /* 86 | These members are just copies of parts of DYNAMIC_ARRAY structure, 87 | which is allocated right after the end of MY_DIR structure (MEM_ROOT 88 | for storing names is also resides there). We've left them here because 89 | we don't want to change code that uses my_dir. 90 | */ 91 | struct fileinfo *dir_entry; 92 | uint number_off_files; 93 | } MY_DIR; 94 | 95 | extern MY_DIR *my_dir(const char *path,myf MyFlags); 96 | extern void my_dirend(MY_DIR *buffer); 97 | extern MY_STAT *my_stat(const char *path, MY_STAT *stat_area, myf my_flags); 98 | extern int my_fstat(int filenr, MY_STAT *stat_area, myf MyFlags); 99 | 100 | #endif /* MY_DIR_H */ 101 | 102 | #ifdef __cplusplus 103 | } 104 | #endif 105 | #endif 106 | -------------------------------------------------------------------------------- /include/mysql/my_getopt.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved. 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; version 2 of the License. 7 | 8 | This program is distributed in the hope that it will be useful, 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | GNU General Public License for more details. 12 | 13 | You should have received a copy of the GNU General Public License 14 | along with this program; if not, write to the Free Software 15 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 16 | */ 17 | 18 | #ifndef _my_getopt_h 19 | #define _my_getopt_h 20 | 21 | C_MODE_START 22 | 23 | #define GET_NO_ARG 1 24 | #define GET_BOOL 2 25 | #define GET_INT 3 26 | #define GET_UINT 4 27 | #define GET_LONG 5 28 | #define GET_ULONG 6 29 | #define GET_LL 7 30 | #define GET_ULL 8 31 | #define GET_STR 9 32 | #define GET_STR_ALLOC 10 33 | #define GET_DISABLED 11 34 | #define GET_ENUM 12 35 | #define GET_SET 13 36 | #define GET_DOUBLE 14 37 | 38 | #define GET_ASK_ADDR 128 39 | #define GET_TYPE_MASK 127 40 | 41 | enum get_opt_arg_type { NO_ARG, OPT_ARG, REQUIRED_ARG }; 42 | 43 | struct st_typelib; 44 | 45 | struct my_option 46 | { 47 | const char *name; /* Name of the option */ 48 | int id; /* unique id or short option */ 49 | const char *comment; /* option comment, for autom. --help */ 50 | void *value; /* The variable value */ 51 | void *u_max_value; /* The user def. max variable value */ 52 | struct st_typelib *typelib; /* Pointer to possible values */ 53 | ulong var_type; /* Must match the variable type */ 54 | enum get_opt_arg_type arg_type; 55 | longlong def_value; /* Default value */ 56 | longlong min_value; /* Min allowed value */ 57 | ulonglong max_value; /* Max allowed value */ 58 | longlong sub_size; /* Subtract this from given value */ 59 | long block_size; /* Value should be a mult. of this */ 60 | void *app_type; /* To be used by an application */ 61 | }; 62 | 63 | typedef my_bool (*my_get_one_option)(int, const struct my_option *, char *); 64 | typedef void (*my_error_reporter)(enum loglevel level, const char *format, ...) 65 | ATTRIBUTE_FORMAT_FPTR(printf, 2, 3); 66 | 67 | /** 68 | Used to retrieve a reference to the object (variable) that holds the value 69 | for the given option. For example, if var_type is GET_UINT, the function 70 | must return a pointer to a variable of type uint. A argument is stored in 71 | the location pointed to by the returned pointer. 72 | */ 73 | typedef void *(*my_getopt_value)(const char *, uint, const struct my_option *, 74 | int *); 75 | 76 | extern char *disabled_my_option; 77 | extern my_bool my_getopt_print_errors; 78 | extern my_bool my_getopt_skip_unknown; 79 | extern my_error_reporter my_getopt_error_reporter; 80 | 81 | extern int handle_options (int *argc, char ***argv, 82 | const struct my_option *longopts, my_get_one_option); 83 | extern void my_cleanup_options(const struct my_option *options); 84 | extern void my_print_help(const struct my_option *options); 85 | extern void my_print_variables(const struct my_option *options); 86 | extern void my_getopt_register_get_addr(my_getopt_value); 87 | 88 | ulonglong getopt_ull_limit_value(ulonglong num, const struct my_option *optp, 89 | my_bool *fix); 90 | longlong getopt_ll_limit_value(longlong, const struct my_option *, 91 | my_bool *fix); 92 | my_bool getopt_compare_strings(const char *s, const char *t, uint length); 93 | 94 | C_MODE_END 95 | 96 | #endif /* _my_getopt_h */ 97 | 98 | -------------------------------------------------------------------------------- /include/mysql/my_list.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2000, 2002-2005, 2007 MySQL AB 2 | 3 | This program is free software; you can redistribute it and/or modify 4 | it under the terms of the GNU General Public License as published by 5 | the Free Software Foundation; version 2 of the License. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program; if not, write to the Free Software 14 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ 15 | 16 | #ifndef _list_h_ 17 | #define _list_h_ 18 | 19 | #ifdef __cplusplus 20 | extern "C" { 21 | #endif 22 | 23 | typedef struct st_list { 24 | struct st_list *prev,*next; 25 | void *data; 26 | } LIST; 27 | 28 | typedef int (*list_walk_action)(void *,void *); 29 | 30 | extern LIST *list_add(LIST *root,LIST *element); 31 | extern LIST *list_delete(LIST *root,LIST *element); 32 | extern LIST *list_cons(void *data,LIST *root); 33 | extern LIST *list_reverse(LIST *root); 34 | extern void list_free(LIST *root,unsigned int free_data); 35 | extern unsigned int list_length(LIST *); 36 | extern int list_walk(LIST *,list_walk_action action,unsigned char * argument); 37 | 38 | #define list_rest(a) ((a)->next) 39 | #define list_push(a,b) (a)=list_cons((b),(a)) 40 | #define list_pop(A) {LIST *old=(A); (A)=list_delete(old,old) ; my_free((unsigned char *) old,MYF(MY_FAE)); } 41 | 42 | #ifdef __cplusplus 43 | } 44 | #endif 45 | #endif 46 | -------------------------------------------------------------------------------- /include/mysql/my_net.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2000-2004, 2006 MySQL AB 2 | 3 | This program is free software; you can redistribute it and/or modify 4 | it under the terms of the GNU General Public License as published by 5 | the Free Software Foundation; version 2 of the License. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program; if not, write to the Free Software 14 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ 15 | 16 | /* 17 | thread safe version of some common functions: 18 | my_inet_ntoa 19 | 20 | This file is also used to make handling of sockets and ioctl() 21 | portable accross systems. 22 | 23 | */ 24 | 25 | #ifndef _my_net_h 26 | #define _my_net_h 27 | C_MODE_START 28 | 29 | #include 30 | #ifdef HAVE_SYS_SOCKET_H 31 | #include 32 | #endif 33 | #ifdef HAVE_NETINET_IN_H 34 | #include 35 | #endif 36 | #ifdef HAVE_ARPA_INET_H 37 | #include 38 | #endif 39 | #ifdef HAVE_POLL 40 | #include 41 | #endif 42 | #ifdef HAVE_SYS_IOCTL_H 43 | #include 44 | #endif 45 | 46 | #if !defined(__WIN__) && !defined(HAVE_BROKEN_NETINET_INCLUDES) && !defined(__BEOS__) && !defined(__NETWARE__) 47 | #include 48 | #include 49 | #include 50 | #if !defined(alpha_linux_port) 51 | #include 52 | #endif 53 | #endif 54 | 55 | #if defined(__WIN__) 56 | #define O_NONBLOCK 1 /* For emulation of fcntl() */ 57 | 58 | /* 59 | SHUT_RDWR is called SD_BOTH in windows and 60 | is defined to 2 in winsock2.h 61 | #define SD_BOTH 0x02 62 | */ 63 | #define SHUT_RDWR 0x02 64 | 65 | #endif 66 | 67 | /* 68 | On OSes which don't have the in_addr_t, we guess that using uint32 is the best 69 | possible choice. We guess this from the fact that on HP-UX64bit & FreeBSD64bit 70 | & Solaris64bit, in_addr_t is equivalent to uint32. And on Linux32bit too. 71 | */ 72 | #ifndef HAVE_IN_ADDR_T 73 | #define in_addr_t uint32 74 | #endif 75 | 76 | /* On some operating systems (e.g. Solaris) INADDR_NONE is not defined */ 77 | #ifndef INADDR_NONE 78 | #define INADDR_NONE -1 /* Error value from inet_addr */ 79 | #endif 80 | 81 | /* Thread safe or portable version of some functions */ 82 | 83 | void my_inet_ntoa(struct in_addr in, char *buf); 84 | 85 | /* 86 | Handling of gethostbyname_r() 87 | */ 88 | 89 | #if !defined(HPUX10) 90 | struct hostent; 91 | #endif /* HPUX */ 92 | #if !defined(HAVE_GETHOSTBYNAME_R) 93 | struct hostent *my_gethostbyname_r(const char *name, 94 | struct hostent *result, char *buffer, 95 | int buflen, int *h_errnop); 96 | void my_gethostbyname_r_free(); 97 | #elif defined(HAVE_PTHREAD_ATTR_CREATE) || defined(_AIX) || defined(HAVE_GETHOSTBYNAME_R_GLIBC2_STYLE) 98 | struct hostent *my_gethostbyname_r(const char *name, 99 | struct hostent *result, char *buffer, 100 | int buflen, int *h_errnop); 101 | #define my_gethostbyname_r_free() 102 | #if !defined(HAVE_GETHOSTBYNAME_R_GLIBC2_STYLE) && !defined(HPUX10) 103 | #define GETHOSTBYNAME_BUFF_SIZE sizeof(struct hostent_data) 104 | #endif /* !defined(HAVE_GETHOSTBYNAME_R_GLIBC2_STYLE) */ 105 | 106 | #elif defined(HAVE_GETHOSTBYNAME_R_RETURN_INT) 107 | #define GETHOSTBYNAME_BUFF_SIZE sizeof(struct hostent_data) 108 | struct hostent *my_gethostbyname_r(const char *name, 109 | struct hostent *result, char *buffer, 110 | int buflen, int *h_errnop); 111 | #define my_gethostbyname_r_free() 112 | #else 113 | #define my_gethostbyname_r(A,B,C,D,E) gethostbyname_r((A),(B),(C),(D),(E)) 114 | #define my_gethostbyname_r_free() 115 | #endif /* !defined(HAVE_GETHOSTBYNAME_R) */ 116 | 117 | #ifndef GETHOSTBYNAME_BUFF_SIZE 118 | #define GETHOSTBYNAME_BUFF_SIZE 2048 119 | #endif 120 | 121 | /* On SCO you get a link error when refering to h_errno */ 122 | #ifdef SCO 123 | #undef h_errno 124 | #define h_errno errno 125 | #endif 126 | 127 | C_MODE_END 128 | #endif 129 | -------------------------------------------------------------------------------- /include/mysql/my_no_pthread.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2000, 2002, 2003, 2006 MySQL AB, 2009 Sun Microsystems, Inc. 3 | Use is subject to license terms. 4 | 5 | This program is free software; you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation; version 2 of the License. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program; if not, write to the Free Software 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 17 | */ 18 | 19 | 20 | #if !defined(_my_no_pthread_h) && !defined(THREAD) 21 | #define _my_no_pthread_h 22 | 23 | 24 | /* 25 | This block is to access some thread-related type definitions 26 | even in builds which do not need thread functions, 27 | as some variables (based on these types) are declared 28 | even in non-threaded builds. 29 | Case in point: 'mf_keycache.c' 30 | */ 31 | #if defined(__WIN__) 32 | #else /* Normal threads */ 33 | #include 34 | 35 | #endif /* defined(__WIN__) */ 36 | 37 | 38 | /* 39 | This undefs some pthread mutex locks when one isn't using threads 40 | to make thread safe code, that should also work in single thread 41 | environment, easier to use. 42 | */ 43 | #define pthread_mutex_init(A,B) 44 | #define pthread_mutex_lock(A) 45 | #define pthread_mutex_unlock(A) 46 | #define pthread_mutex_destroy(A) 47 | #define my_rwlock_init(A,B) 48 | #define rw_rdlock(A) 49 | #define rw_wrlock(A) 50 | #define rw_unlock(A) 51 | #define rwlock_destroy(A) 52 | 53 | typedef int my_pthread_once_t; 54 | #define MY_PTHREAD_ONCE_INIT 0 55 | #define MY_PTHREAD_ONCE_DONE 1 56 | 57 | #define my_pthread_once(C,F) do { \ 58 | if (*(C) != MY_PTHREAD_ONCE_DONE) { F(); *(C)= MY_PTHREAD_ONCE_DONE; } \ 59 | } while(0) 60 | 61 | #endif 62 | -------------------------------------------------------------------------------- /include/mysql/my_xml.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2000, 2002, 2003, 2005, 2007 MySQL AB 2 | 3 | This program is free software; you can redistribute it and/or modify 4 | it under the terms of the GNU General Public License as published by 5 | the Free Software Foundation; version 2 of the License. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program; if not, write to the Free Software 14 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ 15 | 16 | 17 | #ifndef _my_xml_h 18 | #define _my_xml_h 19 | 20 | #ifdef __cplusplus 21 | extern "C" { 22 | #endif 23 | 24 | 25 | #define MY_XML_OK 0 26 | #define MY_XML_ERROR 1 27 | 28 | /* 29 | A flag whether to use absolute tag names in call-back functions, 30 | like "a", "a.b" and "a.b.c" (used in character set file parser), 31 | or relative names like "a", "b" and "c". 32 | */ 33 | #define MY_XML_FLAG_RELATIVE_NAMES 1 34 | 35 | /* 36 | A flag whether to skip normilization of text values before calling 37 | call-back functions: i.e. skip leading/trailing spaces, 38 | \r, \n, \t characters. 39 | */ 40 | #define MY_XML_FLAG_SKIP_TEXT_NORMALIZATION 2 41 | 42 | enum my_xml_node_type 43 | { 44 | MY_XML_NODE_TAG, /* can have TAG, ATTR and TEXT children */ 45 | MY_XML_NODE_ATTR, /* can have TEXT children */ 46 | MY_XML_NODE_TEXT /* cannot have children */ 47 | }; 48 | 49 | typedef struct xml_stack_st 50 | { 51 | int flags; 52 | enum my_xml_node_type current_node_type; 53 | char errstr[128]; 54 | char attr[128]; 55 | char *attrend; 56 | const char *beg; 57 | const char *cur; 58 | const char *end; 59 | void *user_data; 60 | int (*enter)(struct xml_stack_st *st,const char *val, size_t len); 61 | int (*value)(struct xml_stack_st *st,const char *val, size_t len); 62 | int (*leave_xml)(struct xml_stack_st *st,const char *val, size_t len); 63 | } MY_XML_PARSER; 64 | 65 | void my_xml_parser_create(MY_XML_PARSER *st); 66 | void my_xml_parser_free(MY_XML_PARSER *st); 67 | int my_xml_parse(MY_XML_PARSER *st,const char *str, size_t len); 68 | 69 | void my_xml_set_value_handler(MY_XML_PARSER *st, int (*)(MY_XML_PARSER *, 70 | const char *, 71 | size_t len)); 72 | void my_xml_set_enter_handler(MY_XML_PARSER *st, int (*)(MY_XML_PARSER *, 73 | const char *, 74 | size_t len)); 75 | void my_xml_set_leave_handler(MY_XML_PARSER *st, int (*)(MY_XML_PARSER *, 76 | const char *, 77 | size_t len)); 78 | void my_xml_set_user_data(MY_XML_PARSER *st, void *); 79 | 80 | size_t my_xml_error_pos(MY_XML_PARSER *st); 81 | uint my_xml_error_lineno(MY_XML_PARSER *st); 82 | 83 | const char *my_xml_error_string(MY_XML_PARSER *st); 84 | 85 | #ifdef __cplusplus 86 | } 87 | #endif 88 | 89 | #endif /* _my_xml_h */ 90 | -------------------------------------------------------------------------------- /include/mysql/mysql_embed.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved. 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; version 2 of the License. 7 | 8 | This program is distributed in the hope that it will be useful, 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | GNU General Public License for more details. 12 | 13 | You should have received a copy of the GNU General Public License 14 | along with this program; if not, write to the Free Software 15 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 16 | */ 17 | 18 | /* Defines that are unique to the embedded version of MySQL */ 19 | 20 | #ifdef EMBEDDED_LIBRARY 21 | 22 | /* Things we don't need in the embedded version of MySQL */ 23 | /* TODO HF add #undef HAVE_VIO if we don't want client in embedded library */ 24 | 25 | #undef HAVE_OPENSSL 26 | #undef HAVE_SMEM /* No shared memory */ 27 | #undef HAVE_NDBCLUSTER_DB /* No NDB cluster */ 28 | 29 | #define DONT_USE_RAID 30 | 31 | #endif /* EMBEDDED_LIBRARY */ 32 | -------------------------------------------------------------------------------- /include/mysql/mysql_time.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2004, 2006 MySQL AB 2 | 3 | This program is free software; you can redistribute it and/or modify 4 | it under the terms of the GNU General Public License as published by 5 | the Free Software Foundation; version 2 of the License. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program; if not, write to the Free Software 14 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ 15 | 16 | #ifndef _mysql_time_h_ 17 | #define _mysql_time_h_ 18 | 19 | /* 20 | Time declarations shared between the server and client API: 21 | you should not add anything to this header unless it's used 22 | (and hence should be visible) in mysql.h. 23 | If you're looking for a place to add new time-related declaration, 24 | it's most likely my_time.h. See also "C API Handling of Date 25 | and Time Values" chapter in documentation. 26 | */ 27 | 28 | enum enum_mysql_timestamp_type 29 | { 30 | MYSQL_TIMESTAMP_NONE= -2, MYSQL_TIMESTAMP_ERROR= -1, 31 | MYSQL_TIMESTAMP_DATE= 0, MYSQL_TIMESTAMP_DATETIME= 1, MYSQL_TIMESTAMP_TIME= 2 32 | }; 33 | 34 | 35 | /* 36 | Structure which is used to represent datetime values inside MySQL. 37 | 38 | We assume that values in this structure are normalized, i.e. year <= 9999, 39 | month <= 12, day <= 31, hour <= 23, hour <= 59, hour <= 59. Many functions 40 | in server such as my_system_gmt_sec() or make_time() family of functions 41 | rely on this (actually now usage of make_*() family relies on a bit weaker 42 | restriction). Also functions that produce MYSQL_TIME as result ensure this. 43 | There is one exception to this rule though if this structure holds time 44 | value (time_type == MYSQL_TIMESTAMP_TIME) days and hour member can hold 45 | bigger values. 46 | */ 47 | typedef struct st_mysql_time 48 | { 49 | unsigned int year, month, day, hour, minute, second; 50 | unsigned long second_part; 51 | my_bool neg; 52 | enum enum_mysql_timestamp_type time_type; 53 | } MYSQL_TIME; 54 | 55 | #endif /* _mysql_time_h_ */ 56 | -------------------------------------------------------------------------------- /include/mysql/mysql_version.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 1996, 1999-2004, 2007 MySQL AB 2 | This file is public domain and comes with NO WARRANTY of any kind */ 3 | 4 | /* Version numbers for protocol & mysqld */ 5 | 6 | #ifndef _mysql_version_h 7 | #define _mysql_version_h 8 | #ifdef _CUSTOMCONFIG_ 9 | #include 10 | #else 11 | #define PROTOCOL_VERSION 10 12 | #define MYSQL_SERVER_VERSION "5.1.73" 13 | #define MYSQL_BASE_VERSION "mysqld-5.1" 14 | #define MYSQL_SERVER_SUFFIX_DEF "" 15 | #define FRM_VER 6 16 | #define MYSQL_VERSION_ID 50173 17 | #define MYSQL_PORT 3306 18 | #define MYSQL_PORT_DEFAULT 0 19 | #define MYSQL_UNIX_ADDR "/var/lib/mysql/mysql.sock" 20 | #define MYSQL_CONFIG_NAME "my" 21 | #define MYSQL_COMPILATION_COMMENT "Source distribution" 22 | 23 | /* mysqld compile time options */ 24 | #endif /* _CUSTOMCONFIG_ */ 25 | 26 | #ifndef LICENSE 27 | #define LICENSE GPL 28 | #endif /* LICENSE */ 29 | 30 | #endif /* _mysql_version_h */ 31 | -------------------------------------------------------------------------------- /include/mysql/sql_common.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved. 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; version 2 of the License. 7 | 8 | This program is distributed in the hope that it will be useful, 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | GNU General Public License for more details. 12 | 13 | You should have received a copy of the GNU General Public License 14 | along with this program; if not, write to the Free Software 15 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ 16 | 17 | 18 | extern const char *unknown_sqlstate; 19 | extern const char *cant_connect_sqlstate; 20 | extern const char *not_error_sqlstate; 21 | 22 | #ifdef __cplusplus 23 | extern "C" { 24 | #endif 25 | 26 | extern CHARSET_INFO *default_client_charset_info; 27 | MYSQL_FIELD *unpack_fields(MYSQL *mysql, MYSQL_DATA *data,MEM_ROOT *alloc, 28 | uint fields, my_bool default_value, 29 | uint server_capabilities); 30 | void free_rows(MYSQL_DATA *cur); 31 | void free_old_query(MYSQL *mysql); 32 | void end_server(MYSQL *mysql); 33 | my_bool mysql_reconnect(MYSQL *mysql); 34 | void mysql_read_default_options(struct st_mysql_options *options, 35 | const char *filename,const char *group); 36 | my_bool 37 | cli_advanced_command(MYSQL *mysql, enum enum_server_command command, 38 | const unsigned char *header, ulong header_length, 39 | const unsigned char *arg, ulong arg_length, 40 | my_bool skip_check, MYSQL_STMT *stmt); 41 | unsigned long cli_safe_read(MYSQL *mysql); 42 | void net_clear_error(NET *net); 43 | void set_stmt_errmsg(MYSQL_STMT *stmt, NET *net); 44 | void set_stmt_error(MYSQL_STMT *stmt, int errcode, const char *sqlstate, 45 | const char *err); 46 | void set_mysql_error(MYSQL *mysql, int errcode, const char *sqlstate); 47 | #ifdef __cplusplus 48 | } 49 | #endif 50 | 51 | #define protocol_41(A) ((A)->server_capabilities & CLIENT_PROTOCOL_41) 52 | 53 | -------------------------------------------------------------------------------- /include/mysql/sslopt-case.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2000-2002 MySQL AB 2 | 3 | This program is free software; you can redistribute it and/or modify 4 | it under the terms of the GNU General Public License as published by 5 | the Free Software Foundation; version 2 of the License. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program; if not, write to the Free Software 14 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ 15 | 16 | #ifdef HAVE_OPENSSL 17 | case OPT_SSL_KEY: 18 | case OPT_SSL_CERT: 19 | case OPT_SSL_CA: 20 | case OPT_SSL_CAPATH: 21 | case OPT_SSL_CIPHER: 22 | /* 23 | Enable use of SSL if we are using any ssl option 24 | One can disable SSL later by using --skip-ssl or --ssl=0 25 | */ 26 | opt_use_ssl= 1; 27 | break; 28 | #endif 29 | -------------------------------------------------------------------------------- /include/mysql/sslopt-longopts.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved. 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; version 2 of the License. 7 | 8 | This program is distributed in the hope that it will be useful, 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | GNU General Public License for more details. 12 | 13 | You should have received a copy of the GNU General Public License 14 | along with this program; if not, write to the Free Software 15 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 16 | */ 17 | 18 | #ifdef HAVE_OPENSSL 19 | 20 | {"ssl", OPT_SSL_SSL, 21 | "Enable SSL for connection (automatically enabled with other flags)." 22 | "Disable with --skip-ssl.", &opt_use_ssl, &opt_use_ssl, 0, GET_BOOL, 23 | NO_ARG, 0, 0, 0, 0, 0, 0}, 24 | {"ssl-ca", OPT_SSL_CA, 25 | "CA file in PEM format (check OpenSSL docs, implies --ssl).", 26 | &opt_ssl_ca, &opt_ssl_ca, 0, GET_STR, REQUIRED_ARG, 27 | 0, 0, 0, 0, 0, 0}, 28 | {"ssl-capath", OPT_SSL_CAPATH, 29 | "CA directory (check OpenSSL docs, implies --ssl).", 30 | &opt_ssl_capath, &opt_ssl_capath, 0, GET_STR, REQUIRED_ARG, 31 | 0, 0, 0, 0, 0, 0}, 32 | {"ssl-cert", OPT_SSL_CERT, "X509 cert in PEM format (implies --ssl).", 33 | &opt_ssl_cert, &opt_ssl_cert, 0, GET_STR, REQUIRED_ARG, 34 | 0, 0, 0, 0, 0, 0}, 35 | {"ssl-cipher", OPT_SSL_CIPHER, "SSL cipher to use (implies --ssl).", 36 | &opt_ssl_cipher, &opt_ssl_cipher, 0, GET_STR, REQUIRED_ARG, 37 | 0, 0, 0, 0, 0, 0}, 38 | {"ssl-key", OPT_SSL_KEY, "X509 key in PEM format (implies --ssl).", 39 | &opt_ssl_key, &opt_ssl_key, 0, GET_STR, REQUIRED_ARG, 40 | 0, 0, 0, 0, 0, 0}, 41 | #ifdef MYSQL_CLIENT 42 | {"ssl-verify-server-cert", OPT_SSL_VERIFY_SERVER_CERT, 43 | "Verify server's \"Common Name\" in its cert against hostname used " 44 | "when connecting. This option is disabled by default.", 45 | &opt_ssl_verify_server_cert, &opt_ssl_verify_server_cert, 46 | 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0}, 47 | #endif 48 | #endif /* HAVE_OPENSSL */ 49 | -------------------------------------------------------------------------------- /include/mysql/sslopt-vars.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2000, 2001, 2006 MySQL AB 2 | 3 | This program is free software; you can redistribute it and/or modify 4 | it under the terms of the GNU General Public License as published by 5 | the Free Software Foundation; version 2 of the License. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program; if not, write to the Free Software 14 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ 15 | 16 | #ifdef HAVE_OPENSSL 17 | #ifdef SSL_VARS_NOT_STATIC 18 | #define SSL_STATIC 19 | #else 20 | #define SSL_STATIC static 21 | #endif 22 | SSL_STATIC my_bool opt_use_ssl = 0; 23 | SSL_STATIC char *opt_ssl_ca = 0; 24 | SSL_STATIC char *opt_ssl_capath = 0; 25 | SSL_STATIC char *opt_ssl_cert = 0; 26 | SSL_STATIC char *opt_ssl_cipher = 0; 27 | SSL_STATIC char *opt_ssl_key = 0; 28 | #ifdef MYSQL_CLIENT 29 | SSL_STATIC my_bool opt_ssl_verify_server_cert= 0; 30 | #endif 31 | #endif 32 | -------------------------------------------------------------------------------- /include/mysql/typelib.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2000, 2003, 2004, 2006, 2007 MySQL AB 2 | 3 | This program is free software; you can redistribute it and/or modify 4 | it under the terms of the GNU General Public License as published by 5 | the Free Software Foundation; version 2 of the License. 6 | 7 | This program is distributed in the hope that it will be useful, 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | GNU General Public License for more details. 11 | 12 | You should have received a copy of the GNU General Public License 13 | along with this program; if not, write to the Free Software 14 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ 15 | 16 | 17 | #ifndef _typelib_h 18 | #define _typelib_h 19 | 20 | #include "my_alloc.h" 21 | 22 | typedef struct st_typelib { /* Different types saved here */ 23 | unsigned int count; /* How many types */ 24 | const char *name; /* Name of typelib */ 25 | const char **type_names; 26 | unsigned int *type_lengths; 27 | } TYPELIB; 28 | 29 | extern my_ulonglong find_typeset(char *x, TYPELIB *typelib,int *error_position); 30 | extern int find_type_or_exit(const char *x, TYPELIB *typelib, 31 | const char *option); 32 | extern int find_type(char *x, const TYPELIB *typelib, unsigned int full_name); 33 | extern void make_type(char *to,unsigned int nr,TYPELIB *typelib); 34 | extern const char *get_type(TYPELIB *typelib,unsigned int nr); 35 | extern TYPELIB *copy_typelib(MEM_ROOT *root, TYPELIB *from); 36 | 37 | extern TYPELIB sql_protocol_typelib; 38 | 39 | #endif /* _typelib_h */ 40 | -------------------------------------------------------------------------------- /lib/libcore.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/liulingjun886/LinuxServer/6934c84dcffd1c49767e5665038b310390afe704/lib/libcore.a -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "./Defines.h" 4 | #include "Server.h" 5 | #include 6 | #include "./CenterServer/CenterServer.h" 7 | #include "./UserServer/UserServer.h" 8 | #include "./DataServer/DataServer.h" 9 | #include "./GameServer/GameServer.h" 10 | #include "./ConntionServer/ConnectServer.h" 11 | 12 | CServer* g_pSer = NULL; 13 | 14 | 15 | int main(int argc,char* argv[]) 16 | { 17 | uint16 nSrvType; 18 | uint16 nSrvNo; 19 | 20 | char option[] = ":s:t::"; 21 | int result; 22 | int num = 0; 23 | while((result = getopt(argc, argv, option)) != -1) 24 | { 25 | switch(result) 26 | { 27 | 28 | case 's': //server Id 29 | nSrvNo = (unsigned short)atoi(optarg); 30 | if (nSrvNo < 0) 31 | { 32 | printf("sid=%d\n", nSrvNo); 33 | return -1; 34 | } 35 | num++; 36 | break; 37 | case 't': //server type 38 | nSrvType= (unsigned short)atoi(optarg); 39 | if (nSrvType <= 0) 40 | { 41 | printf("m_nLevel=%d\n", nSrvType); 42 | return -1; 43 | } 44 | num++; 45 | break; 46 | case '?': 47 | printf("Invalid option.\r\n"); 48 | return -1; 49 | case ':': 50 | printf("Lack of option argument.\r\n"); 51 | return -1; 52 | } 53 | }; 54 | 55 | switch(nSrvType) 56 | { 57 | case SRV_TYPE_CENTER: 58 | g_pSer = new CCenterServer; 59 | break; 60 | case SRV_TYPE_USER: 61 | g_pSer = new CUserServer; 62 | break; 63 | case SRV_TYPE_DATA: 64 | g_pSer = new CDataServer; 65 | break; 66 | case SRV_TYPE_GAME: 67 | g_pSer = new CGameServer; 68 | break; 69 | case SRV_TYPE_CONN: 70 | g_pSer = new CConnectServer; 71 | break; 72 | default: 73 | break; 74 | } 75 | 76 | if(NULL == g_pSer) 77 | { 78 | printf("invalid argvs \n"); 79 | return -1; 80 | } 81 | 82 | if(0 != g_pSer->Init(nSrvType, nSrvNo)) 83 | return 0; 84 | 85 | g_pSer->Run(); 86 | delete g_pSer; 87 | return 0; 88 | } 89 | -------------------------------------------------------------------------------- /run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # -t 服务器类型 1-中心服 2-玩家管理服 3-数据处理服 4-游戏服 5-连接服 按类型顺序依次启动 -s 该类服务器编号 从0开始 3 | killall server 4 | sleep 2 5 | ./server -t1 -s0 6 | sleep 1 7 | ./server -t2 -s0 8 | sleep 1 9 | ./server -t3 -s0 10 | sleep 1 11 | ./server -t4 -s0 12 | sleep 1 13 | ./server -t5 -s0 14 | -------------------------------------------------------------------------------- /test/RedisTest.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include"./include/hiredis/hiredis.h" 4 | using namespace std; 5 | int main() 6 | { 7 | redisContext* c = redisConnect("127.0.0.1", 6379); 8 | printf("error = %d\n",c->err); 9 | 10 | redisReply* reply = (redisReply*)redisCommand(c,"AUTH 123"); 11 | printf("%d\n",reply->type); 12 | freeReplyObject(reply); 13 | 14 | 15 | reply = (redisReply*)redisCommand(c, "SCRIPT LOAD %s","return"); 16 | printf("%d,%d,%s\n",reply->type, (int)reply->len,reply->str); 17 | 18 | // reply = (redisReply*)redisCommand(c, "EVALSHA %s %d", reply->str, 0); 19 | // printf("%d,%d\n", (int)reply->type,(int)reply->elements); 20 | freeReplyObject(reply); 21 | redisFree(c); 22 | } 23 | 24 | -------------------------------------------------------------------------------- /test/SyncService.cpp: -------------------------------------------------------------------------------- 1 | #include "SyncService.h" 2 | #include "DataServer.h" 3 | 4 | enum en_time_id 5 | { 6 | TIME_ID_SYNC = 1, 7 | }; 8 | 9 | #define SYNC_TIME 10 10 | 11 | extern CDataServer* g_pDataServer; 12 | 13 | CSyncService::CSyncService() 14 | { 15 | m_set_sync_user.clear(); 16 | m_timer_sync.InitTimerObj(this, TIME_ID_SYNC); 17 | } 18 | 19 | CSyncService::~CSyncService() 20 | { 21 | m_set_sync_user.clear(); 22 | } 23 | 24 | void CSyncService::Initialized() 25 | { 26 | m_timer_sync.StartTimerSec(SYNC_TIME); 27 | } 28 | 29 | 30 | bool CSyncService::HandData(int nType, SERVICEINDEX nSrcIndex, void *pData, DATASIZE nSize) 31 | { 32 | switch(nType) 33 | { 34 | case TIME_MSG: 35 | { 36 | TIMEERID nTimeId = *(TIMEERID*)pData; 37 | return HandTimeMsg(nTimeId); 38 | } 39 | } 40 | } 41 | 42 | bool CSyncService::HandTimeMsg(TIMEERID nTimeId) 43 | { 44 | switch(nTimeId) 45 | { 46 | case TIME_ID_SYNC: 47 | { 48 | SyncUserInfo(); 49 | return true; 50 | } 51 | default: 52 | break; 53 | } 54 | return true; 55 | } 56 | 57 | void CSyncService::SyncUserInfo() 58 | { 59 | if(0 == m_set_sync_user.size()) 60 | return; 61 | SyncInfoToDataBase sync; 62 | std::set::iterator it = m_set_sync_user.begin(); 63 | for(; it != m_set_sync_user.end(); it++) 64 | { 65 | //PostData(g_pDataServer->, int nType, void * pData = 0, DATASIZE size = 0) 66 | sync.nEvType = SYNC_INFO_DATABASE; 67 | g_pDataServer->PostMemDataBaseReq(this, &sync, sizeof(sync)); 68 | } 69 | m_set_sync_user.clear(); 70 | } 71 | 72 | -------------------------------------------------------------------------------- /test/SyncService.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "../include/Services.h" 4 | #include "../include/TimerNode.h" 5 | #include "../Defines.h" 6 | #include 7 | 8 | class CSyncService : public CServices 9 | { 10 | public: 11 | CSyncService(); 12 | ~CSyncService(); 13 | protected: 14 | virtual bool HandData(int nType, SERVICEINDEX nSrcIndex, void *pData, DATASIZE nSize); 15 | virtual void Initialized(); 16 | private: 17 | bool HandTimeMsg(TIMEERID nTimeId); 18 | void SyncUserInfo(); 19 | private: 20 | std::set m_set_sync_user; 21 | CTimer m_timer_sync; 22 | }; -------------------------------------------------------------------------------- /test/test.cpp: -------------------------------------------------------------------------------- 1 | #include "./DataBaseEnginer/MySql.h" 2 | #include 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | /* 9 | 10 | std::string str = "a'a'a'"; 11 | string::size_type pos(0); 12 | for( pos = str.find("'",pos);pos != string::npos ; pos = str.find("'",++pos)) 13 | str.replace(pos++,1,"\'"); 14 | str = "'" + str + "'"; 15 | printf("%s \n",str.c_str());*/ 16 | printf("%d\n",0x8FFFFF); 17 | CMySql mysql; 18 | if(!mysql.Init()) 19 | { 20 | printf("mysql connected failer\n"); 21 | return 0; 22 | } 23 | srand(time(NULL)); 24 | printf("mysql connected sucess\n"); 25 | int nCount = 0; 26 | do 27 | { 28 | mysql.SetSpName("AddInfo"); 29 | mysql.AddStrParam("''james"); 30 | mysql.AddNumParam(rand()%30); 31 | mysql.ExecPro(); 32 | 33 | mysql.SetSpName("GetInfo"); 34 | unsigned char a = 1; 35 | mysql.AddNumParam(a); 36 | mysql.AddOutParam("@ret"); 37 | printf("execret = %d\n",mysql.ExecPro()); 38 | while(mysql.HaveNext()) 39 | { 40 | printf("id = %lld, name = %s, age = %lld, ret = %lld\n",mysql.GetNumValue("Id"),mysql.GetStrValue("name"),mysql.GetNumValue("age"),mysql.GetOutNumValue("@ret")); 41 | mysql.MoveNext(); 42 | } 43 | } 44 | while (++nCount < 1); 45 | 46 | //getchar(); 47 | } 48 | //cancel 49 | //ok -------------------------------------------------------------------------------- /服务器架构图.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/liulingjun886/LinuxServer/6934c84dcffd1c49767e5665038b310390afe704/服务器架构图.docx --------------------------------------------------------------------------------