## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+Max_Write_Queue_Size = 1000
Max_Identifier_Length = 32
Default_Timeout = 60 # for single socket operation
@mytime = 0 # set in start method also
@sente = nil
@watchdog_thread = nil
+ @writer_thread = nil
@main_thread = nil
+ @write_queue = Queue::new
login(str)
end
attr_accessor :name, :password, :socket, :status
- attr_accessor :protocol, :eol, :game, :mytime, :main_thread, :watchdog_thread, :game_name, :sente
+ attr_accessor :protocol, :eol, :game, :mytime, :game_name, :sente
+ attr_accessor :main_thread, :watchdog_thread, :writer_thread, :write_queue
def kill
finish
Thread::kill(@main_thread) if @main_thread
@status = "finished"
log_message(sprintf("user %s finish", @name))
Thread::kill(@watchdog_thread) if @watchdog_thread
+ Thread::kill(@writer_thread) if @writer_thread
@socket.close if (! @socket.closed?)
end
end
+ def write_safe(str)
+ @write_queue.push(str.gsub(/[\r\n]+/, @eol))
+ end
+
+ def writer
+ while (str = @write_queue.pop)
+ @socket.write_safe(str)
+ end
+ end
+
def watchdog(time)
while true
begin
@socket.write_safe('##[HELP] available commands "%%WHO", "%%CHAT str", "%%GAME game_name +", "%%GAME game_name -"')
end
- def write_safe(str)
- @socket.write_safe(str.gsub(/[\r\n]+/, @eol))
- end
-
def login(str)
str =~ /([\r\n]*)$/
@eol = $1
@watchdog_thread = Thread::start do
watchdog(Watchdog_Time)
end
+ @writer_thread = Thread::start do
+ writer()
+ end
end
def run
str = csa_1st_str
csa_1st_str = nil
end
+ if (@write_queue.size > Max_Write_Queue_Size)
+ log_warning(sprintf("write_queue of %s is %d", @name, @write_queue.size))
+ end
+
if (@status == "finished")
return
end
end
def log_warning(str)
- printf("%s message: %s\n", Time::new.to_s, str)
+ printf("%s warning: %s\n", Time::new.to_s, str)
end
def log_error(str)
while (str = client.gets_timeout(Login_Time))
begin
$mutex.lock
- Thread::kill(Thread::current) if (! str) # disconnected
str =~ /([\r\n]*)$/
eol = $1
if (good_login?(str))
client.write_safe("LOGIN:incorrect" + eol)
client.write_safe(sprintf("username %s is already connected%s", player.name, eol)) if (str.split.length >= 4)
client.close
- Thread::kill(Thread::current)
+ Thread::exit
end
end
LEAGUE.add(player)
end # login loop
if (! player)
client.close
- Thread::kill(Thread::current)
+ Thread::exit
end
log_message(sprintf("user %s login", player.name))
player.run