tags:

views:

113

answers:

7

Hello Everybody,

I'm sending messages over TCP/IP and on the other side I parse TCP message.For example this is one of the sent messages.

$DKMSG(requestType=REQUEST_LOGIN&requestId=123&username=metdos&password=123)$EDKMSG

Clarification:

$DKMSG(       //Start
)$EDKMSG      //End
requestType   //Parameter
REQUEST_LOGIN //Parameter Value

Now I also want to add an Xml file to my message. I'm considering this option:

$DKMSG(requestType=REQUEST_LOGIN&xmlData=
 <Item id="56D@MIT" type="SIGNAL">
 <Label>
 <Text>56D</Text>
 <X1>10</X1>
 <Y1>40</Y1>
 <RotateAngle>90</RotateAngle>
 </Label>
 <X1>0</X1>
 <Y1>20</Y1>
 <Width>35</Width>
 <Height>10</Height>
 <Source>sgs3lr</Source>
</Item>
)$EDKMSG

There are problems with this way:

1-)It doesn't seem right to me.

2-)I have to handle delimeter "=" with much more care or I need to change it in parameters.

What are your suggestions, thanks.

+1  A: 

This looks like a homegrown format. You should use something out-of-the-box instead, like JSON, XML, protocol buffers, or even the young upstart: BERT, which even specifies an RPC protocol that uses the format. These formats all have parsers written for them in several languages, and they are all supported on C++.

Marcelo Cantos
If understand correctly, you suggest that sending a XML file for whole message, and use xmlData as a child of this Xml file.
metdos
Actually, I have a pathological aversion to XML, so I would favour any of the other formats over XML, but yes, that is the general idea.
Marcelo Cantos
+1  A: 

You can mimic HTTP, which is cleaner, well understood and easy to parse:

LOGIN DKMSG/1.0
request-id: 123
username: metdos
password: eNcrYpTED
content-type: text/xml
content-length: 237

<Item id="56D@MIT" type="SIGNAL">
 <Label>
 <Text>56D</Text>
 <X1>10</X1>
 <Y1>40</Y1>
 <RotateAngle>90</RotateAngle>
 </Label>
 <X1>0</X1>
 <Y1>20</Y1>
 <Width>35</Width>
 <Height>10</Height>
 <Source>sgs3lr</Source>
</Item>
Vijay Mathew
+1  A: 

Just a practical answer on question 2: a possibility is to encapsulate your data in $XMLDATA(...)$EXMLDATA, so you're not even dependent on XML.

About making a new protocol, as mentioned by Marcelo: check existing ones: XMLRPC, SOAP

stefaanv
+1  A: 

For TCP protocols, there really are two schools of thought. One uses MIME headers, as suggested by Vijay. The other uses binary length-prefixed strings. Length-prefixed strings are much more efficient to process than textual formats and do not require encoding special characters to remove ambiguity. The downside is a server that you can't talk to using a Telnet client.

Ben Voigt
+1  A: 

How about this suggestion?

  • replace "xmlData" with "content"
  • add another attribute "contentType"

Then specify your data format with "contentType" and put your real data in "content". As for the data format (contentType), Base64, considering the overheads it imposes on the actual data, could be a very good candidate if your data is not be too long.

shinkou
A: 

To Ben Voigt: why "The downside is a server that you can't talk to using a Telnet client."? I'm really confused.

Allopen
Brent, the answer box isn't a vehicle for responding to other answers. Use the commenting facility.
Marcelo Cantos
thanks for your response, but i don't have enough reputation to start a comment, :).
Allopen
+1  A: 

Just use the fact that XML is a tree, and can be re-rooted. Any information that you wanted to send alongside the original MXL can be added as attibutes on the new XML root.

<DKMSG requestType="REQUEST_LOGIN">
     <Item id="56D@MIT" type="SIGNAL">
     <Label>
     <Text>56D</Text>
     <X1>10</X1>
     <Y1>40</Y1>
     <RotateAngle>90</RotateAngle>
     </Label>
     <X1>0</X1>
     <Y1>20</Y1>
     <Width>35</Width>
     <Height>10</Height>
     <Source>sgs3lr</Source>
    </Item>
</DKMSG>
MSalters