0 Members and 3 Guests are viewing this topic.
set server_host localhostset server_cmd_port 23073bind pub - !con server_connectbind pub - !dc server_disconnectbind pub - !send server_sendproc read_server_sock {ssock schan} { set each_line [gets $ssock] putserv "PRIVMSG $schan :$each_line"}proc server_connect {nick uhost hand chan arg} { global server_host server_cmd_port server_sock set server_sock [socket $server_host $server_cmd_port] fileevent $server_sock readable [list read_server_sock $server_sock $chan] fconfigure $server_sock -buffering line}proc server_disconnect {nick uhost hand chan arg} { global server_sock close $server_sock putquick "PRIVMSG $chan :disconnected from the server..."}proc server_send {nick uhost hand chan arg} { global server_sock if {$arg != ""} { puts $server_sock $arg flush $server_sock }}
bind pub - !con server_connectbind pub - !dc server_disconnectbind pub - !send server_sendbind pub - !info send_infoproc read_server_sock {ssock schan} { set each_line [gets $ssock] putserv "PRIVMSG $schan :$each_line"}proc server_connect {nick uhost hand chan arg} { global server_host server_cmd_port server_sock set server_sock [socket $server_host $server_cmd_port] fileevent $server_sock readable [list read_server_sock $server_sock $chan] fconfigure $server_sock -buffering lineutimer 1 ref}proc server_disconnect {nick uhost hand chan arg} { global server_sock close $server_sock putquick "PRIVMSG $chan :disconnected from the server..."}proc server_send {nick uhost hand chan arg} { global server_sock array set info [Soldat:ParseRefresh $server_sock] if {$arg != ""} { puts $server_sock $arg flush $server_sock }}proc send_info {nick uhost hand chan arg} {global infoputquick "PRIVMSG $chan :gamemode : $info(gamemode) tlimit: $info(timelimit)"}proc ref {} { global server_sockglobal info array set info [Soldat:ParseRefresh $server_sock]utimer 1 ref}putlog "soldat-admin.tcl ver 0.1 by tomekk loaded" proc Soldat:ParseRefresh packet {if { [string length $packet] != 1188 } {return}binary scan $packet "@800c32s32s32c32c32I32s4ca16iisc" teams kills deaths pings numbers ips scores map_len map timelimit timeleft limit gamemodeset players {}set specs {}for { set i 0 } { $i < 32 } { incr i } {set team [lindex $teams $i]if {$team != -1} {binary scan $packet "@[expr $i*25]ca24" name_len nameset ip [lindex $ips $i]set ip [join [list [expr ($ip >> 24) & 0xFF] [expr ($ip >> 16) & 0xFF] [expr ($ip >> 8) & 0xFF] [expr $ip & 0xFF]] .]array set player [list id [expr [lindex $numbers $i] & 0xFF] \list name [string range $name 0 [expr $name_len-1]] \list team $team \list kills [expr [lindex $kills $i] & 0xFFFF] \list deaths [expr [lindex $deaths $i] & 0xFFFF] \list ping [expr [lindex $pings $i] & 0xFF] \list ip $ip]if {$team < 5} {lappend players [array get player]} else {lappend specs [array get player]}}}for { set i 0 } { $i < 4 } { incr i } {set scores [lreplace $scores $i $i [expr [lindex $scores $i] & 0xFFFF]]}array set info [list gamemode $gamemode \list num_players [llength $players] \list num_specs [llength $specs] \list map [string range $map 0 [expr $map_len-1]] \list scores $scores \list limit $limit \list timelimit [expr $timelimit / 3600] \list timeleft [expr $timeleft / 60] \list players $players \list specs $specs]return [array get info]}
bind pub - !con server_connectbind pub - !dc server_disconnectbind pub - !send server_sendbind pub - !info send_infoset server_host localhostset server_cmd_port 23073set i 0proc read_server_sock {ssock schan} {global i set each_line [gets $ssock] putserv "PRIVMSG $schan :$each_line"if {$i == 2} {set refresh $each_lineset i 0putserv "PRIVMSG #zomfgpl : Length of variable [string length $refresh]"}if {$i == 1} {set i [expr $i + 1]}}proc server_connect {nick uhost hand chan arg} { global server_host server_cmd_port server_sock set server_sock [socket $server_host $server_cmd_port] fileevent $server_sock readable [list read_server_sock $server_sock $chan] fconfigure $server_sock -buffering lineutimer 1 ref}proc server_disconnect {nick uhost hand chan arg} { global server_sock close $server_sock putquick "PRIVMSG $chan :disconnected from the server..."}proc server_send {nick uhost hand chan arg} { global server_sockglobal i if {$arg != ""} { puts $server_sock $arg flush $server_sock } if {$arg == "REFRESH"} {set i [expr $i + 1] }}proc send_info {nick uhost hand chan arg} {global infoputquick "PRIVMSG $chan :gamemode : $info(gamemode) tlimit: $info(timelimit)"}proc ref {} { global server_sockglobal info utimer 1 ref}putlog "soldat-admin.tcl ver 0.1 by tomekk loaded"proc Soldat:ParseRefresh packet {if { [string length $packet] != 1188 } {putquick "PRIVMSG #zomfgpl : Length of variable: [string length $packet]"return}binary scan $packet "@800c32s32s32c32c32I32s4ca16iisc" teams kills deaths pings numbers ips scores map_len map timelimit timeleft limit gamemodeset players {}set specs {}for { set i 0 } { $i < 32 } { incr i } {set team [lindex $teams $i]if {$team != -1} {binary scan $packet "@[expr $i*25]ca24" name_len nameset ip [lindex $ips $i]set ip [join [list [expr ($ip >> 24) & 0xFF] [expr ($ip >> 16) & 0xFF] [expr ($ip >> 8) & 0xFF] [expr $ip & 0xFF]] .]array set player [list id [expr [lindex $numbers $i] & 0xFF] \list name [string range $name 0 [expr $name_len-1]] \list team $team \list kills [expr [lindex $kills $i] & 0xFFFF] \list deaths [expr [lindex $deaths $i] & 0xFFFF] \list ping [expr [lindex $pings $i] & 0xFF] \list ip $ip]if {$team < 5} {lappend players [array get player]} else {lappend specs [array get player]}}}for { set i 0 } { $i < 4 } { incr i } {set scores [lreplace $scores $i $i [expr [lindex $scores $i] & 0xFFFF]]}array set info [list gamemode $gamemode \list num_players [llength $players] \list num_specs [llength $specs] \list map [string range $map 0 [expr $map_len-1]] \list scores $scores \list limit $limit \list timelimit [expr $timelimit / 3600] \list timeleft [expr $timeleft / 60] \list players $players \list specs $specs]return [array get info]}
try{ // I get here with the connection entablished and the password sent already // Writer is a StreamWriter and Reader is a StreamReader. Both conencted to the NetworkSocket // At the beggining send a refresh command to get the players list. Writer.WriteLine("REFRESH"); Writer.Flush(); while (true) { string text = Reader.ReadLine(); if (text.StartsWith("/perrochat ")) continue; bool NickMatches = false; // Check if it's a player saying something. if (text[0] == '[') { foreach (string nick in Players.Values) { if (text.StartsWith("[" + nick + "]")) { NickMatches = true; break; } } } if (NickMatches) { server.SendToAll("M" + text, this); } else if (text == "REFRESH") { GetPlayers(); // This will read the next 1188 bytes and parse some data } else if (text.IndexOf(" has joined the game.") >= 0 || text.IndexOf(" has left the game.") >= 0) { // Player list might have changed so update it. Writer.WriteLine("REFRESH"); Writer.Flush(); } else if ((DateTime.Now.Ticks - LastRefresh) / TimeSpan.TicksPerSecond >= 10) { Writer.WriteLine("REFRESH"); Writer.Flush(); } }}catch{...}
byte[] packet = new byte[1188];int r = Tcp.GetStream().Read(packet, 0, 1188);// .. then I parse the data. r should be 1188 but it's not always 1188 -_-
fconfigure $sock -translation binaryset refresh [read $sock 1188]
This method reads data into the buffer parameter and returns the number of bytes successfully read. If no data is available for reading, the Read method returns 0. The Read operation reads as much data as is available, up to the number of bytes specified by the size parameter. If the remote host shuts down the connection, and all available data has been received, the Read method completes immediately and return zero bytes.
byte[] packet = new byte[1188];int BytesRead = 0;while (BytesRead < 1188) { BytesRead += Stream.Read(packet, BytesRead, 1188 - BytesRead);}
byte[] packet = new byte[1188];while (Stream.Available < 1188) { // loop}Stream.Read(packet, 0, 1188);
This ParseRefresh proc looks broken. Why are there multiple 'list' arguments to 'list' command? and also, you might want to brace expr arguments.array set player [list id [expr {[lindex $numbers $i] & 0xFF}] \list name [string range $name 0 [expr {$name_len-1}]] \list team $team \list kills [expr {[lindex $kills $i] & 0xFFFF}] \list deaths [expr {[lindex $deaths $i] & 0xFFFF}] \list ping [expr {[lindex $pings $i] & 0xFF}] \list ip $ip]The same with second [array set]:array set info [list gamemode $gamemode \list num_players [llength $players] \list num_specs [llength $specs] \list map [string range $map 0 [expr {$map_len-1}]] \list scores $scores \list limit $limit \list timelimit [expr {$timelimit / 3600}] \list timeleft [expr {$timeleft / 60}] \list players $players \list specs $specs]
The original post never did it that way, this guy must've edited it himself (I didn't look at the code so closely because I expected it's exact same as mine). I used many "array set"s before, as you can see from the post chrisgbk linked. I did it the way you suggested though, as you can see from the next post.
Quote from: ramirez on October 12, 2007, 08:27:05 amThe original post never did it that way, this guy must've edited it himself (I didn't look at the code so closely because I expected it's exact same as mine). I used many "array set"s before, as you can see from the post chrisgbk linked. I did it the way you suggested though, as you can see from the next post.Originally you used lots of array sets:array set array_name [list key value]array set array_name [list key value]array set array_name [list key value]...I suggested using single command:array set array_name [list key value key value key value ... ]But apparently you edited your original post to:array set array_name [list key value list key value ... ]Back then I didn't look at it closely either, but it's not the exact same, and has no chance to run properly.