1 | unit GameProtocol;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, Game, VarBlockSerializer, CommPin, SpecializedList;
|
---|
7 |
|
---|
8 | type
|
---|
9 | TCommand = (cmdTextMessage, cmdTurnStart, cmdTurnEnd, cmdGameStart, cmdGameEnd,
|
---|
10 | cmdSurrender, cmdNextPlayer, cmdParametersChanged);
|
---|
11 | TCommandEvent = procedure (Command: TCommand; DataIn, DataOut: TStream);
|
---|
12 |
|
---|
13 | TCommandTextMessage = record
|
---|
14 | Text: string;
|
---|
15 | end;
|
---|
16 |
|
---|
17 | { TGameProtocolClient }
|
---|
18 |
|
---|
19 | TGameProtocolClient = class
|
---|
20 | private
|
---|
21 | FOnGameEnd: TNotifyEvent;
|
---|
22 | FOnGameStart: TNotifyEvent;
|
---|
23 | FOnNextPlayer: TNotifyEvent;
|
---|
24 | FOnParametersChanged: TNotifyEvent;
|
---|
25 | FOnTurnStart: TNotifyEvent;
|
---|
26 | procedure Receive(Sender: TCommPin; Stream: TListByte);
|
---|
27 | procedure SendCmd(Command: TCommand);
|
---|
28 | public
|
---|
29 | Pin: TCommPin;
|
---|
30 | procedure TurnEnd;
|
---|
31 | procedure Surrender;
|
---|
32 | procedure SendMessage(Text: string);
|
---|
33 | constructor Create;
|
---|
34 | destructor Destroy; override;
|
---|
35 | property OnGameStart: TNotifyEvent read FOnGameStart write FOnGameStart;
|
---|
36 | property OnGameEnd: TNotifyEvent read FOnGameEnd write FOnGameEnd;
|
---|
37 | property OnTurnStart: TNotifyEvent read FOnTurnStart write FOnTurnStart;
|
---|
38 | property OnNextPlayer: TNotifyEvent read FOnNextPlayer write FOnNextPlayer;
|
---|
39 | property OnParametersChanged: TNotifyEvent read FOnParametersChanged
|
---|
40 | write FOnParametersChanged;
|
---|
41 | end;
|
---|
42 |
|
---|
43 | TSendMessageEvent = procedure (Text: string) of object;
|
---|
44 |
|
---|
45 | { TGameProtocolServer }
|
---|
46 |
|
---|
47 | TGameProtocolServer = class
|
---|
48 | private
|
---|
49 | FOnSendMessage: TSendMessageEvent;
|
---|
50 | FOnSurrender: TNotifyEvent;
|
---|
51 | FOnTurnEnd: TNotifyEvent;
|
---|
52 | procedure Receive(Sender: TCommPin; Stream: TListByte);
|
---|
53 | procedure SendCmd(Command: TCommand);
|
---|
54 | public
|
---|
55 | Pin: TCommPin;
|
---|
56 | constructor Create;
|
---|
57 | destructor Destroy; override;
|
---|
58 | procedure GameStart;
|
---|
59 | procedure GameEnd;
|
---|
60 | procedure TurnStart;
|
---|
61 | procedure ParametersChanged;
|
---|
62 | procedure NextPlayer;
|
---|
63 | property OnSendMessage: TSendMessageEvent read FOnSendMessage
|
---|
64 | write FOnSendMessage;
|
---|
65 | property OnTurnEnd: TNotifyEvent read FOnTurnEnd write FOnTurnEnd;
|
---|
66 | property OnSurrender: TNotifyEvent read FOnSurrender write FOnSurrender;
|
---|
67 | end;
|
---|
68 |
|
---|
69 |
|
---|
70 | implementation
|
---|
71 |
|
---|
72 | { TGameProtocolServer }
|
---|
73 |
|
---|
74 | procedure TGameProtocolServer.Receive(Sender: TCommPin; Stream: TListByte);
|
---|
75 | var
|
---|
76 | Data: TVarBlockSerializer;
|
---|
77 | Command: Integer;
|
---|
78 | begin
|
---|
79 | Data := TVarBlockSerializer.Create;
|
---|
80 | Data.WriteVarList(Stream);
|
---|
81 | Data.Stream.Position := 0;
|
---|
82 | Command := Data.ReadVarUInt;
|
---|
83 | try
|
---|
84 | if Command = Integer(cmdTextMessage) then begin
|
---|
85 | if Assigned(FOnSendMessage) then FOnSendMessage(Data.ReadVarString);
|
---|
86 | end else
|
---|
87 | if Command = Integer(cmdTurnEnd) then begin
|
---|
88 | if Assigned(FOnTurnEnd) then FOnTurnEnd(Self);
|
---|
89 | end else
|
---|
90 | if Command = Integer(cmdSurrender) then begin
|
---|
91 | if Assigned(FOnSurrender) then FOnSurrender(Self);
|
---|
92 | end;
|
---|
93 | finally
|
---|
94 | Data.Free;
|
---|
95 | end;
|
---|
96 | end;
|
---|
97 |
|
---|
98 | procedure TGameProtocolServer.SendCmd(Command: TCommand);
|
---|
99 | var
|
---|
100 | Data: TVarBlockSerializer;
|
---|
101 | Data2: TListByte;
|
---|
102 | begin
|
---|
103 | Data := TVarBlockSerializer.Create;
|
---|
104 | Data2 := TListByte.Create;
|
---|
105 | try
|
---|
106 | Data.WriteVarUInt(Integer(Command));
|
---|
107 | Data.Stream.Position := 0;
|
---|
108 | Data.ReadVarList(Data2);
|
---|
109 | Pin.Send(Data2);
|
---|
110 | finally
|
---|
111 | Data2.Free;
|
---|
112 | Data.Free;
|
---|
113 | end;
|
---|
114 | end;
|
---|
115 |
|
---|
116 | constructor TGameProtocolServer.Create;
|
---|
117 | begin
|
---|
118 | Pin := TCommPin.Create;
|
---|
119 | Pin.OnReceive := Receive;
|
---|
120 | end;
|
---|
121 |
|
---|
122 | destructor TGameProtocolServer.Destroy;
|
---|
123 | begin
|
---|
124 | FreeAndNil(Pin);
|
---|
125 | inherited;
|
---|
126 | end;
|
---|
127 |
|
---|
128 | procedure TGameProtocolServer.GameStart;
|
---|
129 | begin
|
---|
130 | SendCmd(cmdGameStart);
|
---|
131 | end;
|
---|
132 |
|
---|
133 | procedure TGameProtocolServer.GameEnd;
|
---|
134 | begin
|
---|
135 | SendCmd(cmdGameEnd);
|
---|
136 | end;
|
---|
137 |
|
---|
138 | procedure TGameProtocolServer.TurnStart;
|
---|
139 | begin
|
---|
140 | SendCmd(cmdTurnStart);
|
---|
141 | end;
|
---|
142 |
|
---|
143 | procedure TGameProtocolServer.ParametersChanged;
|
---|
144 | begin
|
---|
145 | SendCmd(cmdParametersChanged);
|
---|
146 | end;
|
---|
147 |
|
---|
148 | procedure TGameProtocolServer.NextPlayer;
|
---|
149 | begin
|
---|
150 | SendCmd(cmdNextPlayer);
|
---|
151 | end;
|
---|
152 |
|
---|
153 | { TGameProtocol }
|
---|
154 |
|
---|
155 | procedure TGameProtocolClient.Receive(Sender: TCommPin; Stream: TListByte);
|
---|
156 | var
|
---|
157 | Data: TVarBlockSerializer;
|
---|
158 | Command: Integer;
|
---|
159 | begin
|
---|
160 | Data := TVarBlockSerializer.Create;
|
---|
161 | Data.WriteVarList(Stream);
|
---|
162 | Data.Stream.Position := 0;
|
---|
163 | Command := Data.ReadVarUInt;
|
---|
164 | try
|
---|
165 | if Command = Integer(cmdGameStart) then begin
|
---|
166 | if Assigned(FOnGameStart) then FOnGameStart(Self);
|
---|
167 | end else
|
---|
168 | if Command = Integer(cmdGameEnd) then begin
|
---|
169 | if Assigned(FOnGameEnd) then FOnGameEnd(Self);
|
---|
170 | end else
|
---|
171 | if Command = Integer(cmdTurnStart) then begin
|
---|
172 | if Assigned(FOnTurnStart) then FOnTurnStart(Self);
|
---|
173 | end else
|
---|
174 | if Command = Integer(cmdNextPlayer) then begin
|
---|
175 | if Assigned(FOnNextPlayer) then FOnNextPlayer(Self);
|
---|
176 | end else
|
---|
177 | if Command = Integer(cmdParametersChanged) then begin
|
---|
178 | if Assigned(FOnParametersChanged) then FOnParametersChanged(Self);
|
---|
179 | end;
|
---|
180 | finally
|
---|
181 | Data.Free;
|
---|
182 | end;
|
---|
183 | end;
|
---|
184 |
|
---|
185 | procedure TGameProtocolClient.SendCmd(Command: TCommand);
|
---|
186 | var
|
---|
187 | Data: TVarBlockSerializer;
|
---|
188 | Data2: TListByte;
|
---|
189 | begin
|
---|
190 | Data := TVarBlockSerializer.Create;
|
---|
191 | Data2 := TListByte.Create;
|
---|
192 | try
|
---|
193 | Data.WriteVarUInt(Integer(Command));
|
---|
194 | Data.Stream.Position := 0;
|
---|
195 | Data.ReadVarList(Data2);
|
---|
196 | Pin.Send(Data2);
|
---|
197 | finally
|
---|
198 | Data2.Free;
|
---|
199 | Data.Free;
|
---|
200 | end;
|
---|
201 | end;
|
---|
202 |
|
---|
203 | procedure TGameProtocolClient.TurnEnd;
|
---|
204 | begin
|
---|
205 | SendCmd(cmdTurnEnd);
|
---|
206 | end;
|
---|
207 |
|
---|
208 | procedure TGameProtocolClient.Surrender;
|
---|
209 | begin
|
---|
210 | SendCmd(cmdSurrender);
|
---|
211 | end;
|
---|
212 |
|
---|
213 | procedure TGameProtocolClient.SendMessage(Text: string);
|
---|
214 | var
|
---|
215 | Data: TVarBlockSerializer;
|
---|
216 | Data2: TListByte;
|
---|
217 | begin
|
---|
218 | Data := TVarBlockSerializer.Create;
|
---|
219 | Data2 := TListByte.Create;
|
---|
220 | try
|
---|
221 | Data.WriteVarUInt(Integer(cmdTextMessage));
|
---|
222 | Data.WriteVarString(Text);
|
---|
223 | Data.Stream.Position := 0;
|
---|
224 | Data.ReadVarList(Data2);
|
---|
225 | Pin.Send(Data2);
|
---|
226 | finally
|
---|
227 | Data2.Free;
|
---|
228 | Data.Free;
|
---|
229 | end;
|
---|
230 | end;
|
---|
231 |
|
---|
232 | constructor TGameProtocolClient.Create;
|
---|
233 | begin
|
---|
234 | Pin := TCommPin.Create;
|
---|
235 | Pin.OnReceive := Receive;
|
---|
236 | end;
|
---|
237 |
|
---|
238 | destructor TGameProtocolClient.Destroy;
|
---|
239 | begin
|
---|
240 | FreeAndNil(Pin);
|
---|
241 | inherited;
|
---|
242 | end;
|
---|
243 |
|
---|
244 | end.
|
---|
245 |
|
---|