From 24dbb4c0845a1c7074e60c06fe4cf6c97fc94082 Mon Sep 17 00:00:00 2001 From: Ludovic Orban Date: Fri, 12 Mar 2021 13:35:12 +0100 Subject: [PATCH] fill kinda works Signed-off-by: Ludovic Orban --- .../http3/server/ServerDatagramConnector.java | 4 ++ .../http3/server/ServerDatagramEndPoint.java | 70 +++++++++++++++---- 2 files changed, 60 insertions(+), 14 deletions(-) diff --git a/jetty-http3/http3-server/src/main/java/org/eclipse/jetty/http3/server/ServerDatagramConnector.java b/jetty-http3/http3-server/src/main/java/org/eclipse/jetty/http3/server/ServerDatagramConnector.java index bbd2d17579a..d9dfb24aa21 100644 --- a/jetty-http3/http3-server/src/main/java/org/eclipse/jetty/http3/server/ServerDatagramConnector.java +++ b/jetty-http3/http3-server/src/main/java/org/eclipse/jetty/http3/server/ServerDatagramConnector.java @@ -23,6 +23,7 @@ import org.eclipse.jetty.server.ConnectionFactory; import org.eclipse.jetty.server.HttpConfiguration; import org.eclipse.jetty.server.HttpConnection; import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.util.BufferUtil; import org.eclipse.jetty.util.IO; import org.eclipse.jetty.util.annotation.Name; import org.eclipse.jetty.util.thread.Scheduler; @@ -182,9 +183,12 @@ public class ServerDatagramConnector extends AbstractNetworkConnector protected SocketAddress doReadDatagram(SelectableChannel channel) throws IOException { ByteBuffer buffer = getByteBufferPool().acquire(1200, true); + BufferUtil.flipToFill(buffer); LOG.info("doReadDatagram {}", channel); DatagramChannel datagramChannel = (DatagramChannel)channel; SocketAddress peer = datagramChannel.receive(buffer); + buffer.flip(); + LOG.info("doReadDatagram received {} byte(s)", buffer.remaining()); SocketAddress localAddress = datagramChannel.getLocalAddress(); boolean[] created = new boolean[1]; diff --git a/jetty-http3/http3-server/src/main/java/org/eclipse/jetty/http3/server/ServerDatagramEndPoint.java b/jetty-http3/http3-server/src/main/java/org/eclipse/jetty/http3/server/ServerDatagramEndPoint.java index 4fe6ab2e811..2c360e1369e 100644 --- a/jetty-http3/http3-server/src/main/java/org/eclipse/jetty/http3/server/ServerDatagramEndPoint.java +++ b/jetty-http3/http3-server/src/main/java/org/eclipse/jetty/http3/server/ServerDatagramEndPoint.java @@ -11,7 +11,9 @@ import java.nio.channels.WritePendingException; import org.eclipse.jetty.io.Connection; import org.eclipse.jetty.io.EndPoint; +import org.eclipse.jetty.io.FillInterest; import org.eclipse.jetty.io.ManagedSelector; +import org.eclipse.jetty.util.BufferUtil; import org.eclipse.jetty.util.Callback; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -20,37 +22,58 @@ public class ServerDatagramEndPoint implements EndPoint { private static final Logger LOG = LoggerFactory.getLogger(ServerDatagramEndPoint.class); + private final FillInterest fillInterest = new FillInterest() { + @Override + protected void needsFillInterest() throws IOException + { + + } + }; + private final SocketAddress localAddress; + private final SocketAddress remoteAddress; + private final SelectableChannel channel; + private ManagedSelector selector; + private SelectionKey selectionKey; + private Connection connection; + private ByteBuffer data; + private boolean open; + public ServerDatagramEndPoint(SocketAddress localAddress, SocketAddress remoteAddress, ByteBuffer buffer, SelectableChannel channel) { - + this.localAddress = localAddress; + this.remoteAddress = remoteAddress; + this.channel = channel; + this.data = buffer; } public void init(ManagedSelector selector, SelectionKey selectionKey) { - + this.selector = selector; + this.selectionKey = selectionKey; } public void onData(ByteBuffer buffer) { - + this.data = buffer; + fillInterest.fillable(); } @Override public InetSocketAddress getLocalAddress() { - throw new UnsupportedOperationException(); + return (InetSocketAddress)localAddress; } @Override public InetSocketAddress getRemoteAddress() { - throw new UnsupportedOperationException(); + return (InetSocketAddress)remoteAddress; } @Override public boolean isOpen() { - return false; + return open; } @Override @@ -86,7 +109,21 @@ public class ServerDatagramEndPoint implements EndPoint @Override public int fill(ByteBuffer buffer) throws IOException { - throw new UnsupportedOperationException(); + if (data != null) + { + int before = data.remaining(); + LOG.info("fill; bytes remaining: {} byte(s)", before); + BufferUtil.flipToFill(buffer); + buffer.put(data); + buffer.flip(); + int after = data.remaining(); + if (after == 0) + data = null; + int filled = before - after; + LOG.info("filled {} byte(s)", filled); + return filled; + } + return 0; } @Override @@ -98,7 +135,7 @@ public class ServerDatagramEndPoint implements EndPoint @Override public Object getTransport() { - throw new UnsupportedOperationException(); + return this.channel; } @Override @@ -116,19 +153,24 @@ public class ServerDatagramEndPoint implements EndPoint @Override public void fillInterested(Callback callback) throws ReadPendingException { - throw new UnsupportedOperationException(); + fillInterest.register(callback); + if (data != null) + fillInterest.fillable(); } @Override public boolean tryFillInterested(Callback callback) { - throw new UnsupportedOperationException(); + boolean registered = fillInterest.tryRegister(callback); + if (registered && data != null) + fillInterest.fillable(); + return registered; } @Override public boolean isFillInterested() { - throw new UnsupportedOperationException(); + return fillInterest.isInterested(); } @Override @@ -143,8 +185,6 @@ public class ServerDatagramEndPoint implements EndPoint return connection; } - private Connection connection; - @Override public void setConnection(Connection connection) { @@ -154,13 +194,15 @@ public class ServerDatagramEndPoint implements EndPoint @Override public void onOpen() { + open = true; LOG.info("onOpen"); } @Override public void onClose(Throwable cause) { - throw new UnsupportedOperationException(); + LOG.info("onClose"); + open = false; } @Override