Created
April 3, 2014 01:45
-
-
Save chendo/9946868 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 [email protected]? 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