You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by el...@apache.org on 2013/03/23 11:05:02 UTC

[1/3] git commit: Added UDP tests

Updated Branches:
  refs/heads/trunk abe194309 -> 2cc1d1407


Added UDP tests

Project: http://git-wip-us.apache.org/repos/asf/mina/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina/commit/a63ec81b
Tree: http://git-wip-us.apache.org/repos/asf/mina/tree/a63ec81b
Diff: http://git-wip-us.apache.org/repos/asf/mina/diff/a63ec81b

Branch: refs/heads/trunk
Commit: a63ec81bee5504590adec133aecfb870a4afa848
Parents: abe1943
Author: Emmanuel Lécharny <el...@apache.org>
Authored: Sat Mar 23 10:56:04 2013 +0100
Committer: Emmanuel Lécharny <el...@apache.org>
Committed: Sat Mar 23 10:56:04 2013 +0100

----------------------------------------------------------------------
 ...3ClientVsMina3ServerUdpBenchmarkBinaryTest.java |   10 ++-
 ...3ClientVsMina3ServerUdpBenchmarkBinaryTest.java |   65 +++++++++++++++
 ...ClientVsNetty3ServerUdpBenchmarkBinaryTest.java |   65 +++++++++++++++
 3 files changed, 136 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina/blob/a63ec81b/benchmarks/src/test/java/org/apache/mina/core/Mina3ClientVsMina3ServerUdpBenchmarkBinaryTest.java
----------------------------------------------------------------------
diff --git a/benchmarks/src/test/java/org/apache/mina/core/Mina3ClientVsMina3ServerUdpBenchmarkBinaryTest.java b/benchmarks/src/test/java/org/apache/mina/core/Mina3ClientVsMina3ServerUdpBenchmarkBinaryTest.java
index 6d85a55..30b941d 100644
--- a/benchmarks/src/test/java/org/apache/mina/core/Mina3ClientVsMina3ServerUdpBenchmarkBinaryTest.java
+++ b/benchmarks/src/test/java/org/apache/mina/core/Mina3ClientVsMina3ServerUdpBenchmarkBinaryTest.java
@@ -56,10 +56,12 @@ public class Mina3ClientVsMina3ServerUdpBenchmarkBinaryTest extends BenchmarkBin
 
     @Parameters(name = "{0} messages of size {1}")
     public static Collection<Object[]> getParameters() {
-        Object[][] parameters = new Object[][] { { 1000000, 10, 2 * 60 }, { 1000000, 1 * 1024, 2 * 60 },
-                { 1000000, 10 * 1024, 2 * 60 }, { 1000000, 20 * 1024, 2 * 60 }, { 500000, 50 * 1024, 2 * 60 },
-                { 200000, 100 * 1024, 2 * 60 }, { 100000, 200 * 1024, 2 * 60 }, { 50000, 500 * 1024, 2 * 60 },
-                { 20000, 1024 * 1024, 2 * 60 }, { 2000, 10 * 1024 * 1024, 2 * 60 }, { 500, 64 * 1024 * 1024, 2 * 60 } };
+        Object[][] parameters = new Object[][] { 
+                { 1000000, 10, 2 * 60 }, 
+                { 1000000, 1 * 1024, 2 * 60 },
+                { 1000000, 10 * 1024, 2 * 60 }, 
+                { 1000000, 20 * 1024, 2 * 60 }, 
+                { 500000, 50 * 1024, 2 * 60 }}; // No need to test any further, the maximum size for an UDP message is 64Kb
         return Arrays.asList(parameters);
     }
 }

