zoukankan      html  css  js  c++  java
  • FastNet C++/Python 网络通信库之 协议

    协议可以使用的基础数据类型:

    UInt8,UInt16,UInt32,UInt64
    Int8,Int16,Int32,Int64
    Float,Double,Bool,String

    [T]  数组,T代表元素类型
    [K,V] 字典,K代表键,V代表值,注意键只能为基础类型。

    以及三种不同的协议类型类型:

    Message  和protobuf的message类似  一个Message就代表一个协议,Message可以包含Struct和Enum,可以包含基础数据类型

    Struct  结构体,和Message的区别就是结构体只能包含基础数据类型和枚举(Enum),还有就是结构体在生成代码时不会生成相关的协议ID部分代码,因为Struct就是被设计为被嵌入到其他Message当中。

    Enum  枚举,只能包含整数

    支持默认值(直接在类型后面加上双括号以及变量值)

    下面是协议的一个例子:

    from GenProto import *
     
    class DemoEnum(Enum):
        one = 1
        two = 2
    
    class DemoStruct(Struct):
        u8 = UInt8
        u16 = UInt16
        u32 = UInt32
        u64 = UInt64
        i8 = Int8
        i16 = Int16(1111)
        i32 = Int32
        i64 = Int64(1000000000000)
        f = Float
        d = Double
        b = Bool(True)
        s = String('defVal')
        de = DemoEnum(DemoEnum.two)
    
    class DemoMessage(Message):
        u8 = UInt8(255)
        u16 = UInt16
        u32 = UInt32
        u64 = UInt64
        i8 = Int8(-127)
        i16 = Int16
        i32 = Int32
        i64 = Int64
        f = Float(0.234234)
        d = Double
        b = Bool
        s = String
        de = DemoEnum(DemoEnum.one)
        ds = DemoStruct
        arr = [DemoStruct]
        arr2 = [String]
        dict2 = [String,DemoStruct]
        dict3 = [UInt32,String]

    下面是经过协议工具生成的C++/Python结构体:

      1 #pragma once
      2 #include "FastNet/core/ClassFactory.h"
      3 #include "FastNet/core/Stream.h"
      4 #include "FastNet/core/App.h"
      5 #include "FastNet/core/Cfg.h"
      6 
      7 
      8 namespace pkt{
      9 namespace demo{
     10 
     11 using FastNet::UInt8;
     12 using FastNet::UInt16;
     13 using FastNet::UInt32;
     14 using FastNet::UInt64;
     15 using FastNet::Int8;
     16 using FastNet::Int16;
     17 using FastNet::Int32;
     18 using FastNet::Int64;
     19 using FastNet::Double;
     20 using FastNet::Float;
     21 using FastNet::String;
     22 using FastNet::Bool;
     23     enum class DemoEnum:FastNet::Int32 {
     24         one = 1,
     25         two = 2,
     26     };
     27 
     28     struct DemoStruct : public FastNet::Struct {
     29         bool operator==(DemoStruct const& ) const { return false; }
     30         bool operator!=(DemoStruct const& ) const { return true; }
     31         DefStruct("pkt.demo.DemoStruct",610075309,217574281)
     32         UInt8 u8=0;
     33         UInt16 u16=0;
     34         UInt32 u32=0;
     35         UInt64 u64=0;
     36         Int8 i8=0;
     37         Int16 i16=0;
     38         Int32 i32=0;
     39         Int64 i64=0;
     40         Float f=0.0f;
     41         Double d=0.0;
     42         Bool b=false;
     43         String s="";
     44         DemoEnum de=(DemoEnum)0;
     45 
     46         virtual void Deserialize(FastNet::StreamRead& stream) {
     47             stream.read(u8);
     48             stream.read(u16);
     49             stream.read(u32);
     50             stream.read(u64);
     51             stream.read(i8);
     52             stream.read(i16);
     53             stream.read(i32);
     54             stream.read(i64);
     55             stream.read(f);
     56             stream.read(d);
     57             stream.read(b);
     58             stream.read(s);
     59             stream.read(de);
     60         }
     61 
     62         virtual void Serialize(FastNet::StreamWrite& stream) {
     63             stream.write(u8);
     64             stream.write(u16);
     65             stream.write(u32);
     66             stream.write(u64);
     67             stream.write(i8);
     68             stream.write(i16);
     69             stream.write(i32);
     70             stream.write(i64);
     71             stream.write(f);
     72             stream.write(d);
     73             stream.write(b);
     74             stream.write(s);
     75             stream.write(de);
     76         }
     77 
     78         virtual FastNet::UInt32 SerializeSize() override{
     79             FastNet::UInt32 size(0);
     80             CalcSerializeSize(size,u8);
     81             CalcSerializeSize(size,u16);
     82             CalcSerializeSize(size,u32);
     83             CalcSerializeSize(size,u64);
     84             CalcSerializeSize(size,i8);
     85             CalcSerializeSize(size,i16);
     86             CalcSerializeSize(size,i32);
     87             CalcSerializeSize(size,i64);
     88             CalcSerializeSize(size,f);
     89             CalcSerializeSize(size,d);
     90             CalcSerializeSize(size,b);
     91             CalcSerializeSize(size,s);
     92             CalcSerializeSize(size,de);
     93             return size;
     94         }
     95 
     96         virtual void Reset() override{
     97             u8=0;
     98             u16=0;
     99             u32=0;
    100             u64=0;
    101             i8=0;
    102             i16=0;
    103             i32=0;
    104             i64=0;
    105             f=0.0f;
    106             d=0.0;
    107             b=false;
    108             s="";
    109             de=(DemoEnum)0;
    110         }
    111 
    112         static void Export(){
    113             boost::python::class_<DemoStruct,boost::python::bases<FastNet::Struct>,boost::shared_ptr<DemoStruct>,boost::noncopyable> _e("DemoStruct");
    114             boost::python::scope _s = _e;
    115             _e.def_readwrite("u8",&DemoStruct::u8);
    116             _e.def_readwrite("u16",&DemoStruct::u16);
    117             _e.def_readwrite("u32",&DemoStruct::u32);
    118             _e.def_readwrite("u64",&DemoStruct::u64);
    119             _e.def_readwrite("i8",&DemoStruct::i8);
    120             _e.def_readwrite("i16",&DemoStruct::i16);
    121             _e.def_readwrite("i32",&DemoStruct::i32);
    122             _e.def_readwrite("i64",&DemoStruct::i64);
    123             _e.def_readwrite("f",&DemoStruct::f);
    124             _e.def_readwrite("d",&DemoStruct::d);
    125             _e.def_readwrite("b",&DemoStruct::b);
    126             _e.def_readwrite("s",&DemoStruct::s);
    127             _e.def_readwrite("de",&DemoStruct::de);
    128             _e.add_static_property("sClsId",&DemoStruct::sClsId);
    129             _e.add_static_property("sClsName",&DemoStruct::sClsName);
    130             _e.add_static_property("sSignId",&DemoStruct::sSignId);
    131         }
    132     };
    133 
    134     class DemoMessage final: public FastNet::Message {
    135     public:
    136         DefStruct("pkt.demo.DemoMessage",3264359728,2340124217)
    137         UInt8 u8=0;
    138         UInt16 u16=0;
    139         UInt32 u32=0;
    140         UInt64 u64=0;
    141         Int8 i8=0;
    142         Int16 i16=0;
    143         Int32 i32=0;
    144         Int64 i64=0;
    145         Float f=0.0f;
    146         Double d=0.0;
    147         Bool b=false;
    148         String s="";
    149         DemoEnum de=(DemoEnum)0;
    150         DemoStruct ds;
    151         std::vector<DemoStruct> arr;
    152         std::vector<String> arr2;
    153         std::map<String,DemoStruct> dict2;
    154         std::map<UInt32,String> dict3;
    155 
    156         virtual void Deserialize(FastNet::StreamRead& stream) {
    157             stream.read(u8);
    158             stream.read(u16);
    159             stream.read(u32);
    160             stream.read(u64);
    161             stream.read(i8);
    162             stream.read(i16);
    163             stream.read(i32);
    164             stream.read(i64);
    165             stream.read(f);
    166             stream.read(d);
    167             stream.read(b);
    168             stream.read(s);
    169             stream.read(de);
    170             stream.read(ds);
    171             stream.read(arr);
    172             stream.read(arr2);
    173             stream.read(dict2);
    174             stream.read(dict3);
    175         }
    176 
    177         virtual void Serialize(FastNet::StreamWrite& stream) {
    178             stream.write(u8);
    179             stream.write(u16);
    180             stream.write(u32);
    181             stream.write(u64);
    182             stream.write(i8);
    183             stream.write(i16);
    184             stream.write(i32);
    185             stream.write(i64);
    186             stream.write(f);
    187             stream.write(d);
    188             stream.write(b);
    189             stream.write(s);
    190             stream.write(de);
    191             stream.write(ds);
    192             stream.write(arr);
    193             stream.write(arr2);
    194             stream.write(dict2);
    195             stream.write(dict3);
    196         }
    197 
    198         virtual FastNet::UInt32 SerializeSize() override{
    199             FastNet::UInt32 size(0);
    200             CalcSerializeSize(size,u8);
    201             CalcSerializeSize(size,u16);
    202             CalcSerializeSize(size,u32);
    203             CalcSerializeSize(size,u64);
    204             CalcSerializeSize(size,i8);
    205             CalcSerializeSize(size,i16);
    206             CalcSerializeSize(size,i32);
    207             CalcSerializeSize(size,i64);
    208             CalcSerializeSize(size,f);
    209             CalcSerializeSize(size,d);
    210             CalcSerializeSize(size,b);
    211             CalcSerializeSize(size,s);
    212             CalcSerializeSize(size,de);
    213             CalcSerializeSize(size,ds);
    214             CalcSerializeSize(size,arr);
    215             CalcSerializeSize(size,arr2);
    216             CalcSerializeSize(size,dict2);
    217             CalcSerializeSize(size,dict3);
    218             return size;
    219         }
    220 
    221         virtual void Reset() override{
    222             u8=0;
    223             u16=0;
    224             u32=0;
    225             u64=0;
    226             i8=0;
    227             i16=0;
    228             i32=0;
    229             i64=0;
    230             f=0.0f;
    231             d=0.0;
    232             b=false;
    233             s="";
    234             de=(DemoEnum)0;
    235             ds.Reset();
    236             arr.clear();
    237             arr2.clear();
    238             dict2.clear();
    239             dict3.clear();
    240         }
    241 
    242         static void Export(){
    243             boost::python::class_<DemoMessage,boost::python::bases<FastNet::Message>,boost::shared_ptr<DemoMessage>,boost::noncopyable> _e("DemoMessage");
    244             boost::python::scope _s = _e;
    245             _e.def_readwrite("u8",&DemoMessage::u8);
    246             _e.def_readwrite("u16",&DemoMessage::u16);
    247             _e.def_readwrite("u32",&DemoMessage::u32);
    248             _e.def_readwrite("u64",&DemoMessage::u64);
    249             _e.def_readwrite("i8",&DemoMessage::i8);
    250             _e.def_readwrite("i16",&DemoMessage::i16);
    251             _e.def_readwrite("i32",&DemoMessage::i32);
    252             _e.def_readwrite("i64",&DemoMessage::i64);
    253             _e.def_readwrite("f",&DemoMessage::f);
    254             _e.def_readwrite("d",&DemoMessage::d);
    255             _e.def_readwrite("b",&DemoMessage::b);
    256             _e.def_readwrite("s",&DemoMessage::s);
    257             _e.def_readwrite("de",&DemoMessage::de);
    258             _e.def_readwrite("ds",&DemoMessage::ds);
    259             FastNet::App::RegisterStlVector<std::vector<DemoStruct> >("v_2553082456");
    260             _e.def_readwrite("arr",&DemoMessage::arr);
    261             FastNet::App::RegisterStlVector<std::vector<String> >("v_2147306297");
    262             _e.def_readwrite("arr2",&DemoMessage::arr2);
    263             FastNet::App::RegisterStlMap<std::map<String,DemoStruct> >("m_2979050852");
    264             _e.def_readwrite("dict2",&DemoMessage::dict2);
    265             FastNet::App::RegisterStlMap<std::map<UInt32,String>,true>("m_3331818994");
    266             _e.def_readwrite("dict3",&DemoMessage::dict3);
    267             _e.add_static_property("sClsId",&DemoMessage::sClsId);
    268             _e.add_static_property("sClsName",&DemoMessage::sClsName);
    269             _e.add_static_property("sSignId",&DemoMessage::sSignId);
    270         }
    271     };
    272 
    273 
    274 } //end namespace demo
    275 } //end namespace pkt
    View Code

    使用(Python):

    m = FastNet.Message.Create(demo.DemoMessage.sClsId)
    m.u8 = 255
    m.u16 = 65535
    ...
    
    session.SendMsg(m)

    使用(C++):

    auto m = FastNet::Message::Create<demo::DemoMessage>();
    m->u8 = 255;
    m->u16 = 65535;
    ...
    
    session->SendMsg(m);
  • 相关阅读:
    表单之input的样式修改
    文本省略和文本垂直居中展示
    text-align:justify的使用
    10- 禅道使用
    09- 软件缺陷
    08- 测试用例详解
    07- 场景分析法
    01- Python语言简介
    08. linux下 mv find grep命令
    1.4.19- HTML标签之注释标签
  • 原文地址:https://www.cnblogs.com/winter-yu/p/9723604.html
Copyright © 2011-2022 走看看