32#include <sys/socket.h>
62 AINFO <<
"Opening UDP socket: port " << uint16_t(port);
64 sockfd_ = socket(AF_INET, SOCK_DGRAM, 0);
67 AERROR <<
"Init socket failed, UDP port is " << port;
72 memset(&my_addr, 0,
sizeof(my_addr));
73 my_addr.sin_family = AF_INET;
74 my_addr.sin_port = htons(uint16_t(port));
75 my_addr.sin_addr.s_addr = INADDR_ANY;
78 if (bind(sockfd_,
reinterpret_cast<sockaddr *
>(&my_addr),
sizeof(sockaddr)) ==
80 AERROR <<
"Socket bind failed! Port " << port_;
84 if (fcntl(sockfd_, F_SETFL, O_NONBLOCK | FASYNC) < 0) {
85 AERROR <<
"non-block! Port " << port_;
89 AINFO <<
"Velodyne socket fd is " << sockfd_ <<
", port " << port_;
98 return SOCKET_TIMEOUT;
102 uint8_t bytes[FIRING_DATA_PACKET_SIZE];
104 recvfrom(sockfd_, bytes, FIRING_DATA_PACKET_SIZE, 0,
nullptr,
nullptr);
107 if (errno != EWOULDBLOCK) {
108 AERROR <<
"recvfail from port " << port_;
113 if ((
size_t)nbytes == FIRING_DATA_PACKET_SIZE) {
115 pkt->set_data(bytes, FIRING_DATA_PACKET_SIZE);
119 AERROR <<
"Incomplete Velodyne rising data packet read: " << nbytes
120 <<
" bytes from port " << port_;
136 uint8_t bytes[POSITIONING_DATA_PACKET_SIZE];
137 ssize_t nbytes = recvfrom(sockfd_, bytes, POSITIONING_DATA_PACKET_SIZE, 0,
141 if (errno != EWOULDBLOCK) {
142 AERROR <<
"recvfail from port " << port_;
147 if ((
size_t)nbytes == POSITIONING_DATA_PACKET_SIZE) {
156 AINFO <<
"incomplete Velodyne packet read: " << nbytes
157 <<
" bytes from port " << port_;
163bool SocketInput::input_available(
int timeout) {
164 struct pollfd fds[1];
166 fds[0].events = POLLIN;
189 if (errno != EINTR) {
190 AWARN <<
"Velodyne port " << port_
191 <<
"poll() error: " << strerror(errno);
197 AWARN <<
"Velodyne port " << port_ <<
" poll() timeout";
201 if ((fds[0].revents & POLLERR) || (fds[0].revents & POLLHUP) ||
202 (fds[0].revents & POLLNVAL)) {
203 AERROR <<
"Velodyne port " << port_ <<
"poll() reports Velodyne error";
206 }
while ((fds[0].revents & POLLIN) == 0);
Cyber has builtin time type Time.
static Time Now()
get the current time.
double ToSecond() const
convert time to second.
std::shared_ptr< NMEATime > NMEATimePtr