mirror of
https://github.com/faye/websocket-driver-ruby.git
synced 2025-11-01 13:59:38 +00:00
08189c2a30
- websocket_mask.rb is in lib which in turn then loads websocket_mask.so which should reside in the extensions directory external to the gem directory at least as of rubygems 2.2.2. - Changed extensions directory to match gemname as suggested in rubygems guide Simplifies loading the native extension but only tested on ruby 2.1.1 and only in my environment.
62 lines
1.8 KiB
Java
62 lines
1.8 KiB
Java
package com.jcoglan.websocket;
|
|
|
|
import java.lang.Long;
|
|
import java.io.IOException;
|
|
|
|
import org.jruby.Ruby;
|
|
import org.jruby.RubyArray;
|
|
import org.jruby.RubyClass;
|
|
import org.jruby.RubyFixnum;
|
|
import org.jruby.RubyModule;
|
|
import org.jruby.RubyObject;
|
|
import org.jruby.anno.JRubyMethod;
|
|
import org.jruby.runtime.ObjectAllocator;
|
|
import org.jruby.runtime.ThreadContext;
|
|
import org.jruby.runtime.builtin.IRubyObject;
|
|
import org.jruby.runtime.load.BasicLibraryService;
|
|
|
|
public class WebsocketMaskService implements BasicLibraryService {
|
|
private Ruby runtime;
|
|
|
|
public boolean basicLoad(Ruby runtime) throws IOException {
|
|
this.runtime = runtime;
|
|
RubyModule websocket = runtime.defineModule("WebSocket");
|
|
|
|
RubyClass webSocketMask = websocket.defineClassUnder("Mask", runtime.getObject(), new ObjectAllocator() {
|
|
public IRubyObject allocate(Ruby runtime, RubyClass rubyClass) {
|
|
return new WebsocketMask(runtime, rubyClass);
|
|
}
|
|
});
|
|
|
|
webSocketMask.defineAnnotatedMethods(WebsocketMask.class);
|
|
return true;
|
|
}
|
|
|
|
public class WebsocketMask extends RubyObject {
|
|
public WebsocketMask(final Ruby runtime, RubyClass rubyClass) {
|
|
super(runtime, rubyClass);
|
|
}
|
|
|
|
@JRubyMethod
|
|
public IRubyObject mask(ThreadContext context, IRubyObject payload, IRubyObject mask) {
|
|
int n = ((RubyArray)payload).getLength(), i;
|
|
long p, m;
|
|
RubyArray unmasked = RubyArray.newArray(runtime, n);
|
|
|
|
long[] maskArray = {
|
|
(Long)((RubyArray)mask).get(0),
|
|
(Long)((RubyArray)mask).get(1),
|
|
(Long)((RubyArray)mask).get(2),
|
|
(Long)((RubyArray)mask).get(3)
|
|
};
|
|
|
|
for (i = 0; i < n; i++) {
|
|
p = (Long)((RubyArray)payload).get(i);
|
|
m = maskArray[i % 4];
|
|
unmasked.set(i, p ^ m);
|
|
}
|
|
return unmasked;
|
|
}
|
|
}
|
|
}
|