socket_channel.cpp
Go to the documentation of this file.
1 
11 #include <opc/ua/socket_channel.h>
12 #include <opc/ua/errors.h>
13 
14 
15 #include <errno.h>
16 #include <iostream>
17 
18 #include <stdexcept>
19 #include <string.h>
20 
21 #include <sys/types.h>
22 
23 
24 #ifdef _WIN32
25 #include <WinSock2.h>
26 #else
27 #include <arpa/inet.h>
28 #include <netinet/tcp.h>
29 #include <sys/socket.h>
30 #include <unistd.h>
31 #endif
32 
33 
35  : Socket(sock)
36 {
37  int flag = 1;
38  setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int));
39 
40  if (Socket < 0)
41  {
42  THROW_ERROR(CannotCreateChannelOnInvalidSocket);
43  }
44 }
45 
47 {
48  Stop();
49 }
50 
52 {
53  close(Socket);
54 }
55 
56 std::size_t OpcUa::SocketChannel::Receive(char * data, std::size_t size)
57 {
58  int received = recv(Socket, data, size, MSG_WAITALL);
59 
60  if (received < 0)
61  {
62  THROW_OS_ERROR("Failed to receive data from host.");
63  }
64 
65  if (received == 0)
66  {
67  THROW_OS_ERROR("Connection was closed by host.");
68  }
69 
70  return (std::size_t)size;
71 }
72 
73 void OpcUa::SocketChannel::Send(const char * message, std::size_t size)
74 {
75  int sent = send(Socket, message, size, 0);
76 
77  if (sent != (int)size)
78  {
79  THROW_OS_ERROR("unable to send data to the host. ");
80  }
81 }
#define THROW_ERROR(data)
Definition: exception.h:207
virtual std::size_t Receive(char *data, std::size_t size)
Receive data.
SocketChannel(int sock)
Opc binary cnnection channel. GNU LGPL.
#define THROW_OS_ERROR(UserMsg)
Definition: exception.h:226
message
Definition: server.py:50
virtual void Send(const char *message, std::size_t size)


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