45 namespace simple_socket
50 int rc = this->SOCKET_FAIL;
53 if (this->isConnected())
62 std::vector<char> localBuffer;
63 buffer.
copyTo(localBuffer);
64 rc = rawSendBytes(&localBuffer[0], localBuffer.size());
65 if (this->SOCKET_FAIL != rc)
72 logSocketError(
"Socket sendBytes failed", rc, errno);
78 LOG_ERROR(
"Buffer size: %u, is greater than max socket size: %u", buffer.
getBufferSize(), this->MAX_BUFFER_SIZE);
86 LOG_WARN(
"Not connected, bytes not sent");
91 this->setConnected(
false);
100 int rc = this->SOCKET_FAIL;
110 memset(&this->buffer_, 0,
sizeof(this->buffer_));
116 LOG_WARN(
"Socket buffer max size: %u, is larger than byte array buffer: %u",
119 if (this->isConnected())
122 while (remainBytes > 0 && (timeout_ms < 0 || remainTimeMs > 0))
127 if (this->rawPoll(this->SOCKET_POLL_TO, ready, error))
131 rc = rawReceiveBytes(this->buffer_, remainBytes);
132 if (this->SOCKET_FAIL == rc)
134 this->logSocketError(
"Socket received failed", rc, errno);
141 LOG_WARN(
"Recieved zero bytes: %u", rc);
148 remainBytes = remainBytes - rc;
149 remainTimeMs = timeout_ms;
150 LOG_COMM(
"Byte array receive, bytes read: %u, bytes reqd: %u, bytes left: %u",
151 rc, num_bytes, remainBytes);
152 buffer.
load(&this->buffer_, rc);
153 if (remainBytes <= 0) {
160 LOG_ERROR(
"Socket poll returned an error");
166 LOG_ERROR(
"Uknown error from socket poll");
173 remainTimeMs = remainTimeMs - this->SOCKET_POLL_TO;
174 LOG_COMM(
"Socket poll timeout, trying again");
180 LOG_WARN(
"Not connected, bytes not received");
185 if (!rtn && (timeout_ms < 0 || remainTimeMs > 0))
187 this->setConnected(
false);
Contains platform specific type definitions that guarantee the size of primitive data types...
void init()
Initializes or Reinitializes an empty buffer.
#define LOG_WARN(format,...)
#define LOG_COMM(format,...)
bool load(industrial::shared_types::shared_bool value)
loads a boolean into the byte array
#define LOG_ERROR(format,...)
The byte array wraps a dynamic array of bytes (i.e. char).
unsigned int getMaxBufferSize()
gets current buffer size
void copyTo(std::vector< char > &out)
Copy to std::vector, for raw-ptr access.
unsigned int getBufferSize()
gets current buffer size