-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathteogw_data.go
144 lines (115 loc) · 2.71 KB
/
teogw_data.go
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
// Copyright 2022 Kirill Scherba <[email protected]>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Teogw structure and methods
package teogw
import (
"bytes"
"encoding/binary"
"encoding/json"
"github.com/kirill-scherba/bslice"
"github.com/teonet-go/teowebrtc_server"
)
// Teogw packet data
type TeogwData struct {
ID uint32 `json:"id"`
Address string `json:"address"`
Command string `json:"command"`
Data []byte `json:"data"`
Err string `json:"err"`
}
// Set id
func (gw *TeogwData) SetID(id uint32) {
gw.ID = id
}
// Set data
func (gw *TeogwData) SetCommand(command string) {
gw.Command = command
}
// Set data
func (gw *TeogwData) SetData(data []byte) {
gw.Data = data
}
// Set error
func (gw *TeogwData) SetError(err error) {
gw.Err = err.Error()
}
// Get ID
func (gw TeogwData) GetID() uint32 {
return gw.ID
}
// Get address
func (gw TeogwData) GetAddress() string {
return gw.Address
}
// Get command
func (gw TeogwData) GetCommand() string {
return gw.Command
}
// Get data
func (gw TeogwData) GetData() []byte {
return gw.Data
}
// MarshalBinary marshal Teogw binary packet
func (gw *TeogwData) MarshalBinary() (out []byte, err error) {
var b bslice.ByteSlice
buf := new(bytes.Buffer)
le := binary.LittleEndian
// b.WriteSlice(buf, []byte(gw.id))
binary.Write(buf, le, gw.ID)
b.WriteSlice(buf, []byte(gw.Address))
b.WriteSlice(buf, []byte(gw.Command))
b.WriteSlice(buf, []byte(gw.Data))
b.WriteSlice(buf, []byte(gw.Err))
out = buf.Bytes()
return
}
// UnmarshalBinary unmarshal Teogw binary packet
func (gw *TeogwData) UnmarshalBinary(data []byte) (err error) {
var b bslice.ByteSlice
buf := bytes.NewBuffer(data)
le := binary.LittleEndian
// gw.id, err = b.ReadString(buf)
binary.Read(buf, le, &gw.ID)
if err != nil {
return
}
gw.Address, err = b.ReadString(buf)
if err != nil {
return
}
gw.Command, err = b.ReadString(buf)
if err != nil {
return
}
gw.Data, err = b.ReadSlice(buf)
if err != nil {
return
}
gw.Err, err = b.ReadString(buf)
return
}
// Marshal TeogwData to Json
func (gw TeogwData) MarshalJson(
gwData teowebrtc_server.WebRTCData,
command string, inData []byte, inErr error) ([]byte, error) {
var request = TeogwData{
ID: gwData.GetID(),
Address: gwData.GetAddress(),
Command: command,
Data: inData,
Err: func() (errStr string) {
if inErr != nil {
errStr = inErr.Error()
}
return
}(),
}
return json.Marshal(request)
}
// Unmarshal Json and return TeogwData
func (gw TeogwData) UnmarshalJson(data []byte) (teowebrtc_server.WebRTCData, error) {
var request = TeogwData{}
err := json.Unmarshal(data, &request)
return request, err
}