Home » U++ Library support » U++ Core » Convert struct to string and reconstruct a struct from string
Re: Convert struct to string and reconstruct a struct from string [message #55342 is a reply to message #55335] |
Sun, 01 November 2020 15:39  |
 |
mirek
Messages: 14255 Registered: November 2005
|
Ultimate Member |
|
|
sinpeople wrote on Sat, 31 October 2020 16:43+Mirek
Thank you very much for this great example. It did really broaden my horizon in terms of C++ knowledge as a newbie.
Now I am having difficulties in handing the binary message for sending and receiving; The sending/receiving has been verified. Only the data format seems not very correct.
void LocalCtrl::RpcRequest()
{
TrafficMessage m;
m.traffic = "Lots of Traffic";
String data = StoreAsString(m);
SendCmd(Traffic, data);
}
void LocalCtrl::SendCmd(enum MessageIDs msgID, String data)
{
ClientUDPHead udpHead(local_cfg.nID, regional_cfg.strIP, regional_cfg.nPort);
UdpRpcCmd(udpHead, msgID, data);
}
The "UdpRpcCmd" will eventually calls the following function to send message to server side
void UdpCmd(ClientUDPHead head, enum MessageIDs msgID, String data)
{
UrrClient urr;
urr.SetServer(head.strDestIP, head.nDestPort);
int tm = GetTickCount();
String strCmd = Format("%d%d%s", head.clientID, msgID, data);
strCmd = urr.Call(strCmd);
int tm2 = GetTickCount();
String strMsg;
if(strCmd.GetCount())
{
strMsg = Format("Request: %s, Response: %s in %d ms", data, strCmd, tm2-tm);
}
else
{
strMsg = Format("Request: %s, Time out!", data);
}
//Do something account to RpcCmd request;
notify->OnReplyUdpRpcCmd(strMsg, (int) Random(500), Format(GetSysTime())); //notify result
}
I need to combine three things together before sending it out. Initially I used this one to combine the strings together.
String strCmd = Format("%d%d%s", head.clientID, msgID, data);
The server side can pick up the messages correctly. But I failed to extract them properly with the sample code below
for(;;)
{
UrrRequest r;
if(urr.Accept(r))
{
StringStream ss(~r); // error handling for now omitted
int client_id = ss.Get32();
int message_id = ss.Get32();
One<AMessage> m;
int q = message_maker.Find(message_id);
if(q < 0)
return;
(message_maker[q])(m); // create the required concrete message
ss % *m; // load data to struct
/*
Vector<String> tokens = Split(~r, [](int c) { return c == ':' || c == '\t' || c == ' ' || c == ',' || c == '.' ? 1 : 0; });
if(tokens.GetCount()>=2) // local_ctrl.nID + Command ID;
{
int nFind = ctrl.Find(tokens[0]);
if(nFind != -1)
{
ctrl[nFind]->UdpRpcCmd(r);
}
}
*/
}
}
Both the client_id and message_id are very big numbers. In fact, the clicen_id is an int and message_id is an enum which starts from 1;
How to combine 2 or more strings and separate them properly after network transmission in this case?
Thank you so much!
David WANG
I think you are mixing text interpretation (Format) and binary one (ss.Get32). Use StringStream on both sides..
Mirek
|
|
|
Goto Forum:
Current Time: Sat Apr 26 15:57:14 CEST 2025
Total time taken to generate the page: 0.02686 seconds
|