-
Notifications
You must be signed in to change notification settings - Fork 30
add readCall/writeReply in V1.0 #58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -361,7 +361,7 @@ proto._readSparseObject = function (withType) { | |
| label = this.byteBuffer.getChar(this.byteBuffer.position()); | ||
| } | ||
| // skip 'z' char | ||
| this.byteBuffer.position(this.byteBuffer.position() + 1); | ||
| this.byteBuffer.skip(1); | ||
|
|
||
| // default type info | ||
| if (withType) { | ||
|
|
@@ -461,7 +461,7 @@ proto._readNoLengthArray = function (withType, type) { | |
| label = this.byteBuffer.getChar(this.byteBuffer.position()); | ||
| } | ||
| // skip 'z' char | ||
| this.byteBuffer.position(this.byteBuffer.position() + 1); | ||
| this.byteBuffer.skip(1); | ||
|
|
||
| arr = withType | ||
| ? { $class: type, $: arr } | ||
|
|
@@ -561,6 +561,55 @@ utils.addByteCodes(BYTE_CODES, [ | |
| 0x52, | ||
| ], 'readRef'); | ||
|
|
||
| /** | ||
| * read an call from buffer | ||
| * | ||
| * v1.0 | ||
| * ``` | ||
| * call ::= c(x63) x01 x00 header* m b16 b8 method-string (object)* z | ||
| * ``` | ||
| * | ||
| * @return {Object} | ||
| * @api public | ||
| */ | ||
| proto.readCall = function(withType) { | ||
| this._checkLabel('readCall', 'c'); | ||
|
|
||
| if (this.byteBuffer.get() !== 0x01 || this.byteBuffer.get() !== 0x00) { | ||
| new TypeError('hessian call error, expect code: 0x01 0x00'); | ||
| } | ||
|
|
||
|
|
||
| var header = {}; | ||
| var args = []; | ||
| var result = {header: header, arguments: args}; | ||
|
|
||
| while (this.byteBuffer.getChar(this.byteBuffer.position()) === 'H') { | ||
| this.byteBuffer.position(this.byteBuffer.position() + 1); //skip 'H' char | ||
| var key = this.byteBuffer.readRawString( this.byteBuffer.getUInt16() ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 括号里面前后空格去掉吧 |
||
| header[key] = this.read(withType); | ||
| } | ||
|
|
||
| this._checkLabel('readCall.method', 'm'); | ||
| var methodLength = this.byteBuffer.getUInt16(); | ||
| result.method = this.byteBuffer.readRawString(methodLength); | ||
|
|
||
| // | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 去掉没用到的注释 |
||
| var label = this.byteBuffer.getChar(this.byteBuffer.position()); | ||
| while (label !== 'z') { | ||
| args.push(this.read(withType)); | ||
| label = this.byteBuffer.getChar(this.byteBuffer.position()); | ||
| } | ||
| // skip 'z' char | ||
| this.byteBuffer.skip(1); | ||
|
|
||
| return result; | ||
| }; | ||
|
|
||
| utils.addByteCodes(BYTE_CODES, [ | ||
| 0x63, | ||
| ], 'readCall'); | ||
|
|
||
| /** | ||
| * read any thing | ||
| * | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| /*! | ||
| * hessian.js - test/call-reply.test.js | ||
| * | ||
| * Copyright(c) 2016 | ||
| * MIT Licensed | ||
| * | ||
| * Authors: | ||
| * tangzhi <[email protected]> (http://github.com/tangzhi) | ||
| */ | ||
|
|
||
| "use strict"; | ||
|
|
||
| /** | ||
| * Module dependencies. | ||
| */ | ||
|
|
||
| var should = require('should'); | ||
| var hessian = require('../'); | ||
| var utils = require('./utils'); | ||
|
|
||
| describe('call-reply.test.js', function () { | ||
| //c x01 x00 | ||
| // m x00 x04 add2 | ||
| // I x00 x00 x00 x02 | ||
| // I x00 x00 x00 x03 | ||
| // z | ||
| var simpleBuffer = Buffer.concat([ | ||
| new Buffer(['c'.charCodeAt(0), 0x01, 0x00]), | ||
| new Buffer(['m'.charCodeAt(0), 0x00, 0x04]), new Buffer('add2'), | ||
| new Buffer(['I'.charCodeAt(0), 0x00, 0x00, 0x00, 0x02]), | ||
| new Buffer(['I'.charCodeAt(0), 0x00, 0x00, 0x00, 0x03]), | ||
| new Buffer('z') | ||
| ]); | ||
|
|
||
| it('should read call [add2(2,3)]', function () { | ||
| hessian.decode(simpleBuffer).should.eql({ | ||
| method: 'add2', | ||
| header: {}, | ||
| arguments: [2,3] | ||
| }); | ||
| }); | ||
|
|
||
| it('should write reply 5', function() { | ||
| var buf = hessian.encode({$class:"reply", $:{value: 5}}); | ||
| buf.should.be.a.Buffer; | ||
| // r x01 x00 | ||
| // I x00 x00 x00 x05 | ||
| // z | ||
| buf.should.eql(Buffer.concat([ | ||
| new Buffer(['r'.charCodeAt(0), 0x01, 0x00]), | ||
| new Buffer(['I'.charCodeAt(0), 0x00, 0x00, 0x00, 0x05]), | ||
| new Buffer('z') | ||
| ])); | ||
| }); | ||
|
|
||
| it('should write reply fault', function() { | ||
| var fault = { | ||
| message: 'time out' | ||
| }; | ||
| var buf = hessian.encode({$class:"reply", $:{fault: fault}}); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 只有 encode,decode 的测试用例呢? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 我添加的代码不是双向的,我是按需添加的。 |
||
| buf.should.be.a.Buffer; | ||
| // r x01 x00 | ||
| // f | ||
| // S x00 x04 code | ||
| // S x00 x10 ServiceException | ||
| // S x00 x07 message | ||
| // S x00 x08 time out | ||
| // z | ||
| buf.should.eql(Buffer.concat([ | ||
| new Buffer(['r'.charCodeAt(0), 0x01, 0x00]), | ||
| new Buffer('f'), | ||
| new Buffer(['S'.charCodeAt(0), 0x00, 0x04]), new Buffer('code'), | ||
| new Buffer(['S'.charCodeAt(0), 0x00, 0x10]), new Buffer('ServiceException'), | ||
| new Buffer(['S'.charCodeAt(0), 0x00, 0x07]), new Buffer('message'), | ||
| new Buffer(['S'.charCodeAt(0), 0x00, 0x08]), new Buffer('time out'), | ||
| new Buffer('z') | ||
| ])); | ||
| }); | ||
|
|
||
| it('should write reply fault with detail', function() { | ||
| var fault = { | ||
| message : 'time out', | ||
| detail : { | ||
| $class : 'java.io.FileNotFoundException', | ||
| $: {} | ||
| } | ||
| }; | ||
| var buf = hessian.encode({$class:"reply", $:{fault: fault}}); | ||
| buf.should.be.a.Buffer; | ||
| // r x01 x00 | ||
| // f | ||
| // S x00 x04 code | ||
| // S x00 x10 ServiceException | ||
| // S x00 x07 message | ||
| // S x00 x08 time out | ||
| // S x00 x06 detail | ||
| // M t x00 x1d java.io.FileNotFoundException | ||
| // z | ||
| // z | ||
| buf.should.eql(Buffer.concat([ | ||
| new Buffer(['r'.charCodeAt(0), 0x01, 0x00]), | ||
| new Buffer('f'), | ||
| new Buffer(['S'.charCodeAt(0), 0x00, 0x04]), new Buffer('code'), | ||
| new Buffer(['S'.charCodeAt(0), 0x00, 0x10]), new Buffer('ServiceException'), | ||
| new Buffer(['S'.charCodeAt(0), 0x00, 0x07]), new Buffer('message'), | ||
| new Buffer(['S'.charCodeAt(0), 0x00, 0x08]), new Buffer('time out'), | ||
| new Buffer(['S'.charCodeAt(0), 0x00, 0x06]), new Buffer('detail'), | ||
| new Buffer(['M'.charCodeAt(0), 't'.charCodeAt(0), 0x00, 0x1d]), new Buffer('java.io.FileNotFoundException'), | ||
| new Buffer('zz') | ||
| ])); | ||
| }); | ||
|
|
||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this.byteBuffer.skip(1)