com.fjn.helper.frameworkex.netty.v4.discardserver.DiscardServerHandler.java Source code

Java tutorial

Introduction

Here is the source code for com.fjn.helper.frameworkex.netty.v4.discardserver.DiscardServerHandler.java

Source

/*
 *
 *  Copyright 2018 FJN Corp.
 *
 *  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.
 *
 *  Author                        Date                       Issue
 *  fs1194361820@163.com          2015-01-01                 Initial Version
 *
 */

package com.fjn.helper.frameworkex.netty.v4.discardserver;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.ReferenceCountUtil;

public class DiscardServerHandler extends ChannelInboundHandlerAdapter {
    private String line = "";
    private String content = "";

    /**
     * ??
     */
    public void channelRead2(ChannelHandlerContext ctx, Object msg) throws Exception {
        ByteBuf in = (ByteBuf) msg;
        try {
            while (in.isReadable()) {
                System.out.print((char) in.readByte());
                System.out.flush();
            }
        } finally {
            ReferenceCountUtil.release(msg);
        }
    }

    /**
     * ???
     * @param ctx
     * @param msg
     * @throws Exception
     */
    public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception {
        ByteBuf in = (ByteBuf) msg;
        try {
            while (in.isReadable()) {
                char c = (char) in.readByte();
                content += c;
                if (c == '\n' || c == '\r') {
                    if (!line.isEmpty()) {
                        System.out.println(line);
                    }
                    if ("quit".equals(line)) {
                        ByteBuf bf = ctx.alloc().buffer(content.getBytes().length);
                        bf.setBytes(0, content.getBytes());
                        final ChannelFuture f = ctx.writeAndFlush(bf); // (3)
                        f.addListener(new ChannelFutureListener() {
                            @Override
                            public void operationComplete(ChannelFuture future) throws InterruptedException {
                                assert f == future;
                                ctx.close();
                            }
                        }); // (4)
                    }
                    line = "";
                    continue;
                } else {
                    line += c;
                }
            }
        } finally {
            ReferenceCountUtil.release(msg);
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}