Skip to content

Commit

Permalink
Add Sockets.recvfrom2 to return both host and port
Browse files Browse the repository at this point in the history
  • Loading branch information
jonnystorm committed Jul 29, 2019
1 parent 442d159 commit ffc622e
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion stdlib/Sockets/src/Sockets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,17 @@ Read a UDP packet from the specified socket, returning a tuple of `(address, dat
`address` will be either IPv4 or IPv6 as appropriate.
"""
function recvfrom(sock::UDPSocket)
from, data = recvfrom2(sock)
return (from.host, data)
end

"""
recvfrom2(socket::UDPSocket) -> (host_port, data)
Read a UDP packet from the specified socket, returning a tuple of `(host_port, data)`, where
`host_port` will be an InetAddr{IPv4} or InetAddr{IPv6}, as appropriate.
"""
function recvfrom2(sock::UDPSocket)
iolock_begin()
# If the socket has not been bound, it will be bound implicitly to ::0 and a random port
if sock.status != StatusInit && sock.status != StatusOpen && sock.status != StatusActive
Expand All @@ -348,7 +359,7 @@ function recvfrom(sock::UDPSocket)
From = Union{InetAddr{IPv4}, InetAddr{IPv6}}
Data = Vector{UInt8}
from, data = wait(sock.recvnotify)::Tuple{From, Data}
return (from.host, data)
return (from, data)
finally
unlock(sock.recvnotify)
end
Expand Down

0 comments on commit ffc622e

Please sign in to comment.