z1_sdk/include/unitree_arm_sdk/message/udp.h

60 lines
1.5 KiB
C
Raw Normal View History

2022-11-11 18:17:07 +08:00
#ifndef _UDP_H
#define _UDP_H
2022-07-20 11:20:01 +08:00
#include <arpa/inet.h>
2022-11-11 18:17:07 +08:00
#include <termios.h>
2022-07-20 11:20:01 +08:00
#include <string>
#include <string.h>
2022-11-11 18:17:07 +08:00
#include <vector>
#include "utilities/timer.h"
2022-07-20 11:20:01 +08:00
2022-11-16 16:23:36 +08:00
namespace UNITREE_ARM {
2022-07-20 11:20:01 +08:00
enum class BlockYN{
YES,
NO
};
class IOPort{
public:
IOPort(BlockYN blockYN, size_t recvLength, size_t timeOutUs){
resetIO(blockYN, recvLength, timeOutUs);
}
virtual ~IOPort(){}
virtual size_t send(uint8_t *sendMsg, size_t sendLength) = 0;
virtual size_t recv(uint8_t *recvMsg, size_t recvLength) = 0;
virtual size_t recv(uint8_t *recvMsg) = 0;
void resetIO(BlockYN blockYN, size_t recvLength, size_t timeOutUs);
2022-09-13 19:52:44 +08:00
bool isDisConnect;
2022-07-20 11:20:01 +08:00
protected:
BlockYN _blockYN = BlockYN::NO;
size_t _recvLength;
timeval _timeout;
timeval _timeoutSaved;
2022-09-13 19:52:44 +08:00
uint16_t _isDisConnectCnt;
2022-07-20 11:20:01 +08:00
};
class UDPPort : public IOPort{
public:
UDPPort(std::string toIP, uint toPort, uint ownPort,
size_t recvLength = 0,
BlockYN blockYN = BlockYN::NO,
size_t timeOutUs = 20000);
~UDPPort();
size_t send(uint8_t *sendMsg, size_t sendMsgLength);
size_t recv(uint8_t *recvMsg, size_t recvLength);
size_t recv(uint8_t *recvMsg);
private:
size_t _recvBlock(uint8_t *recvMsg, size_t recvLength);
size_t _recvUnBlock(uint8_t *recvMsg, size_t recvLength);
sockaddr_in _ownAddr, _toAddr, _fromAddr;
socklen_t _sockaddrSize;
int _sockfd;
int _on = 1;
size_t _sentLength;
fd_set _rSet;
};
2022-11-16 16:23:36 +08:00
}
2022-11-11 18:17:07 +08:00
#endif