| 12345678910111213141516171819202122232425262728293031323334353637383940 |
- package com.its.common.cluster.codec;
- import com.its.common.cluster.vo.ClusterMessage;
- import io.netty.buffer.ByteBuf;
- import io.netty.channel.ChannelHandlerContext;
- import io.netty.handler.codec.MessageToByteEncoder;
- import lombok.extern.slf4j.Slf4j;
- import java.io.ByteArrayOutputStream;
- import java.io.ObjectOutputStream;
- @Slf4j
- public class ClusterMessageEncoder extends MessageToByteEncoder<ClusterMessage> {
- private final boolean isLogging;
- public ClusterMessageEncoder(boolean isLogging) {
- this.isLogging = isLogging;
- }
- @Override
- protected void encode(ChannelHandlerContext ctx, ClusterMessage msg, ByteBuf out) throws Exception {
- ByteArrayOutputStream bos = new ByteArrayOutputStream();
- ObjectOutputStream oos = new ObjectOutputStream(bos);
- oos.writeObject(msg);
- oos.flush();
- byte[] bytes = bos.toByteArray();
- if (this.isLogging) {
- log.info("ClusterMessageEncoder.encode: SEND {} Bytes.", bytes.length + 4);
- }
- out.writeInt(bytes.length); // 길이 필드 추가
- out.writeBytes(bytes);
- oos.close();
- bos.close();
- }
- }
|