Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: m0wfo/foxbat
base: f94ad05497
...
head fork: m0wfo/foxbat
compare: bf4d44bd87
Checking mergeability… Don't worry, you can still create the pull request.
  • 2 commits
  • 1 file changed
  • 0 commit comments
  • 1 contributor
Showing with 48 additions and 50 deletions.
  1. +48 −50 lib/em/connection.rb
View
98 lib/em/connection.rb
@@ -72,7 +72,7 @@ def read_channel(buffer=nil)
@block.call(self)
if buffer.nil?
- if @ssl_engine
+ if @secure
return read_ssl_channel
else
bb = ByteBuffer.allocate(BUF_SIZE)
@@ -102,22 +102,15 @@ def read_channel(buffer=nil)
private
- def app_buffer
- buf = ByteBuffer.allocate(@app_buf)
+ def new_buffer(size)
+ buf = ByteBuffer.allocate(size)
buf.clear
buf
end
- def net_buffer
- buf = ByteBuffer.allocate(@net_buf)
- buf.clear
- buf
- end
-
def read_ssl_channel(n_b=nil, a_b=nil, &block)
- setup_ssl
- n_b ||= net_buffer()
- a_b ||= app_buffer()
+ n_b ||= new_buffer(@net_buf)
+ a_b ||= new_buffer(@app_buf)
ssl_reader = Foxbat::Handler.new(@channel) do |c,br|
if br == -1
@@ -125,14 +118,10 @@ def read_ssl_channel(n_b=nil, a_b=nil, &block)
self.unbind
else
n_b.flip
- if @active
- p 'active'
+ if block_given?
+ block.call(n_b, a_b)
else
- if block_given?
- block.call(n_b, a_b)
- else
- handshake(n_b, a_b)
- end
+ handshake(n_b, a_b)
end
end
end
@@ -140,35 +129,46 @@ def read_ssl_channel(n_b=nil, a_b=nil, &block)
@channel.read(n_b, nil, ssl_reader)
end
+ # ByteBuffer -> String
def btos(buf)
return String.from_java_bytes(buf.array[buf.position..(buf.limit-1)])
end
- def handshake(n_b=nil, a_b=nil)
- n_b ||= net_buffer
- a_b ||= app_buffer
-
+ def transfer_data
+ p 'done!'
+ read_ssl_channel do |net,app|
+ if @ssl_engine.getHandshakeStatus == HandshakeStatus::NOT_HANDSHAKING
+ res = @ssl_engine.unwrap(net, app)
+ handle_result(res, :ok => lambda {
+ app.compact
+ p res.to_s
+ p btos(app)
+ })
+ else
+ handshake(net, app)
+ end
+ end
+ end
+
+ def handshake(n_b=nil, a_b=nil, done=false)
case @ssl_engine.getHandshakeStatus
when HandshakeStatus::NEED_TASK
p 'handshake tasks remaining'
task = @ssl_engine.getDelegatedTask
- while !task.nil?
- task.run
- task = @ssl_engine.getDelegatedTask
- end
+ task.run if !task.nil?
handshake(n_b, a_b)
when HandshakeStatus::NEED_WRAP
p 'wrap'
+ n_b.clear
res = @ssl_engine.wrap(a_b, n_b)
handle_result(res,
:ok => lambda {
- p 'ok'
+ p 'wrap ok'
n_b.flip
send_ssl_data(n_b)
- handshake(n_b)
- },
- :overflow => lambda { p 'ovf'; n_b.compact; handshake(n_b) },
- :finished => lambda { p ':-)' })
+ finished = (res.getHandshakeStatus == HandshakeStatus::FINISHED)
+ handshake(n_b, a_b, finished)
+ })
when HandshakeStatus::NEED_UNWRAP
res = @ssl_engine.unwrap(n_b, a_b)
@@ -183,29 +183,27 @@ def handshake(n_b=nil, a_b=nil)
when HandshakeStatus::NOT_HANDSHAKING
p 'not handshaking'
- res = @ssl_engine.unwrap(n_b, a_b)
- handle_result(res,
- :ok => lambda { handshake(n_b, a_b) })
+ if done == true
+ transfer_data
+ else
+ res = @ssl_engine.unwrap(n_b, a_b)
+ handle_result(res,
+ :ok => lambda { handshake(n_b, a_b) })
+ end
end
end
# Handler for SSLEngine results
def handle_result(result, options={})
- if result.getHandshakeStatus == HandshakeStatus::FINISHED
- p "handshake finished."
- @active = true
- read_ssl_channel
- else
- case result.getStatus # SSLEngineResult
- when Status::OK
- (options[:ok] || lambda { p 'ok' }).call
- when Status::BUFFER_OVERFLOW
- (options[:overflow] || lambda { p 'overflow' }).call
- when Status::BUFFER_UNDERFLOW
- (options[:underflow] || lambda { p 'underflow' }).call
- when Status::CLOSED
- (options[:closed] || lambda { p 'closed' }).call
- end
+ case result.getStatus # SSLEngineResult
+ when Status::OK
+ (options[:ok] || lambda { p 'ok' }).call
+ when Status::BUFFER_OVERFLOW
+ (options[:overflow] || lambda { p 'overflow' }).call
+ when Status::BUFFER_UNDERFLOW
+ (options[:underflow] || lambda { p 'underflow' }).call
+ when Status::CLOSED
+ (options[:closed] || lambda { p 'closed' }).call
end
end

No commit comments for this range

Something went wrong with that request. Please try again.