You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

udpcommu.h 1.5 kB

8 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #ifndef UDPCOMMU_H
  2. #define UDPCOMMU_H
  3. #include <string>
  4. #include <functional>
  5. #include <vector>
  6. using namespace std;
  7. #define MAX_RCV_SIZE 4096
  8. typedef function<void (const string& ip, vector<char> &data)> UdpRecvHandler;
  9. class UdpCommu
  10. {
  11. public:
  12. UdpCommu();
  13. public:
  14. /**
  15. * @brief bindTo 绑定到本地端口
  16. * @param port 端口号
  17. * @return 是否绑定成功
  18. */
  19. bool bindTo(int port);
  20. /**
  21. * @brief sentTo 往目标地址发送数据
  22. * @param ip 目标ip
  23. * @param port 目标端口
  24. * @param data 数据指针
  25. * @param size 数据大小
  26. * @return >=0发送成功的字节数,-1发送失败
  27. */
  28. int sentTo(const string &ip, int port, const void *data, int size);
  29. /**
  30. * @brief startAsyncRecv 开始异步接收数据
  31. * @param handler 处理函数
  32. * @return 是否启动成功
  33. */
  34. bool startAsyncRecv(UdpRecvHandler handler);
  35. /**
  36. * @brief close 关闭udp通信
  37. */
  38. void close();
  39. /**
  40. * @brief getBoundMac 获取udp通信绑定的网卡
  41. * @return 绑定的网卡地址
  42. */
  43. string getBoundMac();
  44. public:
  45. /**
  46. * @brief getErrMsg 获取最近一次错误的错误信息
  47. * @return 最近一次的错误信息
  48. */
  49. string getErrMsg();
  50. private:
  51. void recvThread();
  52. bool mAsyncMode=false;
  53. private:
  54. string mErrMsg="";
  55. int mSocket=-1;
  56. UdpRecvHandler mRecvHandler=nullptr;
  57. };
  58. #endif // UDPCOMMU_H

mac下的“飞秋”大多数只是飞鸽传书协议,而且未发现令人满意的开源项目,所以基于c++与qt实现了基础的飞秋协议。

Contributors (1)