Socket Programming in C and Unserialize the Response
I'm writing a program to communicate with certain patient monitor using connection-less (UDP) sockets. I need a suggestion for the fastest and most efficient algorithm that I should use to parse the data received from patient monitor.
Patient monitor sends data as AttributeList structure, the definition of the structure is below:-
typedef struct {
uint16 count;
uint16 length;
AVAType * values;
} AttributeList;
typdef struct {
uint16 id;
uint16 length;
void * data;
} AVAType
So an AttributeList contains many AVAType structures, and every AVAType structure contains id and data as void pointer (data is just an place-holder for actual data in AVAType). In fact, there are about 150 ids defined for AVAType and each ID, data must be placed in some corresponding data structure for parsing purposes.
So if ID in AVAType structure is 1, then data must be parsed using algorithm A but if ID is 2 then data must be parsed using Algorithm B and so on...
My current approach is using 150 if-else statements and loading the particular data-structure for AVAType and then parse the result. Also the patient monitor sends data in network byter order and I need unpack the AVAType as per the ID.
Please suggest some good approach to deal with this situation. It is just an university thing, I do not brother much about safety, I think safety can be improved gradually but right now my concern in speed.
Yes, it is actaully AVAType values[];. Can you be more specific on why is this different from AVAType * ? Can it be any useful if I define my structure other way around?
The usual way to map from integers to functions is just an array of function pointers
typedef void (*Fun_ptr)(int id, void *current_byte);
Fun_ptr fun_ptr[0xFFFF+1] = { funcA, funcB, ...};
If the ids are only one byte, I'd map all 256 (if it can be restricted).
Even with all 16 bit ids, it isn't a lot of storage.
I'd point unused id's at an 'error catch' function.
Use:
next_buffer_position = fun_ptr[id](id, current_buffer_position);
Then the function knows which id it was called for, making the error_catch function easy to write, you only need one.
Are you sure it is AVAType * values;
and void * data;
?
Is values
or data
supposed to be an offset within the packet?
It is not impossible, and may be correct. Currently AVAType and AttributeList are a fixed size.
The fields AVAType * values;
and void * data;
are the receiving computers size for a pointer. That could be anything, but likely 32 bits, or 64 bits. That is a potential problem if you move the software to different machines, and is why I am asking. They hold no data, they are pointers to data, but their size varies depending on the receiving machine. Suspicous. Double check what size they are defined to be.
A common way of handling packets on a network protocol, is with an empty array as the last field in the struct, eg
typdef struct {
uint16 id;
uint16 length;
unsigned char data[];
} AVAType;
In that approach, there is usually a packet length in a fixed position near the start of the packet, and that gives the actual size of the remainder of the packet. So the protocol defines whether the whole packet or data[]
is length bytes. It is a bit 'hackish' but it is easy to use. (NB IIRC unsigned char data[];
is not legal C++)
Summary: It is likely the in-memory structs are exactly the typedefs you've shown, but the on-the-wire protocol is a bit odd bcause of the variable size of a pointer.
链接地址: http://www.djcxy.com/p/61902.html上一篇: C#套接字数据报溢出
下一篇: 用C语言编写Socket并反序列化响应