Jump to content
Sign in to follow this  
MrCamarium

I need to merge these two codes but I have little programming practice.

Recommended Posts

I have to combine these two codes, the first is for sending messages and the second for sending files:

procedure TForm1.serverExecute(AThread: TIdContext);
var
clients:tlist;
i:integer;
messaggioprelevato:string;
begin
messaggioprelevato:=AThread.Connection.IOHandler.ReadLn;
showmessage (messaggioprelevato);
clients := Server.Contexts.LockList;;
try
for i:=0 to connessi-1 do
try
TIdContext(clients.Items[i]).Connection.IOHandler.WriteLn(messaggioprelevato);
except
end;
finally
end;
Server.Contexts.UnlockList;
end;


procedure TForm1.ServerExecute(AContext: TIdContext);
var
FileName: string;
TFSFileIn: TFileStream;
begin
TThread.Synchronize(nil,
procedure
begin
Zilename := 'C:';
end);
TFSFileIn := TFileStream.Create(FileName, fmCreate);
try
AContext.Connection.IOHandler.ReadStream(TFSFileIn);
finally
TFSFileIn.Free;
end;
end;

 

Share this post


Link to post
Posted (edited)
59 minutes ago, MrCamarium said:

procedure TForm1.serverExecute(AThread: TIdContext);

Replace with magic overloaded procedure the compiler knows which to use with args passed.


procedure serverExecute(AThread: TIdContext; const aFileName); overload;
...
procedure TForm1.serverExecute(AThread: TIdContext; const aFileName);

Add second overloaded procedure.

59 minutes ago, MrCamarium said:

/// in interface
procedure ServerExecute(AContext: TIdContext; const AMessage: string); overload;
...
Implementation
procedure TForm1.ServerExecute(AContext: TIdContext; const AMessage: string);

 

 

Never Merge when better to unravel!

 

Edited by Pat Foley
typo

Share this post


Link to post

Combine can mean many things. Realistically though, if you want to do programming, you can't avoid gaining the skills necessary. 

Share this post


Link to post
Posted (edited)

The two procedures "combined". Instead of serverExecute1() and serverExecute2(), give some meaningful names.

procedure TForm1.serverExecute1(AThread: TIdContext);
var
  clients: tlist;
  i: integer;
  messaggioprelevato: string;
begin
  messaggioprelevato := AThread.Connection.IOHandler.ReadLn;
  showmessage(messaggioprelevato);
  clients := Server.Contexts.LockList;;
  try
    for i := 0 to connessi - 1 do
      try
        TIdContext(clients.Items[i]).Connection.IOHandler.WriteLn
          (messaggioprelevato);
      except
      end;
  finally
  end;
  Server.Contexts.UnlockList;
end;



procedure TForm1.serverExecute2(AContext: TIdContext);
var
  FileName: string;
  TFSFileIn: TFileStream;
begin
  TThread.Synchronize(nil,
    procedure
    begin
      Zilename := 'C:';
    end);
  TFSFileIn := TFileStream.Create(FileName, fmCreate);
  try
    AContext.Connection.IOHandler.ReadStream(TFSFileIn);
  finally
    TFSFileIn.Free;
  end;
end;



procedure TForm1.serverExecute(AThread: TIdContext);
begin
     serverExecute1(AThread);
     serverExecute2(AThread);
end;

 

Edited by toms

Share this post


Link to post

new to indy servers myself, currently working on one.

with what i know now, i'd have to say, you can't.

 

at a minimum you need some control mechanism to switch your server from string mode to recv filestream mode.

the showmessage is a no,no

Execute is not in the main thread.

 

got some demos of rolling your own packets here.

 

~q

Share this post


Link to post
15 hours ago, toms said:

The two procedures "combined". Instead of serverExecute1() and serverExecute2(), give some meaningful names.


procedure TForm1.serverExecute1(AThread: TIdContext);
var
  clients: tlist;
  i: integer;
  messaggioprelevato: string;
begin
  messaggioprelevato := AThread.Connection.IOHandler.ReadLn;
  showmessage(messaggioprelevato);
  clients := Server.Contexts.LockList;;
  try
    for i := 0 to connessi - 1 do
      try
        TIdContext(clients.Items[i]).Connection.IOHandler.WriteLn
          (messaggioprelevato);
      except
      end;
  finally
  end;
  Server.Contexts.UnlockList;
end;



procedure TForm1.serverExecute2(AContext: TIdContext);
var
  FileName: string;
  TFSFileIn: TFileStream;
