-
Notifications
You must be signed in to change notification settings - Fork 10
/
smartfox.rb
208 lines (181 loc) · 5.63 KB
/
smartfox.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
require 'bindata'
require 'zlib'
require 'stringio'
module SmartFox2X
TYPE_BOOL = 1
TYPE_BYTE = 2
TYPE_SHORT = 3
TYPE_INT = 4
TYPE_LONG = 5
TYPE_FLOAT = 6
TYPE_DOUBLE = 7
TYPE_STRING = 8
TYPE_INT_ARRAY = 12
TYPE_ARRAY = 17
TYPE_OBJECT = 18
class SFSString < BinData::Primitive
endian :big
hide :len
int16 :len, :value => lambda { data.length }
string :data, :read_length => :len
def get
self.data
end
def set(v)
self.data = v
end
end
class Term < BinData::Record; end
class SFSArray < BinData::Record
endian :big
hide :len
int16 :len, :value => lambda { data.length }
array :data, :type => :term, :initial_length => :len
def convert
data.map do |item|
case item.otype
when TYPE_OBJECT, TYPE_ARRAY then item.payload.convert
when TYPE_BOOL..TYPE_LONG then item.payload.to_i
when TYPE_STRING then item.payload.to_s
else
item.payload
end
end
end
end
class SFSIntArray < BinData::Record
endian :big
hide :len
int16 :len, :value => lambda { data.length }
array :data, :type => :int32, :initial_length => :len
end
class SFSKeyValue < BinData::Record
sfs_string :tag
term :item
def self.build(tag, item)
kv = SFSKeyValue.new
kv.tag = tag
kv.item = item
kv
end
end
class SFSObject < BinData::Record
endian :big
hide :len
int16 :len, :value => lambda { items.length }
array :items, :type => :sfs_key_value, :initial_length => :len
def key?(key)
not send(:[], key).nil?
end
def keys
items.map { |item| item.tag }
end
def [](key)
return super(key) if self.respond_to?(key)
items.map { |item| item.item.payload if item.tag == key }.compact.first
end
def []=(key, value)
return super(key, value) if self.respond_to?(key)
if value.is_a?(String)
str = SFSString.new
str.assign(value)
set(key, Term.build(TYPE_STRING, str))
elsif value.is_a?(SFSObject)
set(key, Term.build(TYPE_OBJECT, value))
elsif value.is_a?(Array)
array = SFSArray.new
array.push(value)
set(key, Term.build(TYPE_ARRAY, array))
elsif value.is_a?(Term)
set(key, value)
elsif value.is_a?(Fixnum) or value.is_a?(Float)
raise "Fixnum and Float are ambiguous types"
end
end
def convert
hash = Hash.new
items.each do |item|
value = case item.item.otype
when TYPE_OBJECT, TYPE_ARRAY then item.item.payload.convert
when TYPE_BOOL..TYPE_LONG then item.item.payload.to_i
when TYPE_STRING then item.item.payload.to_s
else
item.item.payload
end
hash[item.tag.to_s] = value
end
hash
end
def merge(hash)
hash.each do |key, value|
send(:[]=, key, value)
end
end
def set(key, value)
for idx in (0...items.length)
if items[idx].tag == key
items[idx].item = value
return
end
end
items.push(SFSKeyValue.build(key, value))
self.len = items.length
end
end
class Term < BinData::Record
endian :big
hide :otype
uint8 :otype, :initial_value => TYPE_OBJECT
choice :payload, :selection => :otype do
int8 TYPE_BOOL
int8 TYPE_BYTE
int16 TYPE_SHORT
int32 TYPE_INT
int64 TYPE_LONG
float TYPE_FLOAT
double TYPE_DOUBLE
sfs_int_array TYPE_INT_ARRAY
sfs_string TYPE_STRING
sfs_array TYPE_ARRAY
sfs_object TYPE_OBJECT
end
def self.build(type, payload)
term = Term.new
term.otype = type
term.payload = payload
term
end
end
class SFSPacket < BinData::Record
bit1 :binary, :asserted_value => 1
bit1 :encrypted, :asserted_value => 0
bit1 :compressed
bit1 :blueboxed, :asserted_value => 0
bit1 :bigsized
resume_byte_alignment
choice :len, :selection => :bigsized do
uint16be 0
uint32be 1
end
string :data, :read_length => :len
def setData(data)
self.data = data
self.len = data.length
end
def self.read(io)
pkt = super(io)
pkt.data = Zlib::inflate(pkt.data) if pkt.compressed == 1
Term.read(StringIO.new pkt.data)
end
def self.build(p, a = 13, c = 1)
fail "p is expected to be a SFSObject" if not p.is_a?(SFSObject)
envelope = SFSPacket.new
base = Term.new
base.payload["a"] = Term.build(TYPE_SHORT, a)
base.payload["c"] = Term.build(TYPE_BYTE, c)
base.payload["p"] = p
envelope.setData(base.to_binary_s)
envelope
end
end
end