Title: [1113] trunk/jopenssl: Fix a bug about blocking when there is still work to do.
Revision
1113
Author
olabini
Date
2008-08-13 13:51:41 -0400 (Wed, 13 Aug 2008)

Log Message

Fix a bug about blocking when there is still work to do. Fix for JRUBY-2597.

Modified Paths

Diff

Modified: trunk/jopenssl/src/java/org/jruby/ext/openssl/SSLSocket.java (1112 => 1113)


--- trunk/jopenssl/src/java/org/jruby/ext/openssl/SSLSocket.java	2008-08-13 16:24:03 UTC (rev 1112)
+++ trunk/jopenssl/src/java/org/jruby/ext/openssl/SSLSocket.java	2008-08-13 17:51:41 UTC (rev 1113)
@@ -320,6 +320,7 @@
 
     private int readAndUnwrap() throws Exception {
         int bytesRead = c.read(peerNetData);
+
         if(bytesRead == -1) {
             //            engine.closeInbound();			
             if ((peerNetData.position() == 0) || (status == SSLEngineResult.Status.BUFFER_UNDERFLOW)) {
@@ -348,7 +349,7 @@
         if(status == SSLEngineResult.Status.CLOSED) {
             doShutdown();
             return -1;
-        }	
+        }
         peerNetData.compact();
         peerAppData.flip();
         if(!initialHandshake && (hsStatus == SSLEngineResult.HandshakeStatus.NEED_TASK ||
@@ -379,7 +380,6 @@
 
     @JRubyMethod(rest=true)
     public IRubyObject sysread(IRubyObject[] args) throws Exception {
-        //        System.err.println("WARNING: unimplemented method called: SSLSocket#sysread");
         org.jruby.runtime.Arity.checkArgumentCount(getRuntime(),args,1,2);
         int len = RubyNumeric.fix2int(args[0]);
         IRubyObject str = getRuntime().getNil();
@@ -392,7 +392,12 @@
         if(len == 0) {
             return str;
         }
-        waitSelect(rsel);
+
+        // So we need to make sure to only block when there is no data left to process
+        if(engine == null || !(peerAppData.hasRemaining() || peerNetData.position() > 0)) {
+            waitSelect(rsel);
+         }
+
         ByteBuffer dst = ByteBuffer.allocate(len);
         int rr = -1;
         if(engine == null) {
@@ -413,27 +418,23 @@
         if(eof){
             throw getRuntime().newEOFError();
         }
+
         str.callMethod(getRuntime().getCurrentContext(),"<<",RubyString.newString(getRuntime(), out));
         return str;
     }
 
     @JRubyMethod
     public IRubyObject syswrite(IRubyObject arg) throws Exception {
-        ///        System.err.println("WARNING: unimplemented method called: SSLSocket#syswrite");
-        //        System.err.println(type + ".syswrite(" + arg + ")");
+        waitSelect(wsel);
+        byte[] bls = arg.convertToString().getBytes();
+        ByteBuffer b1 = ByteBuffer.wrap(bls);
         if(engine == null) {
-            waitSelect(wsel);
-            byte[] bls = arg.convertToString().getBytes();
-            ByteBuffer b1 = ByteBuffer.wrap(bls);
             c.write(b1);
-            return getRuntime().newFixnum(bls.length);
         } else {
-            waitSelect(wsel);
-            byte[] bls = arg.convertToString().getBytes();
-            ByteBuffer b1 = ByteBuffer.wrap(bls);
             write(b1);
-            return getRuntime().newFixnum(bls.length);
         }
+        ((RubyIO)api.callMethod(this,"io")).flush();
+        return getRuntime().newFixnum(bls.length);
     }
 
     private void close() throws Exception {
@@ -448,8 +449,6 @@
 
     @JRubyMethod
     public IRubyObject sysclose() throws Exception {
-        //        System.err.println("WARNING: unimplemented method called: SSLSocket#sysclose");
-        //        System.err.println(type + ".sysclose");
         close();
         ThreadContext tc = getRuntime().getCurrentContext();
         if(callMethod(tc,"sync_close").isTrue()) {

Modified: trunk/jopenssl/test/openssl/test_ssl.rb (1112 => 1113)


--- trunk/jopenssl/test/openssl/test_ssl.rb	2008-08-13 16:24:03 UTC (rev 1112)
+++ trunk/jopenssl/test/openssl/test_ssl.rb	2008-08-13 17:51:41 UTC (rev 1113)
@@ -92,8 +92,6 @@
           end
           block.call(server, port.to_i)
         end
-      rescue => e
-        puts e, *(e.backtrace)
       ensure
         if server
           $stderr.puts "killing: #{pid}" if $DEBUG

Modified: trunk/jopenssl/test/test_integration.rb (1112 => 1113)


--- trunk/jopenssl/test/test_integration.rb	2008-08-13 16:24:03 UTC (rev 1112)
+++ trunk/jopenssl/test/test_integration.rb	2008-08-13 17:51:41 UTC (rev 1113)
@@ -80,4 +80,21 @@
  
     assert_equal "\253\305\306\372;\374\235\302\357/\006\360\355XO\232\312S\356* #\227\217", encrypted
   end
+  
+  def _test_perf_of_nil
+# require 'net/https'
+# require 'benchmark'
+
+# def request(data)
+#   connection = Net::HTTP.new("www.google.com", 443)
+#   connection.use_ssl = true
+#   connection.verify_mode = OpenSSL::SSL::VERIFY_NONE
+#   connection.start do |connection|
+#     connection.request_post("/tbproxy/spell?lang=en", data, { 'User-Agent' => "Test", 'Accept' => 'text/xml' })
+#   end
+# end
+
+# puts "is not: #{Benchmark.measure { request("") }.to_s.chomp}"
+# puts "is nil: #{Benchmark.measure { request(nil) }.to_s.chomp}"
+  end
 end
_______________________________________________
Jruby-extras-devel mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/jruby-extras-devel

Reply via email to