begin
  TThread.Synchronize(nil,
    procedure
    begin
      Zilename := 'C:';
    end);
  TFSFileIn := TFileStream.Create(FileName, fmCreate);
  try
    AContext.Connection.IOHandler.ReadStream(TFSFileIn);
  finally
    TFSFileIn.Free;
  end;
end;



procedure TForm1.serverExecute(AThread: TIdContext);
begin
     serverExecute1(AThread);
     serverExecute2(AThread);
end;

 

Great! I didn't know this system could be used, I thought that the system should only be used with TButtons. Thank you!

Share this post


Link to post
18 hours ago, David Heffernan said:

Combining can mean many things. Realistically, however, if you want to do programming, you cannot avoid acquiring the necessary skills.

Thank you for your advice, on a scale between 1 - 10 I am 2 with Delphi. Mainly I deal with electronics, I do programming in my spare time, I would like to learn and I must say that in the last few years I have learned new things about Delphi, but if I succeed it is only with your help.

Share this post


Link to post
17 hours ago, toms said:

      try         TIdContext(clients.Items[i]).Connection.IOHandler.WriteLn           (messaggioprelevato);       except       end;

Not sure about the exception swallowing here

Share this post


Link to post
Posted (edited)

I tried this method:

procedure TForm1.serverExecute(AThread: TIdContext);
begin
     serverExecute1(AThread);
     serverExecute2(AThread);
end;

Even if it works it gives me problems. So to explain me better with the code:

procedure TForm1.serverExecute(AThread: TIdContext);
var
  clients: tlist;
  i: integer;
  messaggioprelevato: string;
begin
  messaggioprelevato := AThread.Connection.IOHandler.ReadLn;
  showmessage(messaggioprelevato);
  clients := Server.Contexts.LockList;;
  try
    for i := 0 to connessi - 1 do
      try
        TIdContext(clients.Items[i]).Connection.IOHandler.WriteLn
          (messaggioprelevato);
      except
      end;
  finally
  end;
  Server.Contexts.UnlockList;
end;

I can remotely send messages. Instead with the code:

procedure TForm1.ServerExecute(AContext: TIdContext);
var
FileName: string;
TFSFileIn: TFileStream;
begin
TThread.Synchronize(nil,
procedure
begin
Label1.Caption := 'Arriva qualcosa...';
Filename := 'C:';
end);
TFSFileIn := TFileStream.Create(FileName, fmCreate);
try
AContext.Connection.IOHandler.ReadStream(TFSFileIn);
finally
TFSFileIn.Free;
end;
end;

I can send files to a remote pc. Now the problem is that I can only use procedures once:

Form1.ServerExecute1 (AContext: TIdContext);

So the solution is to implement both functions in one code, but I was only able to do this:

procedure TForm1.serverExecute(AThread: TIdContext);
var
  clients: tlist;
  i: integer;
  messaggioprelevato, FileName: string;
  TFSFileIn: TFileStream;

 

Edited by MrCamarium

Share this post


Link to post

Assembling the code like this:

procedure TForm1.serverExecute(AThread: TIdContext);
var
  clients: tlist;
  i: integer;
  messaggioprelevato, FileName: string;
  TFSFileIn: TFileStream;
begin
  messaggioprelevato := AThread.Connection.IOHandler.ReadLn;
  showmessage(messaggioprelevato);
  clients := Server.Contexts.LockList;;
try
for i := 0 to connessi - 1 do
try
  TIdContext(clients.Items[i]).Connection.IOHandler.WriteLn
  (messaggioprelevato);
except
end;
finally
begin
  TThread.Synchronize(nil,
procedure
begin
  showmessage('Arriva qualcosa...');
  Filename := 'C:';
end);
  TFSFileIn := TFileStream.Create(FileName, fmCreate);
try
  AContext.Connection.IOHandler.ReadStream(TFSFileIn);
finally
  TFSFileIn.Free;
end;
end;
end;
  Server.Contexts.UnlockList;
end;

It gives me this error: [DCC Error] Unit1.pas (59): E2003 Undeclared identifier: 'AContext'

Share this post


Link to post
Quote

Give me six hours to chop down a tree and I will spend the first four sharpening the axe. Abe Lincoln

That means to a painter read the directions on can One gallon to 100 square feet.  To a pilot ground school.  To a programmer a little knowledge is a dangerous thing--drink deep or drink not*. I try make a game of it, reading a chapter out of a book, write some code in notepad, and if runs without error I win big and even a few errors is a win when the coding structure is remembered readily. That how I sharpened my axe in the olden days now I use notepad++ 🙂    

 

