fdpass 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -32,4 +32,5 @@ void Init_fdpass() {
32
32
 
33
33
  Init_fdpass_server();
34
34
  Init_fdpass_client();
35
+ Init_fdpass_fd();
35
36
  }
@@ -26,6 +26,11 @@ struct fdpass_socket {
26
26
  VALUE path;
27
27
  };
28
28
 
29
+ struct fdpass_fd {
30
+ int fd;
31
+ int closed;
32
+ };
33
+
29
34
  #define Check_Socket(p) do { \
30
35
  if ((p)->sock < 0 || (p)->closed || NIL_P((p)->path)) { \
31
36
  rb_raise(rb_eFDPassError, "Invalid socket"); \
@@ -35,5 +40,6 @@ struct fdpass_socket {
35
40
 
36
41
  void Init_fdpass_server();
37
42
  void Init_fdpass_client();
43
+ void Init_fdpass_fd();
38
44
 
39
45
  #endif // __FDPASS_H__
@@ -0,0 +1,65 @@
1
+ #include "fdpass.h"
2
+
3
+ extern VALUE rb_mFDPass;
4
+ VALUE rb_cFDPassFD;
5
+
6
+ static void fdpass_fd_free(struct fdpass_fd *p) {
7
+ if (!p->closed && p->fd >= 0) {
8
+ close(p->fd);
9
+ }
10
+
11
+ xfree(p);
12
+ }
13
+
14
+ static VALUE fdpass_fd_alloc(VALUE klass) {
15
+ struct fdpass_fd *p = ALLOC(struct fdpass_fd);
16
+ p->fd = -1;
17
+ p->closed = 1;
18
+ return Data_Wrap_Struct(klass, 0, fdpass_fd_free, p);
19
+ }
20
+
21
+ static VALUE rd_fdpass_fd_close(VALUE self) {
22
+ struct fdpass_fd *p;
23
+
24
+ Data_Get_Struct(self, struct fdpass_fd, p);
25
+
26
+ if (!p->closed && p->fd >= 0) {
27
+ close(p->fd);
28
+ p->fd = -1;
29
+ p->closed = 1;
30
+ }
31
+
32
+ return Qnil;
33
+ }
34
+
35
+ static VALUE rd_fdpass_fd_is_closed(VALUE self) {
36
+ struct fdpass_fd *p;
37
+ Data_Get_Struct(self, struct fdpass_fd, p);
38
+ return p->closed ? Qtrue : Qfalse;
39
+ }
40
+
41
+ static VALUE rd_fdpass_fd_to_int(VALUE self) {
42
+ struct fdpass_fd *p;
43
+ Data_Get_Struct(self, struct fdpass_fd, p);
44
+ return INT2FIX(p->fd);
45
+ }
46
+
47
+ static VALUE rd_fdpass_fd_initialize(VALUE self, VALUE fd) {
48
+ struct fdpass_fd *p;
49
+
50
+ Data_Get_Struct(self, struct fdpass_fd, p);
51
+
52
+ p->fd = NUM2INT(fd);
53
+ p->closed = 0;
54
+
55
+ return Qnil;
56
+ }
57
+
58
+ void Init_fdpass_fd() {
59
+ rb_cFDPassFD = rb_define_class_under(rb_mFDPass, "FD", rb_cObject);
60
+ rb_define_alloc_func(rb_cFDPassFD, fdpass_fd_alloc);
61
+ rb_define_private_method(rb_cFDPassFD, "initialize", rd_fdpass_fd_initialize, 1);
62
+ rb_define_method(rb_cFDPassFD, "close", rd_fdpass_fd_close, 0);
63
+ rb_define_method(rb_cFDPassFD, "closed?", rd_fdpass_fd_is_closed, 0);
64
+ rb_define_method(rb_cFDPassFD, "to_int", rd_fdpass_fd_to_int, 0);
65
+ }
@@ -2,6 +2,7 @@
2
2
 
3
3
  extern VALUE rb_mFDPass;
4
4
  extern VALUE rb_eFDPassError;
5
+ extern VALUE rb_cFDPassFD;
5
6
  VALUE rb_cFDPassServer;
6
7
 
7
8
  static void unlink_socket(struct fdpass_socket *p) {
@@ -106,6 +107,7 @@ static VALUE rb_fdpass_server_recv(VALUE self) {
106
107
  int *cmsg_data;
107
108
  struct iovec iov;
108
109
  char iov_data[1];
110
+ VALUE fd;
109
111
 
110
112
  Data_Get_Struct(self, struct fdpass_socket, p);
111
113
  Check_Socket(p);
@@ -127,7 +129,8 @@ static VALUE rb_fdpass_server_recv(VALUE self) {
127
129
  cmsg = CMSG_FIRSTHDR(&msg);
128
130
  cmsg_data = (int *) CMSG_DATA(cmsg);
129
131
 
130
- return INT2NUM(*cmsg_data);
132
+ fd = INT2NUM(*cmsg_data);
133
+ return rb_class_new_instance(1, &fd, rb_cFDPassFD);
131
134
  }
132
135
 
133
136
  void Init_fdpass_server() {
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fdpass
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 31
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 1
10
- version: 0.1.1
9
+ - 2
10
+ version: 0.1.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - winebarrel
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-08 00:00:00 +09:00
18
+ date: 2011-01-09 00:00:00 +09:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -29,6 +29,7 @@ extra_rdoc_files: []
29
29
 
30
30
  files:
31
31
  - ext/fdpass_client.c
32
+ - ext/fdpass_fd.c
32
33
  - ext/fdpass_server.c
33
34
  - ext/fdpass.c
34
35
  - ext/fdpass.h