Changeset 365 for trunk/Network/UNetworkClient.pas
- Timestamp:
- Apr 13, 2021, 9:57:36 PM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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
Note:
See TracChangeset
for help on using the changeset viewer.