104 lines
2.4 KiB
C++
104 lines
2.4 KiB
C++
#pragma once
|
|
|
|
#include "ClientMsg.h"
|
|
#include "Messages.h"
|
|
#include "Messages/CustomMsg.h"
|
|
#include "ModelUrlMsg.h"
|
|
#include "NameMsg.h"
|
|
#include "NetworkIdMsg.h"
|
|
#include "ThingMsg.h"
|
|
|
|
#include <list>
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
#include <winsock2.h>
|
|
#elif defined(__unix__) || defined(__APPLE__)
|
|
#include <arpa/inet.h>
|
|
#include <netinet/in.h>
|
|
#include <sys/socket.h>
|
|
#include <unistd.h>
|
|
#elif defined(ARDUINO)
|
|
#include <WiFiUdp.h>
|
|
#endif
|
|
|
|
namespace Passer {
|
|
namespace Control {
|
|
|
|
/// @brief A participant is device which can communicate with other participants
|
|
class Participant {
|
|
public:
|
|
char buffer[1024];
|
|
long publishInterval = 3000; // 3 seconds
|
|
unsigned char networkId = 0;
|
|
|
|
const char *name = "Participant";
|
|
|
|
const char *ipAddress = "0.0.0.0";
|
|
int port = 0;
|
|
int localPort = 0;
|
|
|
|
#if defined(ARDUINO)
|
|
const char *remoteIpAddress = nullptr;
|
|
unsigned short remotePort = 0;
|
|
char *broadcastIpAddress = nullptr;
|
|
|
|
WiFiUDP udp;
|
|
#else
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
SOCKET sock;
|
|
#elif defined(__unix__) || defined(__APPLE__)
|
|
int sock;
|
|
#endif
|
|
sockaddr_in remote_addr;
|
|
sockaddr_in server_addr;
|
|
sockaddr_in broadcast_addr;
|
|
|
|
#endif
|
|
|
|
Participant();
|
|
Participant(int port);
|
|
Participant(const char *ipAddress, int port);
|
|
// Bad design, you cannot use constructor in global scope
|
|
// i.e.
|
|
// Participant p = Participant("127.0.0.1", 8000);
|
|
|
|
void begin();
|
|
bool connected = false;
|
|
|
|
#if defined(ARDUINO)
|
|
virtual void Update();
|
|
#endif
|
|
virtual void Update(unsigned long currentTimeMs);
|
|
|
|
void SendThingInfo(Thing *thing);
|
|
void PublishThingInfo(Thing *thing);
|
|
|
|
bool Send(IMessage *msg);
|
|
bool Publish(IMessage *msg);
|
|
|
|
void ReceiveData(unsigned char bufferSize, Participant *remoteParticipant);
|
|
|
|
protected:
|
|
std::list<Participant *> participants;
|
|
|
|
unsigned long nextPublishMe = 0;
|
|
|
|
void SetupUDP(int localPort, const char *remoteIpAddress, int remotePort);
|
|
|
|
Participant *GetParticipant(const char *ipAddress, int port);
|
|
Participant *AddParticipant(const char *ipAddress, int port);
|
|
|
|
void ReceiveUDP();
|
|
|
|
virtual void Process(Participant *sender, ClientMsg *msg);
|
|
virtual void Process(Participant *sender, NetworkIdMsg *msg);
|
|
virtual void Process(InvestigateMsg *msg);
|
|
virtual void Process(ThingMsg *msg);
|
|
virtual void Process(PoseMsg *msg);
|
|
virtual void Process(CustomMsg *msg);
|
|
};
|
|
|
|
} // namespace Control
|
|
} // namespace Passer
|
|
using namespace Passer::Control; |