http://git-wip-us.apache.org/repos/asf/mina/blob/a63ec81b/benchmarks/src/test/java/org/apache/mina/core/Netty3ClientVsMina3ServerUdpBenchmarkBinaryTest.java
----------------------------------------------------------------------
diff --git a/benchmarks/src/test/java/org/apache/mina/core/Netty3ClientVsMina3ServerUdpBenchmarkBinaryTest.java b/benchmarks/src/test/java/org/apache/mina/core/Netty3ClientVsMina3ServerUdpBenchmarkBinaryTest.java
new file mode 100644
index 0000000..1c2c518
--- /dev/null
+++ b/benchmarks/src/test/java/org/apache/mina/core/Netty3ClientVsMina3ServerUdpBenchmarkBinaryTest.java
@@ -0,0 +1,65 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ *
+ */
+package org.apache.mina.core;
+
+import java.util.Arrays;
+import java.util.Collection;
+
+import org.apache.mina.core.BenchmarkFactory.Type;
+import org.junit.runners.Parameterized.Parameters;
+
+/**
+ * @author <a href="http://mina.apache.org">Apache MINA Project</a>
+ */
+public class Netty3ClientVsMina3ServerUdpBenchmarkBinaryTest extends BenchmarkBinaryTest {
+
+    /**
+     * @param numberOfMessages
+     * @param messageSize
+     */
+    public Netty3ClientVsMina3ServerUdpBenchmarkBinaryTest(int numberOfMessages, int messageSize, int timeout) {
+        super(numberOfMessages, messageSize, timeout);
+    }
+
+    /** {@inheritDoc}
+     */
+    @Override
+    public Type getClientType() {
+        return Type.Netty3_udp;
+    }
+
+    /** {@inheritDoc}
+     */
+    @Override
+    public Type getServerType() {
+        return Type.Mina3_udp;
+    }
+
+    @Parameters(name = "{0} messages of size {1}")
+    public static Collection<Object[]> getParameters() {
+        Object[][] parameters = new Object[][] {
+                { 1000000, 10, 2 * 60 } , 
+                { 1000000, 1 * 1024, 2 * 60 },
+                { 1000000, 10 * 1024, 2 * 60 }, 
+                { 1000000, 20 * 1024, 2 * 60 }, 
+                { 500000, 50 * 1024, 2 * 60 }}; // No need to test any further, the maximum size for an UDP message is 64Kb
+        return Arrays.asList(parameters);
+    }
+}

http://git-wip-us.apache.org/repos/asf/mina/blob/a63ec81b/benchmarks/src/test/java/org/apache/mina/core/Netty3ClientVsNetty3ServerUdpBenchmarkBinaryTest.java
----------------------------------------------------------------------
diff --git a/benchmarks/src/test/java/org/apache/mina/core/Netty3ClientVsNetty3ServerUdpBenchmarkBinaryTest.java b/benchmarks/src/test/java/org/apache/mina/core/Netty3ClientVsNetty3ServerUdpBenchmarkBinaryTest.java
new file mode 100644
index 0000000..8e026c6
--- /dev/null
+++ b/benchmarks/src/test/java/org/apache/mina/core/Netty3ClientVsNetty3ServerUdpBenchmarkBinaryTest.java
@@ -0,0 +1,65 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ *
+ */
+package org.apache.mina.core;
+
+import java.util.Arrays;
+import java.util.Collection;
+
+import org.apache.mina.core.BenchmarkFactory.Type;
+import org.junit.runners.Parameterized.Parameters;
+
+/**
+ * @author <a href="http://mina.apache.org">Apache MINA Project</a>
+ */
+public class Netty3ClientVsNetty3ServerUdpBenchmarkBinaryTest extends BenchmarkBinaryTest {
+
+    /**
+     * @param numberOfMessages
+     * @param messageSize
+     */
+    public Netty3ClientVsNetty3ServerUdpBenchmarkBinaryTest(int numberOfMessages, int messageSize, int timeout) {
+        super(numberOfMessages, messageSize, timeout);
+    }
+
+    /** {@inheritDoc}
+     */
+    @Override
+    public Type getClientType() {
+        return Type.Netty3_udp;
+    }
+
+    /** {@inheritDoc}
+     */
+    @Override
+    public Type getServerType() {
+        return Type.Netty3_udp;
+    }
+
+    @Parameters(name = "{0} messages of size {1}")
+    public static Collection<Object[]> getParameters() {
+        Object[][] parameters = new Object[][] { 
+                { 1000000, 10, 2 * 60 }, 
+                { 1000000, 1 * 1024, 2 * 60 },
+                { 1000000, 10 * 1024, 2 * 60 }, 
+                { 1000000, 20 * 1024, 2 * 60 }, 
+                { 500000, 50 * 1024, 2 * 60 }}; // No need to test any further, the maximum size for an UDP message is 64Kb
+        return Arrays.asList(parameters);
+    }
+}


