Skip to content

Commit

Permalink
Fix UNIXSocket#receive (#15107)
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota authored Oct 22, 2024
1 parent cf801e7 commit 6b390b0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
24 changes: 24 additions & 0 deletions spec/std/socket/unix_socket_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,30 @@ describe UNIXSocket do
end
end

it "#send, #receive" do
with_tempfile("unix_socket-receive.sock") do |path|
UNIXServer.open(path) do |server|
UNIXSocket.open(path) do |client|
server.accept do |sock|
client.send "ping"
message, address = sock.receive
message.should eq("ping")
typeof(address).should eq(Socket::UNIXAddress)
address.path.should eq ""

sock.send "pong"
message, address = client.receive
message.should eq("pong")
typeof(address).should eq(Socket::UNIXAddress)
# The value of path seems to be system-specific. Some implementations
# return the socket path, others an empty path.
["", path].should contain address.path
end
end
end
end
end

# `LibC.socketpair` is not supported in Winsock 2.0 yet:
# https://devblogs.microsoft.com/commandline/af_unix-comes-to-windows/#unsupportedunavailable
{% unless flag?(:win32) %}
Expand Down
6 changes: 3 additions & 3 deletions src/socket/unix_socket.cr
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ class UNIXSocket < Socket
UNIXAddress.new(path.to_s)
end

def receive
bytes_read, sockaddr, addrlen = recvfrom
{bytes_read, UNIXAddress.from(sockaddr, addrlen)}
def receive(max_message_size = 512) : {String, UNIXAddress}
message, address = super(max_message_size)
{message, address.as(UNIXAddress)}
end
end

0 comments on commit 6b390b0

Please sign in to comment.