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.
 
 
 
 
 
 

98 lines
2.4 KiB

/*
Sog 游戏基础库
2016 by zouwei
*/
using System;
using System.Net;
using System.Text;
using Sproto;
namespace Sog
{
public class SProtoPacker : IProtoPacker
{
//private SprotoRpc client;
private SprotoRpc service;
//private SprotoRpc.RpcRequest clientRequest;
public SProtoPacker()
{
//client = new SprotoRpc();
service = new SprotoRpc();
}
public int GetHeaderLength()
{
return 2;
}
public int GetFullHeaderLength()
{
return 2;
}
//打包头
public int PackHeader(MessageHeader header, byte[] headerBytes)
{
byte[] tmpbytes = BitConverter.GetBytes((ushort)header.Length);
headerBytes[0] = tmpbytes[1];
headerBytes[1] = tmpbytes[0];
return 2;
}
//解包头
public int UnPackHeader(byte[] data, int offset, int length, ref MessageHeader header)
{
//注意 sproto 的字节序 ...
int byte0 = data[offset + 1];
int byte1 = data[offset + 0];
header.Length = byte1 * 256 + byte0;
header.Type = 0;
header.ObjectID = 0;
return 2;
}
public virtual bool UnpackMessage(MessageData message, out RequestPacket packet)
{
try
{
SprotoRpc.RpcInfo rpcInfo = service.Dispatch(message.Buffer.Data);
TraceLog.Trace("recv package(type {0}) tag {1} session {2} ", rpcInfo.tag, rpcInfo.session, rpcInfo.type);
int msgId = rpcInfo.tag == null ? 0 : (int)rpcInfo.tag;
uint seqId = rpcInfo.session == null ? 0 : (uint)rpcInfo.session;
packet = new RequestPacket(msgId, seqId, 0);
packet.Message = rpcInfo;
return true;
}
catch(Exception error)
{
TraceLog.Error("sproto decode package error, {0}", error.Message);
packet = null;
}
return false;
}
public virtual bool PackMessage(RequestPacket packet, ref MessageData message)
{
return false;
}
public void RegisterProtoType(int iMsgID, object obj)
{
//MessageParser
}
}
}