Java tutorial
/* * Copyright (c) 2014. Lorem ipsum dolor sit amet, consectetur adipiscing elit. * http://www.apache.org/licenses/LICENSE-2.0 */ package com.dc.gameserver.ServerCore.Controller.AbstractController; import com.alibaba.fastjson.JSON; import com.dc.gameserver.ServerCore.Service.character.PlayerInstance; import com.google.protobuf.MessageLite; import io.netty.buffer.ByteBuf; import io.netty.buffer.PooledByteBufAllocator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.annotation.PostConstruct; import java.nio.charset.Charset; /** * @author : <br/> * Date: 13-1-2<br/> * Time: ?12:49<br/> * connectMethod:13638363871@163.com<br/> * ? * ? * ??protobuffer?? * <p/> * ??json? * ? netty??ByteBuffer ??? enjoy it? */ @SuppressWarnings("unchecked") public abstract class AbstractController implements IController { private static final Logger LOGGER = LoggerFactory.getLogger(AbstractController.class); /** * spring ??controller? * ?--- spring? */ @PostConstruct public abstract void PostConstruct(); /** * ?? * * @param buffer ? * @param player active object * @throws Exception */ public void DispatchByteBuffer(final ByteBuf buffer, final PlayerInstance player) throws Exception { } /*********************??json?json??javaBean*********************/ /** * fastjson??javaBean </br> * ??javaBean </br> * * @param buffer buffer * @param clazz class sub-class * @return Object javaBean */ public static Object parseObject(ByteBuf buffer, Class clazz) { Object o = JSON.parseObject(buffer.toString(buffer.readerIndex(), buffer.writerIndex() - buffer.readerIndex(), Charset.defaultCharset()), clazz); buffer.release();//buf byteBuffer +buf? buffer = null; return o; } /** * 4 * * @param ID ?? * @param messageLite * @return */ public static ByteBuf wrappedBufferInt(int ID, MessageLite messageLite) { byte[] src = messageLite.toByteArray(); int length = 8 + src.length; ByteBuf buffer = PooledByteBufAllocator.DEFAULT.heapBuffer(length, length); buffer.setIndex(0, 0x4);//writeIndex? //4 buffer.writeByte(ID); // 4 buffer.writeBytes(messageLite.toByteArray()); buffer.setInt(0, buffer.writerIndex() - 0x4); messageLite = null; return buffer; } /** * ?? </br> * Encoder buffer </br> * ?4 </br> * proto buffer </br> * </br> * * @param arg1 * @param arg2 * @param messageLite * @return + ID+protoBufferData */ public static ByteBuf wrappedBufferInt(int arg1, int arg2, MessageLite messageLite) { byte[] src = messageLite.toByteArray(); int length = 12 + src.length; ByteBuf buffer = PooledByteBufAllocator.DEFAULT.heapBuffer(length, length); buffer.setIndex(0, 0x4);//writeIndex? //4 buffer.writeByte(arg1); // 4 buffer.writeByte(arg2); // 4 buffer.writeBytes(messageLite.toByteArray()); buffer.setInt(0, buffer.writerIndex() - 0x4); messageLite = null; return buffer; } /** * ?? </br> * Encoder buffer </br> * ?2 </br> * proto buffer </br> * </br> * * @param arg1 * @param arg2 * @param messageLite * @return + ID+protoBufferData */ public static ByteBuf wrappedBufferShort(int arg1, int arg2, MessageLite messageLite) { byte[] src = messageLite.toByteArray(); int length = 10 + src.length; ByteBuf buffer = PooledByteBufAllocator.DEFAULT.heapBuffer(length, length); buffer.setIndex(0, 0x2);//writeIndex? buffer.writeByte(arg1); buffer.writeByte(arg2); buffer.writeBytes(messageLite.toByteArray()); /**?2*/ buffer.setShort(0, buffer.writerIndex() - 0x2); messageLite = null; return buffer; } /** * Encoder buffer </br> * 2 </br> * * @param ID ?? * @param messageLite byte[] * @return */ public static ByteBuf wrappedBufferShort(int ID, MessageLite messageLite) { byte[] src = messageLite.toByteArray(); int length = 6 + src.length; ByteBuf buffer = PooledByteBufAllocator.DEFAULT.heapBuffer(length, length); buffer.setIndex(0, 0x2);//writeIndex? 2 buffer.writeInt(ID); //? 4 buffer.writeBytes(src); buffer.setShort(0, buffer.writerIndex() - 0x2); // short messageLite = null; //set null ,collection by GC return buffer; } }