using System;
namespace Sodao.FastSocket.Server.Protocol
{
///
/// thrift protocol
///
public sealed class ThriftProtocol : IProtocol
{
///
/// parse
///
///
///
///
///
///
/// bad thrift protocol
public Messaging.ThriftMessage Parse(SocketBase.IConnection connection, ArraySegment buffer,
int maxMessageSize, out int readlength)
{
if (buffer.Count < 4)
{
readlength = 0;
return null;
}
//获取message length
var messageLength = SocketBase.Utils.NetworkBitConverter.ToInt32(buffer.Array, buffer.Offset);
if (messageLength < 14) throw new BadProtocolException("bad thrift protocol");
if (messageLength > maxMessageSize) throw new BadProtocolException("message is too long");
readlength = messageLength + 4;
if (buffer.Count < readlength)
{
readlength = 0;
return null;
}
var payload = new byte[messageLength];
Buffer.BlockCopy(buffer.Array, buffer.Offset + 4, payload, 0, messageLength);
return new Messaging.ThriftMessage(payload);
}
}
}