Method, storage medium and system for detecting terminal state with low delay
Technical Field
The present invention relates to the field of communications technologies, and in particular, to a method, a storage medium, and a system for detecting a terminal state with low latency.
Background
During network communication in a long connection state, the client generally does not actively disconnect the connection with the server, however, as the number of connected clients increases or when the network fluctuates, some clients which do not have read-write time for a long time disconnect, so as to ensure that the server is in an optimal state.
The heartbeat mechanism is a mechanism commonly used in computer terminal equipment for detecting and judging whether the terminals are in an effective connection state, the terminals send a self-defined structure body such as a heartbeat packet to another terminal which establishes communication connection at regular time, and the connected terminals confirm whether disconnection occurs according to the sending and receiving of heartbeats.
Most of the existing heartbeat mechanisms send messages regularly through a TCP (transmission control protocol), however, connection establishment based on the TCP consumes a lot of time and CPU (central processing unit) resources, and the existing detection of the connection state of a client based on the TCP generally comprises that a server sets a state field, the client regularly sends heartbeats to the server to maintain the field, if the connection state exceeds a set time limit, if the heartbeat is not received, namely the change of the state field is detected, the client is judged to be offline, the mode is greatly influenced by network fluctuation, particularly in a multi-terminal synchronous connection system, instructions and data do not need to be sent between terminals in real time, and when the network fluctuation occurs, misjudgment of connection detection may be caused;
based on the problem, in practice, the time length of a period of time is set as a detection time interval, the receiving and sending times of the heartbeat signals in the preset time interval are monitored and obtained, finally, the times of successfully receiving or not receiving the heartbeat signals in the time interval are counted, whether the times of the heartbeat signals reach the set times is judged, and the times are used as the detection result of the connection state.
Disclosure of Invention
In order to solve the technical problems, the invention provides a method, a storage medium and a system for detecting the state of a terminal with low delay, the method adopts an unreliable message transmission protocol udp to transmit heartbeat signal messages, the consumption of network resources is reduced, the system can bear higher concurrency under the condition of low delay, the state identification of the terminal is connected by creating byte arrays to store, after a server receives the heartbeat signals, the state quantity stored in a memory unit is continuously updated according to the reception of the heartbeat signals, the byte arrays are recycled, the online state of the terminal is judged by judging the online state identification proportion in the current array in real time, the detection accuracy is improved, and the delay time is reduced.
Specifically, the present invention provides a method for detecting a terminal state with low delay, which is characterized by comprising the following steps:
creating a byte array, equally dividing a memory space therein as a state space of the terminal, and storing a state identifier;
initializing the byte array, and initializing the state identifier of each memory unit to a set state quantity;
receiving a heartbeat signal to change the state quantity stored in the byte array, acquiring and recording the received heartbeat signal, receiving the heartbeat signal of the terminal by the server, and changing the state quantity of a memory unit corresponding to the current heartbeat signal time unit in a memory space corresponding to the terminal according to the receiving condition;
and the terminal online judgment is used for acquiring the state identification result of the byte array corresponding to the memory space of each terminal and judging whether the terminal is online or not according to the state proportion of successfully receiving the heartbeat signal in the state identification result.
Further, the transmission of the heartbeat signal between the terminal and the server adopts a udp protocol to transmit the heartbeat signal message data.
Further, the sending period of the heartbeat signal is 200 ms.
Further, when the state quantity stored in the byte array is changed by receiving the heartbeat signal, the byte array is recycled, the state quantity of the next memory unit is re-assigned in each period of receiving the heartbeat signal, and the state quantity is updated by pointing to the first memory unit after the state quantity of the last memory unit in the corresponding memory space is assigned.
Furthermore, after the state quantity of the current memory unit is reassigned, the state quantity of the next memory unit is changed at the same time, so that the state identifier in the next memory unit is changed into the initial state.
Further, when the terminal judges online, the state quantity of the heartbeat signal message successfully received is counted according to the current state identification result, and whether the preset online state quantity is met or not is judged.
Further, the parameter ratio is a parameter value which is not less than X/2 and rounded up, and X represents the current state space quantity of the memory unit.
The present invention also provides a computer-readable storage medium storing a computer program, wherein the computer program, when executed by a processor, implements the steps of the method for detecting a terminal state with low latency.
The invention also provides a terminal system, wherein the terminal comprises a memory, a processor and a communication module, the memory stores computer programs, the terminal system is characterized in that the terminals are in communication connection through the communication module, and the processor realizes the steps of the method for detecting the state of the terminal with low delay when executing the computer programs.
The invention has the following beneficial effects:
the method comprises the steps of establishing an array for storing state identification of a terminal, utilizing the array to store various types of data, obtaining and updating state quantity stored in the array through a pointer, facilitating statistical calculation, reducing calculation pressure of a CPU, continuously updating data in state spaces corresponding to different terminals in the array by a server according to received heartbeat signals, judging the connection state of the terminal according to the current state quantity ratio in real time, and avoiding misjudgment caused by repeated failure in receiving and sending heartbeat signals under abnormal conditions such as network instability.
The heartbeat signal message is sent by adopting a udp protocol to realize communication, so that the consumption of network resources is reduced, and the concurrency borne by the system is improved under the condition of low delay.
Drawings
FIG. 1 is a schematic overall flow diagram of the process of the present invention;
FIG. 2 is a schematic diagram of array storage in the detection process according to an embodiment of the present invention;
FIG. 3 is a schematic diagram of an apparatus according to an embodiment of the present invention;
FIG. 4 is a schematic diagram of a system architecture in an embodiment of the invention;
FIG. 5 is a flow chart illustrating the execution of the process of the present invention.
Description of the drawings: in fig. 2, a is a state space of an initial state, b is a state space when a heartbeat signal is received once in a first time unit, c is a state space when a heartbeat signal is received four times in a fourth time unit, and d is a state space when an eight-time heartbeat signal is received in an eighth time unit.
Detailed Description
In the following description, technical solutions in the embodiments of the present invention are clearly and completely described, and it is obvious that the described embodiments are only a part of the embodiments of the present invention, and not all of the embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.
Example 1
Embodiment 1 of the present invention provides a method for detecting a terminal state with low delay, where a heartbeat is a very small data packet sent to another party interconnected at intervals in a communication, as shown in fig. 1, including the following steps:
s1: creating a memory space in which a byte array is equally divided as a state space storage state identifier of the terminal;
the state identifier indicates whether scalar data of the heartbeat signal is received or not, memory units are equally divided to correspond to the terminals, and the number of the memory units is adjusted according to the heartbeat signal sending period and the number of the terminals.
S2: initializing the byte array, and initializing the state identifier of each memory unit to a set state quantity;
as shown in a in fig. 2, five consecutive memory units form a state space of one terminal, and initialization sets the state quantities of all the memory units to 0.
S3: acquiring and recording a received heartbeat signal, receiving the heartbeat signal of the terminal by a server, and changing the state quantity of a memory unit corresponding to the time unit in a memory space corresponding to the terminal according to the receiving condition;
a terminal serving as a client sends a heartbeat message to a server in each certain heartbeat signal period, after the server receives a heartbeat signal, the server assigns a state quantity of a T% X memory unit of a state space corresponding to the terminal to 1, sets a T% X +1 memory unit to 0, wherein T represents a current heartbeat period, and X represents a number of memory units contained in the state space, as shown in b in fig. 2, a first time unit, namely, after the first heartbeat period, if the heartbeat is received, a pointer points to the first memory unit of each state space in an array, sets the state quantity to 1, and sets the next memory unit to 0;
referring to c of fig. 2, in the fourth time unit, if four heartbeats are received, the change of the storage state quantity in the array, the array is recycled, according to the divided memory units as the limiting parameters, when the last memory position of a state space is used, the pointer points to the first memory position to update the state quantity again, as shown in d of fig. 2, the variation of the storage state quantity in the array when the eight heartbeats are successfully received in the eighth time unit, so as to avoid counting the successful receiving heartbeat times according to a certain time period, under the condition of longer time interval, the detection result is inaccurate, and the array is recycled, so that the system can judge the connection state of the terminal in real time according to the state space corresponding to the current terminal, and the influence caused by unstable heartbeat receiving caused by abnormal conditions during state detection is reduced.
S4: acquiring state identification results of the byte arrays corresponding to the memory space of each terminal, and judging whether the terminal is on line or not according to the state proportion of successfully receiving heartbeats in the state identification results;
the pointer traverses the array, acquires state quantity data stored in the current array, counts the sum of state quantities in each state space, calculates the proportion of the state quantity of the received heartbeat to the whole memory space of the corresponding terminal, judges whether the state quantity of the received heartbeat is larger than a preset parameter proportion alpha, and judges whether the terminal is on line according to the proportion of the received heartbeat state quantity, wherein the preset parameter proportion alpha is not lower than a parameter value which is rounded up for X/2 in the embodiment.
Example 2
An embodiment 2 of the present invention provides a terminal system, as shown in fig. 3, where a terminal includes a memory, a processor, and a communication module, and in combination with fig. 4, the system is provided with a plurality of terminals, which are respectively used as a server and a client, where the client and the server establish a communication connection through the communication module, in this embodiment, taking a multi-terminal synchronization system as an example, and in combination with fig. 5, a terminal state detection process in the system is as follows:
firstly, starting each terminal of the system, initializing, and initializing a byte array which is created and stored in a server and used for storing state quantity, namely traversing the array to set the stored state quantity to 0;
then the server monitors and receives heartbeats sent by the clients;
judging whether the heartbeat is detected in a set time period, if the heartbeat is successfully received, updating the state quantity of the current corresponding state space, namely that the pointer points to the current position, setting the state quantity to be 1, then adding 1 to the pointer to point to the next memory unit, and simultaneously judging whether the memory unit corresponding to the heartbeat is the last memory position of the state space of the terminal, namely judging whether the pointer of the next memory unit exceeds the memory position of the state space of the current terminal;
if the current terminal state space exceeds the first memory position, pointing the pointer to the first memory position of the current terminal state space, and setting the state quantity stored in the memory unit as 0; if the state quantity stored in the next memory unit does not exceed the state space storage unit to which the current terminal belongs, setting the state quantity stored in the next memory unit to be 0, namely setting the value of the memory unit of the Tth% X to be 0 in the Tth heartbeat cycle and setting the value of the memory unit of the T% X +1 to be 1;
if the time unit does not receive the heartbeat, adding 1 to the pointer to point to the next memory unit, and setting the state quantity stored in the next memory unit to be 0.
In the monitoring process of the server, after the state quantity stored in the array is updated in each heartbeat cycle, the state quantity of each current state space is obtained, the state quantity condition of the received heartbeat is counted, namely, the state quantity stored in the state space corresponding to each terminal is summed, whether the proportion of the memory units in the occupied state space exceeds a preset parameter proportion alpha is judged, if the preset parameter proportion exceeds, the disconnection of the terminal is indicated, prompt is carried out, and if the preset parameter proportion does not exceed, the terminal is in a connection state.
Example 3
Embodiment 3 of the present invention provides a computer-readable storage medium, in which a computer program is stored, where the computer program is executed by a processor to perform the steps of the terminal state detection method, so as to complete a system operation detection process.
The invention is not limited to the foregoing embodiments. The invention extends to any novel feature or any novel combination of features disclosed in this specification and any novel method or process steps or any novel combination of features disclosed.