Вопрос

I want to implement queue on server side functions:

  1. store the packets in queue (copy them from recvfrom buffer)
  2. search the packets by id's and retrieve them for retransmission
  3. take the 2 packets with different id's and process them together
  4. delete packets with the same id
  5. delete all the packets from the queue when timer expires

I have reading a lot, but I'm not sure what is the best data structure to use for this problem, linked lists, hash tables? I don't have experience in this field to I need the advice for the most efficient algorithm

Thank you

Это было полезно?

Решение

For efficient data structure linked list is best choice .for example udp data structure in link list

/* The UDP data packet structure */

struct udp_data
{
   struct udp_data* u_next;
   short id;    /* id for this packet */
   void *   u_data;     /* packet data */

   //Add more field if you want
   ......................................
   .........................................

};

typedef struct udp_data *UDP_DATA;

And most important thing you must know linked list management.

Другие советы

The best way to go with this kind of issue as far i know is to use a standard header far all packets with same header lenght then take header size data, from header know the packet length and take the packet length data from the pool, and repeat the same.

hope to find your answer.

for example

struct Common_header{
        float ver;
        int flag;
        int msg_type;
        int msg_len;    
};

struct Req_Cim_Nim_Snr{
        struct Common_header header;
        char master[30];
        char cpe[30];   
};

struct Req_Cim_Nim_Fwr{
        struct Common_header header;
        char fwr_link[200]; 
};

in both the request messages i have a struct header which contains the length of the message, so i take header first and take msg_len length number of bites to get the whole message.

about the datastructure, since its first in first out, linked list is good enough.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top