socket-keepalive 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +21 -0
- data/ext/extconf.rb +3 -0
- data/ext/socket_keepalive.c +188 -0
- data/ext/socket_keepalive.h +18 -0
- data/lib/socket/keepalive.rb +36 -0
- metadata +59 -0
data/README
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
= socket-keepalive
|
2
|
+
|
3
|
+
== Description
|
4
|
+
|
5
|
+
TCP Keepalive extension for Ruby
|
6
|
+
|
7
|
+
== Install
|
8
|
+
|
9
|
+
gem install socket-keepalive
|
10
|
+
|
11
|
+
== Example
|
12
|
+
|
13
|
+
require 'socket'
|
14
|
+
require 'socket/keepalive'
|
15
|
+
|
16
|
+
s = TCPSocket.open('localhost', 7)
|
17
|
+
|
18
|
+
s.keepalive = true
|
19
|
+
s.keepidle = 15
|
20
|
+
s.keepintvl = 5
|
21
|
+
s.keepcnt = 3
|
data/ext/extconf.rb
ADDED
@@ -0,0 +1,188 @@
|
|
1
|
+
#include "socket_keepalive.h"
|
2
|
+
|
3
|
+
static VALUE rb_mSocketKeepalive;
|
4
|
+
static VALUE rb_eSocketKeepaliveError;
|
5
|
+
|
6
|
+
/* get SO_KEEPALIVE */
|
7
|
+
static VALUE rb_sk_get_keepalive(VALUE self, VALUE vSockfd) {
|
8
|
+
int sockfd, optval, rv;
|
9
|
+
socklen_t optlen;
|
10
|
+
|
11
|
+
sockfd = NUM2INT(vSockfd);
|
12
|
+
|
13
|
+
optlen = sizeof(optval);
|
14
|
+
rv = getsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, (void*) &optval, &optlen);
|
15
|
+
|
16
|
+
if (rv != 0) {
|
17
|
+
rb_raise(rb_eSocketKeepaliveError, "%s", strerror(errno));
|
18
|
+
}
|
19
|
+
|
20
|
+
return(optval ? Qtrue : Qfalse);
|
21
|
+
}
|
22
|
+
|
23
|
+
/* set SO_KEEPALIVE */
|
24
|
+
static VALUE rb_sk_set_keepalive(VALUE self, VALUE vSockfd, VALUE vOptval) {
|
25
|
+
int sockfd, optval, rv;
|
26
|
+
const char *classname;
|
27
|
+
|
28
|
+
sockfd = NUM2INT(vSockfd);
|
29
|
+
|
30
|
+
switch (TYPE(vOptval)) {
|
31
|
+
case T_TRUE:
|
32
|
+
optval = 1;
|
33
|
+
break;
|
34
|
+
case T_FALSE:
|
35
|
+
optval = 0;
|
36
|
+
break;
|
37
|
+
default:
|
38
|
+
classname = rb_class2name(CLASS_OF(vOptval));
|
39
|
+
rb_raise(rb_eTypeError, "wrong argument type %s (expected TrueClass or FalseClass)", classname);
|
40
|
+
break;
|
41
|
+
}
|
42
|
+
|
43
|
+
rv = setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, (void*) &optval, sizeof(optval));
|
44
|
+
|
45
|
+
if (rv != 0) {
|
46
|
+
rb_raise(rb_eSocketKeepaliveError, "%s", strerror(errno));
|
47
|
+
}
|
48
|
+
|
49
|
+
return Qnil;
|
50
|
+
}
|
51
|
+
|
52
|
+
/* get TCP_KEEPIDLE */
|
53
|
+
static VALUE rb_sk_get_keepidle(VALUE self, VALUE vSockfd) {
|
54
|
+
#ifdef TCP_KEEPIDLE
|
55
|
+
int sockfd, optval, rv;
|
56
|
+
socklen_t optlen;
|
57
|
+
|
58
|
+
sockfd = NUM2INT(vSockfd);
|
59
|
+
|
60
|
+
optlen = sizeof(optval);
|
61
|
+
rv = getsockopt(sockfd, IPPROTO_TCP, TCP_KEEPIDLE, (void*) &optval, &optlen);
|
62
|
+
|
63
|
+
if (rv != 0) {
|
64
|
+
rb_raise(rb_eSocketKeepaliveError, "%s", strerror(errno));
|
65
|
+
}
|
66
|
+
|
67
|
+
return INT2NUM(optval);
|
68
|
+
#else
|
69
|
+
return Qnil;
|
70
|
+
#endif
|
71
|
+
}
|
72
|
+
|
73
|
+
/* set TCP_KEEPIDLE */
|
74
|
+
static VALUE rb_sk_set_keepidle(VALUE self, VALUE vSockfd, VALUE vOptval) {
|
75
|
+
#ifdef TCP_KEEPIDLE
|
76
|
+
int sockfd, optval, rv;
|
77
|
+
|
78
|
+
sockfd = NUM2INT(vSockfd);
|
79
|
+
optval = NUM2INT(vOptval);
|
80
|
+
|
81
|
+
rv = setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPIDLE, (void*) &optval, sizeof(optval));
|
82
|
+
|
83
|
+
if (rv != 0) {
|
84
|
+
rb_raise(rb_eSocketKeepaliveError, "%s", strerror(errno));
|
85
|
+
}
|
86
|
+
#endif
|
87
|
+
|
88
|
+
return Qnil;
|
89
|
+
}
|
90
|
+
|
91
|
+
/* get TCP_KEEPINTVL */
|
92
|
+
static VALUE rb_sk_get_keepintvl(VALUE self, VALUE vSockfd) {
|
93
|
+
#ifdef TCP_KEEPINTVL
|
94
|
+
int sockfd, optval, rv;
|
95
|
+
socklen_t optlen;
|
96
|
+
|
97
|
+
sockfd = NUM2INT(vSockfd);
|
98
|
+
|
99
|
+
optlen = sizeof(optval);
|
100
|
+
rv = getsockopt(sockfd, IPPROTO_TCP, TCP_KEEPINTVL, (void*) &optval, &optlen);
|
101
|
+
|
102
|
+
if (rv != 0) {
|
103
|
+
rb_raise(rb_eSocketKeepaliveError, "%s", strerror(errno));
|
104
|
+
}
|
105
|
+
|
106
|
+
return INT2NUM(optval);
|
107
|
+
#else
|
108
|
+
return Qnil;
|
109
|
+
#endif
|
110
|
+
}
|
111
|
+
|
112
|
+
/* set TCP_KEEPINTVL */
|
113
|
+
static VALUE rb_sk_set_keepintvl(VALUE self, VALUE vSockfd, VALUE vOptval) {
|
114
|
+
#ifdef TCP_KEEPINTVL
|
115
|
+
int sockfd, optval, rv;
|
116
|
+
|
117
|
+
sockfd = NUM2INT(vSockfd);
|
118
|
+
optval = NUM2INT(vOptval);
|
119
|
+
|
120
|
+
rv = setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPINTVL, (void*) &optval, sizeof(optval));
|
121
|
+
|
122
|
+
if (rv != 0) {
|
123
|
+
rb_raise(rb_eSocketKeepaliveError, "%s", strerror(errno));
|
124
|
+
}
|
125
|
+
#endif
|
126
|
+
|
127
|
+
return Qnil;
|
128
|
+
}
|
129
|
+
|
130
|
+
/* get TCP_KEEPCNT */
|
131
|
+
static VALUE rb_sk_get_keepcnt(VALUE self, VALUE vSockfd) {
|
132
|
+
#ifdef TCP_KEEPCNT
|
133
|
+
int sockfd, optval, rv;
|
134
|
+
socklen_t optlen;
|
135
|
+
|
136
|
+
sockfd = NUM2INT(vSockfd);
|
137
|
+
|
138
|
+
optlen = sizeof(optval);
|
139
|
+
rv = getsockopt(sockfd, IPPROTO_TCP, TCP_KEEPCNT, (void*) &optval, &optlen);
|
140
|
+
|
141
|
+
if (rv != 0) {
|
142
|
+
rb_raise(rb_eSocketKeepaliveError, "%s", strerror(errno));
|
143
|
+
}
|
144
|
+
|
145
|
+
return INT2NUM(optval);
|
146
|
+
#else
|
147
|
+
return Qnil;
|
148
|
+
#endif
|
149
|
+
}
|
150
|
+
|
151
|
+
/* set TCP_KEEPCNT */
|
152
|
+
static VALUE rb_sk_set_keepcnt(VALUE self, VALUE vSockfd, VALUE vOptval) {
|
153
|
+
#ifdef TCP_KEEPCNT
|
154
|
+
int sockfd, optval, rv;
|
155
|
+
|
156
|
+
sockfd = NUM2INT(vSockfd);
|
157
|
+
optval = NUM2INT(vOptval);
|
158
|
+
|
159
|
+
rv = setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPCNT, (void*) &optval, sizeof(optval));
|
160
|
+
|
161
|
+
if (rv != 0) {
|
162
|
+
rb_raise(rb_eSocketKeepaliveError, "%s", strerror(errno));
|
163
|
+
}
|
164
|
+
|
165
|
+
return Qnil;
|
166
|
+
#endif
|
167
|
+
}
|
168
|
+
|
169
|
+
void Init_socket_keepalive() {
|
170
|
+
rb_mSocketKeepalive = rb_define_module("SocketKeepalive");
|
171
|
+
rb_eSocketKeepaliveError = rb_define_class_under(rb_mSocketKeepalive, "Error", rb_eStandardError);
|
172
|
+
|
173
|
+
// TCP_KEEPIDLE
|
174
|
+
rb_define_module_function(rb_mSocketKeepalive, "get_keepalive", rb_sk_get_keepalive, 1);
|
175
|
+
rb_define_module_function(rb_mSocketKeepalive, "set_keepalive", rb_sk_set_keepalive, 2);
|
176
|
+
|
177
|
+
// TCP_KEEPIDLE
|
178
|
+
rb_define_module_function(rb_mSocketKeepalive, "get_keepidle", rb_sk_get_keepidle, 1);
|
179
|
+
rb_define_module_function(rb_mSocketKeepalive, "set_keepidle", rb_sk_set_keepidle, 2);
|
180
|
+
|
181
|
+
// TCP_KEEPINTVL
|
182
|
+
rb_define_module_function(rb_mSocketKeepalive, "get_keepintvl", rb_sk_get_keepintvl, 1);
|
183
|
+
rb_define_module_function(rb_mSocketKeepalive, "set_keepintvl", rb_sk_set_keepintvl, 2);
|
184
|
+
|
185
|
+
// TCP_KEEPCNT
|
186
|
+
rb_define_module_function(rb_mSocketKeepalive, "get_keepcnt", rb_sk_get_keepcnt, 1);
|
187
|
+
rb_define_module_function(rb_mSocketKeepalive, "set_keepcnt", rb_sk_set_keepcnt, 2);
|
188
|
+
}
|
@@ -0,0 +1,18 @@
|
|
1
|
+
#ifndef __SOCKET_KEEPALIVE_H__
|
2
|
+
#define __SOCKET_KEEPALIVE_H__
|
3
|
+
|
4
|
+
#include <sys/types.h>
|
5
|
+
#include <sys/socket.h>
|
6
|
+
#include <netinet/in.h>
|
7
|
+
#include <netinet/tcp.h>
|
8
|
+
#include <errno.h>
|
9
|
+
#include <ruby.h>
|
10
|
+
|
11
|
+
#ifndef RSTRING_PTR
|
12
|
+
#define RSTRING_PTR(s) (RSTRING(s)->ptr)
|
13
|
+
#endif
|
14
|
+
#ifndef RSTRING_LEN
|
15
|
+
#define RSTRING_LEN(s) (RSTRING(s)->len)
|
16
|
+
#endif
|
17
|
+
|
18
|
+
#endif // __SOCKET_KEEPALIVE_H__
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'socket'
|
2
|
+
require 'socket_keepalive'
|
3
|
+
|
4
|
+
class TCPSocket
|
5
|
+
def keepalive
|
6
|
+
SocketKeepalive.get_keepalive(self.fileno)
|
7
|
+
end
|
8
|
+
|
9
|
+
def keepalive=(optval)
|
10
|
+
SocketKeepalive.set_keepalive(self.fileno, optval)
|
11
|
+
end
|
12
|
+
|
13
|
+
def keepidle
|
14
|
+
SocketKeepalive.get_keepidle(self.fileno)
|
15
|
+
end
|
16
|
+
|
17
|
+
def keepidle=(optval)
|
18
|
+
SocketKeepalive.set_keepidle(self.fileno, optval)
|
19
|
+
end
|
20
|
+
|
21
|
+
def keepintvl
|
22
|
+
SocketKeepalive.get_keepintvl(self.fileno)
|
23
|
+
end
|
24
|
+
|
25
|
+
def keepintvl=(optval)
|
26
|
+
SocketKeepalive.set_keepintvl(self.fileno, optval)
|
27
|
+
end
|
28
|
+
|
29
|
+
def keepcnt
|
30
|
+
SocketKeepalive.get_keepcnt(self.fileno)
|
31
|
+
end
|
32
|
+
|
33
|
+
def keepcnt=(optval)
|
34
|
+
SocketKeepalive.set_keepcnt(self.fileno, optval)
|
35
|
+
end
|
36
|
+
end
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: socket-keepalive
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- winebarrel
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2011-04-08 00:00:00 +09:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: TCP Keepalive extension for Ruby. see tcp(7). (Linux only)
|
17
|
+
email: sgwr_dts@yahoo.co.jp
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions:
|
21
|
+
- ext/extconf.rb
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- ext/socket_keepalive.c
|
26
|
+
- ext/socket_keepalive.h
|
27
|
+
- ext/extconf.rb
|
28
|
+
- README
|
29
|
+
- lib/socket/keepalive.rb
|
30
|
+
has_rdoc: true
|
31
|
+
homepage: https://bitbucket.org/winebarrel/socket-keepalive
|
32
|
+
licenses: []
|
33
|
+
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options: []
|
36
|
+
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: "0"
|
44
|
+
version:
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: "0"
|
50
|
+
version:
|
51
|
+
requirements: []
|
52
|
+
|
53
|
+
rubyforge_project:
|
54
|
+
rubygems_version: 1.3.5
|
55
|
+
signing_key:
|
56
|
+
specification_version: 3
|
57
|
+
summary: TCP Keepalive extension for Ruby
|
58
|
+
test_files: []
|
59
|
+
|