Ignore:
Timestamp:
Apr 13, 2021, 9:57:36 PM (4 years ago)
Author:
chronos
Message:
  • Modified: Improved unfinished network client data handling.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Network/UNetworkServer.pas

    r364 r365  
    2424  private
    2525    DataAvailableHandle: Pointer;
     26    ReceiveBuffer: TMemoryStream;
    2627    procedure DisconnectExecute(Sender: TObject);
     28    procedure DataAvailableSync;
     29    procedure DataAvailableExecute(Sender: TObject);
    2730  public
    2831    NetworkServer: TNetworkServer;
     
    3134    Player: TNetworkServerPlayer;
    3235    Connected: Boolean;
    33     procedure DataAvailableExecute(Sender: TObject);
    3436    procedure Run;
    3537    constructor Create;
     
    8486
    8587uses
    86   Global;
     88  Global, UNetworkCommon;
    8789
    8890procedure Client(Command, Player: integer; var Data);
     
    159161procedure TNetworkServerConnection.DisconnectExecute(Sender: TObject);
    160162begin
    161   Connected := False;
     163  {Connected := False;
    162164  if Assigned(Player) then begin
    163165    Player.Connection := nil;
    164166    Player := nil;
    165167  end;
    166 end;
    167 
    168 procedure TNetworkServerConnection.DataAvailableExecute(Sender: TObject);
     168  }
     169end;
     170
     171procedure TNetworkServerConnection.DataAvailableSync;
    169172var
    170173  Data: array of Byte;
     
    174177  Command: TCommand;
    175178begin
    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;
     198end;
     199
     200procedure TNetworkServerConnection.DataAvailableExecute(Sender: TObject);
     201begin
     202  NetworkServer.TCPServerThread.Synchronize(NetworkServer.TCPServerThread, DataAvailableSync);
     203  Sleep(10); // TODO: How to reset this event
    193204end;
    194205
     
    202213constructor TNetworkServerConnection.Create;
    203214begin
     215  ReceiveBuffer := TMemoryStream.Create;
    204216end;
    205217
     
    207219begin
    208220  if Assigned(Player) then Player.Connection := nil;
    209   NetworkServer.TCPServer.EventLoop.ClearDataAvailableNotify(DataAvailableHandle);
     221  if Assigned(DataAvailableHandle) then
     222    NetworkServer.TCPServer.EventLoop.ClearDataAvailableNotify(DataAvailableHandle);
    210223  FreeAndNil(Socket);
    211224  NetworkServer.Connections.Remove(Self);
     225  FreeAndNil(ReceiveBuffer);
    212226  inherited;
    213227end;
     
    221235  Player: TNetworkServerPlayer;
    222236  I: Integer;
     237  InitModuleData: TInitModuleData;
    223238begin
    224239  NewConnection := TNetworkServerConnection.Create;;
     
    239254    NewConnection.Player := Player;
    240255    Player.Connection := NewConnection;
    241     Player.Client(cmInitModule, -1, nil^);
     256    Player.Client(cmInitModule, Player.Id, InitModuleData);
    242257  end else AStream.Free;
    243258end;
Note: See TracChangeset for help on using the changeset viewer.