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

Refactor Socket#system_receive to return Address #14384

Merged
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
2 changes: 1 addition & 1 deletion src/crystal/system/unix/socket.cr
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ module Crystal::System::Socket
LibC.recvfrom(fd, slice, slice.size, 0, sockaddr, pointerof(addrlen))
end

{bytes_read, sockaddr, addrlen}
{bytes_read, ::Socket::Address.from(sockaddr, addrlen)}
end

private def system_close_read
Expand Down
2 changes: 1 addition & 1 deletion src/crystal/system/win32/socket.cr
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ module Crystal::System::Socket
{ret, bytes_received}
end

{bytes_read.to_i32, sockaddr, addrlen}
{bytes_read.to_i32, ::Socket::Address.from(sockaddr, addrlen)}
end

private def system_close_read
Expand Down
8 changes: 3 additions & 5 deletions src/socket.cr
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,10 @@ class Socket < IO
def receive(max_message_size = 512) : {String, Address}
address = nil
message = String.new(max_message_size) do |buffer|
bytes_read, sockaddr, addrlen = system_receive(Slice.new(buffer, max_message_size))
address = Address.from(sockaddr, addrlen)
bytes_read, address = system_receive(Slice.new(buffer, max_message_size))
{bytes_read, 0}
end
{message, address.not_nil!}
{message, address.as(Address)}
end

# Receives a binary message from the previously bound address.
Expand All @@ -274,8 +273,7 @@ class Socket < IO
# bytes_read, client_addr = server.receive(message)
# ```
def receive(message : Bytes) : {Int32, Address}
bytes_read, sockaddr, addrlen = system_receive(message)
{bytes_read, Address.from(sockaddr, addrlen)}
system_receive(message)
end

# Calls `shutdown(2)` with `SHUT_RD`
Expand Down
9 changes: 4 additions & 5 deletions src/socket/udp_socket.cr
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,10 @@ class UDPSocket < IPSocket
def receive(max_message_size = 512) : {String, IPAddress}
address = nil
message = String.new(max_message_size) do |buffer|
bytes_read, sockaddr, addrlen = system_receive(Slice.new(buffer, max_message_size))
address = IPAddress.from(sockaddr, addrlen)
bytes_read, address = system_receive(Slice.new(buffer, max_message_size))
{bytes_read, 0}
end
{message, address.not_nil!}
{message, address.as(IPAddress)}
end

# Receives a binary message from the previously bound address.
Expand All @@ -89,8 +88,8 @@ class UDPSocket < IPSocket
# bytes_read, client_addr = server.receive(message)
# ```
def receive(message : Bytes) : {Int32, IPAddress}
bytes_read, sockaddr, addrlen = system_receive(message)
{bytes_read, IPAddress.from(sockaddr, addrlen)}
bytes_read, address = system_receive(message)
{bytes_read, address.as(IPAddress)}
Comment on lines +91 to +92
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be shortened to, I think:

Suggested change
bytes_read, address = system_receive(message)
{bytes_read, address.as(IPAddress)}
system_receive(message)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Sija but system_receive returns an Address while we want an IPAddress here.

end

# Reports whether transmitted multicast packets should be copied and sent
Expand Down
Loading