ys.artifact-network.1.0.0.source-code.module.btl Maven / Gradle / Ivy
package ${packageName}.modules;
import org.artifact.core.lang.IServer;
import org.artifact.core.context.session.ISession;
import org.artifact.core.context.packet.IPacket;
import cn.hutool.core.map.MapProxy;
import java.util.function.Consumer;
import java.util.Collection;
import java.util.Map;
import java.util.List;
import org.artifact.core.constant.ArtifactErrorCode;
import org.artifact.core.exception.ArtifactExceptionUtil;
import org.artifact.core.lang.IModule;
import ${packageName}.structs.*;
@SuppressWarnings("unchecked")
public interface ${module.name}Module extends IModule{
default int getModuleId(){
return ${module.id};
}
/** 分发 */
default Consumer disp(ISession session,IPacket packet) {
switch (packet.getMethodId()) {
<%for(send in module.protocolSends){%>
case ${send.id}:{
return ${send.requestName}(session,packet);
}
<%}%>
default:
throw ArtifactExceptionUtil.error(ArtifactErrorCode.NOT_FOND_ACTION);
}
}
<%
for(send in module.protocolSends){
%>
/** ${send.remark} */
default Consumer ${send.requestName}(ISession session,IPacket packet) {
MapProxy proxy = MapProxy.create(packet.getBody());
<%
var parameter = "";
var values = "";
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;
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});
<%}%>
return ${send.requestName}(session${values});
}
/** ${send.remark} */
Consumer ${send.requestName}(ISession session${parameter});
<%
parameter = "";
for(rp in send.responseParameter){
parameter = parameter + ", " + rp.value.intact + " " + rp.key;
}
%>
default Consumer _${send.requestName}(ISession session${parameter}){
return (IPacket packet) -> {
if (packet == null){
packet = IServer.me().getContext().getPacketFactory().create();
packet.setCmd(${module.id} << 16 ^ ${send.id});
} else {
packet.getBody().clear();
}
<%
for(rp in send.responseParameter){
var hashCode = HashUtil.javaDefaultHash(rp.key)+"";
%>
packet.getBody().put(${hashCode},${rp.key});
<%}%>
session.sendPacket(packet);
};
}
<%}%>
<%
for(push in module.protocolPushs){
var parameter = "";
for(p in push.pushParameter){
parameter = parameter + ", " + p.value.intact + " " + p.key;
}
%>
/** ${push.remark} */
default void ${push.pushName}(ISession session${parameter}){
IPacket packet = IServer.me().getContext().getPacketFactory().create();
packet.setCmd(${module.id} << 16 ^ ${push.id});
<%
for(rp in push.pushParameter){
var hashCode = HashUtil.javaDefaultHash(rp.key)+"";
%>
packet.getBody().put(${hashCode},${rp.key});
<%}%>
session.sendPacket(packet);
}
default void ${push.pushName}(Collection sessions${parameter}){
IPacket packet = IServer.me().getContext().getPacketFactory().create();
packet.setCmd(${module.id} << 16 ^ ${push.id});
<%
for(rp in push.pushParameter){
var hashCode = HashUtil.javaDefaultHash(rp.key)+"";
%>
packet.getBody().put(${hashCode},${rp.key});
<%}%>
for (ISession session : sessions) {
session.sendPacket(packet);
}
}
<%}%>
}