TWinsock2TcpListener.DoExecute test for data available

General discussion about anything related to Ultibo.
mark
Posts: 1325
Joined: Mon Oct 03, 2016 2:12 am
Location: Indianapolis, US

TWinsock2TcpListener.DoExecute test for data available

Postby mark » Fri Dec 28, 2018 2:27 am

I am in a processing loop in TWinsock2TcpListener.DoExecute and I'd like to check for new data received on the socket similar to http://synapse.ararat.cz/doc/help/blcks ... aitingData

Is such a function available? I have seen the posts on using select and other multithreading tools but I would just like to poll in my current design. Thanks, Mark.
pjde
Posts: 444
Joined: Tue Feb 09, 2016 6:21 am
Location: Sydney, Australia

Re: TWinsock2TcpListener.DoExecute test for data available

Postby pjde » Fri Dec 28, 2018 8:27 pm

Hi Mark

This is what I do.
Don't know if it helps.

Code: Select all

 
function TMyListener.DoExecute (aThread: TWinsock2TCPServerThread): Boolean;
var
  d : boolean;
  b : array [0..255] of byte;
  c : integer;
  closed : boolean;     
begin 
  Result := inherited DoExecute (aThread);
  if not Result then exit;
  c := 256;
  closed := false;
  d := aThread.Server.ReadAvailable (@b[0], 255, c, closed);
  if closed or not d then Result := false;
  if (c = 0) or closed then exit;
  .....
 



Regards

Paul
mark
Posts: 1325
Joined: Mon Oct 03, 2016 2:12 am
Location: Indianapolis, US

Re: TWinsock2TcpListener.DoExecute test for data available

Postby mark » Fri Dec 28, 2018 9:04 pm

pjde wrote:Hi Mark

This is what I do.
Don't know if it helps.

Code: Select all

 
function TMyListener.DoExecute (aThread: TWinsock2TCPServerThread): Boolean;
var
  d : boolean;
  b : array [0..255] of byte;
  c : integer;
  closed : boolean;     
begin 
  Result := inherited DoExecute (aThread);
  if not Result then exit;
  c := 256;
  closed := false;
  d := aThread.Server.ReadAvailable (@b[0], 255, c, closed);
  if closed or not d then Result := false;
  if (c = 0) or closed then exit;
  .....
 



Regards

Paul
Yes that's just what I need. Thanks.
User avatar
Ultibo
Site Admin
Posts: 2224
Joined: Sat Dec 19, 2015 3:49 am
Location: Australia

Re: TWinsock2TcpListener.DoExecute test for data available

Postby Ultibo » Fri Dec 28, 2018 11:52 pm

mark wrote:I am in a processing loop in TWinsock2TcpListener.DoExecute and I'd like to check for new data received on the socket similar to http://synapse.ararat.cz/doc/help/blcks ... aitingData

Paul's solution is ideal because it allows the thread to wait for data to be received without consuming any CPU time and will also return cleanly if the socket is closed or any error occurs.

But if you do need something exactly like WaitingData from the Synapse TBlockSocket class then let us know because it is easy to do as well using IoctlSocket and FIONREAD however you have to make your own arrangements for yielding the CPU while waiting and checking for closed sockets or socket errors.
Ultibo.org | Make something amazing
https://ultibo.org
mark
Posts: 1325
Joined: Mon Oct 03, 2016 2:12 am
Location: Indianapolis, US

Re: TWinsock2TcpListener.DoExecute test for data available

Postby mark » Sat Dec 29, 2018 5:22 am

Ultibo wrote:
mark wrote:I am in a processing loop in TWinsock2TcpListener.DoExecute and I'd like to check for new data received on the socket similar to http://synapse.ararat.cz/doc/help/blcks ... aitingData

Paul's solution is ideal because it allows the thread to wait for data to be received without consuming any CPU time and will also return cleanly if the socket is closed or any error occurs.

But if you do need something exactly like WaitingData from the Synapse TBlockSocket class then let us know because it is easy to do as well using IoctlSocket and FIONREAD however you have to make your own arrangements for yielding the CPU while waiting and checking for closed sockets or socket errors.


Thanks for the offer Garry, but I will just use ReadAvailable as Paul showed. Mark.

Return to “Discussion”

Who is online

Users browsing this forum: No registered users and 0 guests