47 lines
1.4 KiB
C++
47 lines
1.4 KiB
C++
#include "PoxisMQTT.h"
|
|
#if defined(__unix__) || defined(__APPLE__)
|
|
|
|
#include <arpa/inet.h>
|
|
#include <fcntl.h> // For fcntl
|
|
#include <netinet/in.h>
|
|
#include <sys/socket.h>
|
|
#include <unistd.h>
|
|
|
|
namespace RoboidControl {
|
|
|
|
RoboidControl::MQTTParticipant::MQTTParticipant(const char* remoteIpAddress,
|
|
int port)
|
|
: MQTTParticipantBase(remoteIpAddress, port) {}
|
|
|
|
void MQTTParticipant::SetupTCP() {
|
|
// Create a TCP socket
|
|
this->sock = socket(AF_INET, SOCK_STREAM, 0);
|
|
if (this->sock < 0) {
|
|
std::cerr << "Error creating socket" << std::endl;
|
|
return;
|
|
}
|
|
|
|
this->remote_addr.sin_family = AF_INET;
|
|
this->remote_addr.sin_port = htons(this->remoteSite->port);
|
|
inet_pton(AF_INET, this->remoteSite->ipAddress, &remote_addr.sin_addr);
|
|
|
|
int connect_result =
|
|
connect(this->sock, (struct sockaddr*)&remote_addr, sizeof(remote_addr));
|
|
if (connect_result < 0) { //} && errno != EINPROGRESS) {
|
|
std::cerr << "Error connecting to server" << std::endl;
|
|
close(this->sock);
|
|
}
|
|
|
|
std::cout << "TCP connection to " << this->remoteSite->ipAddress << ":" << this->remoteSite->port
|
|
<< "\n";
|
|
|
|
// Set the socket to non-blocking mode
|
|
int flags = fcntl(this->sock, F_GETFL, 0);
|
|
fcntl(this->sock, F_SETFL, flags | O_NONBLOCK);
|
|
}
|
|
void MQTTParticipant::SendTCP(int bufferSize) {
|
|
send(this->sock, this->buffer, bufferSize, 0);
|
|
}
|
|
|
|
} // namespace RoboidControl
|
|
#endif |