Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
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 #55335 is a reply to message #55326] Sat, 31 October 2020 16:43 Go to previous messageGo to previous message
sinpeople is currently offline  sinpeople
Messages: 29
Registered: October 2020
Location: Singapore
Promising Member

+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

[Updated on: Sat, 31 October 2020 16:46]

Report message to a moderator

 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Looking for examples to split string into container
Next Topic: What does this compiling error mean exactly?
Goto Forum:
  


Current Time: Mon May 13 23:18:39 CEST 2024

Total time taken to generate the page: 0.02503 seconds