126 lines
3.6 KiB
C++
126 lines
3.6 KiB
C++
#include "Participant.h"
|
|
|
|
#define BUF_SIZE 1024
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
#include <winsock2.h>
|
|
#pragma comment(lib, "ws2_32.lib")
|
|
#elif defined(__unix__) || defined(__APPLE__)
|
|
#include <arpa/inet.h>
|
|
#include <netinet/in.h>
|
|
#include <sys/socket.h>
|
|
#include <unistd.h>
|
|
#endif
|
|
|
|
Passer::Control::Participant::Participant(const char *ipAddress, int port) {
|
|
sockaddr_in server_addr;
|
|
|
|
// Create a UDP socket
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
// Windows-specific Winsock initialization
|
|
WSADATA wsaData;
|
|
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
|
|
std::cerr << "WSAStartup failed" << std::endl;
|
|
return;
|
|
}
|
|
#endif
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
this->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
|
#elif defined(__unix__) || defined(__APPLE__)
|
|
sock = socket(AF_INET, SOCK_DGRAM, 0);
|
|
#endif
|
|
|
|
if (sock < 0) {
|
|
std::cerr << "Error creating socket" << std::endl;
|
|
return;
|
|
}
|
|
|
|
// Set up the server address
|
|
// memset(&server_addr, 0, sizeof(server_addr));
|
|
// server_addr.sin_family = AF_INET;
|
|
// server_addr.sin_port = htons(PORT);
|
|
// server_addr.sin_addr.s_addr = INADDR_ANY;
|
|
// Set up the server address structure
|
|
memset(&server_addr, 0, sizeof(server_addr));
|
|
server_addr.sin_family = AF_INET;
|
|
server_addr.sin_port = htons(port); // Port to send the packet to
|
|
server_addr.sin_addr.s_addr =
|
|
inet_addr(ipAddress); // Destination IP address (localhost)
|
|
|
|
// Bind the socket
|
|
if (bind(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
|
|
std::cerr << "Error binding socket" << std::endl;
|
|
return;
|
|
}
|
|
|
|
// std::cout << "Server is listening on port " << PORT << "..." << std::endl;
|
|
}
|
|
|
|
void Participant::Update(unsigned long currentTimeMs) {
|
|
// std::cout << "update\n";
|
|
if (currentTimeMs > this->nextPublishMe) {
|
|
std::cout << "publish\n";
|
|
this->Publish(ClientMsg(this->networkId));
|
|
this->nextPublishMe = currentTimeMs + this->publishInterval;
|
|
}
|
|
}
|
|
|
|
bool Participant::SendBuffer(unsigned char bufferSize) { return false; }
|
|
|
|
bool Participant::PublishBuffer(unsigned char bufferSize) { return false; }
|
|
|
|
bool Participant::Publish(IMessage msg) {
|
|
// Send the message to the specified address and port
|
|
int bufferSize = msg.Serialize(this->buffer);
|
|
if (bufferSize <= 0)
|
|
return true;
|
|
|
|
std::cout << "Publish to " << "\n";
|
|
int sent_bytes = sendto(sock, this->buffer, bufferSize, 0,
|
|
(struct sockaddr *)&server_addr, sizeof(server_addr));
|
|
if (sent_bytes == SOCKET_ERROR) {
|
|
std::cerr << "Error sending message" << std::endl;
|
|
closesocket(sock);
|
|
WSACleanup();
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void Participant::ReceiveData(unsigned char bufferSize) {
|
|
unsigned char msgId = this->buffer[0];
|
|
switch (msgId) {
|
|
case NetworkIdMsg::id: {
|
|
// NetworkIdMsg msg = NetworkIdMsg::Receive(this->buffer, bufferSize);
|
|
NetworkIdMsg msg = NetworkIdMsg(this->buffer);
|
|
ProcessNetworkIdMsg(msg);
|
|
} break;
|
|
case InvestigateMsg::id: {
|
|
InvestigateMsg msg = InvestigateMsg(this->buffer);
|
|
ProcessInvestigateMsg(msg);
|
|
} break;
|
|
case ThingMsg::id: {
|
|
ThingMsg msg = ThingMsg(this->buffer);
|
|
ProcessThingMsg(msg);
|
|
} break;
|
|
case PoseMsg::id: {
|
|
PoseMsg msg = PoseMsg(this->buffer);
|
|
ProcessPoseMsg(msg);
|
|
} break;
|
|
case CustomMsg::id: {
|
|
CustomMsg msg = CustomMsg(this->buffer);
|
|
ProcessCustomMsg(msg);
|
|
} break;
|
|
};
|
|
}
|
|
|
|
void Participant::ProcessNetworkIdMsg(NetworkIdMsg msg) {}
|
|
|
|
void Participant::ProcessInvestigateMsg(InvestigateMsg msg) {}
|
|
|
|
void Participant::ProcessThingMsg(ThingMsg msg) {}
|
|
|
|
void Passer::Control::Participant::ProcessPoseMsg(PoseMsg msg) {}
|
|
|
|
void Participant::ProcessCustomMsg(CustomMsg msg) {} |