public class SocketReceive extends AsynchronousSocketStep
AsynchronousSocketChannel
.AsynchronousChannelStep.ChannelCallback<V,C extends java.nio.channels.AsynchronousChannel>, AsynchronousChannelStep.ChannelOperation<C extends java.nio.channels.AsynchronousChannel>
SOCKET_CHANNEL
CHANNEL_GROUP
Constructor and Description |
---|
SocketReceive(java.util.function.Function<Continuation<?>,java.net.SocketAddress> fGetSocketAddress,
java.util.function.BiPredicate<java.lang.Integer,java.nio.ByteBuffer> pCheckFinished)
Creates a new instance.
|
Modifier and Type | Method and Description |
---|---|
static java.util.function.BiPredicate<java.lang.Integer,java.nio.ByteBuffer> |
contentFullyRead()
Returns a new predicate to be used with
#until(Predicate) that
checks whether a byte buffer contains the complete content of an HTTP
response. |
protected boolean |
performAsyncOperation(int nBytesReceived,
java.nio.channels.AsynchronousSocketChannel rChannel,
java.nio.ByteBuffer rData,
AsynchronousChannelStep.ChannelCallback<java.lang.Integer,java.nio.channels.AsynchronousSocketChannel> rCallback)
Implementation of the ChannelOperation functional interface method
signature.
|
protected void |
performBlockingOperation(java.nio.channels.AsynchronousSocketChannel aChannel,
java.nio.ByteBuffer rData)
Must be implemented for the blocking execution of a step.
|
static SocketReceive |
receiveFrom(java.util.function.Function<Continuation<?>,java.net.SocketAddress> fGetSocketAddress)
Suspends until data has been received from a network socket.
|
static SocketReceive |
receiveFrom(java.net.SocketAddress rSocketAddress) |
static SocketReceive |
receiveUntil(java.util.function.BiPredicate<java.lang.Integer,java.nio.ByteBuffer> pCheckFinished)
Suspends until data has been received from a previously connected channel
stored in the currently executed coroutine.
|
SocketReceive |
until(java.util.function.BiPredicate<java.lang.Integer,java.nio.ByteBuffer> pCheckFinished)
Returns a new receive step instance the suspends until data has been
received from a network socket and a certain condition on that data is
met or an end-of-stream signal is received.
|
execute, getSocketAddress, getSocketAddressFactory, getSocketChannel, runAsync
getChannelGroup
fail, runBlocking, terminateCoroutine, toString
deleteRelation, get, getRelation, getRelations, notifyRelationListeners, readRelations, relationsEqual, relationsHashCode, relationsString, set, set, transform, writeRelations
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
public SocketReceive(java.util.function.Function<Continuation<?>,java.net.SocketAddress> fGetSocketAddress, java.util.function.BiPredicate<java.lang.Integer,java.nio.ByteBuffer> pCheckFinished)
fGetSocketAddress
- A function that provides the target socket
address from the current continuationpCheckFinished
- A predicate that checks whether receiving is
complete by evaluating the byte buffer after
readingpublic static java.util.function.BiPredicate<java.lang.Integer,java.nio.ByteBuffer> contentFullyRead()
#until(Predicate)
that
checks whether a byte buffer contains the complete content of an HTTP
response. The test is performed by calculating the full data size from
the 'Content-Length' attribute in the response header and comparing it
with the buffer position.public static SocketReceive receiveFrom(java.util.function.Function<Continuation<?>,java.net.SocketAddress> fGetSocketAddress)
ByteBuffer
of the step. If the
capacity of the buffer is reached before the EOF signal is received the
coroutine will be terminated with a CoroutineException
.
After the data has been fully received Buffer.flip()
will
be invoked on the buffer so that it can be used directly for subsequent
reading from it.
The returned step only receives the next block of data that is sent by
the remote socket and then continues the coroutine execution. If data
should be read until a certain condition is met a derived step needs to
be created with #until(Predicate)
.
fGetSocketAddress
- A function that provides the source socket
address from the current continuationpublic static SocketReceive receiveFrom(java.net.SocketAddress rSocketAddress)
receiveFrom(Function)
public static SocketReceive receiveUntil(java.util.function.BiPredicate<java.lang.Integer,java.nio.ByteBuffer> pCheckFinished)
The predicate argument is the same as for the #until(Predicate)
method.
pCheckFinished
- A predicate that checks whether the data has been
received completelypublic SocketReceive until(java.util.function.BiPredicate<java.lang.Integer,java.nio.ByteBuffer> pCheckFinished)
pCheckFinished
- A predicate that checks whether the data has been
received completelyprotected boolean performAsyncOperation(int nBytesReceived, java.nio.channels.AsynchronousSocketChannel rChannel, java.nio.ByteBuffer rData, AsynchronousChannelStep.ChannelCallback<java.lang.Integer,java.nio.channels.AsynchronousSocketChannel> rCallback) throws java.io.IOException
performAsyncOperation
in class AsynchronousSocketStep
java.io.IOException
AsynchronousChannelStep.ChannelOperation.execute(int,java.nio.channels.AsynchronousChannel,
ByteBuffer, AsynchronousChannelStep.ChannelCallback)
protected void performBlockingOperation(java.nio.channels.AsynchronousSocketChannel aChannel, java.nio.ByteBuffer rData) throws java.lang.Exception
AsynchronousSocketChannel
which must be accessed through the
blocking API (like Future.get()
).performBlockingOperation
in class AsynchronousSocketStep
aChannel
- The channel to perform the operation onrData
- The byte buffer for the operation datajava.lang.Exception
- Any kind of exception may be thrown