Re:Problem with writing the network game
This is a part of code with comments:
while fMain.IdTCPClient1.Connected do begin
fMain.Label2.Caption:=fMain.Label2.Caption+'+';// for 'mini-log' :)
fMain.IdTCPClient1.ReadStrings(UpdateInfo); // When performing
mini-log shows that program does not pass hereinafter this line
fMain.Label2.Caption:=fMain.Label2.Caption+'=';// for 'mini-log' :)
Synchronize(ServerOnUpdate);
fMain.Label2.Caption:=fMain.Label2.Caption+':';// for 'mini-log' :)
UpdateInfo.Clear;
if length(fMain.Label2.Caption)>20 then begin
fMain.Label2.Caption:='';
end;
end;
This is full code of the thread which realizes receiving
information from server (please copy-past to pas-ifle - for best viewing -
attached files not resolved on this server :( )
unit UClientThread;
interface
uses
Classes, IdTCPClient;
const
stConnected=1;
stNotConnected=2;
stDisconnected=3;
type
TClientThread = class(TThread)
State:integer;
UpdateInfo:TStrings;
procedure SetText(iText:string);
procedure OnConnected;
procedure OnNotConnected;
procedure OnDisconnected;
procedure ServerOnUpdate;
private
{ Private declarations }
protected
procedure Execute; override;
end;
var
ClientThread:TClientThread;
procedure MakeConnection;
procedure Disconnect;
procedure SendString(iString:string);
procedure SendStrings(iStrings:TStrings);
implementation
uses Main, UWorld, IdTCPConnection, SysUtils;
procedure MakeConnection;
begin
if Assigned(ClientThread) then begin
Exit;
end;
ClientThread := TClientThread.Create(True);
ClientThread.FreeOnTerminate := True;
ClientThread.Priority := tpNormal;
ClientThread.Resume;
end;
procedure Disconnect;
begin
if Assigned(ClientThread) then begin
fMain.IdTCPClient1.DisconnectSocket;
ClientThread.OnDisconnected;
ClientThread.Terminate;
end;
end;
procedure SendString(iString:string);
var
Command:TStrings;
begin
if ClientThread.State<>stConnected then begin
Exit;
end;
Command:=TStringList.Create;
Command.Add(iString);
fMain.IdTCPClient1.WriteStrings(Command,true);
Command.Free;
//fMain.IdTCPClient1.WriteLn(iString);
end;
procedure SendStrings(iStrings:TStrings);
begin
if ClientThread.State<>stConnected then begin
Exit;
end;
fMain.IdTCPClient1.WriteStrings(iStrings,true);
end;
{ Important: Methods and properties of objects in visual components can only
be
used in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure TClientThread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }
{ TClientThread }
procedure TClientThread.Execute;
begin
{ Place thread code here }
try
fMain.IdTCPClient1.Connect(); // Подключаемся ?сервер?
except
Synchronize(OnNotConnected);
Exit;
end;
Synchronize(OnConnected);
UpdateInfo:=TStringList.Create;
with fMain do begin
while fMain.IdTCPClient1.Connected do begin
fMain.Label2.Caption:=fMain.Label2.Caption+'+';
fMain.IdTCPClient1.ReadStrings(UpdateInfo); // Принимае?данные ?
сервер?
fMain.Label2.Caption:=fMain.Label2.Caption+'=';
Synchronize(ServerOnUpdate);
fMain.Label2.Caption:=fMain.Label2.Caption+':';
UpdateInfo.Clear;
if length(fMain.Label2.Caption)>20 then begin
fMain.Label2.Caption:='';
end;
end;
end;
Synchronize(OnDisconnected);
{repeat
until Terminated;}
end;
procedure TClientThread.ServerOnUpdate;
begin
World.UpdateFromServer(UpdateInfo);
end;
procedure TClientThread.SetText(iText: string);
begin
fMain.Label1.Caption:=iText;
end;
procedure TClientThread.OnConnected;
begin
State:=stConnected;
SetText('Подключени?прошло успешн?');
end;
procedure TClientThread.OnNotConnected;
begin
State:=stNotConnected;
SetText('Подключиться не удалос?');
end;
procedure TClientThread.OnDisconnected;
begin
State:=stDisconnected;
SetText('Соединение завершен?');
end;
end.