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

Fix UNIXSocket#receive #15107

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
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
Loading