Merge pull request #19982 from wlk/wlk-19500-make-simpleFramingProtocol-decoder-encoder-public
make simpleFramingProtocol decoder and encoder public
This commit is contained in:
commit
87113f9b14
1 changed files with 14 additions and 5 deletions
|
|
@ -74,8 +74,20 @@ object Framing {
|
||||||
* included in this limit.
|
* included in this limit.
|
||||||
*/
|
*/
|
||||||
def simpleFramingProtocol(maximumMessageLength: Int): BidiFlow[ByteString, ByteString, ByteString, ByteString, NotUsed] = {
|
def simpleFramingProtocol(maximumMessageLength: Int): BidiFlow[ByteString, ByteString, ByteString, ByteString, NotUsed] = {
|
||||||
val decoder = lengthField(4, 0, maximumMessageLength + 4, ByteOrder.BIG_ENDIAN).map(_.drop(4))
|
BidiFlow.fromFlowsMat(simpleFramingProtocolEncoder(maximumMessageLength), simpleFramingProtocolDecoder(maximumMessageLength))(Keep.left)
|
||||||
val encoder = Flow[ByteString].transform(() ⇒ new PushStage[ByteString, ByteString] {
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Protocol decoder that is used by [[Framing#simpleFramingProtocol]]
|
||||||
|
*/
|
||||||
|
def simpleFramingProtocolDecoder(maximumMessageLength: Int): Flow[ByteString, ByteString, NotUsed] =
|
||||||
|
lengthField(4, 0, maximumMessageLength + 4, ByteOrder.BIG_ENDIAN).map(_.drop(4))
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Protocol encoder that is used by [[Framing#simpleFramingProtocol]]
|
||||||
|
*/
|
||||||
|
def simpleFramingProtocolEncoder(maximumMessageLength: Int): Flow[ByteString, ByteString, NotUsed] =
|
||||||
|
Flow[ByteString].transform(() ⇒ new PushStage[ByteString, ByteString] {
|
||||||
override def onPush(message: ByteString, ctx: Context[ByteString]): SyncDirective = {
|
override def onPush(message: ByteString, ctx: Context[ByteString]): SyncDirective = {
|
||||||
val msgSize = message.size
|
val msgSize = message.size
|
||||||
if (msgSize > maximumMessageLength)
|
if (msgSize > maximumMessageLength)
|
||||||
|
|
@ -87,9 +99,6 @@ object Framing {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
BidiFlow.fromFlowsMat(encoder, decoder)(Keep.left)
|
|
||||||
}
|
|
||||||
|
|
||||||
class FramingException(msg: String) extends RuntimeException(msg)
|
class FramingException(msg: String) extends RuntimeException(msg)
|
||||||
|
|
||||||
private final val bigEndianDecoder: (ByteIterator, Int) ⇒ Int = (bs, length) ⇒ {
|
private final val bigEndianDecoder: (ByteIterator, Int) ⇒ Int = (bs, length) ⇒ {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue