Created
October 21, 2011 03:53
-
-
Save Zapotek/1303058 to your computer and use it in GitHub Desktop.
EventMachine bug: Incomplete transfers when using TLS with large data.
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 'eventmachine' | |
module EventMachine::Protocols::ObjectProtocol | |
def serializer | |
Marshal | |
end | |
def receive_data data | |
(@buf ||= '') << data | |
p "------ got data: #{data.size} bytes" | |
while @buf.size >= 4 | |
sz = @buf.unpack('N').first | |
perc = (@buf.size / Float( sz ) ) * 100 | |
p "buffering: #{@buf.size}/#{sz} [#{perc.to_s[0..5]}%]" | |
if @buf.size >= 4+([email protected]('N').first) | |
@buf.slice!(0,4) | |
p "done" | |
p "-----------" | |
receive_object serializer.load(@buf.slice!(0,size)) | |
else | |
break | |
end | |
end | |
end | |
def send_object obj | |
data = serializer.dump(obj) | |
p "Size: #{data.bytesize}" | |
send_data [data.respond_to?(:bytesize) ? data.bytesize : data.size, data].pack('Na*') | |
end | |
end | |
class Server < EventMachine::Connection | |
include ::EM::P::ObjectProtocol | |
def initialize( obj ) | |
@obj = obj | |
end | |
def post_init | |
start_tls | |
end | |
def receive_object( obj ) | |
send_object( @obj ) | |
end | |
end | |
class Client < EventMachine::Connection | |
include ::EM::P::ObjectProtocol | |
def post_init | |
start_tls | |
end | |
def receive_object( obj ) | |
p '----- SUCCESS ------' | |
EM.stop | |
end | |
end | |
obj = (0..30000).to_a | |
::EM.epoll | |
::EM.run do | |
::EM.start_server( '127.0.0.1', 7331, Server, obj ) | |
::EM.connect( '127.0.0.1', 7331, Client ).send_object( 'poke' ) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment