mIRC Home    About    Download    Register    News    Help

Print Thread
Joined: Oct 2005
Posts: 827
P
pouncer Offline OP
Hoopy frood
OP Offline
Hoopy frood
P
Joined: Oct 2005
Posts: 827
Code:
on *:SOCKREAD:contactlist: {
  if ($sockerr) halt
  else {
    while ($sock($sockname).rq) {
      sockread &binvar
      bwrite contacts.xml -1 -1 &binvar
    }
  }
}


how can i tell when ive reached the end of the sockread?

Joined: Mar 2004
Posts: 54
Z
Zed Offline
Babel fish
Offline
Babel fish
Z
Joined: Mar 2004
Posts: 54
When $sockbr = 0

Joined: Sep 2005
Posts: 2,881
H
Hoopy frood
Offline
Hoopy frood
H
Joined: Sep 2005
Posts: 2,881
The code you have is capable of that...

Code:
on *:SOCKREAD:contactlist: {
  if ($sockerr) halt
  else {
    while ($sock($sockname).rq) {
      sockread &binvar
      bwrite contacts.xml -1 -1 &binvar
    }
    ; When your script gets to here the socket has finished receiving data.
  }
}

Joined: Oct 2003
Posts: 3,918
A
Hoopy frood
Offline
Hoopy frood
A
Joined: Oct 2003
Posts: 3,918
Assuming you haven't sent a Connection Keep-Alive, you can check ON SOCKCLOSE to test if your socket is done processing.

That means the only code you need is:

Code:
on *:SOCKREAD:contactlist:sockread &binvar | bwrite contacts.xml -1 -1 &binvar



- argv[0] on EFnet #mIRC
- "Life is a pointer to an integer without a cast"
Joined: Jul 2008
Posts: 236
S
Fjord artisan
Offline
Fjord artisan
S
Joined: Jul 2008
Posts: 236
That's pretty optimistic of you. A few quotes from the mIRC help file might just explain a thing or two about why, sometimes, more code is "better" wink

Quote:
$sockerr is set to a value after each socket command/event and must be checked after each socket command and before processing an event to see if an error occurred.


Quote:
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. If your script doesn't read the whole buffer, the on sockread event is re-triggered if:
a) you were reading into a &binvar.
b) you were reading into a %var and there is still a $crlf terminated line in the buffer waiting to be read.

Last edited by s00p; 22/09/09 02:58 PM.
Joined: Sep 2005
Posts: 2,881
H
Hoopy frood
Offline
Hoopy frood
H
Joined: Sep 2005
Posts: 2,881
I don't think his socket is connected to a HTTP server.. "contactlist" sounds more like the messenger network, in which case sockets are keep-alive by default.

Joined: Oct 2003
Posts: 3,918
A
Hoopy frood
Offline
Hoopy frood
A
Joined: Oct 2003
Posts: 3,918
Firstly, $sockerr is irrelevant to the one line script. If there is a $sockerr, /sockread will read nothing and therefore will write nothing-- the socket will then subsequently close. It's not necessary to check this.

Secondly, "faster" is relative. Practically speaking, and given that packets are a few k each and xml/html files are on the order of a few k, this means a few extra triggers max; it shouldn't be noticeably slower.

I very much understand what the help file says. Of course rather than making assumptions about what the help file means, I prefer to test things myself:

Code:
alias testx { .remove test.txt | %xticks = $ticks | sockopen x yahoo.com 80 }
on *:SOCKOPEN:x:sockwrite -tn x GET / HTTP/1.0 | sockwrite -tn x Host: m.www.yahoo.com | sockwrite -tn x
on *:SOCKREAD:x:sockread &binvar | bwrite test.txt -1 -1 &binvar
on *:SOCKCLOSE:x:echo -a $calc($ticks - %xticks) ticks : $file(test.txt).size bytes

alias testy { .remove test.txt | %yticks = $ticks | sockopen y www.yahoo.com 80 }
on *:SOCKOPEN:y:sockwrite -tn y GET / HTTP/1.0 | sockwrite -tn y Host: m.www.yahoo.com | sockwrite -tn y
on *:SOCKREAD:y: {
  if ($sockerr) halt
  else {
    var %br = 1
    while (%br) {
      sockread &binvar
      bwrite test.txt -1 -1 &binvar
      %br = $sockbr
    }
  }
}
on *:SOCKCLOSE:y:echo -a $calc($ticks - %yticks) ticks : $file(test.txt).size bytes


Using the 1 line method, the results are:

Code:
min: 109 ticks : 10031 bytes
max: 484 ticks : 10031 bytes

The average over 20 runs is 331.55 ticks


For the loop method you get:
Code:
min: 218 ticks : 10031 bytes
max: 343 ticks : 10031 bytes

The average over 20 runs is 268.35 ticks


So it's slightly faster, yes. Significantly? 20% might be considered significant if you were downloading 1000s of pages, however in that case, the benefit of a slower script would be extra responsiveness in mIRC, since while loops block the UI from updating. In normal cases it should be barely noticeable.

Note #1: the file downloaded is on the fat end compared to other HTML pages which are usually around 2-3k, not 10k. This is already the worst case scenario for the one line method. You would get less re-triggering and variance from smaller pages like google, which yields ~180 ticks vs ~140 ticks.

Also of note is that the variance for the one line method is much wider-- the best case scenario is actually twice as fast as the best case of the loop method... draw your own conclusion from that one.

In any case, for most situations, a one liner is quite sufficient in terms of performance. The loop method is certainly a far cry from the far faster the help file claims.



- argv[0] on EFnet #mIRC
- "Life is a pointer to an integer without a cast"

Link Copied to Clipboard