-
Notifications
You must be signed in to change notification settings - Fork 7
/
UDMClient.pas
180 lines (154 loc) · 4.36 KB
/
UDMClient.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
unit UDMClient;
interface
uses
System.Classes, DzSocket;
type
TDMClient = class(TDataModule)
C: TDzTCPClient;
procedure CRead(Sender: TObject; Socket: TDzSocket; const Cmd: Char;
const A: string);
procedure CDisconnect(Sender: TObject; Socket: TDzSocket;
const WasConnected: Boolean);
procedure CConnectionLost(Sender: TObject; Socket: TDzSocket);
procedure CError(Sender: TObject; Socket: TDzSocket;
const Event: TErrorEvent; const ErrorCode: Integer;
const ErrorMsg: string);
procedure DataModuleCreate(Sender: TObject);
procedure CLoginRequest(Sender: TObject; Socket: TDzSocket;
var Data: string);
procedure CLoginResponse(Sender: TObject; Socket: TDzSocket;
Accepted: Boolean; const Data: string);
procedure CConnect(Sender: TObject; Socket: TDzSocket);
private
procedure OtherClientConnected(const A: string);
procedure OtherClientDisconnected(const A: string);
procedure ReceivedListOfClients(const A: string);
procedure ReceivedOpenRound(const A: string);
procedure ClientPointed(const A: string);
procedure ReceivedCloseRound(const A: string);
end;
var
DMClient: TDMClient;
implementation
{%CLASSGROUP 'Vcl.Controls.TControl'}
{$R *.dfm}
uses Vars, UFrm, UItem, System.SysUtils, Utils, ULanguage,
USyncClient, System.SyncObjs;
procedure TDMClient.DataModuleCreate(Sender: TObject);
begin
C.Port := INT_PORT;
end;
procedure TDMClient.CRead(Sender: TObject; Socket: TDzSocket; const Cmd: Char;
const A: string);
begin
SyncClient.Enter;
try
case Cmd of
'C': OtherClientConnected(A);
'D': OtherClientDisconnected(A);
'L': ReceivedListOfClients(A);
'A': ReceivedOpenRound(A);
'P': ClientPointed(A);
'X': ReceivedCloseRound(A);
end;
finally
SyncClient.Leave;
end;
end;
procedure TDMClient.CConnect(Sender: TObject; Socket: TDzSocket);
begin
Log(Lang.Get('LOG_CONNECTED'));
end;
procedure TDMClient.CLoginRequest(Sender: TObject; Socket: TDzSocket;
var Data: string);
begin
//send version and user name to the server
Data := ArrayToData([STR_VERSION, pubUser]);
end;
procedure TDMClient.CLoginResponse(Sender: TObject; Socket: TDzSocket;
Accepted: Boolean; const Data: string);
begin
if Accepted then
begin
//server accepted client connection
pubClientID := Data.ToInteger;
Frm.ClientAccepted;
end else
begin
if Data.StartsWith('V') then
Log(Format(Lang.Get('REJECT_WRONG_VERSION'), [Data.Remove(0, 1)]))
else
if Data='U' then
Log(Lang.Get('REJECT_USER_ALREADY'))
else
Log(Format('Unknown reject data: "%s"', [Data]));
end;
end;
procedure TDMClient.CDisconnect(Sender: TObject; Socket: TDzSocket;
const WasConnected: Boolean);
begin
if WasConnected then
Log(Lang.Get('LOG_DISCONNECTED'));
Frm.ClientDisconnected;
end;
procedure TDMClient.CConnectionLost(Sender: TObject; Socket: TDzSocket);
begin
Log(Lang.Get('LOG_CONNECTION_LOST'));
end;
procedure TDMClient.CError(Sender: TObject; Socket: TDzSocket;
const Event: TErrorEvent; const ErrorCode: Integer; const ErrorMsg: string);
begin
Log(Format(Lang.Get('LOG_SOCKET_ERROR'), [ErrorMsg]));
end;
procedure TDMClient.OtherClientConnected(const A: string);
begin
//a client connected - add into list
Frm.AddItem(A);
Frm.AtCountConnections;
end;
procedure TDMClient.OtherClientDisconnected(const A: string);
var Idx: Integer;
begin
//a client disconnected - remove from list
Idx := FindItemIndexByID(A.ToInteger);
if Idx<>-1 then
begin
GetItemByIndex(Idx).Free;
Frm.L.Items.Delete(Idx);
Frm.AtCountConnections;
end;
end;
procedure TDMClient.ClientPointed(const A: string);
var
Idx: Integer;
Item: TItem;
begin
//a client made an estimate
Idx := FindItemIndexByID(A.ToInteger);
if Idx<>-1 then
begin
Item := GetItemByIndex(Idx);
Item.Estimated := True;
Frm.L.Invalidate;
end;
end;
procedure TDMClient.ReceivedOpenRound(const A: string);
begin
Log(Lang.Get('LOG_ROUND_OPENED'));
//server opened estimate round
Frm.FillClientsList(A);
PlaySound;
end;
procedure TDMClient.ReceivedCloseRound(const A: string);
begin
Log(Lang.Get('LOG_ROUND_CLOSED'));
//server closed estimate round
Frm.FillClientsList(A);
end;
procedure TDMClient.ReceivedListOfClients(const A: string);
begin
Log(Lang.Get('LOG_LIST_RECEIVED'));
//clients list received
Frm.FillClientsList(A);
end;
end.