[2/3] git commit: Cleaned the client and server code, removing the System.out cals and commented lines

Posted by el...@apache.org.
Cleaned the client and server code, removing the System.out cals and
commented lines

Project: http://git-wip-us.apache.org/repos/asf/mina/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina/commit/a61d0e70
Tree: http://git-wip-us.apache.org/repos/asf/mina/tree/a61d0e70
Diff: http://git-wip-us.apache.org/repos/asf/mina/diff/a61d0e70

Branch: refs/heads/trunk
Commit: a61d0e700fb4816ba80352c9d3b38ed8e7a6a980
Parents: a63ec81
Author: Emmanuel Lécharny <el...@apache.org>
Authored: Sat Mar 23 10:59:50 2013 +0100
Committer: Emmanuel Lécharny <el...@apache.org>
Committed: Sat Mar 23 10:59:50 2013 +0100

----------------------------------------------------------------------
 .../mina/core/nio/tcp/Mina3TcpBenchmarkClient.java |    7 -------
 .../mina/core/nio/tcp/Mina3TcpBenchmarkServer.java |    3 ---
 .../mina/core/nio/udp/Mina3UdpBenchmarkClient.java |    7 -------
 .../mina/core/nio/udp/Mina3UdpBenchmarkServer.java |   11 +++++------
 .../core/nio/udp/Netty3UdpBenchmarkClient.java     |    1 +
 5 files changed, 6 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina/blob/a61d0e70/benchmarks/src/test/java/org/apache/mina/core/nio/tcp/Mina3TcpBenchmarkClient.java
