#include "SH_multicast.h"
#include <pibroadcast.h>
#include <QDebug>

#define PACKET_SIGN ushort(0xFED1)


SHMulticast::SHMulticast(bool send_only): PIBroadcast(send_only) {
	createCryptKey("SHS_mcast");
	setMulticastGroup("232.13.3.15");
	setMulticastPort(13380);
	setBroadcastPort(13382);
	setLoopbackPort (13390);
	setLoopbackPortsCount(5);
}


SHMulticast::~SHMulticast() {
	stop();
	stopRead();
}


void SHMulticast::send(const PIByteArray & data, uint id) {
	if (data.isEmpty()) return;
	PIByteArray pckt;
	pckt << PACKET_SIGN << id;
	pckt.append(data);
	PIBroadcast::send(pckt);
}


void SHMulticast::received(PIByteArray data) {
	if (data.size_s() < 6) return;
	ushort sign = 0;
	uint id = 0;
	data >> sign >> id;
	if (sign != PACKET_SIGN) return;
	receivedSH(data, id);
	receiveEventSH(data, id);
}
