47 UdpServer::UdpServer()
49 this->setConnected(
false);
52 UdpServer::~UdpServer()
58 bool UdpServer::init(
int port_num)
60 int rc = this->SOCKET_FAIL;
62 SOCKLEN_T addrSize = 0;
69 rc = SOCKET(AF_INET, SOCK_DGRAM, 0);
70 if (this->SOCKET_FAIL != rc)
72 this->setSockHandle(rc);
74 LOG_DEBUG(
"Socket handle: %d", this->getSockHandle());
77 memset(&this->sockaddr_, 0,
sizeof(this->sockaddr_));
78 this->sockaddr_.sin_family = AF_INET;
79 this->sockaddr_.sin_addr.s_addr = INADDR_ANY;
80 this->sockaddr_.sin_port = HTONS(port_num);
85 addrSize =
sizeof(this->sockaddr_);
86 rc = BIND(this->getSockHandle(), (sockaddr *)&(this->sockaddr_), addrSize);
88 if (this->SOCKET_FAIL != rc)
91 LOG_INFO(
"Server socket successfully initialized");
95 LOG_ERROR(
"Failed to bind socket, rc: %d", rc);
96 CLOSE(this->getSockHandle());
102 LOG_ERROR(
"Failed to create socket, rc: %d", rc);
109 bool UdpServer::makeConnect()
112 char sendHS = this->CONNECT_HANDSHAKE;
115 const int timeout = 1000;
118 send.
load((
void*)&sendHS,
sizeof(sendHS));
120 if (!this->isConnected())
122 this->setConnected(
false);
130 if (this->isReadyReceive(timeout))
132 bytesRcvd = this->rawReceiveBytes(this->buffer_, 0);
136 LOG_DEBUG(
"UDP server received %d bytes while waiting for handshake", bytesRcvd);
137 recv.
init(&this->buffer_[0], bytesRcvd);
138 recv.
unload((
void*)&recvHS,
sizeof(recvHS));
143 while(recvHS != sendHS);
148 std::vector<char> localBuffer(sendLen);
149 send.
unload(localBuffer.data(), sendLen);
152 this->rawSendBytes(localBuffer.data(), sendLen);
153 this->setConnected(
true);
159 LOG_WARN(
"Tried to connect when socket already in connected state");
void init()
Initializes or Reinitializes an empty buffer.
#define LOG_WARN(format,...)
bool load(industrial::shared_types::shared_bool value)
loads a boolean into the byte array
#define LOG_ERROR(format,...)
#define LOG_INFO(format,...)
The byte array wraps a dynamic array of bytes (i.e. char).
#define LOG_DEBUG(format,...)
unsigned int getBufferSize()
gets current buffer size
bool unload(industrial::shared_types::shared_bool &value)
unloads a boolean value from the byte array