Skip to content

Instantly share code, notes, and snippets.

@icedac
Created May 26, 2015 16:40
Show Gist options
  • Save icedac/bbfa29ff6eec275472c0 to your computer and use it in GitHub Desktop.
Save icedac/bbfa29ff6eec275472c0 to your computer and use it in GitHub Desktop.
server_code.cpp, client_code.cpp
using namespace T_ms_db;
/****************************************************************************
* main server
*/
class MS_ClientSession : public ITDBToTMSHandler { // 생성된 handler class에서 상속
/*...*/
template < typename T >
void QueueSend(const T&) {} // 테스트 코드용 dummy Send
int On_DM_GetAccountRes(const Recv_DM_GetAccountRes *packet) override;
void Dispatch( const char* data, size_t size )
{
// header=4bytes
// 실제로는 더 복잡하지만 임의로 코드를 대충 적어놓음.
WORD size, type;
if ( size < sizeof(size)+sizeof(type))
return false;
WORD* h = (WORD*)data;
size = *h++;
type = *h++;
// 생성된 dispatch, OnTDBToTMS() 호출
OnTDBToTMS( type, (const char*)h, size - (sizeof(WORD)*2), true, (void*)this );
}
void SendGetAccount();
};
void MS_ClientSession::SendGetAccount()
{ // packet send test
basic::TPacketSendBuffer< Send_MD_GetAccountReq > buf;
buf.pk.acct_name = "icedac";
QueueSend(&buf);
}
int MS_ClientSession::On_DM_GetAccountRes(const Recv_DM_GetAccountRes *packet)
{ // packet handler
packet->acct_id;
packet->acct_name;
packet->nickname;
packet->creation_time;
printf("id:%d nick:%s creation_time:%I64x\n", packet->acct_id, packet->acct_name, packet->creation_time );
return RESULT_TRUE;
}
using namespace T_ms_db;
/****************************************************************************
* db server
*/
class DB_ClientSession : public ITMSToTDBHandler {
/*...*/
template < typename T >
void QueueSend( const T& ) {} // dummy
void Dispatch( const char* data, size_t size ) // dummy
{
// header=4bytes
// 실제로는 더 복잡하지만 임의로 코드를 대충 적어놓음.
WORD size, type;
if ( size < sizeof(size)+sizeof(type))
return false;
WORD* h = (WORD*)data;
size = *h++;
type = *h++;
// 생성된 dispatch, OnTMSToTDB() 호출
OnTMSToTDB( type, (const char*)h, size - (sizeof(WORD)*2), true, (void*)this );
}
int On_MD_GetAccountReq(const Recv_MD_GetAccountReq *packet) override;
void select_insert_sp_test();
};
int DB_ClientSession::On_MD_GetAccountReq(const Recv_MD_GetAccountReq *packet)
{
auto* dbpool = App::Instance().GetDBPooler();
const auto* dbname = App::Instance().GetDBName();
auto* db = dbpool->GetUpdater();
typedef Recv_MD_GetAccountReq ReqType;
auto sendF = [this, packet](Error ec) -> int {
basic::TPacketSendBuffer< ReqType::Ack > buf;
buf.pk.result = Enum_AsInt(ec);
buf.pk.acct_name = packet->acct_name;
this->QueueSend(&buf);
return RESULT_TRUE;
};
basic::TPacketSendBuffer< ReqType::Ack > buf;
{
test_db::sp_get_account_by_name sp(packet->acct_name);
sp.open(db, dbname);
// check result parameter
if (sp.out->result == 0)
sendF(Error::ERR_RESOURCE_NOT_FOUND);
buf.pk.result = ms_db::Enum_AsInt(Error::SUCCESS);
for (const auto& item : sp)
{
item.copy_to(buf.pk);
break;
}
}
QueueSend(&buf);
return RESULT_TRUE;
}
void DB_ClientSession::select_insert_sp_test()
{
auto* dbpool = CAppDBProxy::Instance().GetDBPooler();
const auto* dbname = CAppDBProxy::Instance().GetDBName();
auto db_u = dbpool->GetUpdater();
auto* db = db_u.Get();
{ // version check
test_db::sp_version_check sp;
sp.open(db, dbname);
sp.out->version;
sp.out->minor_version;
printf("v:%I64x mv:%I64x\n", sp.out->version, sp.out->minor_version );
}
{ // select
auto acct_list = test_db::tbl_account::search(db, dbname);
for ( const auto& item : acct_list )
{
printf("id:%d nick:%s creation_time:%I64x\n", item.acct_id, item.acct_name, item.creation_time );
}
}
{ // select where
test_db::tbl_account::K_by_acct_name where;
where.acct_name = "icedac";
auto acct_list = test_db::tbl_account::search(db, where, dbname );
for (const auto& item : acct_list)
{
printf("id:%d nick:%s creation_time:%I64x\n", item.acct_id, item.acct_name, item.creation_time);
}
}
{ // insert, delete
auto& persist = test_db::tbl_account::persist(db, dbname);
test_db::tbl_account::table_t item;
item.acct_id = 1234;
item.acct_name = "icedac";
persist.Insert(item);
test_db::tbl_account::K_by_acct_name where;
where.acct_name = "icedac";
persist.Erase(where);
}
{ // sp
test_db::sp_get_account_by_name sp("icedac");
sp.open(db, dbname);
// if (sp.out->result == 0)
for (const auto& item : sp)
{
printf("id:%d nick:%s creation_time:%I64x\n", item.acct_id, item.acct_name, item.creation_time);
// item.copy_to(buf.pk);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment