- Mesaj
- 102
- Çözümler
- 3
- Beğeni
- 26
- Puan
- 439
- Ticaret Puanı
- 0
bu konudakileri yaptım link
Frebsd build
AsyncSQL.cpp (Srcs/Server/libsql)
AsyncSQL.h (Srcs/Server/libsql)
Frebsd build
Kod:
root@Light5:/home/server/Srcs/Server # gmake -j4
gmake -C liblua/5.0 clean
gmake -C libsql dep
gmake -C libgame/src dep
gmake -C libpoly dep
gmake[1]: Entering directory '/home/server/Srcs/Server/liblua/5.0'
cd include; gmake clean
gmake[1]: Entering directory '/home/server/Srcs/Server/libsql'
touch Depend
gmake[1]: Entering directory '/home/server/Srcs/Server/libpoly'
gmake[1]: Entering directory '/home/server/Srcs/Server/libgame/src'
touch Depend
touch Depend
AsyncSQL.cpp:(.text+0x149): undefined reference to `server_mysql_set_character_set'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x1bd): undefined reference to `server_mysql_error'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x1c7): undefined reference to `server_mysql_errno'
/usr/local/bin/ld: ../../libsql/libsql.a(AsyncSQL.o): in function `CAsyncSQL::Connect()':
AsyncSQL.cpp:(.text+0x210): undefined reference to `server_mysql_init'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x254): undefined reference to `server_mysql_real_connect'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x272): undefined reference to `server_mysql_options'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x2c5): undefined reference to `server_mysql_error'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x2f5): undefined reference to `server_mysql_options'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x309): undefined reference to `server_mysql_error'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x335): undefined reference to `server_mysql_error'
/usr/local/bin/ld: ../../libsql/libsql.a(AsyncSQL.o): in function `CAsyncSQL::ChildLoop()':
AsyncSQL.cpp:(.text+0x11c1): undefined reference to `server_mysql_real_query'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x11d1): undefined reference to `server_mysql_errno'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x11e5): undefined reference to `server_mysql_error'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x12d5): undefined reference to `server_mysql_num_rows'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x133d): undefined reference to `server_mysql_store_result'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x1355): undefined reference to `server_mysql_affected_rows'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x13c9): undefined reference to `server_mysql_free_result'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x149e): undefined reference to `server_mysql_real_query'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x14ae): undefined reference to `server_mysql_errno'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x14bf): undefined reference to `server_mysql_error'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x1555): undefined reference to `server_mysql_num_rows'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x15bd): undefined reference to `server_mysql_store_result'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x15d5): undefined reference to `server_mysql_affected_rows'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x1649): undefined reference to `server_mysql_free_result'
/usr/local/bin/ld: ../../libsql/libsql.a(AsyncSQL.o): in function `CAsyncSQL::DirectQuery(char const*)':
AsyncSQL.cpp:(.text+0x1e68): undefined reference to `server_mysql_real_query'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x1e8d): undefined reference to `server_mysql_num_rows'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x1efe): undefined reference to `server_mysql_store_result'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x1f19): undefined reference to `server_mysql_affected_rows'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x1fcd): undefined reference to `server_mysql_error'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x2002): undefined reference to `server_mysql_errno'
collect2: error: ld returned 1 exit status
gmake[1]: *** [Makefile:79: ../db_r41025] Error 1
gmake[1]: Leaving directory '/home/server/Srcs/Server/db/src'
gmake: *** [Makefile:55: db] Error 2
/usr/local/bin/ld: ../../libsql/libsql.a(AsyncSQL.o): in function `CAsyncSQL::Destroy()':
AsyncSQL.cpp:(.text+0x7a): undefined reference to `server_mysql_close'
/usr/local/bin/ld: ../../libsql/libsql.a(AsyncSQL.o): in function `CAsyncSQL::QueryLocaleSet()':
AsyncSQL.cpp:(.text+0x149): undefined reference to `server_mysql_set_character_set'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x1bd): undefined reference to `server_mysql_error'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x1c7): undefined reference to `server_mysql_errno'
/usr/local/bin/ld: ../../libsql/libsql.a(AsyncSQL.o): in function `CAsyncSQL::Connect()':
AsyncSQL.cpp:(.text+0x210): undefined reference to `server_mysql_init'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x254): undefined reference to `server_mysql_real_connect'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x272): undefined reference to `server_mysql_options'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x2c5): undefined reference to `server_mysql_error'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x2f5): undefined reference to `server_mysql_options'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x309): undefined reference to `server_mysql_error'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x335): undefined reference to `server_mysql_error'
/usr/local/bin/ld: ../../libsql/libsql.a(AsyncSQL.o): in function `CAsyncSQL::ChildLoop()':
AsyncSQL.cpp:(.text+0x11c1): undefined reference to `server_mysql_real_query'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x11d1): undefined reference to `server_mysql_errno'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x11e5): undefined reference to `server_mysql_error'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x12d5): undefined reference to `server_mysql_num_rows'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x133d): undefined reference to `server_mysql_store_result'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x1355): undefined reference to `server_mysql_affected_rows'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x13c9): undefined reference to `server_mysql_free_result'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x149e): undefined reference to `server_mysql_real_query'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x14ae): undefined reference to `server_mysql_errno'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x14bf): undefined reference to `server_mysql_error'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x1555): undefined reference to `server_mysql_num_rows'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x15bd): undefined reference to `server_mysql_store_result'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x15d5): undefined reference to `server_mysql_affected_rows'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x1649): undefined reference to `server_mysql_free_result'
/usr/local/bin/ld: ../../libsql/libsql.a(AsyncSQL.o): in function `CAsyncSQL::DirectQuery(char const*)':
AsyncSQL.cpp:(.text+0x1e68): undefined reference to `server_mysql_real_query'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x1e8d): undefined reference to `server_mysql_num_rows'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x1efe): undefined reference to `server_mysql_store_result'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x1f19): undefined reference to `server_mysql_affected_rows'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x1fcd): undefined reference to `server_mysql_error'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x2002): undefined reference to `server_mysql_errno'
collect2: error: ld returned 1 exit status
gmake[1]: *** [Makefile:176: ../game_r41025] Error 1
gmake[1]: Leaving directory '/home/server/Srcs/Server/game/src'
gmake: *** [Makefile:48: game] Error 2
root@Light5:/home/server/Srcs/Server # gmake clean
AsyncSQL.cpp:(.text+0x149): undefined reference to `server_mysql_set_character_set'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x1bd): undefined reference to `server_mysql_error'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x1c7): undefined reference to `server_mysql_errno'
/usr/local/bin/ld: ../../libsql/libsql.a(AsyncSQL.o): in function `CAsyncSQL::Connect()':
AsyncSQL.cpp:(.text+0x210): undefined reference to `server_mysql_init'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x254): undefined reference to `server_mysql_real_connect'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x272): undefined reference to `server_mysql_options'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x2c5): undefined reference to `server_mysql_error'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x2f5): undefined reference to `server_mysql_options'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x309): undefined reference to `server_mysql_error'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x335): undefined reference to `server_mysql_error'
/usr/local/bin/ld: ../../libsql/libsql.a(AsyncSQL.o): in function `CAsyncSQL::ChildLoop()':
AsyncSQL.cpp:(.text+0x11c1): undefined reference to `server_mysql_real_query'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x11d1): undefined reference to `server_mysql_errno'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x11e5): undefined reference to `server_mysql_error'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x12d5): undefined reference to `server_mysql_num_rows'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x133d): undefined reference to `server_mysql_store_result'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x1355): undefined reference to `server_mysql_affected_rows'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x13c9): undefined reference to `server_mysql_free_result'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x149e): undefined reference to `server_mysql_real_query'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x14ae): undefined reference to `server_mysql_errno'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x14bf): undefined reference to `server_mysql_error'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x1555): undefined reference to `server_mysql_num_rows'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x15bd): undefined reference to `server_mysql_store_result'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x15d5): undefined reference to `server_mysql_affected_rows'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x1649): undefined reference to `server_mysql_free_result'
/usr/local/bin/ld: ../../libsql/libsql.a(AsyncSQL.o): in function `CAsyncSQL::DirectQuery(char const*)':
AsyncSQL.cpp:(.text+0x1e68): undefined reference to `server_mysql_real_query'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x1e8d): undefined reference to `server_mysql_num_rows'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x1efe): undefined reference to `server_mysql_store_result'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x1f19): undefined reference to `server_mysql_affected_rows'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x1fcd): undefined reference to `server_mysql_error'
/usr/local/bin/ld: AsyncSQL.cpp:(.text+0x2002): undefined reference to `server_mysql_errno'
collect2: error: ld returned 1 exit status
gmake[1]: *** [Makefile:176: ../game_r41026] Error 1
gmake[1]: Leaving directory '/home/server/Srcs/Server/game/src'
gmake: *** [Makefile:115: all] Error 2
root@Light5:/home/server/Srcs/Server #
AsyncSQL.cpp (Srcs/Server/libsql)
Kod:
#ifndef __WIN32__
#include <sys/time.h>
#endif
#include <cstdlib>
#include <cstring>
#include <memory>
#include "AsyncSQL.h"
// TODO: Consider providing platform-independent mutex class.
#ifndef __WIN32__
#define MUTEX_LOCK(mtx) pthread_mutex_lock(mtx)
#define MUTEX_UNLOCK(mtx) pthread_mutex_unlock(mtx)
#else
#define MUTEX_LOCK(mtx) ::EnterCriticalSection(mtx)
#define MUTEX_UNLOCK(mtx) ::LeaveCriticalSection(mtx)
#endif
CAsyncSQL::CAsyncSQL()
: m_stHost(""), m_stUser(""), m_stPassword(""), m_stDB(""), m_stLocale(""),
m_iMsgCount(0), m_iPort(0), m_bEnd(false),
#ifndef __WIN32__
m_hThread(0),
#else
m_hThread(INVALID_HANDLE_VALUE),
#endif
m_iQueryFinished(0), m_ulThreadID(0), m_bConnected(false), m_iCopiedQuery(0)
{
memset( &m_hDB, 0, sizeof(m_hDB) );
m_aiPipe[0] = 0;
m_aiPipe[1] = 0;
}
CAsyncSQL::~CAsyncSQL()
{
Quit();
Destroy();
}
void CAsyncSQL::Destroy()
{
if (m_hDB.host)
{
sys_log(0, "AsyncSQL: closing mysql connection.");
mysql_close(&m_hDB);
m_hDB.host = NULL;
}
if (m_mtxQuery)
{
#ifndef __WIN32__
pthread_mutex_destroy(m_mtxQuery.get());
#else
::DeleteCriticalSection(m_mtxQuery.get());
#endif
m_mtxQuery.release();
}
if (m_mtxResult)
{
#ifndef __WIN32__
pthread_mutex_destroy(m_mtxResult.get());
#else
::DeleteCriticalSection(m_mtxResult.get());
#endif
m_mtxResult.release();
}
}
#ifndef __WIN32__
void * AsyncSQLThread(void * arg)
#else
unsigned int __stdcall AsyncSQLThread(void* arg)
#endif
{
CAsyncSQL * pSQL = ((CAsyncSQL *) arg);
if (!pSQL->Connect())
return NULL;
pSQL->ChildLoop();
return NULL;
}
bool CAsyncSQL::QueryLocaleSet()
{
if (0 == m_stLocale.length())
{
sys_err("m_stLocale == 0");
return true;
}
else if (m_stLocale == "ascii")
{
sys_err("m_stLocale == ascii");
return true;
}
if (mysql_set_character_set(&m_hDB, m_stLocale.c_str()))
{
sys_err("cannot set locale %s by 'mysql_set_character_set', errno %u %s", m_stLocale.c_str(), mysql_errno(&m_hDB) , mysql_error(&m_hDB));
return false;
}
sys_log(0, "\t--mysql_set_character_set(%s)", m_stLocale.c_str());
return true;
}
bool CAsyncSQL::Connect()
{
if (0 == mysql_init(&m_hDB))
{
fprintf(stderr, "mysql_init failed\n");
return false;
}
//mysql_options(&m_hDB, MYSQL_SET_CHARSET_NAME, m_stLocale.c_str());
if (!m_stLocale.empty())
{
//mysql_options(&m_hDB, MYSQL_SET_CHARSET_DIR , " /usr/local/share/mysql/charsets/");
//mysql_options(&m_hDB, MYSQL_SET_CHARSET_DIR , "/usr/local/share/mysql/charsets");
//mysql_options(&m_hDB, MYSQL_SET_CHARSET_DIR , "/usr/local/share/mysql");
if (mysql_options(&m_hDB, MYSQL_SET_CHARSET_NAME, m_stLocale.c_str()) != 0)
{
fprintf(stderr, "mysql_option failed : MYSQL_SET_CHARSET_NAME %s ", mysql_error(&m_hDB));
}
}
if (!mysql_real_connect(&m_hDB, m_stHost.c_str(), m_stUser.c_str(), m_stPassword.c_str(), m_stDB.c_str(), m_iPort, NULL, CLIENT_MULTI_STATEMENTS))
{
fprintf(stderr, "mysql_real_connect: %s\n", mysql_error(&m_hDB));
return false;
}
my_bool reconnect = true;
if (0 != mysql_options(&m_hDB, MYSQL_OPT_RECONNECT, &reconnect))
fprintf(stderr, "mysql_option: %s\n", mysql_error(&m_hDB));
fprintf(stdout, "AsyncSQL: connected to %s (reconnect)\n", m_stHost.c_str());
//while (!QueryLocaleSet());
m_ulThreadID = mysql_thread_id(&m_hDB);
m_bConnected = true;
return true;
}
bool CAsyncSQL::Setup(CAsyncSQL * sql, bool bNoThread)
{
return Setup(sql->m_stHost.c_str(),
sql->m_stUser.c_str(),
sql->m_stPassword.c_str(),
sql->m_stDB.c_str(),
sql->m_stLocale.c_str(),
bNoThread,
sql->m_iPort);
}
bool CAsyncSQL::Setup(const char * c_pszHost, const char * c_pszUser, const char * c_pszPassword, const char * c_pszDB, const char * c_pszLocale, bool bNoThread, int iPort)
{
m_stHost = c_pszHost;
m_stUser = c_pszUser;
m_stPassword = c_pszPassword;
m_stDB = c_pszDB;
m_iPort = iPort;
if (c_pszLocale)
{
m_stLocale = c_pszLocale;
sys_log(0, "AsyncSQL: locale %s", m_stLocale.c_str());
}
if (!bNoThread)
{
#ifndef __WIN32__
m_mtxQuery = std::make_unique<pthread_mutex_t>();
m_mtxResult = std::make_unique<pthread_mutex_t>();
if (0 != pthread_mutex_init(m_mtxQuery.get(), NULL))
{
perror("pthread_mutex_init");
exit(0);
}
if (0 != pthread_mutex_init(m_mtxResult.get(), NULL))
{
perror("pthread_mutex_init");
exit(0);
}
pthread_create(&m_hThread, NULL, AsyncSQLThread, this);
#else
m_mtxQuery = std::make_unique<CRITICAL_SECTION>();
m_mtxResult = std::make_unique<CRITICAL_SECTION>();
::InitializeCriticalSection(m_mtxQuery.get());
::InitializeCriticalSection(m_mtxResult.get());
m_hThread = (HANDLE)::_beginthreadex(NULL, 0, AsyncSQLThread, this, 0, NULL);
if (m_hThread == INVALID_HANDLE_VALUE) {
perror("CAsyncSQL::Setup");
return false;
}
#endif
return true;
}
else
return Connect();
}
void CAsyncSQL::Quit()
{
m_bEnd = true;
m_sem.Release();
#ifndef __WIN32__
if (m_hThread)
{
pthread_join(m_hThread, NULL);
m_hThread = NULL;
}
#else
if (m_hThread != INVALID_HANDLE_VALUE) {
::WaitForSingleObject(m_hThread, INFINITE);
m_hThread = INVALID_HANDLE_VALUE;
}
#endif
}
std::unique_ptr<SQLMsg> CAsyncSQL::DirectQuery(const char * c_pszQuery)
{
if (m_ulThreadID != mysql_thread_id(&m_hDB))
{
sys_log(0, "MySQL connection was reconnected. querying locale set"); // @warme012
while (!QueryLocaleSet());
m_ulThreadID = mysql_thread_id(&m_hDB);
}
auto p = std::make_unique<SQLMsg>();
p->m_pkSQL = &m_hDB;
p->iID = ++m_iMsgCount;
p->stQuery = c_pszQuery;
if (mysql_real_query(&m_hDB, p->stQuery.c_str(), p->stQuery.length()))
{
char buf[1024];
snprintf(buf, sizeof(buf),
"AsyncSQL::DirectQuery : mysql_query error: %s\nquery: %s",
mysql_error(&m_hDB), p->stQuery.c_str());
sys_err(buf);
p->uiSQLErrno = mysql_errno(&m_hDB);
}
p->Store();
return p;
}
void CAsyncSQL::AsyncQuery(const char * c_pszQuery)
{
SQLMsg * p = new SQLMsg;
p->m_pkSQL = &m_hDB;
p->iID = ++m_iMsgCount;
p->stQuery = c_pszQuery;
PushQuery(p);
}
void CAsyncSQL::ReturnQuery(const char * c_pszQuery, void * pvUserData)
{
SQLMsg * p = new SQLMsg;
p->m_pkSQL = &m_hDB;
p->iID = ++m_iMsgCount;
p->stQuery = c_pszQuery;
p->bReturn = true;
p->pvUserData = pvUserData;
PushQuery(p);
}
void CAsyncSQL::PushResult(SQLMsg * p)
{
MUTEX_LOCK(m_mtxResult.get());
m_queue_result.push(p);
MUTEX_UNLOCK(m_mtxResult.get());
}
bool CAsyncSQL::PopResult(SQLMsg ** pp)
{
MUTEX_LOCK(m_mtxResult.get());
if (m_queue_result.empty())
{
MUTEX_UNLOCK(m_mtxResult.get());
return false;
}
*pp = m_queue_result.front();
m_queue_result.pop();
MUTEX_UNLOCK(m_mtxResult.get());
return true;
}
void CAsyncSQL::PushQuery(SQLMsg * p)
{
MUTEX_LOCK(m_mtxQuery.get());
m_queue_query.push(p);
//m_map_kSQLMsgUnfinished.insert(std::make_pair(p->iID, p));
m_sem.Release();
MUTEX_UNLOCK(m_mtxQuery.get());
}
bool CAsyncSQL::PeekQuery(SQLMsg ** pp)
{
MUTEX_LOCK(m_mtxQuery.get());
if (m_queue_query.empty())
{
MUTEX_UNLOCK(m_mtxQuery.get());
return false;
}
*pp = m_queue_query.front();
MUTEX_UNLOCK(m_mtxQuery.get());
return true;
}
bool CAsyncSQL::PopQuery(int iID)
{
MUTEX_LOCK(m_mtxQuery.get());
if (m_queue_query.empty())
{
MUTEX_UNLOCK(m_mtxQuery.get());
return false;
}
m_queue_query.pop();
//m_map_kSQLMsgUnfinished.erase(iID);
MUTEX_UNLOCK(m_mtxQuery.get());
return true;
}
bool CAsyncSQL::PeekQueryFromCopyQueue(SQLMsg ** pp)
{
if (m_queue_query_copy.empty())
return false;
*pp = m_queue_query_copy.front();
return true;
}
int CAsyncSQL::CopyQuery()
{
MUTEX_LOCK(m_mtxQuery.get());
if (m_queue_query.empty())
{
MUTEX_UNLOCK(m_mtxQuery.get());
return -1;
}
while (!m_queue_query.empty())
{
SQLMsg * p = m_queue_query.front();
m_queue_query_copy.push(p);
m_queue_query.pop();
}
//m_map_kSQLMsgUnfinished.erase(iID);
int count = m_queue_query_copy.size();
MUTEX_UNLOCK(m_mtxQuery.get());
return count;
}
bool CAsyncSQL::PopQueryFromCopyQueue()
{
if (m_queue_query_copy.empty())
{
return false;
}
m_queue_query_copy.pop();
//m_map_kSQLMsgUnfinished.erase(iID);
return true;
}
int CAsyncSQL::GetCopiedQueryCount()
{
return m_iCopiedQuery;
}
void CAsyncSQL::ResetCopiedQueryCount()
{
m_iCopiedQuery = 0;
}
void CAsyncSQL::AddCopiedQueryCount(int iCopiedQuery)
{
m_iCopiedQuery += iCopiedQuery;
}
DWORD CAsyncSQL::CountQuery()
{
return m_queue_query.size();
}
DWORD CAsyncSQL::CountResult()
{
return m_queue_result.size();
}
void __timediff(struct timeval *a, struct timeval *b, struct timeval *rslt)
{
if (a->tv_sec < b->tv_sec)
rslt->tv_sec = rslt->tv_usec = 0;
else if (a->tv_sec == b->tv_sec)
{
if (a->tv_usec < b->tv_usec)
rslt->tv_sec = rslt->tv_usec = 0;
else
{
rslt->tv_sec = 0;
rslt->tv_usec = a->tv_usec - b->tv_usec;
}
}
else
{ /* a->tv_sec > b->tv_sec */
rslt->tv_sec = a->tv_sec - b->tv_sec;
if (a->tv_usec < b->tv_usec)
{
rslt->tv_usec = a->tv_usec + 1000000 - b->tv_usec;
rslt->tv_sec--;
} else
rslt->tv_usec = a->tv_usec - b->tv_usec;
}
}
class cProfiler
{
public:
cProfiler()
{
m_nInterval = 0 ;
memset( &prev, 0, sizeof(prev) );
memset( &now, 0, sizeof(now) );
memset( &interval, 0, sizeof(interval) );
Start();
}
cProfiler(int nInterval = 100000)
{
m_nInterval = nInterval;
memset( &prev, 0, sizeof(prev) );
memset( &now, 0, sizeof(now) );
memset( &interval, 0, sizeof(interval) );
Start();
}
void Start()
{
gettimeofday (&prev , (struct timezone *) 0);
}
void Stop()
{
gettimeofday(&now, (struct timezone*) 0);
__timediff(&now, &prev, &interval);
}
bool IsOk()
{
if (interval.tv_sec > (m_nInterval / 1000000))
return false;
if (interval.tv_usec > m_nInterval)
return false;
return true;
}
struct timeval * GetResult() { return &interval; }
long GetResultSec() { return interval.tv_sec; }
long GetResultUSec() { return interval.tv_usec; }
private:
int m_nInterval;
struct timeval prev;
struct timeval now;
struct timeval interval;
};
void CAsyncSQL::ChildLoop()
{
cProfiler profiler(500000);
while (!m_bEnd)
{
m_sem.Wait();
int count = CopyQuery();
if (count <= 0)
continue;
AddCopiedQueryCount(count);
SQLMsg * p;
while (count--)
{
profiler.Start();
if (!PeekQueryFromCopyQueue(&p))
continue;
if (m_ulThreadID != mysql_thread_id(&m_hDB))
{
sys_log(0, "MySQL connection was reconnected. querying locale set"); // @warme012
while (!QueryLocaleSet());
m_ulThreadID = mysql_thread_id(&m_hDB);
}
if (mysql_real_query(&m_hDB, p->stQuery.c_str(), p->stQuery.length()))
{
p->uiSQLErrno = mysql_errno(&m_hDB);
sys_err("AsyncSQL: query failed: %s (query: %s errno: %d)",
mysql_error(&m_hDB), p->stQuery.c_str(), p->uiSQLErrno);
switch (p->uiSQLErrno)
{
case CR_SOCKET_CREATE_ERROR:
case CR_CONNECTION_ERROR:
case CR_IPSOCK_ERROR:
case CR_UNKNOWN_HOST:
case CR_SERVER_GONE_ERROR:
case CR_CONN_HOST_ERROR:
case ER_NOT_KEYFILE:
case ER_CRASHED_ON_USAGE:
case ER_CANT_OPEN_FILE:
case ER_HOST_NOT_PRIVILEGED:
case ER_HOST_IS_BLOCKED:
case ER_PASSWORD_NOT_ALLOWED:
case ER_PASSWORD_NO_MATCH:
case ER_CANT_CREATE_THREAD:
case ER_INVALID_USE_OF_NULL:
m_sem.Release();
sys_err("AsyncSQL: retrying");
continue;
}
}
profiler.Stop();
if (!profiler.IsOk())
sys_log(0, "[QUERY : LONG INTERVAL(OverSec %ld.%ld)] : %s",
profiler.GetResultSec(), profiler.GetResultUSec(), p->stQuery.c_str());
PopQueryFromCopyQueue();
if (p->bReturn)
{
p->Store();
PushResult(p);
}
else
delete p;
++m_iQueryFinished;
}
}
SQLMsg * p;
while (PeekQuery(&p))
{
if (m_ulThreadID != mysql_thread_id(&m_hDB))
{
sys_log(0, "MySQL connection was reconnected. querying locale set"); // @warme012
while (!QueryLocaleSet());
m_ulThreadID = mysql_thread_id(&m_hDB);
}
if (mysql_real_query(&m_hDB, p->stQuery.c_str(), p->stQuery.length()))
{
p->uiSQLErrno = mysql_errno(&m_hDB);
sys_err("AsyncSQL::ChildLoop : mysql_query error: %s:\nquery: %s",
mysql_error(&m_hDB), p->stQuery.c_str());
switch (p->uiSQLErrno)
{
case CR_SOCKET_CREATE_ERROR:
case CR_CONNECTION_ERROR:
case CR_IPSOCK_ERROR:
case CR_UNKNOWN_HOST:
case CR_SERVER_GONE_ERROR:
case CR_CONN_HOST_ERROR:
case ER_NOT_KEYFILE:
case ER_CRASHED_ON_USAGE:
case ER_CANT_OPEN_FILE:
case ER_HOST_NOT_PRIVILEGED:
case ER_HOST_IS_BLOCKED:
case ER_PASSWORD_NOT_ALLOWED:
case ER_PASSWORD_NO_MATCH:
case ER_CANT_CREATE_THREAD:
case ER_INVALID_USE_OF_NULL:
continue;
}
}
sys_log(0, "QUERY_FLUSH: %s", p->stQuery.c_str());
PopQuery(p->iID);
if (p->bReturn)
{
p->Store();
PushResult(p);
}
else
delete p;
++m_iQueryFinished;
}
}
int CAsyncSQL::CountQueryFinished()
{
return m_iQueryFinished;
}
void CAsyncSQL::ResetQueryFinished()
{
m_iQueryFinished = 0;
}
MYSQL * CAsyncSQL::GetSQLHandle()
{
return &m_hDB;
}
size_t CAsyncSQL::EscapeString(char* dst, size_t dstSize, const char *src, size_t srcSize)
{
if (0 == srcSize)
{
memset(dst, 0, dstSize);
return 0;
}
if (0 == dstSize)
return 0;
if (dstSize < srcSize * 2 + 1)
{
char tmp[256];
size_t tmpLen = sizeof(tmp) > srcSize ? srcSize : sizeof(tmp);
strlcpy(tmp, src, tmpLen);
sys_err("FATAL ERROR!! not enough buffer size (dstSize %u srcSize %u src%s: %s)",
dstSize, srcSize, tmpLen != srcSize ? "(trimmed to 255 characters)" : "", tmp);
dst[0] = '\0';
return 0;
}
return mysql_real_escape_string(GetSQLHandle(), dst, src, srcSize);
}
void CAsyncSQL2::SetLocale(const std::string & stLocale)
{
m_stLocale = stLocale;
QueryLocaleSet();
}
//martysama0134's aad276684955eb3421d3edd3e79cd0dc
AsyncSQL.h (Srcs/Server/libsql)
Kod:
#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/server/mysql.h>
#include <mysql/server/errmsg.h>
#include <mysql/server/mysqld_error.h>
#include <memory>
#include <mysql/server/mysql/psi/psi_memory.h>
#include "Semaphore.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;
unsigned int uiResultPos;
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);
std::unique_ptr<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;
std::unique_ptr<pthread_mutex_t> m_mtxQuery;
std::unique_ptr<pthread_mutex_t> m_mtxResult;
#else
HANDLE m_hThread;
std::unique_ptr<CRITICAL_SECTION> m_mtxQuery;
std::unique_ptr<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
//martysama0134's aad276684955eb3421d3edd3e79cd0dc
Son düzenleme: