mIRC Home    About    Download    Register    News    Help

Print Thread
#103675 24/11/04 01:22 AM
Joined: Apr 2004
Posts: 66
C
Cyrex Offline OP
Babel fish
OP Offline
Babel fish
C
Joined: Apr 2004
Posts: 66
When using the on SOCKREAD event, is it necessary to use a loop to keep checking the receive buffer? I notice that many socket scripts don't bother even using a loop. I don't notice any difference using a loop or not. What can happen if I don't use a loop?

Take these two on SOCKREAD codes for example:

Example 1

Code:
 
on *:sockread:test:{
  if ($sockerr > 0) { return }
  sockread %temp
  if ($sockbr == 0) { return }
  echo -a %temp
}
 

Example 2

Code:
 
on *:sockread:test:{
  if ($sockerr > 0) { return }
  :nextread
  sockread %temp
  if ($sockbr == 0) { return }
  echo -a %temp
  goto nextread
}
 


Both do the same thing. Why use a loop?

Joined: Dec 2002
Posts: 788
C
Hoopy frood
Offline
Hoopy frood
C
Joined: Dec 2002
Posts: 788
I would imagine its just to ensure the socket keeps reading until it closes, and not just reads once.

Though, mIRC nowadays seems to do that automatically..

Eamonn.

Joined: Mar 2004
Posts: 457
D
Fjord artisan
Offline
Fjord artisan
D
Joined: Mar 2004
Posts: 457
i never use any loops in my sockread events and i've never had any problems.

Joined: Dec 2002
Posts: 127
F
Vogon poet
Offline
Vogon poet
F
Joined: Dec 2002
Posts: 127
that's probably because you aren't trying to read more text than /sockread can on one pass (4096 bytes i believe). if there is more data to be read, your single pass /sockread will just read what it can and then end.


------
deep down, i'm really superficial.
Joined: Feb 2003
Posts: 307
T
Fjord artisan
Offline
Fjord artisan
T
Joined: Feb 2003
Posts: 307
I only wait for the event to trigger, saves lots of cpu and you don't lose a single byte.

And since the binary vars can handle "almost unlimited" amounts off data i force it to read always 8k since the socket can only buffer max 16k.

From tests i have made, when the event is triggered it is very unusual to have more than 8kb waiting... unless your machine is heavily loaded by another reason...

In conclusion, reading 8k seems a very good compromise to have good performance.

Joined: Dec 2002
Posts: 1,922
O
Hoopy frood
Offline
Hoopy frood
O
Joined: Dec 2002
Posts: 1,922
Quote:
When using the on SOCKREAD event, is it necessary to use a loop to keep checking the receive buffer?


Not necessary but advisable.


Quote:
I don't notice any difference using a loop or not.


There is performance difference which I can notice here with my good old 300MHz processor. Let me quote the help file on this:

Note: A single /sockread may not be enough to read the entire buffer. You should keep reading until $sockbr (bytes read) is set to zero. This is far faster than letting mIRC re-trigger the event.

Now as far as I'm aware, a loop would speed up things only in either of these cases:
  1. You're reading into a %variable.
  2. You're using the -n switch to read one line at a time into a &variable.


Here's how I make my loops:

Code:
On *:sockread:sockname:{
  var %var
  sockread %var
  while $sockbr {
    ; process %var
    ...
    sockread %var
  }
}


Link Copied to Clipboard