Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7196128
  • 博文数量: 510
  • 博客积分: 12019
  • 博客等级: 上将
  • 技术积分: 6836
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-01 16:46
文章分类

全部博文(510)

文章存档

2022年(2)

2021年(6)

2020年(59)

2019年(4)

2018年(10)

2017年(5)

2016年(2)

2015年(4)

2014年(4)

2013年(16)

2012年(47)

2011年(65)

2010年(46)

2009年(34)

2008年(52)

2007年(52)

2006年(80)

2005年(22)

分类: JavaScript

2013-07-16 13:34:51

nodejs中c++和js交换二进制数据,采用  buffer。同时以下列子展示了v8常用到的一些其他类型转换技巧.

项目中#include <zlib.h>  是zlib库,在linux下编译时候注意带上 -lz参数,比如vi ./build/***.target.mk  修改这个文件,找到LIBS :=
然后修改为 LIBS := -lz 即可编译运行。
binary-v8-buffer.rar   可参考用例。

点击(此处)折叠或打开

  1. #include <string.h>
  2. #include <string>
  3. #include <v8.h>
  4. #include <node.h>
  5. #include <node_buffer.h>
  6. #include <zlib.h> //zlib库
  7. using namespace node;
  8. using namespace v8;
  9. using namespace std;

  10. Handle<Value> lz_uncompress(const Arguments& args) {
  11.     HandleScope scope;
  12.     if(args.Length() < 1) {
  13.      ThrowException(Exception::TypeError(String::New("Wrong number of arguments")));
  14.      return scope.Close(Undefined());
  15.     }
  16.     
  17.     //String, Number, Boolean, Object, Array等对象都是从Value继承而来,
  18.     //所以很好判断类型,通过 "Is系列函数",如 IsNumber
  19.     //另外通过“To系列函数”,如ToNumber
  20.     //
  21.     Local<Value> arg1 = args[0];
  22.     if (Buffer::HasInstance(arg1)) {
  23.         size_t size = Buffer::Length(arg1->ToObject());
  24.         char* bufferdata = Buffer::Data(arg1->ToObject());
  25.         
  26.         unsigned char buf [1024] = {'\0'};
  27.         uLong bufLen = sizeof(buf);
  28.         int ret = uncompress(buf, &bufLen, (const Bytef*)(bufferdata), (uLong)size);
  29.         if(ret != Z_OK){
  30.             ThrowException(Exception::TypeError(String::New("destination memory too little")));
  31.             return scope.Close(Undefined());
  32.         }
  33.         return scope.Close(String::New((const char*)buf,bufLen));
  34.     }
  35.     else{
  36.         ThrowException(Exception::TypeError(String::New("input is not Buffer data")));
  37.         return scope.Close(Undefined());
  38.     }
  39.   
  40.  }

  41. // Convert a JavaScript string to a std::string.
  42. // To not bother too much with string encodings
  43. //we just use ascii.
  44. string ObjectToString(Local<Value> value) {
  45.   String::Utf8Value utf8_value(value);
  46.   return string(*utf8_value);
  47. }

  48. Handle<Value> lz_compress(const Arguments& args) {
  49.     HandleScope scope;
  50.     if(args.Length() < 1) {
  51.      ThrowException(Exception::TypeError(String::New("Wrong number of arguments")));
  52.      return scope.Close(Undefined());
  53.     }


  54.   //v8::String::Utf8Value param1(args[0]->ToString());
  55.   //std::string rawStr = std::string(*param1);
  56.   Local<Value> arg1 = args[0];
  57.     std::string rawStr = ObjectToString(arg1);
  58.     
  59.   unsigned char buf [1024] = {'\0'};
  60.     uLong bufLen = sizeof(buf);
  61.   int ret = compress(buf, &bufLen, (const Bytef*)(rawStr.c_str()), (uLong)rawStr.length());
  62.     if(ret != Z_OK){
  63.         ThrowException(Exception::TypeError(String::New("destination memory too little")));
  64.     return scope.Close(Undefined());
  65.     }
  66.      
  67.      Buffer *outBuf = Buffer::New ((const char*)buf, bufLen);
  68.   return scope.Close(outBuf->handle_);
  69. }


  70. void init(Handle<Object> target) {
  71.     
  72.   NODE_SET_METHOD(target, "compress", lz_compress);
  73.   NODE_SET_METHOD(target, "uncompress", lz_uncompress);
  74. }

  75. NODE_MODULE(LZzlib, init);


阅读(9363) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~