binary_connection.cpp
Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 #include <opc/ua/client/remote_connection.h>
00012 #include <opc/ua/errors.h>
00013 #include <opc/ua/socket_channel.h>
00014 
00015 
00016 #include <errno.h>
00017 #include <iostream>
00018 #include <stdexcept>
00019 #include <string.h>
00020 #include <sys/types.h>
00021 
00022 #ifdef _WIN32
00023   #include <WinSock2.h>
00024 #else
00025   #include <arpa/inet.h>
00026   #include <netdb.h>
00027   #include <netinet/in.h>
00028   #include <sys/socket.h>
00029 #endif
00030 
00031 namespace
00032 {
00033 
00034   unsigned long GetIPAddress(const std::string& hostName)
00035   {
00036     // TODO Use getaddrinfo
00037     hostent* host = gethostbyname(hostName.c_str());
00038     if (!host)
00039     {
00040       THROW_OS_ERROR("Unable to to resolve host '" + hostName + "'.");
00041     }
00042     return *(unsigned long*)host->h_addr_list[0];
00043   }
00044 
00045   int ConnectToRemoteHost(const std::string& host, unsigned short port)
00046   {
00047     int sock = socket(AF_INET, SOCK_STREAM, 0);
00048     if (sock < 0)
00049     {
00050       THROW_OS_ERROR("Unable to create socket for connecting to the host '" + host + ".");
00051     }
00052 
00053     sockaddr_in addr = {0};
00054     addr.sin_family = AF_INET;
00055     addr.sin_port = htons(port);
00056     addr.sin_addr.s_addr = GetIPAddress(host);
00057     
00058     int error = connect(sock, (sockaddr*)& addr, sizeof(addr));
00059     if (error < 0)
00060     {
00061       THROW_OS_ERROR(std::string("Unable connect to host '") + host + std::string("'. "));
00062     }
00063     return sock;
00064   }
00065 
00066   class BinaryConnection : public OpcUa::RemoteConnection
00067   {
00068   public:
00069     BinaryConnection(int sock, const std::string& host, unsigned short port)
00070       : HostName(host)
00071       , Port(port)
00072       , Channel(sock)
00073     {
00074     }
00075 
00076     virtual ~BinaryConnection()
00077     {
00078     }
00079 
00080     virtual std::size_t Receive(char* data, std::size_t size)
00081     {
00082       return Channel.Receive(data, size);
00083     }
00084 
00085     virtual void Send(const char* message, std::size_t size)
00086     {
00087       return Channel.Send(message, size);
00088     }
00089 
00090 
00091     virtual void Stop()
00092     {
00093       Channel.Stop();
00094     }
00095 
00096     virtual std::string GetHost() const
00097     {
00098       return HostName;
00099     }
00100 
00101     virtual unsigned GetPort() const
00102     {
00103       return Port;
00104     }
00105 
00106   private:
00107     const std::string HostName;
00108     const unsigned Port;
00109     OpcUa::SocketChannel Channel;
00110   };
00111 
00112 }
00113 
00114 std::unique_ptr<OpcUa::RemoteConnection> OpcUa::Connect(const std::string& host, unsigned port)
00115 {
00116   const int sock = ConnectToRemoteHost(host, port);
00117   return std::unique_ptr<RemoteConnection>(new BinaryConnection(sock, host, port));
00118 }
00119 


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Sat Jun 8 2019 18:24:39