Author: fhanik
Date: Fri Mar 24 07:26:05 2006
New Revision: 388561

URL: http://svn.apache.org/viewcvs?rev=388561&view=rev
Log:
Using long lifed bytebuffers for sending

Modified:
    
tomcat/container/tc5.5.x/modules/groupcom/src/share/org/apache/catalina/tribes/tcp/nio/NioSender.java

Modified: 
tomcat/container/tc5.5.x/modules/groupcom/src/share/org/apache/catalina/tribes/tcp/nio/NioSender.java
URL: 
http://svn.apache.org/viewcvs/tomcat/container/tc5.5.x/modules/groupcom/src/share/org/apache/catalina/tribes/tcp/nio/NioSender.java?rev=388561&r1=388560&r2=388561&view=diff
==============================================================================
--- 
tomcat/container/tc5.5.x/modules/groupcom/src/share/org/apache/catalina/tribes/tcp/nio/NioSender.java
 (original)
+++ 
tomcat/container/tc5.5.x/modules/groupcom/src/share/org/apache/catalina/tribes/tcp/nio/NioSender.java
 Fri Mar 24 07:26:05 2006
@@ -59,6 +59,7 @@
      * STATE VARIABLES *
      */
     protected ByteBuffer readbuf = null;
+    protected ByteBuffer writebuf = null;
     protected byte[] current = null;
     protected int curPos=0;
     protected XByteBuffer ackbuf = new XByteBuffer(128,true);
@@ -169,7 +170,6 @@
                 //weve written everything, or we are starting a new package
                 //protect against buffer overwrite
                 int length = current.length-curPos;
-                ByteBuffer writebuf = ByteBuffer.wrap(current,curPos,length);
                 int byteswritten = socketChannel.write(writebuf);
                 curPos += byteswritten;
                 remaining -= byteswritten;
@@ -201,6 +201,12 @@
         } else {
             readbuf.clear();
         }
+        if ( writebuf == null ) {
+            writebuf = getWriteBuffer();
+        } else {
+            writebuf.clear();
+        }
+        
         InetSocketAddress addr = new InetSocketAddress(getAddress(),getPort());
         if ( socketChannel != null ) throw new IOException("Socket channel has 
already been established. Connection might be in progress.");
         socketChannel = SocketChannel.open();
@@ -244,6 +250,7 @@
             readbuf = getReadBuffer();
         }
         if ( readbuf != null ) readbuf.clear();
+        if ( writebuf != null ) writebuf.clear();
         current = null;
         curPos = 0;
         ackbuf.clear();
@@ -254,8 +261,16 @@
         setConnectTime(-1);
     }
 
-    private ByteBuffer getReadBuffer() {
-        return 
(getDirectBuffer()?ByteBuffer.allocateDirect(getRxBufSize()):ByteBuffer.allocate(getRxBufSize()));
+    private ByteBuffer getReadBuffer() { 
+        return getBuffer(getRxBufSize());
+    }
+    
+    private ByteBuffer getWriteBuffer() {
+        return getBuffer(getTxBufSize());
+    }
+
+    private ByteBuffer getBuffer(int size) {
+        return 
(getDirectBuffer()?ByteBuffer.allocateDirect(size):ByteBuffer.allocate(size));
     }
     
     /**
@@ -266,11 +281,20 @@
     * @todo Implement this org.apache.catalina.tribes.tcp.IDataSender method
     */
    public synchronized void setMessage(byte[] data) throws IOException {
+       setMessage(data,0,data.length);
+   }
+
+   public synchronized void setMessage(byte[] data,int offset, int length) 
throws IOException {
        if ( data != null ) {
            current = data;
            remaining = current.length;
            curPos = 0;
            ackbuf.clear();
+           if ( writebuf != null ) writebuf.clear();
+           else writebuf = getBuffer(length);
+           if ( writebuf.capacity() > length ) writebuf = getBuffer(length);
+           writebuf.put(data,offset,length);
+           writebuf.rewind();
            if (isConnected()) {
                socketChannel.register(getSelector(), SelectionKey.OP_WRITE, 
this);
            }



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to