using System;
using System.Linq;
using System.Text;
namespace Sodao.FastSocket.Server.Protocol
{
///
/// 命令行协议
///
public sealed class CommandLineProtocol : IProtocol
{
static private readonly string[] SPLITER =
new string[] { " " };
///
/// parse
///
///
///
///
///
///
/// bad command line protocol
public Messaging.CommandLineMessage Parse(SocketBase.IConnection connection, ArraySegment buffer,
int maxMessageSize, out int readlength)
{
if (buffer.Count < 2)
{
readlength = 0;
return null;
}
//查找\r\n标记符
for (int i = buffer.Offset, len = buffer.Offset + buffer.Count; i < len; i++)
{
if (buffer.Array[i] == 13 && i + 1 < len && buffer.Array[i + 1] == 10)
{
readlength = i + 2 - buffer.Offset;
if (readlength == 2) return new Messaging.CommandLineMessage(string.Empty);
if (readlength > maxMessageSize) throw new BadProtocolException("message is too long");
string command = Encoding.UTF8.GetString(buffer.Array, buffer.Offset, readlength - 2);
var arr = command.Split(SPLITER, StringSplitOptions.RemoveEmptyEntries);
if (arr.Length == 0) return new Messaging.CommandLineMessage(string.Empty);
if (arr.Length == 1) return new Messaging.CommandLineMessage(arr[0]);
return new Messaging.CommandLineMessage(arr[0], arr.Skip(1).ToArray());
}
}
readlength = 0;
return null;
}
}
}