diff --git a/src/socket.cr b/src/socket.cr index 34e28e3b822b..e18752edaeed 100644 --- a/src/socket.cr +++ b/src/socket.cr @@ -79,7 +79,8 @@ class Socket < IO end end - protected def initialize(@fd : Int32, @family, @type, @protocol = Protocol::IP, blocking = false) + # Creates a Socket from an existing socket file descriptor. + def initialize(@fd : Int32, @family, @type, @protocol = Protocol::IP, blocking = false) @closed = false init_close_on_exec(@fd) diff --git a/src/socket/tcp_server.cr b/src/socket/tcp_server.cr index badb99b0f1cc..44a66f191d9e 100644 --- a/src/socket/tcp_server.cr +++ b/src/socket/tcp_server.cr @@ -48,6 +48,11 @@ class TCPServer < TCPSocket end end + # Creates a TCPServer from an already configured raw file descriptor + def initialize(*, fd : Int32, family : Family = Family::INET) + super(fd: fd, family: family) + end + # Creates a new TCP server, listening on all local interfaces (`::`). def self.new(port : Int, backlog = SOMAXCONN, reuse_port = false) new("::", port, backlog, reuse_port: reuse_port) @@ -100,7 +105,7 @@ class TCPServer < TCPSocket # ``` def accept? if client_fd = accept_impl - sock = TCPSocket.new(client_fd, family, type, protocol) + sock = TCPSocket.new(fd: client_fd, family: family, type: type, protocol: protocol) sock.sync = sync? sock end diff --git a/src/socket/tcp_socket.cr b/src/socket/tcp_socket.cr index 56b080ccb360..7a5bdbcd2d0f 100644 --- a/src/socket/tcp_socket.cr +++ b/src/socket/tcp_socket.cr @@ -42,6 +42,11 @@ class TCPSocket < IPSocket super fd, family, type, protocol end + # Creates a TCPSocket from an already configured raw file descriptor + def initialize(*, fd : Int32, family : Family = Family::INET) + super fd, family, Type::STREAM, Protocol::TCP + end + # Opens a TCP socket to a remote TCP server, yields it to the block, then # eventually closes the socket when the block returns. # diff --git a/src/socket/unix_server.cr b/src/socket/unix_server.cr index 1562674129d6..359631e8c5f2 100644 --- a/src/socket/unix_server.cr +++ b/src/socket/unix_server.cr @@ -45,6 +45,11 @@ class UNIXServer < UNIXSocket end end + # Creates a UNIXServer from an already configured raw file descriptor + def initialize(*, fd : Int32, type : Type = Type::STREAM, @path : String? = nil) + super(fd: fd, type: type, path: @path) + end + # Creates a new UNIX server and yields it to the block. Eventually closes the # server socket when the block returns. # @@ -64,7 +69,7 @@ class UNIXServer < UNIXSocket # this method. def accept? : UNIXSocket? if client_fd = accept_impl - sock = UNIXSocket.new(client_fd, type, @path) + sock = UNIXSocket.new(fd: client_fd, type: type, path: @path) sock.sync = sync? sock end diff --git a/src/socket/unix_socket.cr b/src/socket/unix_socket.cr index 9956306a929b..aa136f7ad506 100644 --- a/src/socket/unix_socket.cr +++ b/src/socket/unix_socket.cr @@ -28,7 +28,8 @@ class UNIXSocket < Socket super family, type, Protocol::IP end - protected def initialize(fd : Int32, type : Type, @path : String? = nil) + # Creates a UNIXSocket from an already configured raw file descriptor + def initialize(*, fd : Int32, type : Type = Type::STREAM, @path : String? = nil) super fd, Family::UNIX, type, Protocol::IP end @@ -71,7 +72,7 @@ class UNIXSocket < Socket raise Errno.new("socketpair:") end - {UNIXSocket.new(fds[0], type), UNIXSocket.new(fds[1], type)} + {UNIXSocket.new(fd: fds[0], type: type), UNIXSocket.new(fd: fds[1], type: type)} end # Creates a pair of unamed UNIX sockets (see `pair`) and yields them to the