• UDP打洞和心跳包设计


    一、设备终端
    class DeviceClient {
    int deviceID;
    int IP;
    int port;
    char connectID[16];
    time_t lastTime;
    struct event timeoutEv;//超时器
    };
    typedef list<DeviceClient *> deviceList;

    二、移动控制终端
    class UserClient {
    int userID;
    int IP;
    int port;
    char connectID[16];
    time_t lastTime;//接收的时候更新,并开启超时器
    struct event timeoutEv;//超时器:如果超时,认为客户端掉线
    };

    typedef list<UserClient *>userList;

    三、UDP心跳包设计(服务端)
    http://blog.csdn.net/liuguanghui1988/article/details/7090531
    3.1 UDP包接收线程
    1)recvfrom
    2)find connectID 并看看是否是心跳包
    3)如果是心跳包,查找connectID对应的客户端,如果客户端存在,则更新lastTime,如果客户端不存在,则新建客户端并更新lastTime;

    3.2 UDP包超时器
    1)时间的定义(连续三次收不到心跳包,即超时时间为客户端心跳包时间的3倍)
    2)超时事件到达,则认为客户端掉线,删除DeviceClient或UserClient

    四、UDP心跳包设计(客户端)
    每隔30秒发送一次心跳包


    五、打洞设计
    http://www.cppblog.com/Lee7/archive/2008/01/25/41850.html
    打洞由移动控制终端或者设备终端发起。下面以移动控制终端发起为例,详细说明一下打洞的流程
    为了方便描述,定义移动控制终端为UserClient,设备终端为DeviceClient,服务端为Server
    流程如下:
    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

  • 相关阅读:
    ThreadPoolExecutor
    HashMap
    ReentrantReadWriteLock
    AbstractQueuedSynchronizer
    KAFKA:如何做到1秒发布百万级条消息
    Kafka生产者性能优化之吞吐量VS延迟
    Hadoop YARN:调度性能优化实践(转)
    Windows下安装MySQL压缩zip包
    Android 8 蓝牙打开过程
    msm audio platform 驱动代码跟踪
  • 原文地址:https://www.cnblogs.com/wiessharling/p/4821199.html
Copyright © 2020-2023  润新知