-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoodbyePacket.cpp
More file actions
66 lines (52 loc) · 2.31 KB
/
GoodbyePacket.cpp
File metadata and controls
66 lines (52 loc) · 2.31 KB
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
/*This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
If a copy of the MPL was not distributed with this file, You can obtain one at
http://mozilla.org/MPL/2.0/.
The Original Code is the Parlo library.
The Initial Developer of the Original Code is
Mats 'Afr0' Vederhus. All Rights Reserved.
Contributor(s): ______________________________________.
*/
#include "pch.h"
#include "GoodbyePacket.h"
#include <cstring>
GoodbyePacket::GoodbyePacket(int timeoutSeconds)
: timeout(timeoutSeconds),
sentTime(std::chrono::system_clock::now())
{
}
/// <summary>
/// Serializes this class to a byte array.
/// </summary>
/// <returns>A byte array representation of this class.</returns>
std::vector<uint8_t> GoodbyePacket::toByteArray() const
{
std::vector<uint8_t> bytes;
//Serialize timeout (in seconds)
auto timeoutSec = timeout.count();
bytes.insert(bytes.end(), reinterpret_cast<uint8_t*>(&timeoutSec), reinterpret_cast<uint8_t*>(&timeoutSec) + sizeof(timeoutSec));
//Serialize sentTime (as time since epoch in seconds)
auto sentTimeSec = std::chrono::duration_cast<std::chrono::seconds>(sentTime.time_since_epoch()).count();
bytes.insert(bytes.end(), reinterpret_cast<uint8_t*>(&sentTimeSec), reinterpret_cast<uint8_t*>(&sentTimeSec) + sizeof(sentTimeSec));
return bytes;
}
/// <summary>
/// Deserializes this class from a byte array.
/// </summary>
/// <returns>A GoodbyePacket instance.</returns>
GoodbyePacket GoodbyePacket::fromByteArray(const std::vector<uint8_t>& arrBytes)
{
if (arrBytes.size() < sizeof(std::chrono::seconds::rep) + sizeof(std::chrono::system_clock::time_point::duration::rep))
throw std::runtime_error("Byte array too small for GoodbyePacket deserialization.");
GoodbyePacket packet;
std::size_t offset = 0;
//Deserialize timeout
std::chrono::seconds::rep timeoutSec;
std::memcpy(&timeoutSec, arrBytes.data() + offset, sizeof(timeoutSec));
offset += sizeof(timeoutSec);
packet.timeout = std::chrono::seconds(timeoutSec);
//Deserialize sentTime
std::chrono::system_clock::time_point::duration::rep sentTimeSec;
std::memcpy(&sentTimeSec, arrBytes.data() + offset, sizeof(sentTimeSec));
packet.sentTime = std::chrono::system_clock::time_point(std::chrono::seconds(sentTimeSec));
return packet;
}