-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathWsjtxMessageWriter.cs
196 lines (175 loc) · 7.04 KB
/
WsjtxMessageWriter.cs
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
185
186
187
188
189
190
191
192
193
194
195
196
using System;
using System.Buffers.Binary;
using System.Runtime.InteropServices;
using System.Text;
using WsjtxUtils.WsjtxMessages.Messages;
namespace WsjtxUtils.WsjtxMessages
{
/// <summary>
/// Writes WSJT-X messages to a memory source
/// </summary>
public class WsjtxMessageWriter : WsjtxBaseReaderWriter
{
/// <summary>
/// Constructs a WSJT-X message writer to the specified memory source
/// </summary>
/// <param name="source"></param>
public WsjtxMessageWriter(Memory<byte> source) : base(source)
{
if (source.Length < WsjtxConstants.HeaderLengthInBytes)
throw new ArgumentException($"Expecting greater than {WsjtxConstants.HeaderLengthInBytes} bytes for space to fit a valid message, {buffer.Length} found. Recommended size is 1500 bytes.");
}
/// <summary>
/// Write a single byte value to the buffer
/// </summary>
/// <returns></returns>
public void WriteByte(byte value)
{
buffer.Span[Position++] = value;
}
/// <summary>
/// Write a boolean value to the buffer
/// </summary>
/// <param name="value"></param>
public void WriteBool(bool value)
{
WriteByte(value ? WsjtxConstants.TrueValue : WsjtxConstants.FalseValue);
}
/// <summary>
/// Write an Int16 to the buffer
/// </summary>
/// <param name="value"></param>
public void WriteInt16(short value)
{
BinaryPrimitives.WriteInt16BigEndian(buffer.Span.Slice(Position, WsjtxConstants.SizeOfShort), value);
Position += WsjtxConstants.SizeOfShort;
}
/// <summary>
/// Write an UInt16 to the buffer
/// </summary>
/// <param name="value"></param>
public void WriteUInt16(ushort value)
{
BinaryPrimitives.WriteUInt16BigEndian(buffer.Span.Slice(Position, WsjtxConstants.SizeOfShort), value);
Position += WsjtxConstants.SizeOfShort;
}
/// <summary>
/// Write an Int32 to the buffer
/// </summary>
/// <returns></returns>
public void WriteInt32(int value)
{
BinaryPrimitives.WriteInt32BigEndian(buffer.Span.Slice(Position, WsjtxConstants.SizeOfInt), value);
Position += WsjtxConstants.SizeOfInt;
}
/// <summary>
/// Write an Int32 to the buffer
/// </summary>
/// <returns></returns>
public void WriteUInt32(uint value)
{
BinaryPrimitives.WriteUInt32BigEndian(buffer.Span.Slice(Position, WsjtxConstants.SizeOfInt), value);
Position += WsjtxConstants.SizeOfInt;
}
/// <summary>
/// Write an Int64 to the buffer
/// </summary>
/// <returns></returns>
public void WriteInt64(long value)
{
BinaryPrimitives.WriteInt64BigEndian(buffer.Span.Slice(Position, WsjtxConstants.SizeOfLong), value);
Position += WsjtxConstants.SizeOfLong;
}
/// <summary>
/// Write an UInt64 to the buffer
/// </summary>
/// <returns></returns>
public void WriteUInt64(ulong value)
{
BinaryPrimitives.WriteUInt64BigEndian(buffer.Span.Slice(Position, WsjtxConstants.SizeOfLong), value);
Position += WsjtxConstants.SizeOfLong;
}
/// <summary>
/// Write a double to the buffer
/// </summary>
/// <param name="value"></param>
public void WriteDouble(double value)
{
WriteInt64(BitConverter.DoubleToInt64Bits(value));
}
/// <summary>
/// Write a string to the buffer
/// </summary>
/// <param name="value"></param>
/// <exception cref="InsufficientMemoryException">Exception thrown if the string size exceeds the allocated buffer</exception>
public void WriteString(string value)
{
int textByteCount = string.IsNullOrEmpty(value) ? 0 : Encoding.UTF8.GetByteCount(value);
BinaryPrimitives.WriteUInt32BigEndian(buffer.Span.Slice(Position, WsjtxConstants.SizeOfInt), Convert.ToUInt32(textByteCount));
Position += WsjtxConstants.SizeOfInt;
if (textByteCount == 0)
return;
if (!MemoryMarshal.TryGetArray(buffer.Slice(Position), out ArraySegment<byte> segment) && segment.Array != null)
throw new InsufficientMemoryException("Unable to allocate the array from the underlying buffer.");
Encoding.UTF8.GetBytes(value, 0, value.Length, segment.Array!, segment.Offset);
Position += textByteCount;
}
/// <summary>
/// Write a <see cref="QColor" /> to the buffer
/// </summary>
/// <param name="color"></param>
public void WriteColor(QColor color)
{
/*
https://doc.qt.io/archives/qt-5.11/datastreamformat.html
Color spec (qint8)
Alpha value (quint16)
Red value (quint16)
Green value (quint16)
Blue value (quint16)
Pad value (quint16)
*/
// Write color spec
WriteEnum(color.Spec);
// Write color values
WriteUInt16(color.Alpha);
WriteUInt16(color.Red);
WriteUInt16(color.Green);
WriteUInt16(color.Blue);
// Write pad value
WriteUInt16(0);
}
/// <summary>
/// Write an enumeration of type T to the buffer
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value"></param>
/// <exception cref="NotImplementedException">Exception thrown if the underlying type is not implemented</exception>
public void WriteEnum<T>(T value)
{
var enumType = typeof(T);
var underlyingType = Enum.GetUnderlyingType(enumType);
var typeCode = Type.GetTypeCode(underlyingType);
switch (typeCode)
{
case TypeCode.Int32:
WriteInt32(Convert.ToInt32(value));
break;
case TypeCode.UInt32:
WriteUInt32(Convert.ToUInt32(value));
break;
case TypeCode.Int64:
WriteInt64(Convert.ToInt64(value));
break;
case TypeCode.UInt64:
WriteUInt64(Convert.ToUInt64(value));
break;
case TypeCode.Byte:
WriteByte(Convert.ToByte(value));
break;
default:
throw new NotImplementedException($"Enum {enumType.Name} has a type of {underlyingType.Name} which is not implemented");
}
}
}
}