├── .gitignore ├── .travis.yml ├── CHANGELOG.md ├── Gemfile ├── README.md ├── Rakefile ├── ext └── websocket_native_ext │ ├── WebSocketNativeExtService.java │ ├── extconf.rb │ └── websocket_native_ext.c ├── lib └── websocket-native.rb ├── spec ├── spec_helper.rb └── websocket_spec.rb └── websocket-native.gemspec /.gitignore: -------------------------------------------------------------------------------- 1 | Gemfile.lock 2 | lib/websocket_native_ext.* 3 | pkg/*.gem 4 | tmp 5 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: ruby 2 | script: "bundle exec rake spec" 3 | rvm: 4 | - 1.8.7 5 | - 1.9.2 6 | - 1.9.3 7 | - jruby-18mode 8 | - jruby-19mode 9 | - rbx-18mode 10 | - rbx-19mode 11 | - ree 12 | before_script: 13 | - rake compile 14 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## 1.0.0 4 | 5 | - initial release 6 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "http://rubygems.org" 2 | 3 | gemspec 4 | 5 | gem 'rake' 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # WebSocket Ruby Native Extension 2 | 3 | - Travis CI build: [![Build Status](https://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. 37 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 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 21 | -------------------------------------------------------------------------------- /ext/websocket_native_ext/WebSocketNativeExtService.java: -------------------------------------------------------------------------------- 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 | } 63 | -------------------------------------------------------------------------------- /ext/websocket_native_ext/extconf.rb: -------------------------------------------------------------------------------- 1 | require 'mkmf' 2 | extension_name = 'websocket_native_ext' 3 | dir_config(extension_name) 4 | create_makefile(extension_name) 5 | -------------------------------------------------------------------------------- /ext/websocket_native_ext/websocket_native_ext.c: -------------------------------------------------------------------------------- 1 | #include 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 | } 35 | -------------------------------------------------------------------------------- /lib/websocket-native.rb: -------------------------------------------------------------------------------- 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 31 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | 3 | require 'websocket-native' 4 | -------------------------------------------------------------------------------- /spec/websocket_spec.rb: -------------------------------------------------------------------------------- 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 26 | -------------------------------------------------------------------------------- /websocket-native.gemspec: -------------------------------------------------------------------------------- 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.require_paths = ["lib"] 20 | 21 | if RUBY_PLATFORM =~ /java/ 22 | s.platform = "java" 23 | s.files << "lib/websocket_native_ext.jar" 24 | else 25 | s.extensions << "ext/websocket_native_ext/extconf.rb" 26 | end 27 | end 28 | --------------------------------------------------------------------------------