First, equipment terminal
Class Deviceclient {
int DeviceID;
int IP;
int port;
Char connectid[16];
time_t Lasttime;
struct Event timeoutev;//Timeout
};
typedef list<deviceclient *> Devicelist;
Second, mobile control terminal
Class Userclient {
int UserID;
int IP;
int port;
Char connectid[16];
time_t lasttime;//When receiving the update, and turn on the timeout device
struct Event timeoutev;//timeout: If timeout, the client is considered to be out of line
};
typedef list<userclient *>userlist;
Third, the UDP heartbeat packet Design (service side)
http://blog.csdn.net/liuguanghui1988/article/details/7090531
3.1 UDP packet Receive thread
1) recvfrom
2) Find Connectid and see if it's a heartbeat pack
3) If it is a heartbeat packet, look for the connectid corresponding client, if the client exists, update the Lasttime, and if the client does not exist, create a new client and update the Lasttime;
3.2 UDP Packet Timeout
1) Definition of time (three consecutive times the heartbeat packet is not received, that is, the time-out is 3 times times the client heartbeat packet time)
2) When the timeout event arrives, the client is considered to be out of line, deleting deviceclient or userclient
Four, UDP heartbeat packet Design (client)
Heartbeat packets are sent every 30 seconds
Five, the design of punching hole
Http://www.cppblog.com/Lee7/archive/2008/01/25/41850.html
Holes are initiated by mobile control terminals or device terminals. The following is an example of mobile control terminal Initiation, detailing the process of punching holes
To facilitate the description, define the mobile control terminal as userclient, the device terminal is deviceclient, the server
The process is as follows:
1) userclient Login
2) Deviceclient Login
3) userclient send "P2p_punch DeviceID" Message to Server
4) Server send "P2p_wantyou UserID" Message to Deviceclient
5) userclient send "P2p_message msg" Message to Deviceclient
6) deviceclient send "P2p_message msg" Message to Userclient
UDP Punch and Heartbeat package design