aardio 官方社区

 找回密码
 注册会员

QQ登录

只需一步,快速开始

搜索
查看: 53750|回复: 3

请教protobuf的bytes类型使用

[复制链接]

39

主题

163

回帖

1351

积分

四级会员

积分
1351
发表于 2012-7-19 15:58:29 | 显示全部楼层 |阅读模式
本帖最后由 wudijushi 于 2012-7-23 11:05 编辑

定义的message如下:
  1. message Msg{
  2. required int32 cmd = 1;
  3. optional bool compress = 2;

  4. message Sub{
  5. optional int32 cmd = 1;
  6. optional bytes msg = 2;
  7. }
  8. optional Sub context = 3;
  9. }
复制代码
bytesMay contain any arbitrary sequence of bytes.stringByteString
protobuf language guide 是这么描述bytes类型的,我理解它是个字节数组(不是是否有错?)


我的目的是使用 msg 字段存储编译过的aardio代码
  1. var bytes = {byte msg[] = dumpcode( assert( loadcode("test.aardio")))};
  2. var send = Msg();
  3. send.context = Msg.Sub();
  4. send.cmd = 1;
  5. send.context.msg = bytes.msg;
  6. send.serializeToString();
复制代码
其实我也尝试过直接传递  string.load("test.aardio")  或者 dumpcode( assert( loadcode("test.aardio"))) 到send.context.msg,但都会报错!
估计是我使用 bytes 类型不当,message库报的错也不理解,所以不知怎样修正,请求大家指导,谢谢!

-------------------------------------------------------------------------------------------------------------------------------------------------------
二次补充:
protobuf 生成的aardio库:
   
import protobuf.message;
import util.metaProperty;
class Msg {
   
ctor(reader){
        
this = ..protobuf.message(reader)
        
        
this.fields[1] = "..protobuf.type.int";
        
this.values[1] = null;
     
        
this.fields[2] = "..protobuf.type.bool";
        
this.values[2] = null;
     
        
this.fields[3] = "..Msg.Sub";
        
this.values[3] = null;
      
    }
    @_metaProperty ;
}

namespace Msg {
   
class Sub {
        
ctor(reader){
            
this = ..protobuf.message(reader)
            
               
this.fields[1] = "..protobuf.type.int";
            
this.values[1] = null;
         
            
this.fields[2] = "..protobuf.type.bytes";
            
this.values[2] = null;
           
        }
        @_metaProperty ;
    }
   
   
namespace Sub {
      
      
        _metaProperty  = ..util.metaProperty(
            _tostring =
function(){
               
return owner.serializeToString();
            };
            
            cmd = {
                _get =
function(){
                    
return owner._get_value(1);
                }
                _set =
function( value ){
                    
return owner._set_value(1, value);
                }   
            };
         
            msg = {
                _get =
function(){
                    
return owner._get_value(2);
                }
                _set =
function( value ){
                    
return owner._set_value(2, value);
                }   
            };
            
        )
    }
   
/**intellisense()
Msg.Sub = protobuf消息对象
Msg.Sub() = 创建protobuf消息对象
?Msg.Sub = !Msg_Sub.
!Msg_Sub.parseFromString(__/*字符串*/) = 二进制数据反序列化到消息对象
此函数自动清空所有数组值,但不会重置其他非数组字段值.
因此应对新创建的对象调用此函数.
!Msg_Sub.serializeToString() = 序列化消息对象,返回二进制字符串
!Msg_Sub.cmd = protobuf.type.int
!Msg_Sub.msg = protobuf.type.bytes
end intellisense**/
  
   
    _metaProperty  = ..util.metaProperty(
        _tostring =
function(){
            
return owner.serializeToString();
        };
        
    cmd = {
            _get =
function(){
               
return owner._get_value(1);
            }
            _set =
function( value ){
               
return owner._set_value(1, value);
            }   
        };
     
        compress = {
            _get =
function(){
               
return owner._get_value(2);
            }
            _set =
function( value ){
               
return owner._set_value(2, value);
            }   
        };
     
        context = {
            _get =
function(){
               
return owner._get_value(3);
            }
            _set =
function( value ){
               
return owner._set_value(3, value);
            }   
        };
        
    )
}

/**intellisense()
Msg = protobuf消息对象
Msg() = 创建protobuf消息对象
?Msg = !Msg.
!Msg.parseFromString(__/*字符串*/) = 二进制数据反序列化到消息对象
此函数自动清空所有数组值,但不会重置其他非数组字段值.
因此应对新创建的对象调用此函数.
!Msg.serializeToString() = 序列化消息对象,返回二进制字符串
!Msg.cmd = protobuf.type.int
!Msg.compress = protobuf.type.bool
!Msg.context = !Msg_Sub.
end intellisense**/


/*
message Msg{
    required int32 cmd = 1;
    optional bool compress = 2;
   
    message Sub{
        optional int32 cmd = 1;
        optional bytes msg = 2;
    }
    optional Sub context = 3;
}
*/


单独测试代码:
var code = /*
import win;
win.msgbox("hello")
*/

import console
import Msg;

var send = Msg();
send.context = Msg.Sub()

send.cmd =1 ;
send.context.msg =
dumpcode(assert(loadcode(code)))

var fun = loadcode(send.context.msg);
fun()

var bin = send.serializeToString()

console.log(
"size of send.context.msg",#send.context.msg)
console.log(
"size of bin:",#bin)

var recv = Msg();
recv.context = Msg.Sub();

recv.parseFromString(bin);  
// 出错了!! 'I dont understand this wired code:5'

console.log(
"size of recv.context.msg",#recv.context.msg)

4

主题

852

回帖

4689

积分

荣誉会员

积分
4689
发表于 2012-7-19 17:00:34 | 显示全部楼层
protobuf 中的 bytes就是aardio中的字符串,
所以不需要写的那么复杂,直接赋值就可以。

protobuf中的string类型也是aardio中的字符串,
区别是他会转换为UTF8编码,并且不能包含文本终结符'\0' (也就是C里的字符串指针)

另外测试你的代码并没有报错。

39

主题

163

回帖

1351

积分

四级会员

积分
1351
 楼主| 发表于 2012-7-19 21:40:47 来自手机 | 显示全部楼层
不争 发表于 2012-7-19 17:00  protobuf 中的 bytes就是aardio中的字符串,  所以不需要写的那么复杂,直接赋值就可以。  

你好,<不争>,不好意思,搞错了,应该是在反序列时出错的…sorry

1

主题

4

回帖

119

积分

一级会员

积分
119
发表于 2017-11-20 21:31:58 | 显示全部楼层
应该还不支持3.0以上吧。3.0有个默认不需要required,还多了一个map。不知道这个库能否支撑
您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

手机版|未经许可严禁引用或转载本站文章|aardio.com|aardio 官方社区 ( 皖ICP备09012014号 )

GMT+8, 2025-5-25 04:44 , Processed in 0.056701 second(s), 22 queries .

Powered by Discuz! X3.5

Copyright © 2001-2024 Tencent Cloud.

快速回复 返回顶部 返回列表