117 lines
3.9 KiB
C#
117 lines
3.9 KiB
C#
using System.IO;
|
|
using System.Text;
|
|
using Module.Log;
|
|
|
|
public class BinaryMessageHandle
|
|
{
|
|
private delegate void ReadPacketAction<in T>(T packet, BinaryWriter writer) where T : PacketDistributed;
|
|
|
|
public static void PacketRecive(GC_TRANDATA packet)
|
|
{
|
|
ProcessReceive(packet, ReadGC_TRANDATA);
|
|
}
|
|
|
|
public static void PacketRecive(CG_TRANDATA packet)
|
|
{
|
|
ProcessReceive(packet, ReadCG_TRANDATA);
|
|
}
|
|
|
|
private static void ProcessReceive<T>(T packet, ReadPacketAction<T> readAction) where T : PacketDistributed
|
|
{
|
|
using (var ms = new MemoryStream())
|
|
{
|
|
using (var writer = new BinaryWriter(ms))
|
|
{
|
|
readAction(packet, writer);
|
|
ms.Position = 0;
|
|
using (var reader = new BinaryReader(ms))
|
|
{
|
|
var sysId = reader.ReadByte();
|
|
var cmdId = reader.ReadByte();
|
|
var msgInfo = BinaryMessageID.GetMessageObj(sysId, cmdId);
|
|
if (msgInfo != null)
|
|
{
|
|
msgInfo.ReadMsg(reader);
|
|
var msgHandle = BinaryMessageID.GetMessageHandle(sysId, cmdId);
|
|
msgHandle.Exclude(msgInfo);
|
|
}
|
|
//else
|
|
{
|
|
LuaMessageManager.Instance.LuaProcessPacket(sysId, cmdId, ms);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void PacketSend(MemoryStream memoryStream, BinaryWriter binaryWriter)
|
|
{
|
|
var lastBytes = memoryStream.Length % 4;
|
|
for (var i = 0; i < 4 - lastBytes; ++i)
|
|
binaryWriter.Write((byte) 0);
|
|
using (var binaryReader = new BinaryReader(memoryStream))
|
|
{
|
|
var writeIntLength = memoryStream.Length / 4;
|
|
if (writeIntLength == 0)
|
|
{
|
|
var bytes = memoryStream.ToArray();
|
|
LogModule.ErrorLog("Binary packetSend Error:" + bytes);
|
|
}
|
|
else
|
|
{
|
|
memoryStream.Position = 0;
|
|
var packet = (CG_TRANDATA)PacketDistributed.CreatePacket(MessageID.PACKET_CG_TRANDATA);
|
|
for (var i = 0; i < writeIntLength; ++i)
|
|
packet.AddData(binaryReader.ReadInt32());
|
|
packet.SetLen((int)writeIntLength);
|
|
packet.SendPacket();
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void ReadGC_TRANDATA(GC_TRANDATA packet, BinaryWriter writer)
|
|
{
|
|
for (var i = 0; i < packet.dataCount; ++i)
|
|
writer.Write(packet.GetData(i));
|
|
}
|
|
|
|
private static void ReadCG_TRANDATA(CG_TRANDATA packet, BinaryWriter writer)
|
|
{
|
|
for (var i = 0; i < packet.dataCount; ++i)
|
|
writer.Write(packet.GetData(i));
|
|
}
|
|
|
|
public static BinaryMessage CreateMessage(int sysId, int cmdId)
|
|
{
|
|
return new BinaryMessage(sysId, cmdId);
|
|
}
|
|
|
|
// 以下几个函数暂时不敢移除
|
|
// 需要大规模重构Lua解析结构才能执行重构
|
|
public static string ReadString(BinaryReader binaryReader)
|
|
{
|
|
var charCht = binaryReader.ReadInt16();
|
|
var chatList = binaryReader.ReadBytes(charCht);
|
|
var str = Encoding.UTF8.GetString(chatList);
|
|
binaryReader.ReadByte();
|
|
return str;
|
|
}
|
|
|
|
public static BinaryReader GetLuaPacketReader(MemoryStream memoryStream)
|
|
{
|
|
memoryStream.Position = 0;
|
|
var binaryReader = new BinaryReader(memoryStream);
|
|
return binaryReader;
|
|
}
|
|
|
|
//public static void TestCSSendPacket()
|
|
//{
|
|
// var testProto = new TestProto();
|
|
// testProto.name = "测试ceshi";
|
|
// testProto.nodes.Add(new TestStuct());
|
|
// testProto.nodes.Add(new TestStuct());
|
|
// testProto.nodes.Add(new TestStuct());
|
|
// testProto.nodes.Add(new TestStuct());
|
|
// testProto.SendMsg();
|
|
//}
|
|
} |