Author Topic: lnet TCP server - OnCanSend  (Read 259 times)


  • New Member
  • *
  • Posts: 15
lnet TCP server - OnCanSend
« on: April 24, 2023, 11:19:33 am »
I have created a class that wraps the lnet TLTCPComponent to provide a set of functions to simplify Tx/Rx to/from clients.

The "standard" Send function for my server works OK
Code: Pascal  [Select][+][-]
  1. procedure TDemo.Send(const AValue array of byte)
  2. var
  3.   n: integer;
  4. begin
  5.   MySocket.IterReset;
  6.   while MySocket.IterNext do  begin
  7.       n := MySocket.Send(AValue[0], Length(AValue), MySocket.Iterator);
  8.   end;
  9. end;
As expected this sends the byte arrays to each connected client. So far so good.
The problem I have is when one (or more) of the clients can't accept data at the rate I'm sending it (up to 200kByte per sec, generated in 2kByte chunks). I can't change the clients so need to cope with this problem at the server end.

I'm aware that I need to more towards using the OnCanSend event but I've been unable to find a decent example that works with the server end of the connection.
I believe I can do some thing like the following:
Code: Pascal  [Select][+][-]
  1. TxBuffer: Array of byte;
  2. P: integer;
  3. procedure TDemo.Send(const AValue array of byte);var  n: integer;
  4. begin  MySocket.IterReset;  while MySocket.IterNext do  begin    SetLength(TxBuffer, Length(AValue));
  5.     Move(AValue[0], TxBuffer[0], Length(AValue));    P := 0;
  6.     n := DoOnCanSend(MySocket.Iterator);
  7.   end;
  8. end;
Where TxBuffer is used
Code: Pascal  [Select][+][-]
  1. procedure TDemo.DoOnCanSend(aSocket: TLSocket);var  n: integer;
  2. begin  repeat  begin    if (Length(TxBuffer) > 0) and (P < Length(TxBuffer) then    begin
  3.       n := MySocket.Send(TxBuffer[P], Length(TxBuffer) - P, aSocket);
  4.       inc(P, n);
  5.     end
  6.   end
  7.   until (n = 0) or (P >= Length(TxBuffer));
  8. end;
Where I'm struggling is to manage the sending the data to all the connected clients. Do I need to create an instance of TxBuffer for each client?

Suggestions to good server examples welcome!

Dephi Dino.


TinyPortal © 2005-2018