The codec framework
10.1 ์ฝ๋ฑ์ด๋ ๋ฌด์์ธ๊ฐ?
๋ชจ๋ ๋คํธ์ํฌ ์ ํ๋ฆฌ์ผ์ด์ ์ ํผ์ด ๊ฐ์ ์ ์ก๋๋ ์์ ๋ฐ์ดํธ๋ฅผ ๋์ ํ๋ก๊ทธ๋จ์ ๋ฐ์ดํฐ ํ์์ผ๋ก ๋ณํํ๋ ๋ฐฉ๋ฒ์ ์ ์ํด์ผ ํฉ๋๋ค.
์ด ๋ณํ ๋ก์ง์ ์ฝ๋ฑ์ ์ํด ์ฒ๋ฆฌ๋๋ฉฐ, ์ฝ๋ฑ์ ๊ฐ๊ฐ ํ ํ์์ ๋ฐ์ดํธ ์คํธ๋ฆผ์ ๋ค๋ฅธ ํ์์ผ๋ก ๋ณํํ๋ ์ธ์ฝ๋์ ๋์ฝ๋๋ก ๊ตฌ์ฑ๋ฉ๋๋ค. ์ธ์ฝ๋๋ ์ก์ ๋ฐ์ดํฐ๋ฅผ ์ฒ๋ฆฌํ๊ณ ๋์ฝ๋๋ ์์ ๋ฐ์ดํฐ๋ฅผ ์ฒ๋ฆฌํฉ๋๋ค.
10.2 ๋์ฝ๋
์ด ํด๋์ค๋ ๋ ๊ฐ์ง ์ฌ์ฉ ์ฌ๋ก๋ฅผ ๋ค๋ฃน๋๋ค:
๋ฐ์ดํธ๋ฅผ ๋ฉ์์ง๋ก ๋์ฝ๋ฉํ๋
ByteToMessageDecoder
์ReplayingDecoder
ํ๋์ ๋ฉ์์ง ํ์์ ๋ค๋ฅธ ๋ฉ์์ง ํ์์ผ๋ก ๋์ฝ๋ฉํ๋
MessageToMessageDecoder
๋์ฝ๋๋ ์์ ๋ฐ์ดํฐ๋ฅผ ํ ํ์์์ ๋ค๋ฅธ ํ์์ผ๋ก ๋ณํํ๋ ์ฑ
์์ ์ง๊ธฐ ๋๋ฌธ์, Netty์ ๋์ฝ๋๋ ChannelInboundHandler
๋ฅผ ๊ตฌํํฉ๋๋ค.
10.2.1 ์ถ์ ํด๋์ค ByteToMessageDecoder
ByteToMessageDecoder
๋ฐ์ดํธ๋ฅผ ๋ฉ์์ง๋ก ๋๋ ๋ค๋ฅธ ๋ฐ์ดํธ ์ํ์ค๋ก ๋์ฝ๋ฉํ๋ ๊ฒ์ ๋งค์ฐ ์ผ๋ฐ์ ์ธ ์์
์ด๊ธฐ ๋๋ฌธ์, Netty๋ ์ด๋ฅผ ์ํ ์ถ์ ๊ธฐ๋ณธ ํด๋์ค์ธ ByteToMessageDecoder
๋ฅผ ์ ๊ณตํฉ๋๋ค.
class ToIntegerDecoder : ByteToMessageDecoder() {
override fun decode(ctx: ChannelHandlerContext, `in`: ByteBuf, out: List<Any>) {
if (`in`.readableBytes() >= 4) {
out + `in`.readInt()
}
}
}
10.2.2 ์ถ์ ํด๋์ค ReplayingDecoder
ReplayingDecoder
๋ ByteToMessageDecoder
๋ฅผ ํ์ฅํ์ฌ readableBytes()
๋ฅผ ํธ์ถํ ํ์๋ฅผ ์์ฑ๋๋ค.
์ด๋ ๋ค์ด์ค๋ ByteBuf
๋ฅผ ์ฌ์ฉ์ ์ ์ ByteBuf
๊ตฌํ์ธ ReplayingDecoderBuffer
๋ก ๋ํํ์ฌ ๋ด๋ถ์ ์ผ๋ก ์ด ํธ์ถ์ ์คํํจ์ผ๋ก์จ ๋ฌ์ฑ๋ฉ๋๋ค.
abstract class ReplayingDecoder<S> : ByteToMessageDecoder()
์ฌ๊ธฐ์ ๋งค๊ฐ๋ณ์ S
๋ ์ํ ๊ด๋ฆฌ๋ฅผ ์ํด ์ฌ์ฉํ ํ์์ ์ง์ ํฉ๋๋ค.
์ํ ๊ด๋ฆฌ๋ฅผ ์ํํ์ง ์์ ๊ฒฝ์ฐ Void
๋ฅผ ์ฌ์ฉํฉ๋๋ค.
class ToIntegerDecoder2 : ReplayingDecoder<Void>() {
@Throws(Exception::class)
override fun decode(ctx: ChannelHandlerContext, `in`: ByteBuf, out: MutableList<Any>) {
out.add(`in`.readInt()) // 4๋ฐ์ดํธ๋ฅผ ์ฝ์ด ์ ์๋ก ๋ณํ ํ `out` ๋ฆฌ์คํธ์ ์ถ๊ฐ
}
}
ReplayingDecoder
๋ฅผ ์ฌ์ฉํ๋ฉด ByteToMessageDecoder
๋ณด๋ค ์ฝ๋๊ฐ ๋จ์ํด์ง์ง๋ง, ์ฝ๊ฐ์ ์ค๋ฒํค๋๊ฐ ๋ฐ์ํฉ๋๋ค.
๋ฐ๋ผ์ ๋ณต์ก์ฑ์ด ํฌ๊ฒ ์ฆ๊ฐํ์ง ์๋ ํ ByteToMessageDecoder
๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ์ข์ต๋๋ค.
10.3.2 ์ถ์ ํด๋์ค MessageToMessageEncoder
์ด์ ๋ฉ์์ง ํ์์ ๋ณํํ๋ MessageToMessageEncoder
๋ฅผ ์ดํด๋ณด๊ฒ ์ต๋๋ค.
์ด ํด๋์ค๋ ์ฃผ๋ก ํ ํ์์ ๋ฉ์์ง๋ฅผ ๋ค๋ฅธ ํ์์ ๋ฉ์์ง๋ก ๋ณํํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค.
abstract class MessageToMessageEncoder<I> : ChannelOutboundHandlerAdapter()
class IntegerToStringEncoder : MessageToMessageEncoder<Int>() {
@Throws(Exception::class)
override fun encode(ctx: ChannelHandlerContext, msg: Int, out: MutableList<Any>) {
out.add(msg.toString()) // ์ ์๋ฅผ ๋ฌธ์์ด๋ก ๋ณํํ์ฌ `out` ๋ฆฌ์คํธ์ ์ถ๊ฐ
}
}
10.4 ์ถ์ ์ฝ๋ฑ ํด๋์ค
์ง๊ธ๊น์ง ๋์ฝ๋์ ์ธ์ฝ๋๋ฅผ ๋ณ๋์ ์ํฐํฐ๋ก ๋ค๋ฃจ์ด ์์ง๋ง, ๋๋ก๋ ํ๋์ ํด๋์ค์์ ์ธ๋ฐ์ด๋ ๋ฐ ์์๋ฐ์ด๋ ๋ฐ์ดํฐ์ ๋ฉ์์ง์ ๋ณํ์ ๋ชจ๋ ๊ด๋ฆฌํ๋ ๊ฒ์ด ์ ์ฉํ ์ ์์ต๋๋ค.
Netty์ ์ถ์ ์ฝ๋ฑ ํด๋์ค๋ ์ด๋ฌํ ๋ชฉ์ ์ผ๋ก ์ ์ฉํ๋ฉฐ, ๋์ฝ๋์ ์ธ์ฝ๋ ์์ ํจ๊ป ๋ฌถ์ด ๋ ๊ฐ์ง ์์ ์ ์ฒ๋ฆฌํ ์ ์์ต๋๋ค.
10.4.1 ์ถ์ ํด๋์ค ByteToMessageCodec
ByteToMessageCodec
์ ๋ฐ์ดํธ๋ฅผ ๋ฉ์์ง๋ก ๋์ฝ๋ฉํ๊ณ , ๋ฉ์์ง๋ฅผ ๋ค์ ๋ฐ์ดํธ๋ก ์ธ์ฝ๋ฉํ๋ ์์
์ ์ฒ๋ฆฌํฉ๋๋ค.
decode()
๋ฐ์ดํธ๋ฅผ ๋ฉ์์ง๋ก ๋์ฝ๋ฉํ๋ ๋ฉ์๋์ ๋๋ค.
decodeLast()
์ฑ๋์ด ๋นํ์ฑ ์ํ๊ฐ ๋ ๋ ํ ๋ฒ ํธ์ถ๋ฉ๋๋ค.
encode()
๋ฉ์์ง๋ฅผ ๋ฐ์ดํธ๋ก ์ธ์ฝ๋ฉํ๋ ๋ฉ์๋์ ๋๋ค.
10.4.2 ์ถ์ ํด๋์ค MessageToMessageCodec
MessageToMessageCodec
์ ํ ๋ฉ์์ง ํ์์ ๋ค๋ฅธ ๋ฉ์์ง ํ์์ผ๋ก ๋์ฝ๋ฉํ๊ณ , ๋ค์ ์๋ ํ์์ผ๋ก ์ธ์ฝ๋ฉํ๋ ์์
์ ์ฒ๋ฆฌํฉ๋๋ค.
abstract class MessageToMessageCodec<INBOUND_IN, OUTBOUND_IN> : ChannelDuplexHandler()
Last updated