Переглянути джерело

timeout code to write to conns to find lost conns

Paul Klumpp 12 роки тому
батько
коміт
1e3a1c109c
1 змінених файлів з 23 додано та 6 видалено
  1. 23 6
      socket_central.rb

+ 23 - 6
socket_central.rb

@@ -4,6 +4,9 @@
 require 'rubygems'
 require 'socket'
 require 'digest/md5'
+require 'timeout'
+
+
 
 # .. allowed commands .. ROLES = CAPABILITIES
 # normal users have ROLE broadcast. Roles are defined on a per-user basis .. in a config.
@@ -312,17 +315,25 @@ def thinking(c, user, t_display)
 	end
 
 	
-	write_user(user, "HELLO Hi user '#{user}'! How are you? I'm #{$version}")
+	write_user(user, "HELLO Hi user '#{user}'! How are you? I'm '#{$version}'")
 	write_user(user, "ROLES #{my_roles(user).join(", ")}")
 	write_user(user, "COMMANDS #{my_cmds(user).join(", ")}")
-	write_role("everyone", "JOINED User '#{user}' just joined the party.", user)
+	write_role($default_role, "JOINED User '#{user}' just joined the party.", user)
 	
 	# reading the client input ... to write it somewhere, possibly to the client himself.
 	begin
-		input = ""
+		input = nil
 		while input != "PART" do
-			input = c.gets.chomp
-			execution = inputting(user, input)
+			
+			begin
+				status = Timeout::timeout(90) do
+					input = c.gets.chomp # waits for user input
+					execution = inputting(user, input)
+				end
+			rescue Exception => e
+				write_user(user, "PING Still alive?") # if writing fails, then it may be a broken pipe .. so it will lose the connection and delete the user from the online user list.
+			end
+			           
 		end
 	rescue Exception => e  
 		puts e.message  
@@ -334,6 +345,10 @@ def thinking(c, user, t_display)
 	
 end
 
+def pingtimer(c, user)
+	
+end
+
 def spawn_server
 	$display_queue = Hash.new
 	$broadcasts = Hash.new
@@ -343,10 +358,11 @@ def spawn_server
 	
 	loop do
 		Thread.start(server.accept) do |c|
-			user = c.gets.chomp
+			user = c.gets.chomp # waits for user name input
 			
 			# only one connection from a user allowed - we don't handle multiple display_queues for one user!!!!!11111
 			if online_users.user(user)
+				user = c.puts "Only one connection allowed per user."
 				c.close
 			else
 				$display_queue[user] = Array.new
@@ -354,6 +370,7 @@ def spawn_server
 
 				# this thread reads what others want you to read .. 
 				t_user_display = Thread.new{ displaying(c, user) }
+				
 				begin
 					execution = thinking(c, user, t_user_display)