Sproto协议简析

sproto所有的编码都以小端方式编码。
以传输proto协议字符串为例子,使用sproto定义协议时,格式为: 

.package {
  type 0 : integer 
  session 1 : integer
} 
get 1 { 
  request { 
    what 0 : string 
  } 
  response { 
    result 0 : string
  }
}
 实际传输时会被拆分为两部分:

protocol = {
  get = {
    tag = 1, 
    request = get.request, 
    response = get.response, 
  }
}, 
type = { 
  package = { 
    {tag = 0 typename = integer name = type }, 
    { tag = 1 typename = integer name = session },
  },
  get.request = {
    { tag = 0 typename = string name = what }, 
  }, 
  get.response = { 
    { tag = 0 typename = string name=result },
  }, 
};
 通过一系列复杂的打包拆包,最后转化为C里面的数据结构,sproto的结构由内存池、类型,协议和两个标记组成:

struct sproto { 
  struct pool memory; 
  int type_n; 
  int protocol_n; 
  struct sproto_type * type; 
  struct protocol * proto;
}; 

打包拆包的部分还在看,基本就是一些对数据块读取的协议。 

本来想着要将sproto绑定lua转化到可以直接通过C++去绑定,但是后来看到一个仁兄的做法,简单粗暴,将写好的协议直接调用sprotoparser打包然后保存起来,再通过在C里面去读取这个包来获得协议;需要处理的就是对信息的打包和拆包了。

猜你喜欢

转载自blog.csdn.net/q8547957/article/details/51850394
今日推荐