bones-rpc-adapter-msgpack 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/bones/rpc/adapter/msgpack.rb +27 -16
- data/lib/bones/rpc/adapter/msgpack/ruby.rb +128 -0
- data/lib/bones/rpc/adapter/msgpack/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c1a820a5d6ab6ad54a540a935c9e3aa952f184ec
|
4
|
+
data.tar.gz: 2c177cbe7c16a83b33b86fe5581529d06c26e4c7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7b7cf534927b6abdd024020db390532d1389866b246c3ef053ce4593d07c7e79c9b23a1e80df030515f994070abeaacedcf218971a9d6090c577e18f51efec42
|
7
|
+
data.tar.gz: 9b3a85d3401427150f6e3136c043da9b215b93201a034d1376462135eb14077e0032be0ae98eb6bb00951e607568d22b7afb9a2158bea82ff982ef44191a2e8b
|
@@ -1,11 +1,6 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
require 'bones/rpc/adapter'
|
3
|
-
|
4
|
-
if !!(RUBY_PLATFORM =~ /java/)
|
5
|
-
require 'msgpack-jruby'
|
6
|
-
else
|
7
|
-
require 'msgpack'
|
8
|
-
end
|
3
|
+
require 'msgpack'
|
9
4
|
|
10
5
|
module Bones
|
11
6
|
module RPC
|
@@ -32,23 +27,39 @@ module Bones
|
|
32
27
|
end
|
33
28
|
|
34
29
|
def unpacker(data)
|
35
|
-
|
30
|
+
Unpacker.new(StringIO.new(data))
|
36
31
|
end
|
37
32
|
|
38
33
|
def parser(data)
|
39
34
|
Adapter::Parser.new(self, data)
|
40
35
|
end
|
41
36
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
37
|
+
if !!(RUBY_PLATFORM =~ /java/)
|
38
|
+
require 'bones/rpc/adapter/msgpack/ruby'
|
39
|
+
Unpacker = Bones::RPC::Adapter::Msgpack::Ruby::Decoder
|
40
|
+
|
41
|
+
def unpacker_pos(parser)
|
42
|
+
parser.unpacker.buffer.pos
|
43
|
+
end
|
44
|
+
|
45
|
+
def unpacker_seek(parser, n)
|
46
|
+
parser.unpacker.buffer.seek(n)
|
47
|
+
return n
|
48
|
+
end
|
49
|
+
else
|
50
|
+
Unpacker = ::MessagePack::Unpacker
|
51
|
+
|
52
|
+
def unpacker_pos(parser)
|
53
|
+
size = parser.unpacker.buffer.size
|
54
|
+
pos = parser.unpacker.buffer.io.pos
|
55
|
+
(pos > size) ? (pos - size) : 0
|
56
|
+
end
|
47
57
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
58
|
+
def unpacker_seek(parser, n)
|
59
|
+
pos = unpacker_pos(parser)
|
60
|
+
parser.unpacker.buffer.skip(n - pos) if pos < n
|
61
|
+
return pos
|
62
|
+
end
|
52
63
|
end
|
53
64
|
|
54
65
|
Adapter.register self
|
@@ -0,0 +1,128 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Bones
|
4
|
+
module RPC
|
5
|
+
module Adapter
|
6
|
+
module Msgpack
|
7
|
+
module Ruby
|
8
|
+
def self.unpack(bytes)
|
9
|
+
Decoder.new(bytes).next
|
10
|
+
end
|
11
|
+
|
12
|
+
class Decoder
|
13
|
+
attr_reader :buffer
|
14
|
+
|
15
|
+
def initialize(buffer)
|
16
|
+
@buffer = ::Bones::RPC::Parser::Buffer.new(buffer)
|
17
|
+
end
|
18
|
+
|
19
|
+
def next
|
20
|
+
buffer.transaction do
|
21
|
+
consume_next
|
22
|
+
end
|
23
|
+
rescue TypeError
|
24
|
+
raise EOFError
|
25
|
+
end
|
26
|
+
alias :read :next
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
DECODINGS = []
|
31
|
+
DECODINGS[0xc0] = lambda { |d| nil }
|
32
|
+
DECODINGS[0xc2] = lambda { |d| false }
|
33
|
+
DECODINGS[0xc3] = lambda { |d| true }
|
34
|
+
DECODINGS[0xc4] = lambda { |d| d.consume_string(d.consume_byte, Encoding::BINARY) }
|
35
|
+
DECODINGS[0xc5] = lambda { |d| d.consume_string(d.consume_int16, Encoding::BINARY) }
|
36
|
+
DECODINGS[0xc6] = lambda { |d| d.consume_string(d.consume_int32, Encoding::BINARY) }
|
37
|
+
DECODINGS[0xca] = lambda { |d| d.consume_float }
|
38
|
+
DECODINGS[0xcb] = lambda { |d| d.consume_double }
|
39
|
+
DECODINGS[0xcc] = lambda { |d| d.consume_byte }
|
40
|
+
DECODINGS[0xcd] = lambda { |d| d.consume_int16 }
|
41
|
+
DECODINGS[0xce] = lambda { |d| d.consume_int32 }
|
42
|
+
DECODINGS[0xcf] = lambda { |d| d.consume_int64 }
|
43
|
+
DECODINGS[0xd0] = lambda { |d| d.consume_byte - 0x100 }
|
44
|
+
DECODINGS[0xd1] = lambda { |d| d.consume_int16 - 0x10000 }
|
45
|
+
DECODINGS[0xd2] = lambda { |d| d.consume_int32 - 0x100000000 }
|
46
|
+
DECODINGS[0xd3] = lambda { |d| d.consume_int64 - 0x10000000000000000 }
|
47
|
+
DECODINGS[0xd9] = lambda { |d| d.consume_string(d.consume_byte) }
|
48
|
+
DECODINGS[0xda] = lambda { |d| d.consume_string(d.consume_int16) }
|
49
|
+
DECODINGS[0xdb] = lambda { |d| d.consume_string(d.consume_int32) }
|
50
|
+
DECODINGS[0xdc] = lambda { |d| d.consume_array(d.consume_int16) }
|
51
|
+
DECODINGS[0xdd] = lambda { |d| d.consume_array(d.consume_int32) }
|
52
|
+
DECODINGS[0xde] = lambda { |d| Hash[*d.consume_array(d.consume_int16 * 2)] }
|
53
|
+
DECODINGS[0xdf] = lambda { |d| Hash[*d.consume_array(d.consume_int32 * 2)] }
|
54
|
+
|
55
|
+
FLOAT_FMT = 'g'.freeze
|
56
|
+
DOUBLE_FMT = 'G'.freeze
|
57
|
+
|
58
|
+
public
|
59
|
+
|
60
|
+
def consume_byte
|
61
|
+
buffer.getbyte
|
62
|
+
end
|
63
|
+
|
64
|
+
def consume_int16
|
65
|
+
(consume_byte << 8) | consume_byte
|
66
|
+
end
|
67
|
+
|
68
|
+
def consume_int32
|
69
|
+
(consume_byte << 24) | (consume_byte << 16) | (consume_byte << 8) | consume_byte
|
70
|
+
end
|
71
|
+
|
72
|
+
def consume_int64
|
73
|
+
n = (consume_byte << 56)
|
74
|
+
n |= (consume_byte << 48)
|
75
|
+
n |= (consume_byte << 40)
|
76
|
+
n |= (consume_byte << 32)
|
77
|
+
n |= (consume_byte << 24)
|
78
|
+
n |= (consume_byte << 16)
|
79
|
+
n |= (consume_byte << 8)
|
80
|
+
n |= consume_byte
|
81
|
+
n
|
82
|
+
end
|
83
|
+
|
84
|
+
def consume_float
|
85
|
+
f, = buffer.read(4).unpack(FLOAT_FMT)
|
86
|
+
f
|
87
|
+
end
|
88
|
+
|
89
|
+
def consume_double
|
90
|
+
d, = buffer.read(8).unpack(DOUBLE_FMT)
|
91
|
+
d
|
92
|
+
end
|
93
|
+
|
94
|
+
def consume_string(size, encoding=Encoding::UTF_8)
|
95
|
+
s = buffer.read(size)
|
96
|
+
s.force_encoding(encoding)
|
97
|
+
s
|
98
|
+
end
|
99
|
+
|
100
|
+
def consume_array(size)
|
101
|
+
Array.new(size) { consume_next }
|
102
|
+
end
|
103
|
+
|
104
|
+
def consume_next
|
105
|
+
b = consume_byte
|
106
|
+
if (method = DECODINGS[b])
|
107
|
+
method.call(self)
|
108
|
+
elsif b <= 0b01111111
|
109
|
+
b
|
110
|
+
elsif b & 0b11100000 == 0b11100000
|
111
|
+
b - 0x100
|
112
|
+
elsif b & 0b11100000 == 0b10100000
|
113
|
+
size = b & 0b00011111
|
114
|
+
consume_string(size)
|
115
|
+
elsif b & 0b11110000 == 0b10010000
|
116
|
+
size = b & 0b00001111
|
117
|
+
consume_array(size)
|
118
|
+
elsif b & 0b11110000 == 0b10000000
|
119
|
+
size = b & 0b00001111
|
120
|
+
Hash[*consume_array(size * 2)]
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bones-rpc-adapter-msgpack
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Bennett
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-10-
|
11
|
+
date: 2013-10-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bones-rpc
|
@@ -83,6 +83,7 @@ files:
|
|
83
83
|
- bones-rpc-adapter-msgpack.gemspec
|
84
84
|
- lib/bones-rpc-adapter-msgpack.rb
|
85
85
|
- lib/bones/rpc/adapter/msgpack.rb
|
86
|
+
- lib/bones/rpc/adapter/msgpack/ruby.rb
|
86
87
|
- lib/bones/rpc/adapter/msgpack/version.rb
|
87
88
|
homepage: ''
|
88
89
|
licenses:
|