![]() |
|
How to return something to client from the server - Printable Version +- LCKB (https://lckb.dev/forum) +-- Forum: ** OLD LCKB DATABASE ** (https://lckb.dev/forum/forumdisplay.php?fid=109) +--- Forum: Guides & Help Section (https://lckb.dev/forum/forumdisplay.php?fid=193) +---- Forum: Help & Support (https://lckb.dev/forum/forumdisplay.php?fid=157) +----- Forum: Ep4 Support (https://lckb.dev/forum/forumdisplay.php?fid=128) +----- Thread: How to return something to client from the server (/showthread.php?tid=4933) |
- nicolasg - 06-11-2022 Hello, I'm trying to make a daily reward system, I've never done something "so complex" so I'm pretty lost on the subject... I managed to make the request on the client side, but when trying to return something from the server , it closes. This is the code that I have written, could someone tell me if there is an obvious problem? This is my ptype #ifndef __PTYPE_OLD_DO_DAILYREWARD_H__ #define __PTYPE_OLD_DO_DAILYREWARD_H__ #include "ptype_base.h" enum{ MSG_DAILYREWARD_ERROR, MSG_DAILYREWARD_LIST_REQ, MSG_DAILYREWARD_LIST_RES, }; #pragma pack(push, 1) namespace RequestClient{ struct doDailyRewardList : public pTypeBase{ }; } namespace ResponseClient{ struct ReceiveList : public pTypeBase{ std: tring lastDate;}; #ifndef _CLIENT_ inline void DailyRewardErrorMsg(CNetMsg::SP& msg, MSG_DAILYREWARD_ERROR_TYPE err){ msg->Init(MSG_DAILYREWARD); RefMsg(msg) << (unsigned char)MSG_DAILYREWARD_ERROR << (unsigned char)err; } inline void makeDailyRewardReturnList(CNetMsg::SP& msg, std: tring lastDate){ResponseClient::ReceiveList *packet = reinterpret_cast<ResponseClient::ReceiveList*>(msg->m_buf); packet->type = MSG_DAILYREWARD; packet->subType = MSG_DAILYREWARD_LIST_RES; packet->lastDate = lastDate; msg->setSize(sizeof(ResponseClient::ReceiveList)); } #endif } #pragma pack(pop) #endif And this would be the function I'm working on void do_DailyRewardList(CPC* ch, CNetMsg::SP& msg){ CDBCmd cmd; cmd.Init(&gserver->m_dbchar); std: tring sql = boost: tr(boost::format("SELECT * FROM t_daily_reward_system WHERE a_char_index=%d") % ch->m_index);cmd.SetQuery(sql); if(cmd.Open() == true && cmd.MoveNext() == true){ std: tring last_date = ("");cmd.GetRec("a_last_date", last_date); // Format: Datetime //LOG_INFO("DAILY REWARD SYSTEM Char Index : %d, Last Date : %s", ch->m_index, last_date); CNetMsg::SP rmsg(new CNetMsg); ResponseClient::makeDailyRewardReturnList(rmsg, last_date); SEND_Q(rmsg, ch->m_desc); return; }else{ LOG_ERROR("DAILY REWARD SYSTEM : se fue a la putah"); } } - nicolasg - 06-12-2022 I already managed to solve the problem. No more questions for now? - Andrein95 - 06-28-2022 On 6/12/2022 at 8:08 AM, nicolasg said: I already managed to solve the problem. No more questions for now? Wich was the problem in the code above? - nicolasg - 06-29-2022 2 hours ago, Andrein95 said: Wich was the problem in the code above? 2 things... 1) I were trying to send an unregistered package. 2) I did not indicate at any time the size of the package. This is an example of sending a previously defined packet (both server and client) from the server to the client CNetMsg::SP rmsg(new CNetMsg); MsgMessageBox(rmsg, MSG_EX_MSGBOX_INVEN_FULL); SEND_Q(rmsg, ch->m_desc); As long as the package is registered on the server and client, there would be no problem. In case of sending values such as item id or some type of data that is not a simple client server trigger or vice versa, the easiest way is to define a structure (Table or array as you prefer to call it), fill it with information, calculate the size and finally send the package... Example using pTypeBase (Server Side): // Create Simple array with pTypeBase (There's more to it than just pTypeBase, you can see them defined next to pTypeBase) struct MyArray : public pTypeBase{ int lastDate; } /////////////////////////////////////////////////////////////// CNetMsg::SP rmsg(new CNetMsg); MyArray* packet = reinterpret_cast<MyArray*>(rmsg->m_buf); // pTypeBase Requires 2 minimum values (type & subType), then you can add whatever you want. // Logically you must declare these packages packet->type = MSG_UPDATE_DATA_FOR_CLIENT; // In my case i take advantage of this previously defined message. packet->subType = MSG_SUB_UPDATE_DAILYREWARD; // And I define this to trigger my custom action on the client side. // Finally our data, in this case an int type without more packet->lastDate = last_date; // Indicate the size of the package/message to be sent rmsg->setSize(sizeof(MyArray)); // And push it SEND_Q(rmsg, ch->m_desc); Cliente Side: As I said take advantage of 'MSG_UPDATE_DATA_FOR_CLIENT' so i made usage of 'REG_PACKET_R' to associate the packages 'MSG_UPDATE_DATA_FOR_CLIENT' & 'MSG_SUB_UPDATE_DAILYREWARD' to a function that ultimately executes what i want REG_PACKET_R(MSG_UPDATE_DATA_FOR_CLIENT, MSG_SUB_UPDATE_DAILYREWARD, updateDailyReward); Example: void CSessionState::updateDailyReward(CNetworkMessage* istr){ CUIManager* pUIManager = CUIManager::getSingleton(); DailyRewardListRes* pPack = reinterpret_cast<DailyRewardListRes*>(istr->GetBuffer()); switch(pPack->subType){ case MSG_SUB_UPDATE_DAILYREWARD: pUIManager->GetDailyReward()->ReceiveList(istr); break; default: CUIMsgBox_Info MsgBoxInfo; MsgBoxInfo.SetMsgBoxInfo(_s("Daily Reward System"), UMBS_OK, UI_NONE, MSGCMD_NULL); MsgBoxInfo.AddString(_s("MSG FROM SERVER RECEIVED: unknown")); pUIManager->CreateMessageBox(MsgBoxInfo); break; } } final results xd p.s: this is just a hobby, sorry if I use wrong words or if my explanations lack technicalities |