Update of /cvsroot/freenet/freenet/src/freenet/transport
In directory sc8-pr-cvs1:/tmp/cvs-serv2606/src/freenet/transport

Modified Files:
        tcpConnection.java 
Log Message:
d'oh...


Index: tcpConnection.java
===================================================================
RCS file: /cvsroot/freenet/freenet/src/freenet/transport/tcpConnection.java,v
retrieving revision 1.39
retrieving revision 1.40
diff -u -r1.39 -r1.40
--- tcpConnection.java  9 Sep 2003 15:04:28 -0000       1.39
+++ tcpConnection.java  9 Sep 2003 15:28:10 -0000       1.40
@@ -24,6 +24,7 @@
     private boolean shouldThrottle = false;
     private boolean shouldThrottleNow = false;
     private boolean instanceCounted = false; // if the constructor throws, WE STILL 
GET FINALIZED!
+    private static boolean logDEBUG;
     // We do not throttle in FnpLink because it slows down negotiations drastically
     // We do not directly throttle, ever, because of nio.
     // Hence throttling is implemented in *SelectorLoop, not here
@@ -147,6 +148,35 @@
     
     public static boolean logBytes = false;
     
+    private final ByteBuffer getAccumulator() {
+       if(poolBuffers) {
+           synchronized(bufferPool) {
+               if(!bufferPool.isEmpty()) {
+                   accumulator = (ByteBuffer)(bufferPool.removeFirst());
+                   if(logDEBUG)
+                       Core.logger.log(this, "Reused buffer from pool: "+
+                                       accumulator, Logger.DEBUG);
+                   
+               } else {
+                   accumulator = useDirectBuffers ?
+                       ByteBuffer.allocateDirect(BUFFER_SIZE) :
+                       ByteBuffer.allocate(BUFFER_SIZE);
+                   if(logDEBUG)
+                       Core.logger.log(this, "Allocated new buffer because pool 
empty: "+
+                                       accumulator, Logger.DEBUG);
+               }
+           }
+       } else {
+           accumulator = useDirectBuffers ?
+               ByteBuffer.allocateDirect(BUFFER_SIZE) :
+               ByteBuffer.allocate(BUFFER_SIZE);
+           if(logDEBUG)
+               Core.logger.log(this, "Allocated new buffer because not pooling: "+
+                               accumulator, Logger.DEBUG);
+       }
+       return accumulator;
+    }
+    
     /**
      * Used to create an outbound connection.
      */
@@ -155,7 +185,7 @@
        throws ConnectFailedException {
         this(t);
        
-       boolean logDEBUG = Core.logger.shouldLog(Logger.DEBUG);
+       logDEBUG = Core.logger.shouldLog(Logger.DEBUG);
        if(logDEBUG)
            Core.logger.log(this, "tcpConnection (outbound)", 
                            new Exception("debug"), Logger.DEBUG);
@@ -178,21 +208,7 @@
                                                  
            /** NIO related stuff***/
            sock.getChannel().configureBlocking(false);
-           if(poolBuffers) {
-               synchronized(bufferPool) {
-                   if(!bufferPool.isEmpty()) {
-                       accumulator = (ByteBuffer)(bufferPool.removeFirst());
-                   } else {
-                       accumulator = useDirectBuffers ?
-                           ByteBuffer.allocateDirect(BUFFER_SIZE) :
-                           ByteBuffer.allocate(BUFFER_SIZE);
-                   }
-               }
-           } else {
-               accumulator = useDirectBuffers ?
-                   ByteBuffer.allocateDirect(BUFFER_SIZE) :
-                   ByteBuffer.allocate(BUFFER_SIZE);
-           }
+           accumulator = getAccumulator();
            accumulator.limit(0).position(0);
            nioout = new NIOOutputStream(sock.getChannel(),this);
            nioin = new NIOInputStream(accumulator,sock.getChannel(),this);
@@ -311,7 +327,7 @@
     tcpConnection(tcpTransport t, Socket sock, int designator,
                  boolean dontThrottle, boolean doThrottle) throws IOException {
         this(t);
-       boolean logDEBUG = Core.logger.shouldLog(Logger.DEBUG);
+       logDEBUG = Core.logger.shouldLog(Logger.DEBUG);
        if(logDEBUG)
            Core.logger.log(this, "tcpConnection (inbound)", Logger.DEBUG);
         
@@ -320,7 +336,7 @@
        
        /** NIO related stuff***/
        sock.getChannel().configureBlocking(false);
-       accumulator = ByteBuffer.allocate(16*1024); //FIXME:hardcoded
+       accumulator = getAccumulator();
        accumulator.limit(0).position(0);
        nioout = new NIOOutputStream(sock.getChannel(),this);
        nioin = new NIOInputStream(accumulator,sock.getChannel(),this);
@@ -423,7 +439,7 @@
     
     public final void close(boolean fromCloseThread) {
        closeException = new Exception("debug");
-       boolean logDEBUG =Core.logger.shouldLog(Logger.DEBUG);  
+       logDEBUG =Core.logger.shouldLog(Logger.DEBUG);  
        if(logDEBUG)
            Core.logger.log(this, "Closing("+fromCloseThread+
                            ") tcpConnection "+this, 
@@ -507,8 +523,18 @@
        if(!closed) Core.logger.log(this, "finalized without being closed!"+this, 
                                    Logger.NORMAL);
        // Accumulator will not be reused after closure
-       if(poolBuffers)
-           bufferPool.addLast(accumulator);
+       if(poolBuffers) {
+           if(accumulator != null) {
+               synchronized(bufferPool) {
+                   bufferPool.addLast(accumulator);
+                   accumulator = null;
+               }
+           } else {
+               if(logDEBUG)
+                   Core.logger.log(this, "Not repooling accumulator because is null: 
"+
+                                   this, Logger.DEBUG);
+           }
+       }
        try {
            close(true);
        } catch (Throwable t) {

_______________________________________________
cvs mailing list
[EMAIL PROTECTED]
http://dodo.freenetproject.org/cgi-bin/mailman/listinfo/cvs

Reply via email to