mirror of
https://gitlab.com/obbart/universal_robots_ros_driver.git
synced 2026-04-10 01:50:46 +02:00
52 lines
974 B
C++
52 lines
974 B
C++
#include <cstring>
|
|
#include <netinet/tcp.h>
|
|
#include <unistd.h>
|
|
#include "ur_modern_driver/log.h"
|
|
#include "ur_modern_driver/ur/server.h"
|
|
|
|
URServer::URServer(int port)
|
|
: port_(port)
|
|
{
|
|
}
|
|
|
|
std::string URServer::getIP()
|
|
{
|
|
char buf[128];
|
|
int res = ::gethostname(buf, sizeof(buf));
|
|
return std::string(buf);
|
|
}
|
|
|
|
bool URServer::bind()
|
|
{
|
|
std::string empty;
|
|
bool res = TCPSocket::setup(empty, port_);
|
|
state_ = TCPSocket::getState();
|
|
|
|
if(!res)
|
|
return false;
|
|
|
|
if(::listen(getSocketFD(), 1) < 0)
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
bool URServer::accept()
|
|
{
|
|
if(state_ != SocketState::Connected || client_.getSocketFD() > 0)
|
|
return false;
|
|
|
|
struct sockaddr addr;
|
|
socklen_t addr_len;
|
|
int client_fd = ::accept(getSocketFD(), &addr, &addr_len);
|
|
|
|
if(client_fd <= 0)
|
|
return false;
|
|
|
|
return client_.setSocketFD(client_fd);
|
|
}
|
|
|
|
bool URServer::write(const uint8_t* buf, size_t buf_len, size_t &written)
|
|
{
|
|
return client_.write(buf, buf_len, written);
|
|
} |