summaryrefslogtreecommitdiff
path: root/p2pvr/lib/tunerSendSi.cpp
blob: e1f46372a906d29e2e6ad9b74835345b026a8f41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <pch.hpp>
#include "tunerSendSi.h"
#include <logger.h>
#include <boost/crc.hpp>
#include "siParsers/table.h"

SendSi::SendSi(const P2PVR::RawDataClientPrx & c) :
	Tuner::IDataSender(c->ice_collocationOptimized(false))
{
}

SendSi::~SendSi()
{
}

void
SendSi::NewData(const P2PVR::Data & buf)
{
	if (!IsValidSection(buf)) {
		return;
	}
	_packetsSent += 1;
	asyncs.insert(client->begin_NewData(buf));
}

bool
SendSi::IsFinished()
{
	try {
		for (auto c = asyncs.begin(); c != asyncs.end(); ) {
			if ((*c)->isCompleted()) {
				if (client->end_NewData(*c)) {
					return true;
				}
				c = asyncs.erase(c);
			}
			else {
				c++;
			}
		}
		return false;
	}
	catch (const std::exception & ex) {
		Logger()->messagebf(LOG_DEBUG, "%s: Client transmit error (%s)", __PRETTY_FUNCTION__, ex.what());
		return true;
	}
}

bool
SendSi::IsValidSection(const P2PVR::Data & buf)
{
	auto n = buf.size();
	if (n < sizeof(SiTableHeader)) {
		Logger()->messagebf(LOG_WARNING, "Received data too small to be an SI table.");
		return false;
	}
	auto * tab = (const SiTableHeader *)(&buf.front());
	size_t l = sizeof(SiTableHeaderBase) + HILO(tab->section_length);
	if (n < l) {
		Logger()->messagebf(LOG_WARNING, "Received data shorter than its defined length.");
		return false;
	}
	if (n > l) {
		Logger()->messagebf(LOG_WARNING, "Received data longer than its defined length.");
		return false;
	}
	if (!crc32(buf)) {
		Logger()->messagebf(LOG_WARNING, "Received data is corrupted (crc32 failed).");
		return false;
	}
	return true;
}

bool
SendSi::crc32(const P2PVR::Data & buf)
{
	boost::crc_optimal<32, 0x0, 0xFFFFFFFF, 0x0, true, false> crc;
	crc.process_bytes(&buf.front(), buf.size());
	return crc.checksum() == 0;
}