org.tiger.netty.rpc.all.codec.NettyMessageEncoder.java Source code

Java tutorial

Introduction

Here is the source code for org.tiger.netty.rpc.all.codec.NettyMessageEncoder.java

Source

/*
 * Copyright 2013-2018 Lilinfeng.
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.tiger.netty.rpc.all.codec;

import org.tiger.netty.rpc.all.struct.NettyMessage;

import java.io.IOException;
import java.util.Arrays;
import java.util.Map;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/**
 * @author Lilinfeng
 * @version 1.0
 * @date 2014314
 */
public final class NettyMessageEncoder extends MessageToByteEncoder<NettyMessage> {

    MarshallingEncoder marshallingEncoder;

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        super.channelActive(ctx);
        System.out.println("NettyMessageEncoder channelActive " + System.currentTimeMillis());
    }

    public NettyMessageEncoder() throws IOException {
        this.marshallingEncoder = new MarshallingEncoder();
    }

    @Override
    protected void encode(ChannelHandlerContext ctx, NettyMessage msg, ByteBuf sendBuf) throws Exception {
        System.out.println("NettyMessageEncoder encode ..." + System.currentTimeMillis());
        if (msg == null || msg.getHeader() == null)
            throw new Exception("The encode message is null");

        sendBuf.writeInt((msg.getHeader().getCrcCode()));
        //print(sendBuf);
        sendBuf.writeInt((msg.getHeader().getLength()));
        //print(sendBuf);
        sendBuf.writeLong((msg.getHeader().getSessionID()));
        //print(sendBuf);
        sendBuf.writeByte((msg.getHeader().getType()));
        //print(sendBuf);
        sendBuf.writeByte((msg.getHeader().getPriority()));
        //print(sendBuf);
        sendBuf.writeInt((msg.getHeader().getAttachment().size()));
        //print(sendBuf);
        String key = null;
        byte[] keyArray = null;
        Object value = null;
        for (Map.Entry<String, Object> param : msg.getHeader().getAttachment().entrySet()) {
            key = param.getKey();
            keyArray = key.getBytes("UTF-8");
            sendBuf.writeInt(keyArray.length);
            sendBuf.writeBytes(keyArray);
            value = param.getValue();
            marshallingEncoder.encode(value, sendBuf);
        }
        key = null;
        keyArray = null;
        value = null;
        if (msg.getBody() != null) {
            marshallingEncoder.encode(msg.getBody(), sendBuf);
        } else {
            sendBuf.writeInt(0);
            //print(sendBuf);
        }

        System.out.println(sendBuf.readableBytes());
        //???4 int? 8?4?int??4intlengthlengthlength????8
        sendBuf.setInt(4, sendBuf.readableBytes() - 8);
        //print(sendBuf);
        //sendBuf.writeInt(0);
    }

    private void print(ByteBuf sendBuf) {
        byte[] bt = new byte[sendBuf.capacity()];
        sendBuf.getBytes(0, bt);
        System.out.println("bt>" + Arrays.toString(bt));
    }
}