#include "Participant.h" #define BUF_SIZE 1024 #if defined(_WIN32) || defined(_WIN64) #include #include #pragma comment(lib, "ws2_32.lib") #elif defined(__unix__) || defined(__APPLE__) #include #include #include #include #endif Passer::Control::Participant::Participant(const char *ipAddress, int port) { // 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__) this->sock = socket(AF_INET, SOCK_DGRAM, 0); #endif if (this->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((u_short)port); // Port to send the packet to if (inet_pton(AF_INET, "255.255.255.255", &server_addr.sin_addr) <= 0) { std::cerr << "Invalid address" << std::endl; closesocket(sock); WSACleanup(); return; } BOOL broadcast = TRUE; if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char *)&broadcast, sizeof(broadcast)) == SOCKET_ERROR) { std::cerr << "Setting socket option for broadcast failed" << std::endl; } } void Participant::Update(unsigned long currentTimeMs) { // std::cout << "update\n"; if (currentTimeMs > this->nextPublishMe) { ClientMsg msg = ClientMsg(this->networkId); this->Publish(msg); // Console.WriteLine($"{this.name} Sent ClientMsg {this.networkId}"); std::cout << this->name << " sent ClientMsg\n"; this->nextPublishMe = currentTimeMs + this->publishInterval; } } bool Participant::SendBuffer(unsigned char bufferSize) { return false; } bool Participant::PublishBuffer(unsigned char bufferSize) { return false; } #include bool Participant::Publish( ClientMsg msg) { // I want to use IMessage here, but then the serialize // calls the IMessage.Serialize... // Send the message to the specified address and port int bufferSize = msg.Serialize(this->buffer); std::cout << "buffer size " << bufferSize << "\n"; if (bufferSize <= 0) return true; // char ip_str[INET_ADDRSTRLEN]; // inet_ntop(AF_INET, &(server_addr.sin_addr), ip_str, INET_ADDRSTRLEN); // std::cout << "Publish to " << ip_str << ":" << ntohs(server_addr.sin_port) // << "\n"; int sent_bytes = sendto(sock, this->buffer, bufferSize, 0, (struct sockaddr *)&server_addr, sizeof(server_addr)); if (sent_bytes == SOCKET_ERROR) { int error_code = WSAGetLastError(); // Get the error code on Windows std::cerr << "sendto failed with error: " << error_code << 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) {}