All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
ys.artifact-network.1.0.5.source-code.cs.btl Maven / Gradle / Ivy
using System;
using System.Collections;
using System.Collections.Generic;
using Artifact.Network;
using Artifact.Network.Buffer;
using Artifact.Network.Coder;
//namespace client.network.protocol
namespace Artifact.Unity.Network
{
public interface AbstractDTO {
Dictionary toMap(Dictionary map);
object forMap(Dictionary map);
}
<%
for(struct in structs){
var extends = false;
var extendConstant = "";
if(strutil.isNotBlank(struct.extendsName)){
extends = true;
extendConstant = ": " + struct.extendsName;
}else{
extends = false;
extendConstant = ": AbstractDTO";
}
%>
// ${struct.remark}
public class ${struct.structName} ${extendConstant}{
<%
for(field in struct.fields){
%>
public ${field.value.intact} ${uf(field.key.name)} { get; set; }
<%}%>
public Dictionary toMap(Dictionary map){
<%if(extends){%>
base.toMap(map);
<%}%>
<%
var hashArray = [];
for(field in struct.fields){
hashCode = HashUtil.javaDefaultHash(field.key.name)+"";
if(array.contain(hashArray,hashCode)){
println(" "+field.key.name+":HashCode重复,请重命名");
continue;
}else{
array.add(hashArray,hashCode);
}
%>
map.Add(${hashCode},this.${uf(field.key.name)});
<%}%>
return map;
}
public object forMap(Dictionary map){
<%if(extends){%>
base.forMap(map);
<%}%>
<%
for(field in struct.fields){
hashCode = HashUtil.javaDefaultHash(field.key.name)+"";
%>
<%if(field.value.type == "Dictionary"){%>
this.${uf(field.key.name)} = new ${field.value.intact}();
foreach (KeyValuePair kv in (Dictionary)map[${hashCode}])
{
this.${uf(field.key.name)}.Add((${field.value.generics[0]})kv.Key, (${field.value.generics[1]})kv.Value);
}
<%}else if(field.value.type == "List"){%>
this.${uf(field.key.name)} = new ${field.value.intact}();
foreach (object obj in (List)map[${hashCode}])
{
this.${uf(field.key.name)}.Add((${field.value.generics[0]})obj);
}
<%}else{%>
this.${uf(field.key.name)} = (${field.value.intact})map[${hashCode}];
<%}%>
<%}%>
return this;
}
}
<%}%>
<%for(module in modules){%>
public class ${module.name}Module{
public static void disp(Packet packet) {
int command = (int)packet.Head[NetworkHeader.COMMAND];
int module = command >> 16;
int method = module << 16 ^ command;
switch (method) {
<%for(send in module.protocolSends){%>
case ${send.id}:
_${send.requestName}(packet);
break;
<%}%>
<%for(push in module.protocolPushs){%>
case ${push.id}:
_${push.pushName}(packet);
break;
<%}%>
}
}
<%
for(send in module.protocolSends){
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;
}
%>
/** ${send.remark} */
public static void ${send.requestName}(NetClient client${parameter},${uf(send.requestName)}Listener listener) {
Packet packet = new Packet();
string messageId = System.Guid.NewGuid().ToString("N");
packet.Head.Add(NetworkHeader.ID, messageId);
packet.Head.Add(NetworkHeader.COMMAND, ${ProtocolUtil.getCommandId(module.id,send.id)});
packet.Head.Add(NetworkHeader.MODULE, ${module.id});
packet.Head.Add(NetworkHeader.METHOD, ${send.id});
packet.Head.Add(NetworkHeader.TYPE, NetworkHeader.TYPE_DEFAULT);
<%
for(rp in send.requestParameter){
var hashCode = HashUtil.javaDefaultHash(rp.key);
%>
packet.Body.Add(${hashCode}, ${rp.key});
<%}%>
client.AsyncSend(packet);
// 注册监听
if(listener!=null){
ProtocolLListener.registerRequestListener(messageId,listener);
}
}
private static void _${send.requestName}(Packet packet) {
string messageId = (string)packet.Head[NetworkHeader.ID];
Delegate listener = ProtocolLListener.getRequestListener(messageId);
if(listener!=null){
<%
values = "";
for(rp in send.responseParameter){
var hashCode = HashUtil.javaDefaultHash(rp.key);
values = values + rp.key;
if(!rpLP.last){
values = values + " ,";
}
%>
${rp.value.intact} ${rp.key} = (${rp.value.intact})packet.Body[${hashCode}];
<%}%>
// 执行监听
((${uf(send.requestName)}Listener)listener)(${values});
// 移除监听
ProtocolLListener.removeRequestListener(messageId);
}
}
<%
parameter = "";
for(rp in send.responseParameter){
parameter = parameter + rp.value.intact + " " + rp.key;
if(!rpLP.last){
parameter = parameter + " ,";
}
}
%>
public delegate void ${uf(send.requestName)}Listener(${parameter});
<%}%>
<%
for(push in module.protocolPushs){
var parameter = "";
var values = "";
var hashArray = [];
for(rp in push.pushParameter){
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;
}
%>
private static void _${push.pushName}(Packet packet) {
int command = (int)packet.Head[NetworkHeader.COMMAND];
Delegate listener = ProtocolLListener.getNotifyListener(command);
if(listener!=null){
<%
values = "";
for(rp in push.pushParameter){
var hashCode = HashUtil.javaDefaultHash(rp.key);
values = values + rp.key;
if(!rpLP.last){
values = values + " ,";
}
%>
${rp.value.intact} ${rp.key} = (${rp.value.intact})packet.Body[${hashCode}];
<%}%>
// 执行监听
((${uf(push.pushName)}Listener)listener)(${values});
}
}
<%
parameter = "";
for(rp in push.pushParameter){
parameter = parameter + rp.value.intact + " " + rp.key;
if(!rpLP.last){
parameter = parameter + " ,";
}
}
%>
public delegate void ${uf(push.pushName)}Listener(${parameter});
<%}%>
}
<%}%>
// 消息路由
public static class _Dispatcher {
public static void disp(Packet packet) {
// 取得模块ID
int command = (int)packet.Head[NetworkHeader.COMMAND];
int module = command >> 16;
switch (module)
{
<%for(module in modules){%>
case ${module.id}:
${module.name}Module.disp(packet);
break;
<%}%>
default:
break;
}
}
}
public class DtoSerializationProvider{
private delegate AbstractDTO createDTO();
static Dictionary cache = new Dictionary();
static Dictionary codes = new Dictionary();
static DtoSerializationProvider()
{
<%
for(struct in structs){
var hashCode = HashUtil.javaDefaultHash(struct.structName)+"";
%>
// ${struct.remark}
cache.Add(${hashCode},() => new ${struct.structName}());
<%}%>
<%
for(struct in structs){
hashCode = HashUtil.javaDefaultHash(struct.structName)+"";
%>
// ${struct.remark}
codes.Add("${struct.structName}",${hashCode});
<%}%>
}
public static AbstractDTO create(int code)
{
return cache[code]();
}
public static int structCode(string structClass){
return codes[structClass];
}
}
public class ArtifactProtocolStreamBuffer : ArtifactStreamBuffer
{
public const int STRUCT_CODE = 183928832;
public ArtifactProtocolStreamBuffer(byte[] bytes = null) : base(bytes)
{
}
public override void WriteObject(object value)
{
if (value is AbstractDTO)
{
Dictionary table = new Dictionary();
((AbstractDTO)value).toMap(table);
table.Add(STRUCT_CODE, DtoSerializationProvider.structCode(value.GetType().Name));
WriteDict(table);
}
else
{
base.WriteObject(value);
}
}
public override object ReadObject()
{
MarkReaderIndex();
byte tag = Read();
ResetReaderIndex();
if (tag == ArtifactBufferCoder.DICT || ArtifactBufferCoder.DICT_SUB_TYPE.ContainsVal(tag))
{
Dictionary map = ReadDict();
if (map.ContainsKey(STRUCT_CODE))
{
int structCode = (int)map[STRUCT_CODE];
AbstractDTO dto = DtoSerializationProvider.create(structCode);
return dto.forMap(map);
}
else
{
return map;
}
}
else {
return base.ReadObject();
}
}
}
public class SimpleDecoder : Decoder
{
public Packet Handler(NetClient client, byte[] data)
{
using (ByteBuffer byteBuffer = new ArtifactProtocolStreamBuffer(data))
{
Packet packet = new Packet();
packet.ForByteBuff(byteBuffer);
return packet;
}
}
}
public class SimpleEncoder : Encoder
{
public byte[] Handler(NetClient client, Packet packet)
{
using (ByteBuffer byteBuffer = new ArtifactProtocolStreamBuffer())
{
byteBuffer.MarkWriterIndex();
byteBuffer.Write(new byte[4]);
packet.ToByteBuff(byteBuffer);
int length = (int)byteBuffer.GetLength();
byteBuffer.ResetWriterIndex();
byteBuffer.Write(BufferUtil.GetByte(length - 4));
return byteBuffer.ToArray();
}
}
}
public class NetworkHeader
{
/** 唯一表示 */
public const int ID = 0;
/** 命令 */
public const int COMMAND = 1;
/** 状态 */
public const int STATUS = 2;
/** 模块 */
public const int MODULE = 3;
/** 方法 */
public const int METHOD = 4;
/** 类型 */
public const int TYPE = 5;
/** 标签 */
public const int TAG = 6;
/** 后续可扩展加密方式,验证方式、校验和等等 */
/** 类型-默认包 */
public const int TYPE_DEFAULT = 1;
/** 类型-心跳包 */
public const int TYPE_HEARTBEAT = 2;
/** 类型-确认包 */
public const int TYPE_ACK = 3;
}
public class ProtocolLListener {
private static Hashtable request = new Hashtable();
private static Hashtable notify = new Hashtable();
public static void registerNotifyListener(int command, Delegate @delegate) {
notify.Add(command, @delegate);
}
public static void registerRequestListener(string messageId, Delegate @delegate)
{
request.Add(messageId, @delegate);
}
public static void removeNotifyListener(int command)
{
notify.Remove(command);
}
public static void removeRequestListener(string messageId)
{
request.Remove(messageId);
}
public static Delegate getNotifyListener(int command)
{
return (Delegate)notify[command];
}
public static Delegate getRequestListener(string messageId)
{
return (Delegate)request[messageId];
}
public static void clearNotifyListener()
{
notify.Clear();
}
public static void clearRequestListener()
{
request.Clear();
}
}
}