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

ys.artifact-network.1.0.5.source-code.module.btl Maven / Gradle / Ivy

The newest version!
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 co.paralleluniverse.fibers.SuspendExecution;
import ${packageName}.structs.*;

@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) throws SuspendExecution, InterruptedException{
		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) throws SuspendExecution, InterruptedException{
		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 + ":{} ";

			var v = "";
			select(rp.value.type){
			case "byte","java.lang.Byte":
				v = "proxy.getByte";
			case "short","java.lang.Short":
				v = "proxy.getShort";
			case "int","java.lang.Integer":
				v = "proxy.getInt";
			case "long","java.lang.Long":
				v = "proxy.getLong";
			case "float","java.lang.Float":
				v = "proxy.getFloat";
			case "double","java.lang.Double":
				v = "proxy.getDouble";
			case "String","java.lang.String":
				v = "proxy.getStr";
			case "boolean","java.lang.Boolean":
				v = "proxy.getBool";
			default:
				v = "("+rp.value.intact+")proxy.get";
			}
		%>
		${rp.value.intact} ${rp.key} = ${v}(${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}) throws SuspendExecution, InterruptedException;

	<%
		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},${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},${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},${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