All Downloads are FREE. Search and download functionalities are using the official Maven repository.

bak.module.btl Maven / Gradle / Ivy

package ${packageName}.modules;

import cn.hutool.log.LogFactory;
import org.artifact.core.server.ServerContext;
import org.artifact.core.context.session.Session;
import org.artifact.core.context.packet.NetworkPacket;
import org.artifact.core.context.packet.NetworkHeader;
import org.artifact.core.context.bytebuf.IByteBuff;
import cn.hutool.core.map.MapProxy;
import java.util.function.Consumer;
import java.util.function.BiConsumer;
import java.util.Collection;
import java.util.Map;
import java.util.List;
import org.artifact.core.constant.ArtifactErrorCode;
import org.artifact.core.util.LogicUtil;
import org.artifact.core.lang.IModule;
import ${packageName}.structs.*;
import ${packageName}.StructUtil;

@SuppressWarnings("unchecked")
public interface ${module.name}Module extends IModule{
	public static final int MODULE_ID = ${module.id};


	default int getModuleId(){
		return ${module.id};
	}
	
	/** 分发  */
	default Consumer disp(Session session,NetworkPacket packet) {
		int method = packet.getHeader(NetworkHeader.METHOD);
		switch (method) {
			<%for(send in module.protocolSends){%>
			case ${send.id}:{
				return ${send.requestName}Decoder(session,packet);
			}
			<%}%>
			default:
				throw LogicUtil.error(ArtifactErrorCode.NOT_FOND_ACTION);
		}
	}	


	<%
	for(send in module.protocolSends){
	%>
	/** ${send.remark} */
	default Consumer ${send.requestName}Decoder(Session session,NetworkPacket packet) {
		MapProxy proxy = MapProxy.create(packet.getBody());
		<%
		var parameter = "";
		var values = "";
		var logger = "";
		var hashArray = [];
		for(rp in send.requestParameter){
			var hashCode = HashUtil.javaDefaultHash(rp.key)+"";
			if(array.contain(hashArray,hashCode)){
				println("		"+rp.key+":HashCode重复,请重命名");
				continue;
			}else{
				array.add(hashArray,hashCode);
			}
			parameter = parameter + ", " + rp.value.intact + " " + rp.key;
			values = values + ", " + rp.key;
            logger = logger + rp.key + ":{} ";
		%>
		${rp.value.intact} ${rp.key} = StructUtil.tryDecoder(proxy.get(${hashCode}));
		<%}%>
		LogFactory.get().debug("Req ${send.requestName} -> ${logger}"${values});
		return ${send.requestName}(session, packet${values});
	}
	
	/** ${send.remark} */
	Consumer ${send.requestName}(Session session,NetworkPacket packet${parameter});

	<%
		parameter = "";
		values = "";
		logger = "";
		for(rp in send.responseParameter){
			parameter = parameter + ", " + rp.value.intact + " " + rp.key;
			values = values + ", " + rp.key;
			logger = logger + rp.key + ":{} ";
		}
	%>
	default Consumer _${send.requestName}(Session session${parameter}){
		return (NetworkPacket packet) -> {
		        packet.setHeader(NetworkHeader.STATUS,0);
		        packet.getBody().clear();
				<%
				for(rp in send.responseParameter){
					var hashCode = HashUtil.javaDefaultHash(rp.key)+"";
				%>
				packet.getBody().put(${hashCode},StructUtil.tryEncoder(${rp.key}));
				<%}%>
				LogFactory.get().debug("Resp ${send.requestName} -> ${logger}"${values});
				session.send(packet);
			};
	}
	
	default Consumer _${send.requestName}_error(Session session,Throwable throwable,Consumer consumer){
        return (NetworkPacket packet) -> {
            int errorCode = LogicUtil.getErrorCode(throwable,consumer);
            packet.setHeader(NetworkHeader.STATUS, errorCode);
            packet.getBody().clear();
            LogFactory.get().debug("Resp ${send.requestName} -> Error : {}",errorCode);
            session.send(packet);
        };
	}
	<%}%>
	
	<%
	for(push in module.protocolPushs){
		var parameter = "";
		var values = "";
        var logger = "";
		for(p in push.pushParameter){
			parameter = parameter + ", " + p.value.intact + " " + p.key;
			values = values + ", " + p.key;
            logger = logger + p.key + ":{} ";
		}
	%>
	
	/** ${push.remark} */
	default void ${push.pushName}(Session session${parameter},BiConsumer consumer){
		NetworkPacket packet = new NetworkPacket();
		packet.setHeader(NetworkHeader.COMMAND, ${ProtocolUtil.getCommandId(module.id,push.id)});
        packet.setHeader(NetworkHeader.MODULE, ${module.id});
        packet.setHeader(NetworkHeader.METHOD, ${push.id});
		<%
		for(rp in push.pushParameter){
			var hashCode = HashUtil.javaDefaultHash(rp.key)+"";
		%>
		packet.getBody().put(${hashCode},StructUtil.tryEncoder(${rp.key}));
		<%}%>
		LogFactory.get().debug("Push ${push.pushName} -> ${logger}"${values});
		if(session != null){
		    consumer.accept(session,packet);
		}
	}
	
	default void ${push.pushName}(Collection sessions${parameter},BiConsumer consumer){
		NetworkPacket packet = new NetworkPacket();
		packet.setHeader(NetworkHeader.COMMAND, ${ProtocolUtil.getCommandId(module.id,push.id)});
		packet.setHeader(NetworkHeader.MODULE, ${module.id});
        packet.setHeader(NetworkHeader.METHOD, ${push.id});
		<%
		for(rp in push.pushParameter){
			var hashCode = HashUtil.javaDefaultHash(rp.key)+"";
		%>
		packet.getBody().put(${hashCode},StructUtil.tryEncoder(${rp.key}));
		<%}%>
        LogFactory.get().debug("Push ${push.pushName} -> ${logger}"${values});
		// IByteBuff byteBuff = ServerContext.me().getByteBuffFactory().allocate();
		IByteBuff byteBuff = ServerContext.me().getProtocolBufferProvider().create(null);
		packet.toByteBuff(byteBuff);
		byte[] data = byteBuff.array();
		for (Session session : sessions) {
		    if(session != null){
			    consumer.accept(session,data);
			}
		}
	}
	
	<%}%>
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy