Module: Rex::IO::StreamServer

Included in:
Socket::TcpServer
Defined in:
lib/rex/io/stream_server.rb

Overview

This mixin provides the framework and interface for implementing a streaming server that can listen for and accept stream client connections. Stream servers extend this class and are required to implement the following methods:

accept
fd

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#client_waiterObject

Returns the value of attribute client_waiter.



134
135
136
# File 'lib/rex/io/stream_server.rb', line 134

def client_waiter
  @client_waiter
end

#clientsObject

:nodoc:



132
133
134
# File 'lib/rex/io/stream_server.rb', line 132

def clients
  @clients
end

#clients_threadObject

:nodoc:



133
134
135
# File 'lib/rex/io/stream_server.rb', line 133

def clients_thread
  @clients_thread
end

#listener_threadObject

:nodoc:



133
134
135
# File 'lib/rex/io/stream_server.rb', line 133

def listener_thread
  @listener_thread
end

#on_client_close_procObject

This callback procedure can be set and will be called when a client disconnects from the server.



130
131
132
# File 'lib/rex/io/stream_server.rb', line 130

def on_client_close_proc
  @on_client_close_proc
end

#on_client_connect_procObject

This callback procedure can be set and will be called when new clients connect.



120
121
122
# File 'lib/rex/io/stream_server.rb', line 120

def on_client_connect_proc
  @on_client_connect_proc
end

#on_client_data_procObject

This callback procedure can be set and will be called when clients have data to be processed.



125
126
127
# File 'lib/rex/io/stream_server.rb', line 125

def on_client_data_proc
  @on_client_data_proc
end

Instance Method Details

#close_client(client) ⇒ Object

This method closes a client connection and cleans up the resources associated with it.



92
93
94
95
96
97
98
99
100
101
# File 'lib/rex/io/stream_server.rb', line 92

def close_client(client)
	if (client)
		clients.delete(client)

		begin
			client.close
		rescue IOError
		end
	end
end

#on_client_close(client) ⇒ Object

This callback is notified when a client connection has closed.



54
55
56
57
58
# File 'lib/rex/io/stream_server.rb', line 54

def on_client_close(client)
	if (on_client_close_proc)
		on_client_close_proc.call(client)
	end
end

#on_client_connect(client) ⇒ Object

This callback is notified when a client connects.



35
36
37
38
39
# File 'lib/rex/io/stream_server.rb', line 35

def on_client_connect(client)
	if (on_client_connect_proc)
		on_client_connect_proc.call(client)
	end
end

#on_client_data(client) ⇒ Object

This callback is notified when a client connection has data that needs to be processed.



45
46
47
48
49
# File 'lib/rex/io/stream_server.rb', line 45

def on_client_data(client)
	if (on_client_data_proc)
		on_client_data_proc.call(client)
	end
end

#startObject

Start monitoring the listener socket for connections and keep track of all client connections.



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rex/io/stream_server.rb', line 64

def start
	self.clients = []
	self.client_waiter = ::Queue.new

	self.listener_thread = Rex::ThreadFactory.spawn("StreamServerListener", false) {
		monitor_listener
	}
	self.clients_thread = Rex::ThreadFactory.spawn("StreamServerClientMonitor", false) {
		monitor_clients
	}
end

#stopObject

Terminates the listener monitoring threads and closes all active clients.



79
80
81
82
83
84
85
86
# File 'lib/rex/io/stream_server.rb', line 79

def stop
	self.listener_thread.kill
	self.clients_thread.kill

	self.clients.each { |cli|
		close_client(cli)
	}
end

#waitObject

This method waits on the server listener thread



106
107
108
# File 'lib/rex/io/stream_server.rb', line 106

def wait
	self.listener_thread.join if self.listener_thread
end