binary_connection.cpp
Go to the documentation of this file.
1 
12 #include <opc/ua/errors.h>
13 #include <opc/ua/socket_channel.h>
14 #include <opc/ua/protocol/utils.h>
15 
16 
17 #include <errno.h>
18 #include <iostream>
19 #include <stdexcept>
20 #include <string.h>
21 #include <sys/types.h>
22 
23 #ifdef _WIN32
24 #include <WinSock2.h>
25 #else
26 #include <arpa/inet.h>
27 #include <netdb.h>
28 #include <netinet/in.h>
29 #include <sys/socket.h>
30 #endif
31 
32 namespace
33 {
34 
35 unsigned long GetIPAddress(const std::string & hostName)
36 {
37  // TODO Use getaddrinfo
38  hostent * host = gethostbyname(hostName.c_str());
39 
40  if (!host)
41  {
42  THROW_OS_ERROR("Unable to to resolve host '" + hostName + "'.");
43  }
44 
45  return *(unsigned long *)host->h_addr_list[0];
46 }
47 
48 int ConnectToRemoteHost(const std::string & host, unsigned short port)
49 {
50  int sock = socket(AF_INET, SOCK_STREAM, 0);
51 
52  if (sock < 0)
53  {
54  THROW_OS_ERROR("Unable to create socket for connecting to the host '" + host + ".");
55  }
56 
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);
61 
62  int error = connect(sock, (sockaddr *)& addr, sizeof(addr));
63 
64  if (error < 0)
65  {
66 #ifdef _WIN32
67  closesocket(sock);
68 #else
69  close(sock);
70 #endif
71  THROW_OS_ERROR(std::string("Unable connect to host '") + host + std::string("'. "));
72  }
73 
74  return sock;
75 }
76 
77 class BinaryConnection : public OpcUa::RemoteConnection
78 {
79 public:
80  BinaryConnection(int sock, const std::string & host, unsigned short port, const Common::Logger::SharedPtr & logger)
81  : HostName(host)
82  , Port(port)
83  , Channel(sock)
84  , Logger(logger)
85  {
86  }
87 
88  virtual ~BinaryConnection()
89  {
90  }
91 
92  virtual std::size_t Receive(char * data, std::size_t size)
93  {
94  return Channel.Receive(data, size);
95  }
96 
97  virtual void Send(const char * message, std::size_t size)
98  {
99  LOG_TRACE(Logger, "binary_connection | send: {}", OpcUa::ToHexDump(message, size));
100  return Channel.Send(message, size);
101  }
102 
103 
104  virtual void Stop()
105  {
106  Channel.Stop();
107  }
108 
109  virtual std::string GetHost() const
110  {
111  return HostName;
112  }
113 
114  virtual unsigned GetPort() const
115  {
116  return Port;
117  }
118 
119 private:
120  const std::string HostName;
121  const unsigned Port;
122  OpcUa::SocketChannel Channel;
123  Common::Logger::SharedPtr Logger;
124 };
125 
126 }
127 
128 std::unique_ptr<OpcUa::RemoteConnection> OpcUa::Connect(const std::string & host, unsigned port, const Common::Logger::SharedPtr & logger)
129 {
130  const int sock = ConnectToRemoteHost(host, port);
131  return std::unique_ptr<RemoteConnection>(new BinaryConnection(sock, host, port, logger));
132 }
133 
#define LOG_TRACE(__logger__,...)
Definition: common/logger.h:23
#define THROW_OS_ERROR(UserMsg)
Definition: exception.h:226
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.
message
Definition: server.py:50
virtual void Stop()=0
std::string ToHexDump(const char *buf, std::size_t size)
Definition: utils.h:29
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)


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Tue Jan 19 2021 03:06:03