socket_accept_filter 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,27 @@
1
+ Copyright 2005 Eric Hodel, The Robot Co-op. All rights reserved.
2
+
3
+ Redistribution and use in source and binary forms, with or without
4
+ modification, are permitted provided that the following conditions
5
+ are met:
6
+
7
+ 1. Redistributions of source code must retain the above copyright
8
+ notice, this list of conditions and the following disclaimer.
9
+ 2. Redistributions in binary form must reproduce the above copyright
10
+ notice, this list of conditions and the following disclaimer in the
11
+ documentation and/or other materials provided with the distribution.
12
+ 3. Neither the names of the authors nor the names of their contributors
13
+ may be used to endorse or promote products derived from this software
14
+ without specific prior written permission.
15
+
16
+ THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
17
+ OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
+ ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE
20
+ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
21
+ OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
22
+ OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23
+ BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24
+ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25
+ OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26
+ EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
+
@@ -0,0 +1,6 @@
1
+ LICENSE
2
+ Manifest.txt
3
+ README
4
+ Rakefile
5
+ lib/socket_accept_filter.rb
6
+ test/test_socket_accept_filter.rb
data/README ADDED
@@ -0,0 +1,63 @@
1
+ = socket_accept_filter
2
+
3
+ Rubyforge Project:
4
+
5
+ http://rubyforge.org/projects/rctools/
6
+
7
+ Documentation:
8
+
9
+ http://dev.robotcoop.com/Libraries/socket_accept_filter
10
+
11
+ == About
12
+
13
+ Socket#set_accept_filter allows you to enable accept filters on server sockets.
14
+
15
+ In FreeBSD accept filters allow you to delay the return from an accept() call
16
+ until enough data arrives on the connection for processing avoiding extra
17
+ context switches while waiting for the missing data.
18
+
19
+ Consult the accf_data(9), accf_http(9) and accept_filter(9) man pages for
20
+ further details.
21
+
22
+ == Installing socket_accept_filter
23
+
24
+ Run FreeBSD (or any BSD, I think).
25
+
26
+ Then install the gem:
27
+
28
+ $ sudo gem install socket_accept_filter
29
+
30
+ == Using socket_accept_filter
31
+
32
+ === Regular sockets
33
+
34
+ require 'rubygems'
35
+ require 'socket'
36
+ require 'socket_accept_filter'
37
+
38
+ server = TCPServer.new host, port
39
+ server.set_accept_filter 'dataready'
40
+
41
+ Then use the server socket as you would ordinarily.
42
+
43
+ === WEBrick
44
+
45
+ require 'rubygems'
46
+ require 'socket'
47
+ require 'socket_accept_filter'
48
+ require 'webrick'
49
+
50
+ class MyServer < WEBrick::HTTPServer
51
+
52
+ def listen(*args)
53
+ super
54
+
55
+ @listeners.each do |server|
56
+ server.set_accept_filter 'httpready'
57
+ end
58
+ end
59
+
60
+ end
61
+
62
+ Then use MyServer where you would ordinarily use WEBrick::HTTPServer.
63
+
@@ -0,0 +1,71 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/testtask'
4
+ require 'rake/rdoctask'
5
+ require 'rake/gempackagetask'
6
+
7
+ $VERBOSE = nil
8
+
9
+ spec = Gem::Specification.new do |s|
10
+ s.name = 'socket_accept_filter'
11
+ s.version = '1.0.0'
12
+ s.summary = 'Allows use of FreeBSD\'s accept filters for sockets.'
13
+ s.description = <<-EOF
14
+ A library that makes setting the SO_ACCEPTFILTER socket option easy. Only
15
+ known to work on FreeBSD.
16
+ EOF
17
+ s.author = 'Eric Hodel'
18
+ s.email = 'eric@robotcoop.com'
19
+
20
+ s.has_rdoc = true
21
+ s.files = File.read('Manifest.txt').split($/)
22
+ s.require_path = 'lib'
23
+
24
+ s.test_files = Dir.glob('test/test_*.rb')
25
+
26
+ s.add_dependency 'RubyInline', '>= 3.5.0'
27
+ end
28
+
29
+ desc 'Run tests'
30
+ task :default => [ :test ]
31
+
32
+ Rake::TestTask.new('test') do |t|
33
+ t.libs << 'test'
34
+ t.pattern = 'test/test_*.rb'
35
+ t.verbose = true
36
+ end
37
+
38
+ desc 'Update Manifest.txt'
39
+ task :update_manifest do
40
+ sh "find . -type f | sed -e 's%./%%' | egrep -v 'svn|swp|~' | egrep -v '^(doc|pkg)/' | sort > Manifest.txt"
41
+ end
42
+
43
+ desc 'Generate RDoc'
44
+ Rake::RDocTask.new :rdoc do |rd|
45
+ rd.rdoc_dir = 'doc'
46
+ rd.rdoc_files.add 'lib', 'README', 'LICENSE'
47
+ rd.main = 'README'
48
+ rd.options << '-d' if `which dot` =~ /\/dot/
49
+ end
50
+
51
+ desc 'Generate RDoc for dev.robotcoop.com'
52
+ Rake::RDocTask.new :dev_rdoc do |rd|
53
+ rd.rdoc_dir = '../../../www/trunk/dev/html/Libraries/socket_accept_filter'
54
+ rd.rdoc_files.add 'lib', 'README', 'LICENSE'
55
+ rd.main = 'README'
56
+ rd.options << '-d' if `which dot` =~ /\/dot/
57
+ end
58
+
59
+ desc 'Build Gem'
60
+ Rake::GemPackageTask.new spec do |pkg|
61
+ pkg.need_tar = true
62
+ end
63
+
64
+ desc 'Clean up'
65
+ task :clean => [ :clobber_rdoc, :clobber_package ]
66
+
67
+ desc 'Clean up'
68
+ task :clobber => [ :clean ]
69
+
70
+ # vim: syntax=ruby
71
+
@@ -0,0 +1,68 @@
1
+ require 'inline'
2
+ require 'socket'
3
+
4
+ class BasicSocket
5
+
6
+ inline do |builder|
7
+ builder.include '<sys/types.h>'
8
+ builder.include '<sys/socket.h>'
9
+ builder.include '<string.h>'
10
+
11
+ builder.include '"ruby.h"'
12
+ builder.include '"rubyio.h"'
13
+ builder.include '"rubysig.h"'
14
+
15
+ builder.add_compile_flags "-Wall"
16
+ builder.add_compile_flags "-W"
17
+ builder.add_compile_flags "-Wpointer-arith"
18
+ builder.add_compile_flags "-Wcast-qual"
19
+ builder.add_compile_flags "-Wcast-align"
20
+ builder.add_compile_flags "-Wwrite-strings"
21
+ builder.add_compile_flags "-Wmissing-noreturn"
22
+ builder.add_compile_flags "-Werror"
23
+
24
+ builder.add_to_init <<-EOC
25
+ VALUE rb_cSocket = rb_path2class("Socket");
26
+ VALUE mConst = rb_path2class("Socket::Constants");
27
+ rb_define_const(rb_cSocket, "SO_ACCEPTFILTER", INT2FIX(SO_ACCEPTFILTER));
28
+ rb_define_const(mConst, "SO_ACCEPTFILTER", INT2FIX(SO_ACCEPTFILTER));
29
+ EOC
30
+
31
+ builder.c_raw <<-EOC
32
+ static
33
+ VALUE
34
+ bsock_set_accept_filter(int argc, VALUE *argv, VALUE sock) {
35
+ VALUE filter_name;
36
+ OpenFile *sptr;
37
+ int s;
38
+ struct accept_filter_arg afa;
39
+
40
+ rb_scan_args(argc, argv, "10", &filter_name);
41
+
42
+ GetOpenFile(sock, sptr);
43
+ s = fileno(sptr->f);
44
+
45
+ if ((unsigned long)RSTRING(filter_name)->len > sizeof(afa.af_name))
46
+ rb_raise(rb_eArgError, "accept filter name is too long");
47
+
48
+ bzero(&afa, sizeof(afa));
49
+ strncpy(afa.af_name, StringValueCStr(filter_name), sizeof(afa.af_name));
50
+ setsockopt(s, SOL_SOCKET, SO_ACCEPTFILTER, &afa, sizeof(afa));
51
+
52
+ return Qnil;
53
+ }
54
+ EOC
55
+ end
56
+
57
+ ##
58
+ # Enables the accept filter named +filter+ on +socket+.
59
+ #
60
+ # FreeBSD comes with the filters "httpready" and "dataready". You may need
61
+ # to load kernel modules to enable these filters.
62
+ #
63
+ # See accept_filter(9), accf_data(9) and accf_http(9).
64
+
65
+ alias set_accept_filter bsock_set_accept_filter
66
+
67
+ end
68
+
@@ -0,0 +1,38 @@
1
+ #!/usr/local/bin/ruby -w
2
+
3
+ require 'test/unit'
4
+ require 'socket'
5
+
6
+ require 'rubygems'
7
+ require 'socket_accept_filter'
8
+
9
+ class TestSocketAcceptFilter < Test::Unit::TestCase
10
+
11
+ def setup
12
+ @server = TCPServer.new 'localhost', 0
13
+ end
14
+
15
+ def test_set_accept_filter
16
+ options = @server.getsockopt Socket::SOL_SOCKET, Socket::SO_ACCEPTFILTER
17
+ assert_equal "\0" * 256, options
18
+
19
+ @server.set_accept_filter 'httpready'
20
+
21
+ options = @server.getsockopt Socket::SOL_SOCKET, Socket::SO_ACCEPTFILTER
22
+ expected = "\0" * 256
23
+ expected[0...9] = 'httpready'
24
+ assert_equal expected, options
25
+ end
26
+
27
+ def test_set_accept_filter_name_too_long
28
+ assert_nothing_raised do
29
+ @server.set_accept_filter 'a' * 16 # HACK depends on header file
30
+ end
31
+
32
+ assert_raises ArgumentError do
33
+ @server.set_accept_filter 'a' * 17
34
+ end
35
+ end
36
+
37
+ end
38
+
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.8.11.6
3
+ specification_version: 1
4
+ name: socket_accept_filter
5
+ version: !ruby/object:Gem::Version
6
+ version: 1.0.0
7
+ date: 2006-03-23 00:00:00 -08:00
8
+ summary: Allows use of FreeBSD's accept filters for sockets.
9
+ require_paths:
10
+ - lib
11
+ email: eric@robotcoop.com
12
+ homepage:
13
+ rubyforge_project:
14
+ description: A library that makes setting the SO_ACCEPTFILTER socket option easy. Only known to work on FreeBSD.
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Eric Hodel
31
+ files:
32
+ - LICENSE
33
+ - Manifest.txt
34
+ - README
35
+ - Rakefile
36
+ - lib/socket_accept_filter.rb
37
+ - test/test_socket_accept_filter.rb
38
+ test_files:
39
+ - test/test_socket_accept_filter.rb
40
+ rdoc_options: []
41
+
42
+ extra_rdoc_files: []
43
+
44
+ executables: []
45
+
46
+ extensions: []
47
+
48
+ requirements: []
49
+
50
+ dependencies:
51
+ - !ruby/object:Gem::Dependency
52
+ name: RubyInline
53
+ version_requirement:
54
+ version_requirements: !ruby/object:Gem::Version::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: 3.5.0
59
+ version: