supersocket缓冲区_SuperSocket 1.4系列文档(8) 自定义协议

Socket里面的协议解析是Socket通讯程序设计中最复杂的地方,如果你的应用层协议设计或实现不佳,Socket通讯中常见的粘包,分包就难以避免。SuperSocket内置了命令行格式的协议CommandLineProtocol,如果你的用用使用了其它格式的协议,你就必须自行实现自定义协议CustomProtocol。

实现自定义协议

实现自定义协议需要实现ICustomProtocol的Protocol类,和一个实现ICommandReader接口的CommandReader类。

public interface ICustomProtocol

where TCommandInfo : ICommandInfo

{

ICommandReader CreateCommandReader(IAppServer appServer);

}

实现ICustomProtocol需要一个实现ICommandInfo的类TCommandInfo来作为泛型参数,类应包含你的协议中每个命令/包的基本数据结构。实现CreateCommandReader方法只需根据appServer来实例化你的CommandReader, 下面就来讲讲CommandReader。

public interface ICommandReader

where TCommandInfo : ICommandInfo

{

IAppServer AppServer { get; }

TCommandInfo FindCommandInfo(IAppSession session, byte[] readBuffer, int offset, int length, bool isReusableBuffer, out int left);

int LeftBuffe

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值