forked from grishka/libtgvoip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPacketReassembler.cpp
64 lines (50 loc) · 1.29 KB
/
PacketReassembler.cpp
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
//
// Created by Grishka on 19.03.2018.
//
#include "PacketReassembler.h"
#include "logging.h"
#include <assert.h>
using namespace tgvoip;
PacketReassembler::PacketReassembler(){
}
PacketReassembler::~PacketReassembler(){
}
void PacketReassembler::Reset(){
}
void PacketReassembler::AddFragment(Buffer pkt, unsigned int fragmentIndex, unsigned int fragmentCount, uint32_t pts){
//LOGD("add fragment: ts=%u, index=%u of %u", pts, fragmentIndex, fragmentCount);
if(fragmentCount==0 || fragmentCount==1){
callback(std::move(pkt), pts);
return;
}
if(pts!=currentTimestamp){
assert(fragmentCount<=255);
currentTimestamp=pts;
parts.clear();
parts.resize(fragmentCount);
currentPacketPartCount=fragmentCount;
}
if(fragmentIndex<currentPacketPartCount){
parts[fragmentIndex]=std::move(pkt);
}
if(parts.size()<currentPacketPartCount)
return;
bool anyAreEmpty=false;
for(Buffer& b:parts){
if(b.Length()==0){
anyAreEmpty=true;
break;
}
}
if(!anyAreEmpty){
//LOGI("part count %u", (unsigned int)parts.size());
BufferOutputStream out(10240);
for(Buffer& b:parts){
out.WriteBytes(b);
}
callback(Buffer(std::move(out)), currentTimestamp);
}
}
void PacketReassembler::SetCallback(std::function<void(Buffer packet, uint32_t pts)> callback){
this->callback=callback;
}