Changeset 365 for trunk/Network/UNetworkServer.pas
- Timestamp:
- Apr 13, 2021, 9:57:36 PM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Network/UNetworkServer.pas
r364 r365 24 24 private 25 25 DataAvailableHandle: Pointer; 26 ReceiveBuffer: TMemoryStream; 26 27 procedure DisconnectExecute(Sender: TObject); 28 procedure DataAvailableSync; 29 procedure DataAvailableExecute(Sender: TObject); 27 30 public 28 31 NetworkServer: TNetworkServer; … … 31 34 Player: TNetworkServerPlayer; 32 35 Connected: Boolean; 33 procedure DataAvailableExecute(Sender: TObject);34 36 procedure Run; 35 37 constructor Create; … … 84 86 85 87 uses 86 Global ;88 Global, UNetworkCommon; 87 89 88 90 procedure Client(Command, Player: integer; var Data); … … 159 161 procedure TNetworkServerConnection.DisconnectExecute(Sender: TObject); 160 162 begin 161 Connected := False;163 {Connected := False; 162 164 if Assigned(Player) then begin 163 165 Player.Connection := nil; 164 166 Player := nil; 165 167 end; 166 end; 167 168 procedure TNetworkServerConnection.DataAvailableExecute(Sender: TObject); 168 } 169 end; 170 171 procedure TNetworkServerConnection.DataAvailableSync; 169 172 var 170 173 Data: array of Byte; … … 174 177 Command: TCommand; 175 178 begin 176 if not Connected then Exit; 177 Command := TCommand(Socket.ReadDWord); 178 PlayerIndex := Socket.ReadDWord; 179 Subject := Socket.ReadDWord; 180 SetLength(Data, GetCommandDataSize(TCommand(Command))); 181 if Length(Data) > 0 then begin 182 ReadCount := Socket.Read(Data[0], Length(Data)); 183 SetLength(Data, ReadCount); 184 end; 185 if Assigned(Player) then begin 186 if Length(Data) > 0 then 187 Player.Server(Command, PlayerIndex, Subject, Data[0]) 188 else Player.Server(Command, PlayerIndex, Subject, nil^); 189 end; 190 191 NetworkServer.TCPServer.EventLoop.ClearDataAvailableNotify(DataAvailableHandle); 192 DataAvailableHandle := NetworkServer.TCPServer.EventLoop.SetDataAvailableNotify(Socket.Handle, DataAvailableExecute, nil); 179 StreamAppend(ReceiveBuffer, Socket); 180 while ReceiveBuffer.Size >= 3 * SizeOf(Integer) do begin 181 ReceiveBuffer.Position := 0; 182 183 Command := TCommand(ReceiveBuffer.ReadDWord); 184 PlayerIndex := ReceiveBuffer.ReadDWord; 185 Subject := ReceiveBuffer.ReadDWord; 186 SetLength(Data, GetCommandDataSize(TCommand(Command))); 187 if Length(Data) > 0 then begin 188 ReadCount := ReceiveBuffer.Read(Data[0], Length(Data)); 189 SetLength(Data, ReadCount); 190 end; 191 if Assigned(Player) then begin 192 if Length(Data) > 0 then 193 Player.Server(Command, PlayerIndex, Subject, Data[0]) 194 else Player.Server(Command, PlayerIndex, Subject, nil^); 195 end; 196 StreamRemoveRead(ReceiveBuffer); 197 end; 198 end; 199 200 procedure TNetworkServerConnection.DataAvailableExecute(Sender: TObject); 201 begin 202 NetworkServer.TCPServerThread.Synchronize(NetworkServer.TCPServerThread, DataAvailableSync); 203 Sleep(10); // TODO: How to reset this event 193 204 end; 194 205 … … 202 213 constructor TNetworkServerConnection.Create; 203 214 begin 215 ReceiveBuffer := TMemoryStream.Create; 204 216 end; 205 217 … … 207 219 begin 208 220 if Assigned(Player) then Player.Connection := nil; 209 NetworkServer.TCPServer.EventLoop.ClearDataAvailableNotify(DataAvailableHandle); 221 if Assigned(DataAvailableHandle) then 222 NetworkServer.TCPServer.EventLoop.ClearDataAvailableNotify(DataAvailableHandle); 210 223 FreeAndNil(Socket); 211 224 NetworkServer.Connections.Remove(Self); 225 FreeAndNil(ReceiveBuffer); 212 226 inherited; 213 227 end; … … 221 235 Player: TNetworkServerPlayer; 222 236 I: Integer; 237 InitModuleData: TInitModuleData; 223 238 begin 224 239 NewConnection := TNetworkServerConnection.Create;; … … 239 254 NewConnection.Player := Player; 240 255 Player.Connection := NewConnection; 241 Player.Client(cmInitModule, -1, nil^);256 Player.Client(cmInitModule, Player.Id, InitModuleData); 242 257 end else AStream.Free; 243 258 end;
Note:
See TracChangeset
for help on using the changeset viewer.