Fix socket closing by using `closesocket` API in windows instead.
MODIFY
mrbgems/mruby-io/include/mruby/ext/io.h
mrbgems/mruby-io/include/mruby/ext/io.h
@@ -15,7 +15,8 @@ struct mrb_io {
int pid; /* child's pid (for pipes) */
unsigned int readable:1,
writable:1,
- sync:1;
+ sync:1,
+ is_socket:1;
};
#define FMODE_READABLE 0x00000001

MODIFY
mrbgems/mruby-io/src/io.c
mrbgems/mruby-io/src/io.c
@@ -578,9 +578,19 @@ fptr_finalize(mrb_state *mrb, struct mrb_io *fptr, int quiet)
}
if (fptr->fd > 2) {
+#ifdef _WIN32
+ if (fptr->is_socket) {
+ if (closesocket(fptr->fd) != 0) {
+ saved_errno = WSAGetLastError();
+ }
+ fptr->fd = -1;
+ }
+#endif
+ if (fptr->fd != -1) {
if (close(fptr->fd) == -1) {
saved_errno = errno;
}
+ }
fptr->fd = -1;
}

MODIFY
mrbgems/mruby-socket/mrblib/socket.rb
mrbgems/mruby-socket/mrblib/socket.rb
@@ -178,6 +178,7 @@ class BasicSocket
def initialize(*args)
super(*args)
+ self.is_socket = true
@do_not_reverse_lookup = @@do_not_reverse_lookup
end

MODIFY
mrbgems/mruby-socket/src/socket.c
mrbgems/mruby-socket/src/socket.c
#include "mruby/variable.h"
#include "error.h"
+#include "mruby/ext/io.h"
+
#define E_SOCKET_ERROR (mrb_class_get(mrb, "SocketError"))
#if !defined(mrb_cptr)
@@ -479,6 +481,21 @@ mrb_basicsocket_shutdown(mrb_state *mrb, mrb_value self)
return mrb_fixnum_value(0);
}
+static mrb_value
+mrb_basicsocket_set_is_socket(mrb_state *mrb, mrb_value self)
+{
+ mrb_bool b;
+ struct mrb_io *io_p;
+ mrb_get_args(mrb, "b", &b);
+
+ io_p = (struct mrb_io*)DATA_PTR(self);
+ if (io_p) {
+ io_p->is_socket = b;
+ }
+
+ return mrb_bool_value(b);
+}
+
static mrb_value
mrb_ipsocket_ntop(mrb_state *mrb, mrb_value klass)
{
@@ -839,6 +856,7 @@ mrb_mruby_socket_gem_init(mrb_state* mrb)
// #sendmsg_nonblock
mrb_define_method(mrb, bsock, "setsockopt", mrb_basicsocket_setsockopt, MRB_ARGS_REQ(1)|MRB_ARGS_OPT(2));
mrb_define_method(mrb, bsock, "shutdown", mrb_basicsocket_shutdown, MRB_ARGS_OPT(1));
+ mrb_define_method(mrb, bsock, "is_socket=", mrb_basicsocket_set_is_socket, MRB_ARGS_REQ(1));
ipsock = mrb_define_class(mrb, "IPSocket", bsock);
mrb_define_class_method(mrb, ipsock, "ntop", mrb_ipsocket_ntop, MRB_ARGS_REQ(1));

MODIFY
mrbgems/mruby-socket/test/unix.rb
mrbgems/mruby-socket/test/unix.rb
-unless SocketTest.win?
-
def unixserver_test_block
path = SocketTest.tmppath
File.unlink path rescue nil
@@ -126,5 +124,3 @@ assert('UNIXSocket#recvfrom') do
# a[1][1] would be "" or something
end
end
-
-end # win?

Fix socket closing by using `closesocket` API in windows instead.