-
Notifications
You must be signed in to change notification settings - Fork 6
/
Updater.cpp
107 lines (95 loc) · 3.21 KB
/
Updater.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "Updater.h"
Updater::Updater(Server* s)
{
server = s;
onReceiveUpdateEventConnection = EventController::getInstance()->connect_onServerEvent(L"update", boost::bind(&Updater::onReceiveUpdateEvent, this, _1));
onReceiveUpdatePartEventConnection = EventController::getInstance()->connect_onServerEvent(L"update-part", boost::bind(&Updater::onReceiveUpdatePartEvent, this, _1));
onReceiveUpdateFinishedEventConnection = EventController::getInstance()->connect_onServerEvent(L"update-finished", boost::bind(&Updater::onReceiveUpdateFinishedEvent, this, _1));
}
Updater::~Updater(void)
{
if(fileAllocated)
{
free(fileBuffer);
}
onReceiveUpdateEventConnection.disconnect();
onReceiveUpdatePartEventConnection.disconnect();
onReceiveUpdateFinishedEventConnection.disconnect();
}
void
Updater::onReceiveUpdateEvent(Element* pElement)
{
if(!fileAllocated)
{
vector<Attribute>::iterator it;
for(it = pElement->attributes.begin(); it != pElement->attributes.end(); it++)
{
if(it->name == L"file-size") {
fileSize = boost::lexical_cast<int>(it->value);
} else if(it->name == L"file-name") {
fileName = it->value;
}
}
fileBuffer = (char*)malloc(fileSize);
fileAllocated = true;
pElement->name = L"update-accept";
server->sendXMLElement(pElement);
} else {
printf("Updater: ERROR - Can only download one update at a time\n");
pElement->name = L"update-reject";
server->sendXMLElement(pElement);
}
}
void
Updater::onReceiveUpdatePartEvent(Element* pElement)
{
vector<Attribute>::iterator it;
int start = 0;
int end = 0;
int partSize = 0;
for(it = pElement->attributes.begin(); it != pElement->attributes.end(); it++)
{
if(it->name == L"file-part-start") {
start = boost::lexical_cast<int>(it->value);
} else if(it->name == L"file-part-end") {
end = boost::lexical_cast<int>(it->value);
}
}
if(fileAllocated)
{
partSize = end - start;
// TODO Check the checksum of the part is correct
decode_base64(pElement->data);
memcpy((void*)fileBuffer[start], pElement->data, partSize);
} else {
printf("Updater: ERROR - File not allocated: %08x\n", GetLastError());
pElement->name = L"update-error";
server->sendXMLElement(pElement);
}
}
void
Updater::onReceiveUpdateFinishedEvent(Element* pElement)
{
HANDLE hFile;
int offset = 0;
hFile = CreateFile(fileName.c_str(), // file to open
GENERIC_WRITE, // open for reading
FILE_SHARE_READ, // share for reading
NULL, // default security
CREATE_ALWAYS, // existing file only
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS, // normal file
NULL); // no attr. template
if(hFile == INVALID_HANDLE_VALUE)
{
printf("Updater: ERROR - Could not open file: %08x\n", GetLastError());
pElement->name = L"update-error";
server->sendXMLElement(pElement);
} else {
DWORD bytesWritten = 0;
WriteFile(hFile, fileBuffer, fileSize, &bytesWritten, NULL);
pElement->name = L"update-ok";
server->sendXMLElement(pElement);
fileAllocated = false;
free(fileBuffer);
}
}