websocket-native 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/.travis.yml +13 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +5 -0
- data/README.md +36 -0
- data/Rakefile +20 -0
- data/ext/websocket_native_ext/WebSocketNativeExtService.java +62 -0
- data/ext/websocket_native_ext/extconf.rb +4 -0
- data/ext/websocket_native_ext/websocket_native_ext.c +34 -0
- data/lib/websocket-native.rb +30 -0
- data/spec/spec_helper.rb +3 -0
- data/spec/websocket_spec.rb +25 -0
- data/websocket-native.gemspec +21 -0
- metadata +99 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# WebSocket Ruby Native Extension
|
2
|
+
|
3
|
+
- Travis CI build: [![](http://travis-ci.org/imanel/websocket-ruby-native.png)](http://travis-ci.org/imanel/websocket-ruby-native)
|
4
|
+
- Autobahn tests: [server](http://imanel.github.com/websocket-ruby/autobahn/server/), client
|
5
|
+
|
6
|
+
This gem adds native C and Java extensions for [WebSocket gem](http://github.com/imanel/websocket-ruby). Difference between using it and staying with pure Ruby implementation is about 25%(more accurate data are available in autobahn test results)
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
WebSocket Ruby Extensionhas has no external dependencies, so it can be installed from source or directly from rubygems:
|
11
|
+
|
12
|
+
```
|
13
|
+
gem install "websocket-native"
|
14
|
+
```
|
15
|
+
|
16
|
+
or via Gemfile:
|
17
|
+
|
18
|
+
```
|
19
|
+
gem "websocket-native"
|
20
|
+
```
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
You don't need to do anything - WebSocket gem will autoload native extension whenever it is available. It's good idea to add it to Gemfile or gemspec to install it automatically with other gems.
|
25
|
+
|
26
|
+
## License
|
27
|
+
|
28
|
+
(The MIT License)
|
29
|
+
|
30
|
+
Copyright © 2012 Bernard Potocki
|
31
|
+
|
32
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
33
|
+
|
34
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
35
|
+
|
36
|
+
THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
Bundler::GemHelper.install_tasks
|
3
|
+
|
4
|
+
require 'rspec/core/rake_task'
|
5
|
+
|
6
|
+
RSpec::Core::RakeTask.new do |t|
|
7
|
+
t.rspec_opts = ["-c", "-f progress"]
|
8
|
+
t.pattern = 'spec/**/*_spec.rb'
|
9
|
+
end
|
10
|
+
|
11
|
+
task :default => :spec
|
12
|
+
|
13
|
+
spec = Gem::Specification.load('websocket.gemspec')
|
14
|
+
if RUBY_PLATFORM =~ /java/
|
15
|
+
require 'rake/javaextensiontask'
|
16
|
+
Rake::JavaExtensionTask.new('websocket_native_ext', spec)
|
17
|
+
else
|
18
|
+
require 'rake/extensiontask'
|
19
|
+
Rake::ExtensionTask.new('websocket_native_ext', spec)
|
20
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
package org.imanel.websocket;
|
2
|
+
|
3
|
+
import java.lang.Long;
|
4
|
+
import java.io.IOException;
|
5
|
+
|
6
|
+
import org.jruby.Ruby;
|
7
|
+
import org.jruby.RubyArray;
|
8
|
+
import org.jruby.RubyClass;
|
9
|
+
import org.jruby.RubyFixnum;
|
10
|
+
import org.jruby.RubyModule;
|
11
|
+
import org.jruby.RubyObject;
|
12
|
+
import org.jruby.anno.JRubyMethod;
|
13
|
+
import org.jruby.runtime.ObjectAllocator;
|
14
|
+
import org.jruby.runtime.ThreadContext;
|
15
|
+
import org.jruby.runtime.builtin.IRubyObject;
|
16
|
+
import org.jruby.runtime.load.BasicLibraryService;
|
17
|
+
|
18
|
+
public class WebSocketNativeExtService implements BasicLibraryService {
|
19
|
+
private Ruby runtime;
|
20
|
+
|
21
|
+
public boolean basicLoad(Ruby runtime) throws IOException {
|
22
|
+
this.runtime = runtime;
|
23
|
+
RubyModule webSocket = runtime.defineModule("WebSocket");
|
24
|
+
RubyModule webSocketNative = webSocket.defineModuleUnder("Native");
|
25
|
+
|
26
|
+
RubyClass webSocketNativeData = webSocketNative.defineClassUnder("Data", runtime.getObject(), new ObjectAllocator() {
|
27
|
+
public IRubyObject allocate(Ruby runtime, RubyClass rubyClass) {
|
28
|
+
return new WebSocketNativeData(runtime, rubyClass);
|
29
|
+
}
|
30
|
+
});
|
31
|
+
|
32
|
+
webSocketNativeData.defineAnnotatedMethods(WebSocketNativeData.class);
|
33
|
+
return true;
|
34
|
+
}
|
35
|
+
|
36
|
+
public class WebSocketNativeData extends RubyObject {
|
37
|
+
public WebSocketNativeData(final Ruby runtime, RubyClass rubyClass) {
|
38
|
+
super(runtime, rubyClass);
|
39
|
+
}
|
40
|
+
|
41
|
+
@JRubyMethod
|
42
|
+
public IRubyObject mask(ThreadContext context, IRubyObject payload, IRubyObject mask) {
|
43
|
+
int n = ((RubyArray)payload).getLength(), i;
|
44
|
+
long p, m;
|
45
|
+
RubyArray unmasked = RubyArray.newArray(runtime, n);
|
46
|
+
|
47
|
+
long[] maskArray = {
|
48
|
+
(Long)((RubyArray)mask).get(0),
|
49
|
+
(Long)((RubyArray)mask).get(1),
|
50
|
+
(Long)((RubyArray)mask).get(2),
|
51
|
+
(Long)((RubyArray)mask).get(3)
|
52
|
+
};
|
53
|
+
|
54
|
+
for (i = 0; i < n; i++) {
|
55
|
+
p = (Long)((RubyArray)payload).get(i);
|
56
|
+
m = maskArray[i % 4];
|
57
|
+
unmasked.set(i, p ^ m);
|
58
|
+
}
|
59
|
+
return unmasked;
|
60
|
+
}
|
61
|
+
}
|
62
|
+
}
|
@@ -0,0 +1,34 @@
|
|
1
|
+
#include <ruby.h>
|
2
|
+
|
3
|
+
VALUE WebSocket = Qnil;
|
4
|
+
VALUE WebSocketNative = Qnil;
|
5
|
+
VALUE WebSocketNativeData = Qnil;
|
6
|
+
|
7
|
+
void Init_websocket_native_ext();
|
8
|
+
VALUE method_websocket_native_data_mask(VALUE self, VALUE payload, VALUE mask);
|
9
|
+
|
10
|
+
void Init_websocket_native_ext() {
|
11
|
+
WebSocket = rb_define_module("WebSocket");
|
12
|
+
WebSocketNative = rb_define_module_under(WebSocket, "Native");
|
13
|
+
WebSocketNativeData = rb_define_class_under(WebSocketNative, "Data", rb_cObject);
|
14
|
+
rb_define_method(WebSocketNativeData, "mask", method_websocket_native_data_mask, 2);
|
15
|
+
}
|
16
|
+
|
17
|
+
VALUE method_websocket_native_data_mask(VALUE self, VALUE payload, VALUE mask) {
|
18
|
+
int n = RARRAY_LEN(payload), i, p, m;
|
19
|
+
VALUE unmasked = rb_ary_new2(n);
|
20
|
+
|
21
|
+
int mask_array[] = {
|
22
|
+
NUM2INT(rb_ary_entry(mask, 0)),
|
23
|
+
NUM2INT(rb_ary_entry(mask, 1)),
|
24
|
+
NUM2INT(rb_ary_entry(mask, 2)),
|
25
|
+
NUM2INT(rb_ary_entry(mask, 3))
|
26
|
+
};
|
27
|
+
|
28
|
+
for (i = 0; i < n; i++) {
|
29
|
+
p = NUM2INT(rb_ary_entry(payload, i));
|
30
|
+
m = mask_array[i % 4];
|
31
|
+
rb_ary_store(unmasked, i, INT2NUM(p ^ m));
|
32
|
+
}
|
33
|
+
return unmasked;
|
34
|
+
}
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require File.expand_path('../websocket_native_ext', __FILE__)
|
2
|
+
|
3
|
+
# WebSocket Native Extension
|
4
|
+
# @author Bernard "Imanel" Potocki
|
5
|
+
# @see http://github.com/imanel/websocket-ruby-extension main repository
|
6
|
+
module WebSocket
|
7
|
+
|
8
|
+
if RUBY_PLATFORM =~ /java/
|
9
|
+
require 'jruby'
|
10
|
+
org.imanel.websocket.WebSocketNativeExtService.new.basicLoad(JRuby.runtime)
|
11
|
+
end
|
12
|
+
|
13
|
+
module Frame
|
14
|
+
class Data < String
|
15
|
+
def mask_native(payload, mask)
|
16
|
+
::WebSocket::Native::Data.mask(payload, mask)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
module Native
|
22
|
+
class Data
|
23
|
+
def self.mask(payload, mask)
|
24
|
+
@instance ||= new
|
25
|
+
@instance.mask(payload, mask)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'WebSocket::Frame::Data' do
|
4
|
+
|
5
|
+
subject { WebSocket::Frame::Data.new }
|
6
|
+
|
7
|
+
it "should have mask_native defined" do
|
8
|
+
subject.respond_to?(:mask_native).should be_true
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should mask basic frame" do
|
12
|
+
bytes = [1, 2, 3, 4]
|
13
|
+
mask = [5, 6, 7, 8]
|
14
|
+
result = [4, 4, 4, 12]
|
15
|
+
subject.mask_native(bytes, mask).should eql(result)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should bask more advanced frame" do
|
19
|
+
bytes = [72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33]
|
20
|
+
mask = [23, 142, 94, 24]
|
21
|
+
result = [95, 235, 50, 116, 120, 162, 126, 111, 120, 252, 50, 124, 54]
|
22
|
+
subject.mask_native(bytes, mask).should eql(result)
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = "websocket-native"
|
5
|
+
s.version = "1.0.0"
|
6
|
+
s.platform = Gem::Platform::RUBY
|
7
|
+
s.authors = ["Bernard Potocki"]
|
8
|
+
s.email = ["bernard.potocki@imanel.org"]
|
9
|
+
s.homepage = "http://github.com/imanel/websocket-ruby-native"
|
10
|
+
s.summary = %q{Native Extension for WebSocket gem}
|
11
|
+
s.description = %q{Native Extension for WebSocket gem}
|
12
|
+
|
13
|
+
s.add_development_dependency 'rspec', '~> 2.11'
|
14
|
+
s.add_development_dependency 'rake-compiler'
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.extensions << "ext/websocket_native_ext/extconf.rb"
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: websocket-native
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Bernard Potocki
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-19 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '2.11'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '2.11'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake-compiler
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: Native Extension for WebSocket gem
|
47
|
+
email:
|
48
|
+
- bernard.potocki@imanel.org
|
49
|
+
executables: []
|
50
|
+
extensions:
|
51
|
+
- ext/websocket_native_ext/extconf.rb
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- .gitignore
|
55
|
+
- .travis.yml
|
56
|
+
- CHANGELOG.md
|
57
|
+
- Gemfile
|
58
|
+
- README.md
|
59
|
+
- Rakefile
|
60
|
+
- ext/websocket_native_ext/WebSocketNativeExtService.java
|
61
|
+
- ext/websocket_native_ext/extconf.rb
|
62
|
+
- ext/websocket_native_ext/websocket_native_ext.c
|
63
|
+
- lib/websocket-native.rb
|
64
|
+
- spec/spec_helper.rb
|
65
|
+
- spec/websocket_spec.rb
|
66
|
+
- websocket-native.gemspec
|
67
|
+
homepage: http://github.com/imanel/websocket-ruby-native
|
68
|
+
licenses: []
|
69
|
+
post_install_message:
|
70
|
+
rdoc_options: []
|
71
|
+
require_paths:
|
72
|
+
- lib
|
73
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ! '>='
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
segments:
|
80
|
+
- 0
|
81
|
+
hash: 940693438748597920
|
82
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
84
|
+
requirements:
|
85
|
+
- - ! '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
segments:
|
89
|
+
- 0
|
90
|
+
hash: 940693438748597920
|
91
|
+
requirements: []
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 1.8.24
|
94
|
+
signing_key:
|
95
|
+
specification_version: 3
|
96
|
+
summary: Native Extension for WebSocket gem
|
97
|
+
test_files:
|
98
|
+
- spec/spec_helper.rb
|
99
|
+
- spec/websocket_spec.rb
|