Fleshing out WebSocketPing impl
This commit is contained in:
parent
3b2aa4c45e
commit
4f30fe2a40
|
@ -33,7 +33,7 @@ public class WebSocketBlockingConnection
|
|||
}
|
||||
else
|
||||
{
|
||||
throw new IllegalArgumentException("Unsupported implementation of WebSocketConnection");
|
||||
throw new IllegalArgumentException("WebSocketConnection must implement internal RawConnection interface");
|
||||
}
|
||||
this.bufferPool = this.conn.getBufferPool();
|
||||
this.policy = conn.getPolicy();
|
||||
|
|
|
@ -1,19 +1,61 @@
|
|||
package org.eclipse.jetty.websocket.api.io;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
import org.eclipse.jetty.io.ByteBufferPool;
|
||||
import org.eclipse.jetty.util.FutureCallback;
|
||||
import org.eclipse.jetty.websocket.api.WebSocketConnection;
|
||||
import org.eclipse.jetty.websocket.api.WebSocketPolicy;
|
||||
import org.eclipse.jetty.websocket.frames.PingFrame;
|
||||
import org.eclipse.jetty.websocket.generator.Generator;
|
||||
import org.eclipse.jetty.websocket.io.RawConnection;
|
||||
|
||||
public class WebSocketPing
|
||||
{
|
||||
private WebSocketConnection conn;
|
||||
private RawConnection conn;
|
||||
private ByteBufferPool bufferPool;
|
||||
private WebSocketPolicy policy;
|
||||
private Generator generator;
|
||||
|
||||
public WebSocketPing(WebSocketConnection conn)
|
||||
{
|
||||
this.conn = conn;
|
||||
if (conn instanceof RawConnection)
|
||||
{
|
||||
this.conn = (RawConnection)conn;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new IllegalArgumentException("WebSocketConnection must implement internal RawConnection interface");
|
||||
}
|
||||
this.bufferPool = this.conn.getBufferPool();
|
||||
this.policy = conn.getPolicy();
|
||||
this.generator = new Generator(this.policy);
|
||||
}
|
||||
|
||||
public void sendPing(byte buf[], int offset, int len)
|
||||
public void sendPing(byte data[]) throws IOException
|
||||
{
|
||||
// TODO: implement
|
||||
// TODO: should this block and wait for a pong? (how?)
|
||||
PingFrame frame = new PingFrame(data);
|
||||
ByteBuffer buf = bufferPool.acquire(policy.getBufferSize(),false);
|
||||
try
|
||||
{
|
||||
generator.generate(buf,frame);
|
||||
FutureCallback<Void> blocking = new FutureCallback<>();
|
||||
this.conn.writeRaw(null,blocking,buf);
|
||||
blocking.get(); // block till finished sending?
|
||||
}
|
||||
catch (InterruptedException e)
|
||||
{
|
||||
throw new IOException("Blocking write failed",e);
|
||||
}
|
||||
catch (ExecutionException e)
|
||||
{
|
||||
FutureCallback.rethrow(e);
|
||||
}
|
||||
finally
|
||||
{
|
||||
bufferPool.release(buf);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,7 +30,7 @@ public class EchoBroadcastPingSocket extends EchoBroadcastSocket
|
|||
System.err.println("Ping " + pinger);
|
||||
byte data[] = new byte[]
|
||||
{ (byte)1, (byte)2, (byte)3 };
|
||||
pinger.sendPing(data,0,3);
|
||||
pinger.sendPing(data);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
|
|
Loading…
Reference in New Issue