----------------------------------------------------------------------
diff --git a/benchmarks/src/test/java/org/apache/mina/core/nio/tcp/Mina3TcpBenchmarkClient.java b/benchmarks/src/test/java/org/apache/mina/core/nio/tcp/Mina3TcpBenchmarkClient.java
index 98fa5bb..26eab19 100755
--- a/benchmarks/src/test/java/org/apache/mina/core/nio/tcp/Mina3TcpBenchmarkClient.java
+++ b/benchmarks/src/test/java/org/apache/mina/core/nio/tcp/Mina3TcpBenchmarkClient.java
@@ -52,23 +52,17 @@ public class Mina3TcpBenchmarkClient implements BenchmarkClient {
             }
 
             public void sessionOpened(IoSession session) {
-                //System.out.println("Client session opened");
                 sendMessage(session, data);
             }
 
             public void messageReceived(IoSession session, Object message) {
-                //System.out.println("Client message received : " + message);
                 if (message instanceof ByteBuffer) {
                     ByteBuffer buffer = (ByteBuffer) message;
-                    //System.out.println("length=" + buffer.remaining());
                     for (int i = 0; i < buffer.remaining(); ++i) {
                         counter.countDown();
                         long count = counter.getCount();
                         if (count > 0) {
                             sendMessage(session, data);
-                            if (count % 100000 == 0) {
-                                System.out.println("Received " + count);
-                            }
                         }
                     }
                 }
@@ -88,7 +82,6 @@ public class Mina3TcpBenchmarkClient implements BenchmarkClient {
 
             @Override
             public void messageSent(IoSession session, Object message) {
-                //System.out.println("Client message sent : " + message);
             }
 
             @Override

http://git-wip-us.apache.org/repos/asf/mina/blob/a61d0e70/benchmarks/src/test/java/org/apache/mina/core/nio/tcp/Mina3TcpBenchmarkServer.java
----------------------------------------------------------------------
diff --git a/benchmarks/src/test/java/org/apache/mina/core/nio/tcp/Mina3TcpBenchmarkServer.java b/benchmarks/src/test/java/org/apache/mina/core/nio/tcp/Mina3TcpBenchmarkServer.java
index 4443a00..148fc77 100755
--- a/benchmarks/src/test/java/org/apache/mina/core/nio/tcp/Mina3TcpBenchmarkServer.java
+++ b/benchmarks/src/test/java/org/apache/mina/core/nio/tcp/Mina3TcpBenchmarkServer.java
@@ -64,12 +64,10 @@ public class Mina3TcpBenchmarkServer implements BenchmarkServer {
         tcpServer.getSessionConfig().setTcpNoDelay(true);
         tcpServer.setIoHandler(new IoHandler() {
             public void sessionOpened(IoSession session) {
-                //System.out.println("Server session opened");
                 session.setAttribute(STATE_ATTRIBUTE, State.WAIT_FOR_FIRST_BYTE_LENGTH);
             }
 
             public void messageReceived(IoSession session, Object message) {
-                //System.out.println("Server Message received : " + message);
                 if (message instanceof ByteBuffer) {
                     ByteBuffer buffer = (ByteBuffer) message;
 
@@ -134,7 +132,6 @@ public class Mina3TcpBenchmarkServer implements BenchmarkServer {
 
             @Override
             public void messageSent(IoSession session, Object message) {
-                //System.out.println("Server message sent :" + message);
             }
 
             @Override

http://git-wip-us.apache.org/repos/asf/mina/blob/a61d0e70/benchmarks/src/test/java/org/apache/mina/core/nio/udp/Mina3UdpBenchmarkClient.java
----------------------------------------------------------------------
diff --git a/benchmarks/src/test/java/org/apache/mina/core/nio/udp/Mina3UdpBenchmarkClient.java b/benchmarks/src/test/java/org/apache/mina/core/nio/udp/Mina3UdpBenchmarkClient.java
index 562c087..71696ec 100755
--- a/benchmarks/src/test/java/org/apache/mina/core/nio/udp/Mina3UdpBenchmarkClient.java
+++ b/benchmarks/src/test/java/org/apache/mina/core/nio/udp/Mina3UdpBenchmarkClient.java
@@ -52,23 +52,17 @@ public class Mina3UdpBenchmarkClient implements BenchmarkClient {
             }
 
             public void sessionOpened(IoSession session) {
-                //System.out.println("Client session opened");
                 sendMessage(session, data);
             }
 
             public void messageReceived(IoSession session, Object message) {
-                //System.out.println("Client message received : " + message);
                 if (message instanceof ByteBuffer) {
                     ByteBuffer buffer = (ByteBuffer) message;
-                    //System.out.println("length=" + buffer.remaining());
                     for (int i = 0; i < buffer.remaining(); ++i) {
                         counter.countDown();
                         long count = counter.getCount();
                         if (count > 0) {
                             sendMessage(session, data);
-                            if (count % 100000 == 0) {
-                                System.out.println("Received " + count);
-                            }
                         }
                     }
                 }
@@ -88,7 +82,6 @@ public class Mina3UdpBenchmarkClient implements BenchmarkClient {
 
             @Override
             public void messageSent(IoSession session, Object message) {
-                //System.out.println("Client message sent : " + message);
             }
 
             @Override

http://git-wip-us.apache.org/repos/asf/mina/blob/a61d0e70/benchmarks/src/test/java/org/apache/mina/core/nio/udp/Mina3UdpBenchmarkServer.java
----------------------------------------------------------------------
diff --git a/benchmarks/src/test/java/org/apache/mina/core/nio/udp/Mina3UdpBenchmarkServer.java b/benchmarks/src/test/java/org/apache/mina/core/nio/udp/Mina3UdpBenchmarkServer.java
index 7ed7cfe..3bf9e29 100755
--- a/benchmarks/src/test/java/org/apache/mina/core/nio/udp/Mina3UdpBenchmarkServer.java
+++ b/benchmarks/src/test/java/org/apache/mina/core/nio/udp/Mina3UdpBenchmarkServer.java
@@ -30,6 +30,8 @@ import org.apache.mina.api.IoSession;
 import org.apache.mina.core.BenchmarkServer;
 import org.apache.mina.session.AttributeKey;
 import org.apache.mina.transport.nio.NioUdpServer;
+import org.apache.mina.transport.udp.DefaultUdpSessionConfig;
+import org.apache.mina.transport.udp.UdpSessionConfig;
 
 /**
  * @author <a href="http://mina.apache.org">Apache MINA Project</a>
@@ -59,14 +61,11 @@ public class Mina3UdpBenchmarkServer implements BenchmarkServer {
      * {@inheritDoc}
      */
     public void start(int port) throws IOException {
-        udpServer = new NioUdpServer();
+        UdpSessionConfig config = new DefaultUdpSessionConfig();
+        config.setReadBufferSize(65536);
+        udpServer = new NioUdpServer(config);
         udpServer.setIoHandler(new IoHandler() {
-            public void sessionCreated(IoSession session) {
-                System.out.println("Session created...");
-            }
-
             public void sessionOpened(IoSession session) {
-                //System.out.println("Server session opened");
                 session.setAttribute(STATE_ATTRIBUTE, State.WAIT_FOR_FIRST_BYTE_LENGTH);
             }
 

http://git-wip-us.apache.org/repos/asf/mina/blob/a61d0e70/benchmarks/src/test/java/org/apache/mina/core/nio/udp/Netty3UdpBenchmarkClient.java
----------------------------------------------------------------------
diff --git a/benchmarks/src/test/java/org/apache/mina/core/nio/udp/Netty3UdpBenchmarkClient.java b/benchmarks/src/test/java/org/apache/mina/core/nio/udp/Netty3UdpBenchmarkClient.java
index 14177c6..33214f3 100644
--- a/benchmarks/src/test/java/org/apache/mina/core/nio/udp/Netty3UdpBenchmarkClient.java
+++ b/benchmarks/src/test/java/org/apache/mina/core/nio/udp/Netty3UdpBenchmarkClient.java
@@ -56,6 +56,7 @@ public class Netty3UdpBenchmarkClient implements BenchmarkClient {
     public void start(final int port, final CountDownLatch counter, final byte[] data) throws IOException {
         factory = new NioDatagramChannelFactory();
         ConnectionlessBootstrap bootstrap = new ConnectionlessBootstrap(factory);
+        bootstrap.setOption("sendBufferSize", 65536);
         bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
             public ChannelPipeline getPipeline() throws Exception {
                 return Channels.pipeline(new SimpleChannelUpstreamHandler() {


[3/3] git commit: o Replaced a call to DatagramChannel.getLocalAddress(), as it does not exist in Java 6 o Connected the DatagramChannel before a write instead of calling send() o Implemented the conversion to a direct buffer o A bit of cleanup

Posted by el...@apache.org.
o Replaced a call to DatagramChannel.getLocalAddress(), as it does not
exist in Java 6
o Connected the DatagramChannel before a write instead of calling send()
o Implemented the conversion to a direct buffer
o A bit of cleanup

Project: http://git-wip-us.apache.org/repos/asf/mina/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina/commit/2cc1d140
Tree: http://git-wip-us.apache.org/repos/asf/mina/tree/2cc1d140
Diff: http://git-wip-us.apache.org/repos/asf/mina/diff/2cc1d140

Branch: refs/heads/trunk
Commit: 2cc1d14072ee1633ae9cef201c9f7c663c7e1f2d
Parents: a61d0e7
Author: Emmanuel Lécharny <el...@apache.org>
Authored: Sat Mar 23 11:02:33 2013 +0100
Committer: Emmanuel Lécharny <el...@apache.org>
Committed: Sat Mar 23 11:02:33 2013 +0100

----------------------------------------------------------------------
 .../apache/mina/transport/nio/NioUdpServer.java    |   10 ++++--
 .../apache/mina/transport/nio/NioUdpSession.java   |   23 ++++++++++++++-
 2 files changed, 28 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina/blob/2cc1d140/core/src/main/java/org/apache/mina/transport/nio/NioUdpServer.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mina/transport/nio/NioUdpServer.java b/core/src/main/java/org/apache/mina/transport/nio/NioUdpServer.java
index 9390bf5..1833f2f 100644
--- a/core/src/main/java/org/apache/mina/transport/nio/NioUdpServer.java
+++ b/core/src/main/java/org/apache/mina/transport/nio/NioUdpServer.java
@@ -207,6 +207,7 @@ public class NioUdpServer extends AbstractUdpServer implements SelectorListener
     @Override
     public void ready(final boolean accept, boolean connect, final boolean read, final ByteBuffer readBuffer,
             final boolean write) {
+        // Process the reads first
         if (read) {
             try {
                 LOG.debug("readable datagram for UDP service : {}", this);
@@ -222,8 +223,6 @@ public class NioUdpServer extends AbstractUdpServer implements SelectorListener
                     NioUdpSession session = sessions.get(source);
 
                     if (session == null) {
-                        //session = new NioUdpSession(this, idleChecker, address, source);
-
                         session = createSession(source, datagramChannel);
                     }
 
@@ -233,6 +232,8 @@ public class NioUdpServer extends AbstractUdpServer implements SelectorListener
                 LOG.error("IOException while reading the socket", ex);
             }
         }
+
+        // Now, process the writes
         if (write) {
             // TODO : flush session
         }
@@ -242,8 +243,9 @@ public class NioUdpServer extends AbstractUdpServer implements SelectorListener
             throws IOException {
         LOG.debug("create session");
         UdpSessionConfig config = getSessionConfig();
-        final NioUdpSession session = new NioUdpSession(this, idleChecker, datagramChannel,
-                datagramChannel.getLocalAddress(), remoteAddress);
+        SocketAddress localAddress = new InetSocketAddress(datagramChannel.socket().getLocalAddress(), datagramChannel
+                .socket().getLocalPort());
+        final NioUdpSession session = new NioUdpSession(this, idleChecker, datagramChannel, localAddress, remoteAddress);
 
         // apply idle configuration
         session.getConfig().setIdleTimeInMillis(IdleStatus.READ_IDLE, config.getIdleTimeInMillis(IdleStatus.READ_IDLE));

http://git-wip-us.apache.org/repos/asf/mina/blob/2cc1d140/core/src/main/java/org/apache/mina/transport/nio/NioUdpSession.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mina/transport/nio/NioUdpSession.java b/core/src/main/java/org/apache/mina/transport/nio/NioUdpSession.java
index 12d80a8..431b00a 100644
--- a/core/src/main/java/org/apache/mina/transport/nio/NioUdpSession.java
+++ b/core/src/main/java/org/apache/mina/transport/nio/NioUdpSession.java
@@ -218,8 +218,16 @@ public class NioUdpSession extends AbstractIoSession implements SelectorListener
             // Check that we can write into the channel
             if (!isRegisteredForWrite()) {
                 // We don't have pending writes
+                // First, connect if we aren't already connected
+                if (!((DatagramChannel) channel).isConnected()) {
+                    ((DatagramChannel) channel).connect(remoteAddress);
+                }
+
+                // And try to write the data. We will either write them all,
+                // or none
                 return ((DatagramChannel) channel).write((ByteBuffer) message);
             } else {
+                System.out.println("Cannot write");
                 return -1;
             }
         } catch (final IOException e) {
@@ -235,7 +243,20 @@ public class NioUdpSession extends AbstractIoSession implements SelectorListener
      */
     @Override
     protected ByteBuffer convertToDirectBuffer(WriteRequest writeRequest, boolean createNew) {
-        return (ByteBuffer) writeRequest.getMessage();
+        ByteBuffer message = (ByteBuffer) writeRequest.getMessage();
+
+        if (!message.isDirect()) {
+            int remaining = message.remaining();
+
+            ByteBuffer directBuffer = ByteBuffer.allocateDirect(remaining);
+            directBuffer.put(message);
+            directBuffer.flip();
+            writeRequest.setMessage(directBuffer);
+
+            return directBuffer;
+        }
+
+        return message;
     }
 
     void setSelectionKey(SelectionKey key) {