You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
116 lines
3.3 KiB
116 lines
3.3 KiB
/*
|
|
Sog 游戏基础库
|
|
2016 by zouwei
|
|
*/
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Sog
|
|
{
|
|
/// <summary>
|
|
/// 底层控制类型,65500开始
|
|
/// </summary>
|
|
public enum SpecialMessageType
|
|
{
|
|
/// <summary>
|
|
/// Server->Server 服务器cluster连上后的第一个消息,注册用
|
|
/// </summary>
|
|
ClusterClientRegister = 32750,
|
|
/// <summary>
|
|
/// Gate -> Server的消息
|
|
/// </summary>
|
|
GateMsgHeaderType = 32751,
|
|
/// <summary>
|
|
/// Server->Server 大消息
|
|
/// </summary>
|
|
BigMessageStart = 32752,
|
|
/// <summary>
|
|
/// Server->Server 大消息
|
|
/// </summary>
|
|
BigMessageTrans = 32753,
|
|
|
|
/// <summary>
|
|
/// 公钥
|
|
/// </summary>
|
|
PublicKey = 32760,
|
|
|
|
/// <summary>
|
|
/// 加密钥匙,tea
|
|
/// </summary>
|
|
SessionKey = 32761,
|
|
}
|
|
|
|
/// <summary>
|
|
/// 打包后的消息头是变长的,有可能是2+2+4=8(主要和客户端使用) 有可能是2+2+4+8=16(主要服务器使用)
|
|
/// </summary>
|
|
public struct MessageHeader
|
|
{
|
|
public int Length; // 数据长度(不包括消息头自己,后面跟的数据的长度,数据长度可以为0,这种情况表示只有头信息,没有实际数据)
|
|
public int Type; // 类型
|
|
public uint ServerID; // 自定义ID,服务器ID
|
|
public long ObjectID; // 自定义ID,和逻辑有关,一般是玩家ID,sessionID,GlobalID等
|
|
|
|
public bool isZip; // 消息是否压缩, 不打包到通信协议中
|
|
}
|
|
|
|
public struct MessageCachedBuffer
|
|
{
|
|
//是否使用cache
|
|
public bool UseCache;
|
|
|
|
//数据在Data中的开始位置
|
|
public int Pos;
|
|
|
|
//数据长度,必须要有这个,实际数据长度和byte[]数组长度并不相同,一定注意了
|
|
public int Length;
|
|
|
|
// 打包后的数据,UseCache情况下请不要用Data.Length,这个是不对的
|
|
public byte[] Data;
|
|
}
|
|
|
|
public class MessageData
|
|
{
|
|
//消息头信息
|
|
public MessageHeader Header;
|
|
|
|
// 打包后的数据
|
|
public MessageCachedBuffer Buffer;
|
|
|
|
public MessageData()
|
|
{
|
|
// new struct调用默认构造函数进行struct初始化, 否则字段初始化前struct不可用
|
|
Header = new MessageHeader();
|
|
}
|
|
|
|
public void MallocData(int size)
|
|
{
|
|
this.Buffer.Data = Sog.Memory.ByteArrayCacheMgr.Instance.Malloc(size);
|
|
this.Buffer.Length = size;
|
|
this.Buffer.UseCache = true;
|
|
}
|
|
|
|
public void FreeData()
|
|
{
|
|
if(Buffer.UseCache && Buffer.Data != null)
|
|
{
|
|
Sog.Memory.ByteArrayCacheMgr.Instance.Free(this.Buffer.Data);
|
|
}
|
|
this.Buffer.Data = null;
|
|
this.Buffer.Pos = 0;
|
|
this.Buffer.Length = 0;
|
|
this.Buffer.UseCache = false;
|
|
}
|
|
|
|
public MessageData Clone()
|
|
{
|
|
MessageData newMD = new MessageData();
|
|
newMD.Header = this.Header;
|
|
newMD.MallocData(this.Buffer.Length);
|
|
System.Buffer.BlockCopy(this.Buffer.Data, 0, newMD.Buffer.Data, 0, this.Buffer.Length);
|
|
return newMD;
|
|
}
|
|
}
|
|
}
|
|
|