21 #include <sys/types.h> 26 #include <arpa/inet.h> 28 #include <netinet/in.h> 29 #include <sys/socket.h> 35 unsigned long GetIPAddress(
const std::string & hostName)
38 hostent * host = gethostbyname(hostName.c_str());
45 return *(
unsigned long *)host->h_addr_list[0];
48 int ConnectToRemoteHost(
const std::string & host,
unsigned short port)
50 int sock = socket(AF_INET, SOCK_STREAM, 0);
54 THROW_OS_ERROR(
"Unable to create socket for connecting to the host '" + host +
".");
57 sockaddr_in addr = {0};
58 addr.sin_family = AF_INET;
59 addr.sin_port = htons(port);
60 addr.sin_addr.s_addr = GetIPAddress(host);
62 int error =
connect(sock, (sockaddr *)& addr,
sizeof(addr));
80 BinaryConnection(
int sock,
const std::string & host,
unsigned short port,
const Common::Logger::SharedPtr & logger)
88 virtual ~BinaryConnection()
92 virtual std::size_t
Receive(
char * data, std::size_t size)
94 return Channel.Receive(data, size);
97 virtual void Send(
const char *
message, std::size_t size)
100 return Channel.Send(message, size);
114 virtual unsigned GetPort()
const 123 Common::Logger::SharedPtr Logger;
128 std::unique_ptr<OpcUa::RemoteConnection>
OpcUa::Connect(
const std::string & host,
unsigned port,
const Common::Logger::SharedPtr & logger)
130 const int sock = ConnectToRemoteHost(host, port);
131 return std::unique_ptr<RemoteConnection>(
new BinaryConnection(sock, host, port, logger));
#define LOG_TRACE(__logger__,...)
#define THROW_OS_ERROR(UserMsg)
bool connect(ros_opcua_srvs::Connect::Request &req, ros_opcua_srvs::Connect::Response &res)
virtual unsigned GetPort() const =0
virtual std::size_t Receive(char *data, std::size_t size)=0
Receive data.
std::string ToHexDump(const char *buf, std::size_t size)
virtual void Send(const char *message, std::size_t size)=0
virtual std::string GetHost() const =0
std::unique_ptr< RemoteConnection > Connect(const std::string &host, unsigned port, const Common::Logger::SharedPtr &logger)