Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow creating sockets from raw file descriptors #6894

Merged
merged 1 commit into from
Jan 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/socket.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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)
asterite marked this conversation as resolved.
Show resolved Hide resolved
@closed = false
init_close_on_exec(@fd)

Expand Down
7 changes: 6 additions & 1 deletion src/socket/tcp_server.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/socket/tcp_socket.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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.
#
Expand Down
7 changes: 6 additions & 1 deletion src/socket/unix_server.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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.
#
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/socket/unix_socket.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down