Changeset 202 for trunk/UGameProtocol.pas
- Timestamp:
- May 17, 2018, 5:41:47 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UGameProtocol.pas
r185 r202 22 22 FOnGameEnd: TNotifyEvent; 23 23 FOnGameStart: TNotifyEvent; 24 FOnTurnStart: TNotifyEvent; 24 25 procedure Receive(Sender: TCommPin; Stream: TListByte); 26 procedure SendCmd(Command: TCommand); 25 27 public 26 28 Pin: TCommPin; 29 procedure TurnEnd; 27 30 procedure SendMessage(Text: string); 28 31 constructor Create; … … 32 35 property OnGameEnd: TNotifyEvent read FOnGameEnd 33 36 write FOnGameEnd; 37 property OnTurnStart: TNotifyEvent read FOnTurnStart 38 write FOnTurnStart; 34 39 end; 35 40 … … 41 46 private 42 47 FOnSendMessage: TSendMessageEvent; 48 FOnTurnEnd: TNotifyEvent; 43 49 procedure Receive(Sender: TCommPin; Stream: TListByte); 50 procedure SendCmd(Command: TCommand); 44 51 public 45 52 Pin: TCommPin; … … 48 55 procedure GameStart; 49 56 procedure GameEnd; 57 procedure TurnStart; 50 58 property OnSendMessage: TSendMessageEvent read FOnSendMessage 51 59 write FOnSendMessage; 60 property OnTurnEnd: TNotifyEvent read FOnTurnEnd 61 write FOnTurnEnd; 52 62 end; 53 63 … … 69 79 if Command = Integer(cmdTextMessage) then begin 70 80 if Assigned(FOnSendMessage) then FOnSendMessage(Data.ReadVarString); 81 end else 82 if Command = Integer(cmdTurnEnd) then begin 83 if Assigned(FOnTurnEnd) then FOnTurnEnd(Self); 71 84 end; 72 85 finally … … 75 88 end; 76 89 77 constructor TGameProtocolServer.Create; 78 begin 79 Pin := TCommPin.Create; 80 Pin.OnReceive := Receive; 81 end; 82 83 destructor TGameProtocolServer.Destroy; 84 begin 85 Pin.Free; 86 inherited Destroy; 87 end; 88 89 procedure TGameProtocolServer.GameStart; 90 procedure TGameProtocolServer.SendCmd(Command: TCommand); 90 91 var 91 92 Data: TVarBlockSerializer; … … 95 96 Data2 := TListByte.Create; 96 97 try 97 Data.WriteVarUInt(Integer( cmdGameStart));98 Data.WriteVarUInt(Integer(Command)); 98 99 Data.Stream.Position := 0; 99 100 Data.ReadVarList(Data2); … … 105 106 end; 106 107 108 constructor TGameProtocolServer.Create; 109 begin 110 Pin := TCommPin.Create; 111 Pin.OnReceive := Receive; 112 end; 113 114 destructor TGameProtocolServer.Destroy; 115 begin 116 Pin.Free; 117 inherited Destroy; 118 end; 119 120 procedure TGameProtocolServer.GameStart; 121 begin 122 SendCmd(cmdGameStart); 123 end; 124 107 125 procedure TGameProtocolServer.GameEnd; 108 var 109 Data: TVarBlockSerializer; 110 Data2: TListByte; 111 begin 112 Data := TVarBlockSerializer.Create; 113 Data2 := TListByte.Create; 114 try 115 Data.WriteVarUInt(Integer(cmdGameEnd)); 116 Data.Stream.Position := 0; 117 Data.ReadVarList(Data2); 118 Pin.Send(Data2); 119 finally 120 Data2.Free; 121 Data.Free; 122 end; 126 begin 127 SendCmd(cmdGameEnd); 128 end; 129 130 procedure TGameProtocolServer.TurnStart; 131 begin 132 SendCmd(cmdTurnStart); 123 133 end; 124 134 … … 131 141 begin 132 142 Data := TVarBlockSerializer.Create; 133 Data. ReadVarList(Stream);143 Data.WriteVarList(Stream); 134 144 Data.Stream.Position := 0; 135 145 Command := Data.ReadVarSInt; … … 140 150 if Command = Integer(cmdGameEnd) then begin 141 151 if Assigned(FOnGameEnd) then FOnGameEnd(Self); 152 end else 153 if Command = Integer(cmdTurnStart) then begin 154 if Assigned(FOnTurnStart) then FOnTurnStart(Self); 142 155 end; 143 156 finally 144 157 Data.Free; 145 158 end; 159 end; 160 161 procedure TGameProtocolClient.SendCmd(Command: TCommand); 162 var 163 Data: TVarBlockSerializer; 164 Data2: TListByte; 165 begin 166 Data := TVarBlockSerializer.Create; 167 Data2 := TListByte.Create; 168 try 169 Data.WriteVarUInt(Integer(Command)); 170 Data.Stream.Position := 0; 171 Data.ReadVarList(Data2); 172 Pin.Send(Data2); 173 finally 174 Data2.Free; 175 Data.Free; 176 end; 177 end; 178 179 procedure TGameProtocolClient.TurnEnd; 180 begin 181 SendCmd(cmdTurnEnd); 146 182 end; 147 183
Note:
See TracChangeset
for help on using the changeset viewer.