48 UdpClient::UdpClient()
52 UdpClient::~UdpClient()
56 bool UdpClient::init(
char *buff,
int port_num)
69 rc = SOCKET(AF_INET, SOCK_DGRAM, 0);
70 if (this->SOCKET_FAIL != rc)
72 this->setSockHandle(rc);
75 memset(&this->sockaddr_, 0,
sizeof(this->sockaddr_));
76 this->sockaddr_.sin_family = AF_INET;
79 hints.ai_family = AF_INET;
80 hints.ai_socktype = SOCK_DGRAM;
82 hints.ai_protocol = 0;
83 hints.ai_canonname = NULL;
86 if (0 == GETADDRINFO(buff, NULL, &hints, &result))
88 this->sockaddr_ = *((sockaddr_in *)result->ai_addr);
92 this->sockaddr_.sin_addr.s_addr = INET_ADDR(buff);
94 this->sockaddr_.sin_port = HTONS(port_num);
101 LOG_ERROR(
"Failed to create socket, rc: %d", rc);
108 bool UdpClient::makeConnect()
111 char sendHS = this->CONNECT_HANDSHAKE;
114 const int timeout = 1000;
117 if (!this->isConnected())
119 this->setConnected(
false);
120 send.
load((
void*)&sendHS,
sizeof(sendHS));
125 std::vector<char> localBuffer(sendLen);
126 send.
unload(localBuffer.data(), sendLen);
132 LOG_DEBUG(
"UDP client sending handshake");
133 this->rawSendBytes(localBuffer.data(), sendLen);
134 if (this->isReadyReceive(timeout))
136 bytesRcvd = this->rawReceiveBytes(this->buffer_, 0);
137 LOG_DEBUG(
"UDP client received possible handshake");
138 recv.
init(&this->buffer_[0], bytesRcvd);
139 recv.
unload((
void*)&recvHS,
sizeof(recvHS));
142 while(recvHS != sendHS);
145 this->setConnected(
true);
151 LOG_WARN(
"Tried to connect when socket already in connected state");