Forum > Networking and Web Programming

lnet TCP server - OnCanSend

(1/1)

DelphiDinosaur:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TDemo.Send(const AValue array of byte)var  n: integer;begin  MySocket.IterReset;  while MySocket.IterNext do  begin      n := MySocket.Send(AValue[0], Length(AValue), MySocket.Iterator);  end;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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---TxBuffer: Array of byte;P: integer;procedure TDemo.Send(const AValue array of byte);var  n: integer;begin  MySocket.IterReset;  while MySocket.IterNext do  begin    SetLength(TxBuffer, Length(AValue));    Move(AValue[0], TxBuffer[0], Length(AValue));    P := 0;    n := DoOnCanSend(MySocket.Iterator);  end;end;Where TxBuffer is used
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TDemo.DoOnCanSend(aSocket: TLSocket);var  n: integer;begin  repeat  begin    if (Length(TxBuffer) > 0) and (P < Length(TxBuffer) then    begin      n := MySocket.Send(TxBuffer[P], Length(TxBuffer) - P, aSocket);      inc(P, n);    end  end  until (n = 0) or (P >= Length(TxBuffer));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.

Navigation

[0] Message Index

Go to full version