Sha256: 3dc60b999002ae40f05badeb569b1bca75463fa87964fd10048f29e3439eafb9

Contents?: true

Size: 1.22 KB

Versions: 4

Compression:

Stored size: 1.22 KB

Contents

#!/usr/bin/env ruby

$LOAD_PATH.unshift File.expand_path("../../lib", __dir__)

require 'async'
require 'async/io/trap'
require 'async/io/host_endpoint'
require 'async/io/stream'

endpoint = Async::IO::Endpoint.tcp('localhost', 4578)

interrupt = Async::IO::Trap.new(:INT)

Async do |top|
	interrupt.install!
	
	endpoint.bind do |server, task|
		Async.logger.info(server) {"Accepting connections on #{server.local_address.inspect}"}
		
		task.async do |subtask|
			interrupt.wait
			
			Async.logger.info(server) {"Closing server socket..."}
			server.close
			
			interrupt.default!
			
			Async.logger.info(server) {"Waiting for connections to close..."}
			subtask.sleep(4)
			
			Async.logger.info(server) do |buffer|
				buffer.puts "Stopping all tasks..."
				task.print_hierarchy(buffer)
				buffer.puts "", "Reactor Hierarchy"
				task.reactor.print_hierarchy(buffer)
			end
			
			task.stop
		end
		
		server.listen(128)
		
		server.accept_each do |peer|
			stream = Async::IO::Stream.new(peer)
			
			while chunk = stream.read_partial
				Async.logger.debug(self) {chunk.inspect}
				stream.write(chunk)
				stream.flush
				
				Async.logger.info(server) do |buffer|
					task.reactor.print_hierarchy(buffer)
				end
			end
		end
	end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
async-io-1.27.2 examples/echo/server.rb
async-io-1.27.1 examples/echo/server.rb
async-io-1.27.0 examples/echo/server.rb
async-io-1.26.0 examples/echo/server.rb