Introduction to Hp-Socket High-Performance Network Library One

1. Introduction to Directory Structure

—DOC: Officially provided documents under the directory, including Development Guide

—DotNet: The development steps of the .net project, HPsocket.net can be deployed directly through nuget

—Linux: Linux projects, including android build

-MacOS: Development steps for mac environment

—Windows: Windows engineering, the most commonly used development environment

  --Bin:编译好的dll和lib

  --Demo:官方提供的使用示例,包括PUSH模型示例、 PULL模型示例、 PACK模型示例、 性能测试示例以及其它编程语言示例

  --Project:项目的VS工程

  --Include:使用HP-Socket库需要的头文件

  --Src是核心功能源代码,Project只是工程,源代码在这里

  --Common是通用功能源代码

  --Other Languages 其他语言项目地址

Currently supports Windows & Linux platforms

2. Model selection

2.1 What is a sticky package or a broken package

Since TCP is a streaming socket, the data received by the socket may not be a complete packet or a sticky packet. At this time, the application layer is needed to unpack and pack. For example, the client continuously sends three data packets with sizes of 300, 500, and 100 respectively. But the data received by the receiving end may be 200,400,100,200. So at this time, we need to pack and unpack the received data.
Since 200 is less than one data packet, the next data packet 400 needs to be combined. At this time, the data size is 600, but the first data packet sent by the client is 300, so you need to split 600 into 300+300 at this time. At this time, you can get the first data packet 300 and the remaining data 300, but the second data packet is 500. The data is not enough, so you need to combine the next data 100 with a total of 400 data. It is still not enough for one packet, and continue to pack the remaining 200. At this time, the data 600 is accepted, which is enough for one data packet 500, so 600 is disassembled into 500+100. Get the second data packet 500 and the remaining data 100, which exactly matches the third data packet 100. At this point, the data packet is parsed. Basic logic

while(true)
{
    data_size = recv_data();
    if(data_size < 数据包长度)
        continue;//继续接受数据
    ///循环拆包,当不够一个包的时候继续接受数据等待一个完整的包
    while(true)
    {
        //足够包长度,拆包
        data_size -= 数据包长度;
        //更新数据缓冲区,处理包
        hanle_pack(pack);
        if(data_size < 数据包长度)
            break;
    }
}

2.2 Three receiving models provided by Hp-Socket

HP-Socket provides PUSH / PULL / PACK and other receiving models, and the application can flexibly choose to process the package and unpack in a manual, semi-automatic or fully automatic manner.

Receiving model Receive event Description
PUSH OnReceive(pSender, dwConnID, pData, iLength) Manual way/Native way
PULL OnReceive(pSender, dwConnID, iLength) Semi-automatic
PACK OnReceive(pSender, dwConnID, pData, iLength) Fully automatic
  • When the PUSH  model component triggers the OnReceive (pSender, dwConnID, pData, iLength)
    event of the listener object , the application needs to process the received data immediately, such as: sticky packet processing, protocol analysis, etc. The component
    will not provide any assistance to data processing at the application layer.

  • When the PULL  model component triggers the OnReceive(pSender, dwConnID, iTotalLength) event of the listener object
    , the application detects whether the received data length (iTotalLength) meets the processing
    conditions according to the application layer protocol , and selectively processes it. This event can be ignored when iTotalLength is less than the current expected length
    ; when iTotalLength is greater than or equal to the current expected length, the
    Fetch(dwConnID, pData, iDataLength) method of the component is called cyclically to pull out the required data until the remaining number is reached.
    The data length is less than the currently expected length.
    The PULL model is suitable for
    scenarios where the application layer protocol is completely clear, and the application layer protocol can know the length of the next data packet based on the current data packet . A typical scenario is Head + Body. The length of the Head is fixed. The first packet is Head.
    The length of the Body is known from the Head. After receiving the Body, the next packet must be the Head.

  • When the PACK  model component triggers the OnReceive(pSender, dwConnID, pData, iLength)
    event of the listener object , it will ensure that pData is a complete data packet. PACK model components have application sends
    each data packet automatically adds 4-byte (32-bit) header, a data component in accordance with the header information received from the
    moving sub, each complete data packet sent to the application via an event OnReceive program.

Guess you like

Origin blog.csdn.net/weixin_41761608/article/details/115357183