Changeset 365
- Timestamp:
- Apr 13, 2021, 9:57:36 PM (4 years ago)
- Location:
- trunk
- Files:
-
- 1 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/GameServer.pas
r364 r365 1480 1480 CallPlayer(cShowShipChange, p1, ShowShipChange); 1481 1481 end; 1482 end 1483 end 1482 end; 1483 end; 1484 1484 end; 1485 1485 -
trunk/Integrated.lpi
r364 r365 95 95 </Item2> 96 96 </RequiredPackages> 97 <Units Count="4 7">97 <Units Count="48"> 98 98 <Unit0> 99 99 <Filename Value="Integrated.lpr"/> … … 356 356 <IsPartOfProject Value="True"/> 357 357 </Unit46> 358 <Unit47> 359 <Filename Value="Network\UNetworkCommon.pas"/> 360 <IsPartOfProject Value="True"/> 361 </Unit47> 358 362 </Units> 359 363 </ProjectOptions> -
trunk/Network/UNetworkClient.pas
r364 r365 8 8 Classes, SysUtils, fpsock, fpAsync, Protocol; 9 9 10 11 procedure Client(Command, Player: Integer; var Data); stdcall;12 13 14 implementation15 16 uses17 LocalPlayer, Global;18 19 10 type 20 21 11 { TTCPClientThread } 22 12 … … 33 23 TCPClientThread: TTCPClientThread; 34 24 ReceiveBuffer: TMemoryStream; 35 procedure AppendStream(Stream: TStream; SourceStream: TStream);36 25 procedure DataAvailableExecute(Sender: TObject); 37 26 procedure ConnectionStateChangeExecute(Sender: TClientConnectionSocket; 38 27 OldState, NewState: TConnectionState); 28 procedure DataAvailableSync; 39 29 public 40 30 AuxServer: TServerCall; … … 49 39 var 50 40 NetworkClient: TNetworkClient; 41 42 procedure Client(Command, Player: Integer; var Data); stdcall; 43 44 45 implementation 46 47 uses 48 LocalPlayer, Global, UNetworkCommon; 49 50 function LocalServer(Command, Player, Subject: Integer; var Data): Integer; stdcall; 51 begin 52 if Assigned(NetworkClient) then 53 Result := NetworkClient.Server(TCommand(Command), Player, Subject, Data); 54 end; 51 55 52 56 procedure Client(Command, Player: Integer; var Data); … … 78 82 { TNetworkClient } 79 83 80 procedure TNetworkClient.AppendStream(Stream: TStream; SourceStream: TStream); 81 var 82 Buffer: array of Byte; 83 ReadCount: Integer; 84 Base: Integer; 85 const 86 ChunkSize = 4096; 84 procedure TNetworkClient.DataAvailableExecute(Sender: TObject); 87 85 begin 88 SetLength(Buffer, 0); 89 Base := 0; 90 repeat 91 SetLength(Buffer, Length(Buffer) + ChunkSize); 92 ReadCount := SourceStream.Read(Buffer[Base], ChunkSize); 93 Inc(Base, ReadCount); 94 SetLength(Buffer, Base); 95 until ReadCount < ChunkSize; 96 97 if Length(Buffer) > 0 then begin 98 Stream.Position := Stream.Size; 99 Stream.Write(Buffer[0], Length(Buffer)); 100 end; 101 end; 102 103 procedure TNetworkClient.DataAvailableExecute(Sender: TObject); 104 var 105 Command: Integer; 106 Player: Integer; 107 Data: array of Byte; 108 begin 109 AppendStream(ReceiveBuffer, TCPClient.Stream); 110 ReceiveBuffer.Position := 0; 111 Command := Integer(ReceiveBuffer.ReadDWord); 112 Player := Integer(ReceiveBuffer.ReadDWord); 113 SetLength(Data, GetCommandDataSize(TCommand(Command))); 114 if Length(Data) > 0 then 115 LocalClient(Command, Player, Data[0]) 116 else LocalClient(Command, Player, nil^); 117 118 // Remove already read data from start of memory stream 119 Move(PByte(ReceiveBuffer.Memory + ReceiveBuffer.Position)^, ReceiveBuffer.Memory^, ReceiveBuffer.Size - ReceiveBuffer.Position); 120 ReceiveBuffer.SetSize(ReceiveBuffer.Size - ReceiveBuffer.Position); 86 TCPClientThread.Synchronize(TCPClientThread, DataAvailableSync); 121 87 122 88 ClientEventLoop.ClearDataAvailableNotify(DataAvailableHandle); … … 129 95 if NewState = connConnected then 130 96 DataAvailableHandle := ClientEventLoop.SetDataAvailableNotify(TCPClient.Stream.Handle, DataAvailableExecute, nil); 97 end; 98 99 procedure TNetworkClient.DataAvailableSync; 100 var 101 Command: Integer; 102 ReadCount: Integer; 103 Player: Integer; 104 Data: array of Byte; 105 begin 106 StreamAppend(ReceiveBuffer, TCPClient.Stream); 107 while ReceiveBuffer.Size >= 2 * SizeOf(Integer) do begin 108 ReceiveBuffer.Position := 0; 109 Command := Integer(ReceiveBuffer.ReadDWord); 110 Player := Integer(ReceiveBuffer.ReadDWord); 111 SetLength(Data, GetCommandDataSize(TCommand(Command))); 112 if Length(Data) > 0 then begin 113 ReadCount := ReceiveBuffer.Read(Data[0], Length(Data)); 114 SetLength(Data, ReadCount); 115 end; 116 117 // Rewrite server address received from network by local handler 118 if Command = cInitModule then begin 119 PInitModuleData(@Data[0])^.Server := LocalServer; 120 end; 121 122 if Length(Data) > 0 then 123 LocalClient(Command, Player, Data[0]) 124 else LocalClient(Command, Player, nil^); 125 126 StreamRemoveRead(ReceiveBuffer); 127 end; 131 128 end; 132 129 -
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; -
trunk/Protocol.pas
r364 r365 1722 1722 Flags: Integer; 1723 1723 end; 1724 PInitModuleData = ^TInitModuleData; 1724 1725 1725 1726 TNewGameData = record … … 1982 1983 cmShowNego: Result := SizeOf(TShowNegoData); 1983 1984 cmNewGame, cmLoadGame, cmMovie, cmNewMap: Result := SizeOf(TNewGameData); 1985 cmShowShipChange: Result := SizeOf(TShowShipChange); 1986 cmShowGreatLibTech: Result := SizeOf(Integer); 1987 cmShowCityChanged: Result := SizeOf(Integer); 1988 cmShowPeaceViolation: Result := SizeOf(Integer); 1989 cmShowMoving: Result := SizeOf(TShowMove); 1990 cmShowUnitChanged: Result := SizeOf(Integer); 1991 cmShowMissionResult: Result := SizeOf(Cardinal); 1992 cmShowAfterMove: Result := SizeOf(Integer); 1993 cmShowAfterAttack: Result := SizeOf(Integer); 1994 cmShowSupportAllianceAgainst: Result := SizeOf(Integer); 1995 cmShowCancelTreatyByAlliance: Result := SizeOf(Integer); 1996 cmShowEndContact: Result := 0; 1997 //sIntCancelTreaty: Result := SizeOf(Integer); 1984 1998 else begin 1985 1999 Result := 0;
Note:
See TracChangeset
for help on using the changeset viewer.