forked from n2ic/MorseRunner
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUdpHandler.pas
191 lines (174 loc) · 4.94 KB
/
UdpHandler.pas
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
//------------------------------------------------------------------------------
//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/.
//------------------------------------------------------------------------------
unit UdpHandler;
{$MODE Delphi}
interface
uses
LCLIntf, LCLType, LMessages, Messages, SysUtils, Classes, Forms, SyncObjs, MMSystem, SndTypes,
Windows, Station, blcksock;
Type
TUdpThread = class(TThread)
private
rcvdstring: string;
rcvdmsg: TStationMessage;
procedure SetCall;
procedure SetNumber;
procedure SendEnter;
procedure SendMessage;
procedure SendEsc;
procedure TuneRit;
protected
procedure Execute; override;
public
Constructor Create(CreateSuspended : boolean);
end;
Implementation
uses Main;
{ TUpdThread }
constructor TUdpThread.Create(CreateSuspended : boolean);
begin
FreeOnTerminate := True;
inherited Create(CreateSuspended);
end;
procedure TUdpThread.SetCall;
// this method is executed by the mainthread and can therefore access all GUI elements.
begin
if rcvdstring = 'clear' then
begin
MainForm.Edit1.Text := '';
exit;
end;
MainForm.Edit1.Text := rcvdstring;
end;
procedure TUdpThread.SetNumber;
// this method is executed by the mainthread and can therefore access all GUI elements.
begin
MainForm.Edit3.Text := rcvdstring;
end;
procedure TUdpThread.TuneRIT;
begin
if rcvdstring = 'up' then
begin
MainForm.IncRit(1)
end
else
begin
MainForm.IncRit(-1)
end
end;
procedure TUdpThread.SendEnter;
begin
MainForm.ProcessEnter;
end;
procedure TUdpThread.SendEsc;
var
Key:char;
begin
Key := #27;
MainForm.FormKeyPress(Self, Key);
end;
procedure TUdpThread.SendMessage;
begin
MainForm.SendMsg(rcvdmsg);
end;
procedure TUdpThread.Execute;
var
Sock:TUDPBlockSocket;
buf:string;
stringlist: TStringList;
numparsed: integer;
token:string;
i: integer;
radionr:string;
socket:integer;
basesocket:integer;
tempstr:string;
begin
basesocket := 13064;
Sock:=TUDPBlockSocket.Create;
stringlist := TStringList.Create;
try
socket := basesocket + StrToInt(MainForm.Name);
sock.bind('0.0.0.0', IntToStr(socket));
if sock.LastError<>0 then exit;
stringlist.Delimiter := ' ';
while True do
begin
if terminated then break;
buf := sock.RecvPacket(-1);
if sock.lasterror=0 then
begin
rcvdstring := buf;
stringlist.DelimitedText := rcvdstring;
numparsed := stringlist.Count;
i := 0;
while i < (numparsed) do
begin
token := stringlist[i];
if token = '[radionr]' then
begin
radionr := stringlist[i + 1];
if radionr <> MainForm.Name then exit;
end;
if token = '[call]' then
begin
rcvdstring := stringlist[i + 1];
Synchronize(SetCall);
end;
if token = '[rcvdnr]' then
begin
rcvdstring := stringlist[i + 1];
Synchronize(SetNumber);
end;
if token = '[rit]' then
begin
rcvdstring := stringlist[i + 1];
Synchronize(TuneRit);
end;
if LeftStr(token,2) = '[F' then
begin
tempstr := Copy(token,3,1);
//rcvdmsg := TStationMessage.msgTU;
rcvdmsg := TStationMessage(strtoint(tempstr));
Synchronize(SendMessage);
end;
if token = '[enter]' then
begin
Synchronize(SendEnter);
end;
if token = '[stop]' then
begin
Synchronize(SendEsc);
end;
i := i + 2;
end;
//lasttoken := stringlist[numparsed - 1];
//if lasttoken = '[call]' then
// begin
// rcvdstring := stringlist[0];
// Synchronize(SetCall);
// end
//else if lasttoken = '[enter]' then
// begin
// Synchronize(SendEnter);
// end
//else if lasttoken = '[number]' then
// begin
// rcvdstring := stringlist[0];
// Synchronize(SetNumber);
// end
// do something with data and prepare response data
//sock.SendString(Buf);
end;
sleep(1);
end;
sock.CloseSocket;
finally
sock.free;
stringlist.Free;
end;
end;
end.