*A quote quoted in Pascal with Excellence.

Share this post


Link to post
4 hours ago, MrCamarium said:

It gives me this error: [DCC Error] Unit1.pas (59): E2003 Undeclared identifier: 'AContext'

That message is clear. At least it is clear to me. Do you understand it.

 

It's very hard to express this without sounding mean, but you aren't going to succeed in your goals without first getting the appropriate tools. Which in this case means more programming skills. You said yourself that these are lacking. Stop trying to solve problems that are beyond your current capacity, and start seeking to gain the skills you need.

Share this post


Link to post

the two codes I used were working, in making the change it gives me the error I mentioned.

Share this post


Link to post
47 minutes ago, MrCamarium said:

the two codes I used were working, in making the change it gives me the error I mentioned.

Start by reading the error message and thinking about what it means. Then look up the documentation for the error message. Don't give up so easily. Just because you made a mistake, don't assume that you can't solve it yourself. Think harder. Keep trying. 

Share this post


Link to post
procedure TForm1.serverExecute(AContext: TIdContext);
var
  clients: tlist;
  i: integer;
  messaggioprelevato, FileName: string;
  TFSFileIn: TFileStream;
begin
  messaggioprelevato := AContext.Connection.IOHandler.ReadLn;
//send this magic string just before you send file stream
if messaggioprelevato <>'!FILE#' then
  begin
  clients := Server.Contexts.LockList;
try
for i := 0 to connessi - 1 do
  TIdContext(clients.Items[i]).Connection.IOHandler.WriteLn
  (messaggioprelevato);
finally
  Server.Contexts.UnlockList;
end;

end else
   begin
  //recv file stream
  TThread.Synchronize(nil,
procedure
begin
  showmessage('Arriva qualcosa...');
  Filename := 'C:';
end);

  TFSFileIn := TFileStream.Create(FileName, fmCreate);
try
  AContext.Connection.IOHandler.ReadStream(TFSFileIn);
finally
  TFSFileIn.Free;
end;
end;
end;

good luck..

Share this post


Link to post

Just as well run a Nick Hodges Nu 7 on it    Control-d  "to save time formatting"  if you spent the money for chain saw.

procedure TForm1.serverExecute(AContext: TIdContext);
var
  clients: tlist;
  i: integer;
  messaggioprelevato, FileName: string;
  FileStreamIn: TFileStream;
begin
  messaggioprelevato := AContext.Connection.IOHandler.ReadLn;
  // send this magic string just before you send file stream
  if messaggioprelevato <> '!FILE#' then
  begin
    clients := Server.Contexts.LockList;
    try
      for i := 0 to connessi - 1 do
        TIdContext(clients.Items[i]).Connection.IOHandler.WriteLn
          (messaggioprelevato);
    finally
      Server.Contexts.UnlockList;
    end;

  end
  else
  begin
    // recv file stream
    TThread.Synchronize(nil,
      procedure
      begin
        showmessage('Arriva qualcosa...');
        FileName := 'C:' //suspect drive name in Windows
      end);

    FileStreamIn := TFileStream.Create(FileName, fmCreate);
    try
      AContext.Connection.IOHandler.ReadStream(aFileStreamIn);
    finally
      FileStreamIn.Free;
    end;
  end;
end;

  

 

 

Share this post


Link to post

Sorry lost color when I tried different names in the code in Notepad++.    

 

Share this post


Link to post
procedure TForm1.serverExecute(AContext: TIdContext);
var
  clients: tlist;
  i: integer;
  messaggioprelevato, FileName: string;
  FileStreamIn: TFileStream;
begin
  messaggioprelevato := AContext.Connection.IOHandler.ReadLn;
  // send this magic string just before you send file stream
  if messaggioprelevato <> '!FILE#' then
  begin
    clients := Server.Contexts.LockList;
    try
      for i := 0 to connessi - 1 do
        TIdContext(clients.Items[i]).Connection.IOHandler.WriteLn
          (messaggioprelevato);
    finally
      Server.Contexts.UnlockList;
    end;

  end
  else
  begin
    // recv file stream
    TThread.Synchronize(nil,
      procedure
      begin
        showmessage('Arriva qualcosa...');
        FileName := 'C:' //suspect drive name in Windows
      end);

    FileStreamIn := TFileStream.Create(FileName, fmCreate);
    try
      AContext.Connection.IOHandler.ReadStream(aFileStreamIn);
    finally
      FileStreamIn.Free;
    end;
  end;
end;

there, much better.. only gots notepad, not sure what the ++ is about..

lol, completely untested i should have added..

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×