Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port #106 to ros2 #120

Merged
merged 2 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions CPPLINT.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
# { should almost always be at the end of the previous line
#
# Allow:
# bool SocketAPM::pollout(uint32_t timeout_ms)
# bool SocketUDP::pollin(uint32_t timeout_ms)
# {
#
# Instead of:
# bool SocketAPM::pollout(uint32_t timeout_ms) {
# bool SocketUDP::pollin(uint32_t timeout_ms) {
#
# 2. [-runtime/references]
# Is this a non-const reference? If so, make const or use a pointer
#
# Allow:
# void last_recv_address(const char *&ip_addr, uint16_t &port);
# void get_client_address(const char *&ip_addr, uint16_t &port);
#
# Instead of:
# void last_recv_address(const char *&ip_addr, uint16_t *port);
# void get_client_address(const char *&ip_addr, uint16_t *port);
#
# 3. [-whitespace/indent]
# private: should be indented +1 space
Expand Down Expand Up @@ -49,8 +49,12 @@
# 7. [-build/c++11], [+build/c++14]
#
# This is to allow headers not approved for C++11 such as <chrono> etc.
#
#
# 8. [-legal/copyright]
# Do not require a copyright header in each file
#

set noparent
filter=-whitespace/braces,-runtime/references,-whitespace/indent,-whitespace/blank_line,-whitespace/newline,-build/include_subdir,-build/c++11,+build/c++14
filter=-whitespace/braces,-runtime/references,-whitespace/indent,-whitespace/blank_line,-whitespace/newline,-build/include_subdir,-build/c++11,+build/c++14,-legal/copyright
linelength=80
root=include
28 changes: 17 additions & 11 deletions include/SocketUDP.hh
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,19 @@

#include <fcntl.h>
#include <unistd.h>

#ifdef _WIN32
#include <winsock2.h>
#include <Ws2tcpip.h>
#include <winsock2.h>
#include <Ws2tcpip.h>
#else
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <sys/select.h>

#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <sys/select.h>

#endif

/// \brief Simple UDP socket handling class.
Expand All @@ -51,7 +54,8 @@ public:
bool set_blocking(bool blocking);

/// \brief Send data to address and port.
ssize_t sendto(const void *buf, size_t size, const char *address, uint16_t port);
ssize_t
sendto(const void *buf, size_t size, const char *address, uint16_t port);

/// \brief Receive data.
ssize_t recv(void *pkt, size_t size, uint32_t timeout_ms);
Expand All @@ -61,7 +65,7 @@ public:

private:
/// \brief File descriptor.
struct sockaddr_in in_addr {};
struct sockaddr_in in_addr{};

/// \brief File descriptor.
int fd = -1;
Expand All @@ -70,6 +74,8 @@ private:
bool pollin(uint32_t timeout_ms);

/// \brief Make a sockaddr_in struct from address and port.
void make_sockaddr(const char *address, uint16_t port, struct sockaddr_in &sockaddr);
void make_sockaddr(const char *address, uint16_t port,
struct sockaddr_in &sockaddr);
};

#endif // SOCKETUDP_HH_
17 changes: 11 additions & 6 deletions src/SocketUDP.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ bool SocketUDP::bind(const char *address, uint16_t port) {
struct sockaddr_in server_addr{};
make_sockaddr(address, port, server_addr);

if (::bind(fd, reinterpret_cast<sockaddr *>(&server_addr), sizeof(server_addr)) != 0) {
if (::bind(fd, reinterpret_cast<sockaddr *>(&server_addr),
sizeof(server_addr)) != 0) {
perror("SocketUDP Bind failed");
#ifdef _WIN32
closesocket(fd);
Expand Down Expand Up @@ -90,11 +91,14 @@ bool SocketUDP::set_blocking(bool blocking) {
}


ssize_t SocketUDP::sendto(const void *buf, size_t size, const char *address, uint16_t port) {
ssize_t SocketUDP::sendto(const void *buf, size_t size, const char *address,
uint16_t port) {
struct sockaddr_in sockaddr_out{};
make_sockaddr(address, port, sockaddr_out);

return ::sendto(fd, buf, size, 0, reinterpret_cast<sockaddr *>(&sockaddr_out), sizeof(sockaddr_out));
return ::sendto(fd, buf, size, 0,
reinterpret_cast<sockaddr *>(&sockaddr_out),
sizeof(sockaddr_out));
}

/*
Expand All @@ -105,7 +109,8 @@ ssize_t SocketUDP::recv(void *buf, size_t size, uint32_t timeout_ms) {
return -1;
}
socklen_t len = sizeof(in_addr);
return ::recvfrom(fd, buf, size, MSG_DONTWAIT, reinterpret_cast<sockaddr *>(&in_addr), &len);
return ::recvfrom(fd, buf, size, MSG_DONTWAIT,
reinterpret_cast<sockaddr *>(&in_addr), &len);
}


Expand All @@ -131,8 +136,8 @@ bool SocketUDP::pollin(uint32_t timeout_ms) {
return true;
}

void SocketUDP::make_sockaddr(const char *address, uint16_t port, struct sockaddr_in &sockaddr)
{
void SocketUDP::make_sockaddr(const char *address, uint16_t port,
struct sockaddr_in &sockaddr) {
sockaddr = {};

sockaddr.sin_family = AF_INET;
Expand Down
Loading