Skip to content

Instantly share code, notes, and snippets.

@chendo
Created April 3, 2014 01:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chendo/9946868 to your computer and use it in GitHub Desktop.
Save chendo/9946868 to your computer and use it in GitHub Desktop.
require 'celluloid/io'
require 'socket'
class Server
include Celluloid::IO
def run
@tcp = TCPServer.new('0.0.0.0', 31337)
loop do
Connection.new_link(@tcp.accept).async.run
end
end
class Connection
include Celluloid
finalizer :shutdown
def initialize(socket)
@socket = socket
end
def run
while !@socket.closed? do
@socket.write(@socket.readpartial(4096))
end
rescue EOFError, Errno::ECONNRESET
end
def shutdown
puts "Shutdown called"
@socket.close
end
end
end
class Client
def initialize(host, port)
@tcp = TCPSocket.new(host, port)
end
def write(data)
@tcp.write(data)
end
def shutdown
@tcp.close
end
end
s = Server.new
s.async.run
c = Client.new('localhost', 31337)
sleep 1
s.terminate
# Could not cleanly terminate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment