forked from metin2/server
Add project files.
This commit is contained in:
210
libsql/include/CAsyncSQL.h
Normal file
210
libsql/include/CAsyncSQL.h
Normal file
@ -0,0 +1,210 @@
|
||||
#ifndef __INC_METIN_II_ASYNCSQL_H__
|
||||
#define __INC_METIN_II_ASYNCSQL_H__
|
||||
|
||||
#include <libthecore/include/stdafx.h>
|
||||
#include <libthecore/include/log.h>
|
||||
|
||||
#include <string>
|
||||
#include <queue>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <mysql/mysql.h>
|
||||
#include <mysql/errmsg.h>
|
||||
#include <mysql/mysqld_error.h>
|
||||
|
||||
#include "CSemaphore.h"
|
||||
|
||||
#define QUERY_MAX_LEN 8192
|
||||
|
||||
typedef struct _SQLResult
|
||||
{
|
||||
_SQLResult()
|
||||
: pSQLResult(NULL), uiNumRows(0), uiAffectedRows(0), uiInsertID(0)
|
||||
{
|
||||
}
|
||||
|
||||
~_SQLResult()
|
||||
{
|
||||
if (pSQLResult)
|
||||
{
|
||||
mysql_free_result(pSQLResult);
|
||||
pSQLResult = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
MYSQL_RES * pSQLResult;
|
||||
uint32_t uiNumRows;
|
||||
uint32_t uiAffectedRows;
|
||||
uint32_t uiInsertID;
|
||||
} SQLResult;
|
||||
|
||||
typedef struct _SQLMsg
|
||||
{
|
||||
_SQLMsg() : m_pkSQL(NULL), iID(0), uiResultPos(0), pvUserData(NULL), bReturn(false), uiSQLErrno(0)
|
||||
{
|
||||
}
|
||||
|
||||
~_SQLMsg()
|
||||
{
|
||||
std::vector<SQLResult *>::iterator first = vec_pkResult.begin();
|
||||
std::vector<SQLResult *>::iterator past = vec_pkResult.end();
|
||||
|
||||
while (first != past)
|
||||
delete *(first++);
|
||||
|
||||
vec_pkResult.clear();
|
||||
}
|
||||
|
||||
void Store()
|
||||
{
|
||||
do
|
||||
{
|
||||
SQLResult * pRes = new SQLResult;
|
||||
|
||||
pRes->pSQLResult = mysql_store_result(m_pkSQL);
|
||||
pRes->uiInsertID = mysql_insert_id(m_pkSQL);
|
||||
pRes->uiAffectedRows = mysql_affected_rows(m_pkSQL);
|
||||
|
||||
if (pRes->pSQLResult)
|
||||
{
|
||||
pRes->uiNumRows = mysql_num_rows(pRes->pSQLResult);
|
||||
}
|
||||
else
|
||||
{
|
||||
pRes->uiNumRows = 0;
|
||||
}
|
||||
|
||||
vec_pkResult.push_back(pRes);
|
||||
} while (!mysql_next_result(m_pkSQL));
|
||||
}
|
||||
|
||||
SQLResult * Get()
|
||||
{
|
||||
if (uiResultPos >= vec_pkResult.size())
|
||||
return NULL;
|
||||
|
||||
return vec_pkResult[uiResultPos];
|
||||
}
|
||||
|
||||
bool Next()
|
||||
{
|
||||
if (uiResultPos + 1 >= vec_pkResult.size())
|
||||
return false;
|
||||
|
||||
++uiResultPos;
|
||||
return true;
|
||||
}
|
||||
|
||||
MYSQL * m_pkSQL;
|
||||
int iID;
|
||||
std::string stQuery;
|
||||
|
||||
std::vector<SQLResult *> vec_pkResult; // result <20><><EFBFBD><EFBFBD>
|
||||
unsigned int uiResultPos; // <20><><EFBFBD><EFBFBD> result <20><>ġ
|
||||
|
||||
void * pvUserData;
|
||||
bool bReturn;
|
||||
|
||||
unsigned int uiSQLErrno;
|
||||
} SQLMsg;
|
||||
|
||||
class CAsyncSQL
|
||||
{
|
||||
public:
|
||||
CAsyncSQL();
|
||||
virtual ~CAsyncSQL();
|
||||
|
||||
void Quit();
|
||||
|
||||
bool Setup(const char * c_pszHost, const char * c_pszUser, const char * c_pszPassword, const char * c_pszDB, const char * c_pszLocale,
|
||||
bool bNoThread = false, int iPort = 0);
|
||||
bool Setup(CAsyncSQL * sql, bool bNoThread = false);
|
||||
|
||||
bool Connect();
|
||||
bool IsConnected() { return m_bConnected; }
|
||||
bool QueryLocaleSet();
|
||||
|
||||
void AsyncQuery(const char * c_pszQuery);
|
||||
void ReturnQuery(const char * c_pszQuery, void * pvUserData);
|
||||
SQLMsg * DirectQuery(const char * c_pszQuery);
|
||||
|
||||
DWORD CountQuery();
|
||||
DWORD CountResult();
|
||||
|
||||
void PushResult(SQLMsg * p);
|
||||
bool PopResult(SQLMsg ** pp);
|
||||
|
||||
void ChildLoop();
|
||||
|
||||
MYSQL * GetSQLHandle();
|
||||
|
||||
int CountQueryFinished();
|
||||
void ResetQueryFinished();
|
||||
|
||||
size_t EscapeString(char* dst, size_t dstSize, const char *src, size_t srcSize);
|
||||
|
||||
protected:
|
||||
void Destroy();
|
||||
|
||||
void PushQuery(SQLMsg * p);
|
||||
|
||||
bool PeekQuery(SQLMsg ** pp);
|
||||
bool PopQuery(int iID);
|
||||
|
||||
bool PeekQueryFromCopyQueue(SQLMsg ** pp );
|
||||
INT CopyQuery();
|
||||
bool PopQueryFromCopyQueue();
|
||||
|
||||
public:
|
||||
int GetCopiedQueryCount();
|
||||
void ResetCopiedQueryCount();
|
||||
void AddCopiedQueryCount( int iCopiedQuery );
|
||||
|
||||
//private:
|
||||
protected:
|
||||
MYSQL m_hDB;
|
||||
|
||||
std::string m_stHost;
|
||||
std::string m_stUser;
|
||||
std::string m_stPassword;
|
||||
std::string m_stDB;
|
||||
std::string m_stLocale;
|
||||
|
||||
int m_iMsgCount;
|
||||
int m_aiPipe[2];
|
||||
int m_iPort;
|
||||
|
||||
std::queue<SQLMsg *> m_queue_query;
|
||||
std::queue<SQLMsg *> m_queue_query_copy;
|
||||
//std::map<int, SQLMsg *> m_map_kSQLMsgUnfinished;
|
||||
|
||||
std::queue<SQLMsg *> m_queue_result;
|
||||
|
||||
volatile bool m_bEnd;
|
||||
|
||||
#ifndef __WIN32__
|
||||
pthread_t m_hThread;
|
||||
pthread_mutex_t * m_mtxQuery;
|
||||
pthread_mutex_t * m_mtxResult;
|
||||
#else
|
||||
HANDLE m_hThread;
|
||||
CRITICAL_SECTION* m_mtxQuery;
|
||||
CRITICAL_SECTION* m_mtxResult;
|
||||
#endif
|
||||
|
||||
CSemaphore m_sem;
|
||||
|
||||
int m_iQueryFinished;
|
||||
|
||||
unsigned long m_ulThreadID;
|
||||
bool m_bConnected;
|
||||
int m_iCopiedQuery;
|
||||
};
|
||||
|
||||
class CAsyncSQL2 : public CAsyncSQL
|
||||
{
|
||||
public:
|
||||
void SetLocale ( const std::string & stLocale );
|
||||
};
|
||||
|
||||
#endif
|
22
libsql/include/CSemaphore.h
Normal file
22
libsql/include/CSemaphore.h
Normal file
@ -0,0 +1,22 @@
|
||||
#ifndef __INC_METIN_II_SEMAPHORE_H__
|
||||
#define __INC_METIN_II_SEMAPHORE_H__
|
||||
|
||||
#include <semaphore.h>
|
||||
|
||||
class CSemaphore
|
||||
{
|
||||
private:
|
||||
sem_t * m_hSem;
|
||||
|
||||
|
||||
public:
|
||||
CSemaphore();
|
||||
~CSemaphore();
|
||||
|
||||
int Initialize();
|
||||
void Clear();
|
||||
void Destroy();
|
||||
int Wait();
|
||||
int Release(int count = 1);
|
||||
};
|
||||
#endif
|
41
libsql/include/CStatement.h
Normal file
41
libsql/include/CStatement.h
Normal file
@ -0,0 +1,41 @@
|
||||
#ifndef __INC_METIN_II_LIBSQL_STATEMENT_H__
|
||||
#define __INC_METIN_II_LIBSQL_STATEMENT_H__
|
||||
|
||||
#include "CAsyncSQL.h"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
class CStmt
|
||||
{
|
||||
public:
|
||||
CStmt();
|
||||
virtual ~CStmt();
|
||||
|
||||
bool Prepare(CAsyncSQL * sql, const char * c_pszQuery);
|
||||
bool BindParam(enum_field_types type, void * p, int iMaxLen=0);
|
||||
bool BindResult(enum_field_types type, void * p, int iMaxLen=0);
|
||||
int Execute();
|
||||
bool Fetch();
|
||||
|
||||
void Error(const char * c_pszMsg);
|
||||
|
||||
public:
|
||||
int iRows;
|
||||
|
||||
private:
|
||||
void Destroy();
|
||||
|
||||
MYSQL_STMT * m_pkStmt;
|
||||
|
||||
std::string m_stQuery;
|
||||
|
||||
std::vector<MYSQL_BIND> m_vec_param;
|
||||
unsigned int m_uiParamCount;
|
||||
long unsigned int * m_puiParamLen;
|
||||
|
||||
std::vector<MYSQL_BIND> m_vec_result;
|
||||
unsigned int m_uiResultCount;
|
||||
};
|
||||
|
||||
#endif
|
16
libsql/include/Tellwait.h
Normal file
16
libsql/include/Tellwait.h
Normal file
@ -0,0 +1,16 @@
|
||||
#ifndef __INC_METIN_II_TELLWAIT_H__
|
||||
#define __INC_METIN_II_TELLWAIT_H__
|
||||
|
||||
#ifndef __WIN32__
|
||||
|
||||
extern void TELL_WAIT();
|
||||
|
||||
extern void WAIT_CHILD();
|
||||
extern void TELL_CHILD(pid_t pid);
|
||||
|
||||
extern void WAIT_PARENT();
|
||||
extern void TELL_PARENT(pid_t pid);
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
1
libsql/include/libsql.h
Normal file
1
libsql/include/libsql.h
Normal file
@ -0,0 +1 @@
|
||||
#include "AsyncSQL.h"
|
2
libsql/include/stdafx.h
Normal file
2
libsql/include/stdafx.h
Normal file
@ -0,0 +1,2 @@
|
||||
#include <libthecore/include/stdafx.h>
|
||||
#include "CAsyncSQL.h"
|
Reference in New Issue
Block a user