com.witjit.game.server.communication.netty.handler.AbstractMessageOutHandler.java Source code

Java tutorial

Introduction

Here is the source code for com.witjit.game.server.communication.netty.handler.AbstractMessageOutHandler.java

Source

/*
 * Here comes the text of your license
 * Each line should be prefixed with  * 
 */
package com.witjit.game.server.communication.netty.handler;

import com.witjit.game.server.communication.core.Message;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOutboundHandlerAdapter;
import io.netty.channel.ChannelPromise;

/**
 *
 * @author Administrator
 */
public abstract class AbstractMessageOutHandler<M> extends ChannelOutboundHandlerAdapter {

    @Override
    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
        if (!(msg instanceof Message)) {
            //TODO: handle error message.
            return;
        }
        Message<M> message = (Message<M>) msg;
        ByteBuf buf = Unpooled.buffer(256);
        writeEndpointId(buf, message.getReceiver());
        writeEndpointId(buf, message.getSender());
        writeMessage(buf, message.getContent());
        ctx.write(buf, promise);
    }

    protected abstract void writeEndpointId(ByteBuf mb, long endpointId);

    protected abstract void writeMessage(ByteBuf mb, M content);
}