File size: 5,623 Bytes
7e9dc27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
-- This file is part of broadcaster library
-- Licensed under MIT License
-- Copyright (c) 2019 Ranx
-- https://github.com/r4nx/broadcaster
-- Version 0.2.0

local packet = {}

local utils = require 'broadcaster.utils'

local logger = require 'log'
local inspect = require 'inspect'

packet.PACKETS_ID = {
    START_TRANSFER = 1,
    STOP_TRANSFER = 2,
    DATA = 3,
    HANDLER_ID = 4
}

packet.StartTransferPacket = {}
packet.StartTransferPacket.__index = packet.StartTransferPacket

setmetatable(packet.StartTransferPacket, {
    __call = function(cls, ...)
        local self = setmetatable({}, cls)
        self:_init(...)
        return self
    end
})

-- Args:
--    sessionId <number> (decimal)
function packet.StartTransferPacket:_init(sessionId)
    self.sessionId = sessionId
end

function packet.StartTransferPacket:pack()
    local packetId = utils.decToBin(packet.PACKETS_ID.START_TRANSFER, 3)
    local sessionId = utils.decToBin(self.sessionId, 4)
    return utils.padBinary(utils.tablesConcat(packetId, sessionId), -16)
end

function packet.StartTransferPacket.unpack(bin)
    local packetId = utils.binToDec({unpack(bin, 1, 3)})
    local sessionId = utils.binToDec({unpack(bin, 4, 7)})

    return packet.StartTransferPacket(sessionId)
end



packet.StopTransferPacket = {}
packet.StopTransferPacket.__index = packet.StopTransferPacket

setmetatable(packet.StopTransferPacket, {
    __call = function(cls, ...)
        local self = setmetatable({}, cls)
        self:_init(...)
        return self
    end
})

-- Args:
--    sessionId <number> (decimal)
function packet.StopTransferPacket:_init(sessionId)
    self.sessionId = sessionId
end

function packet.StopTransferPacket:pack()
    local packetId = utils.decToBin(packet.PACKETS_ID.STOP_TRANSFER, 3)
    local sessionId = utils.decToBin(self.sessionId, 4)
    return utils.padBinary(utils.tablesConcat(packetId, sessionId), -16)
end

function packet.StopTransferPacket.unpack(bin)
    local packetId = utils.binToDec({unpack(bin, 1, 3)})
    local sessionId = utils.binToDec({unpack(bin, 4, 7)})

    return packet.StopTransferPacket(sessionId)
end



packet.DataPacket = {}
packet.DataPacket.__index = packet.DataPacket

setmetatable(packet.DataPacket, {
    __call = function(cls, ...)
        local self = setmetatable({}, cls)
        self:_init(...)
        return self
    end
})

-- Args:
--    data <number> - data to transfer (1 byte in decimal number system)
--    sessionId <number> (decimal)
function packet.DataPacket:_init(data, sessionId)
    if select(2, math.frexp(data)) > 8 then
        error('data is too large: only 1 byte allowed')
    end
    self.data = data
    self.sessionId = sessionId
end

function packet.DataPacket:pack()
    local packetId = utils.decToBin(packet.PACKETS_ID.DATA, 3)
    local sessionId = utils.decToBin(self.sessionId, 4)
    local parityBit = {utils.getParity(self.data) and 1 or 0}
    local data = utils.decToBin(self.data, 8)

    return utils.padBinary(utils.tablesConcat(packetId, sessionId, parityBit, data), -16)
end

function packet.DataPacket.unpack(bin)
    local packetId = utils.binToDec({unpack(bin, 1, 3)})
    logger.debug('data packet > packet id: ' .. packetId)
    local sessionId = utils.binToDec({unpack(bin, 4, 7)})
    logger.debug('data packet > session id: ' .. sessionId)
    local parityBit = bin[8]
    logger.debug('data packet > parity bit:' .. parityBit)
    local data = utils.binToDec({unpack(bin, 9, 16)})
    logger.debug('data packet > data: ' .. inspect(data))

    if (utils.getParity(data) and 1 or 0) ~= parityBit then
        logger.warn('got parity bit: ' .. parityBit)
        logger.warn('actual parity bit: ' .. (utils.getParity(data) and 1 or 0))
        error('corrupted data packet: parity bits do not match')
    end

    return packet.DataPacket(data, sessionId)
end



packet.HandlerIdPacket = {}
packet.HandlerIdPacket.__index = packet.HandlerIdPacket

setmetatable(packet.HandlerIdPacket, {
    __call = function(cls, ...)
        local self = setmetatable({}, cls)
        self:_init(...)
        return self
    end
})

-- Args:
--    handlerId <number> - handler id, decimal, 1 byte
--    sessionId <number> (decimal)
function packet.HandlerIdPacket:_init(handlerId, sessionId)
    if select(2, math.frexp(handlerId)) > 8 then
        error('handlerId is too large: only 1 byte allowed')
    end
    self.handlerId = handlerId
    self.sessionId = sessionId
end

function packet.HandlerIdPacket:pack()
    local packetId = utils.decToBin(packet.PACKETS_ID.HANDLER_ID, 3)
    local sessionId = utils.decToBin(self.sessionId, 4)
    local parityBit = {utils.getParity(self.handlerId) and 1 or 0}
    local handlerId = utils.decToBin(self.handlerId, 8)

    return utils.padBinary(utils.tablesConcat(packetId, sessionId, parityBit, handlerId), -16)
end

function packet.HandlerIdPacket.unpack(bin)
    local packetId = utils.binToDec({unpack(bin, 1, 3)})
    local sessionId = utils.binToDec({unpack(bin, 4, 7)})
    local parityBit = bin[8]
    local handlerId = utils.binToDec({unpack(bin, 9, 16)})

    if (utils.getParity(handlerId) and 1 or 0) ~= parityBit then
        logger.warn('got parity bit: ' .. parityBit)
        logger.warn('actual parity bit: ' .. (utils.getParity(handlerId) and 1 or 0))
        error('corrupted handler id packet: parity bits do not match')
    end

    return packet.HandlerIdPacket(handlerId, sessionId)
end

return packet