Skip to content

Commit

Permalink
move Sockets out of Base (JuliaLang#25935)
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Mar 1, 2018
1 parent 4c75937 commit 180111f
Show file tree
Hide file tree
Showing 25 changed files with 890 additions and 836 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ jobs:
/tmp/julia/bin/julia --sysimage-native-code=no -e 'true' &&
/tmp/julia/bin/julia-debug --sysimage-native-code=no -e 'true' &&
pushd /tmp/julia/share/julia/test &&
/tmp/julia/bin/julia --check-bounds=yes runtests.jl all --skip socket | bar -i 30 &&
/tmp/julia/bin/julia --check-bounds=yes runtests.jl all --skip Sockets | bar -i 30 &&
/tmp/julia/bin/julia --check-bounds=yes runtests.jl LibGit2/online Pkg/pkg Pkg3/pkg download &&
popd &&
mkdir /tmp/embedding-test &&
Expand Down
11 changes: 0 additions & 11 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,6 @@ end
@deprecate (convert(::Type{Integer}, x::Enum{T}) where {T<:Integer}) Integer(x)
@deprecate (convert(::Type{T}, x::Enum{T2}) where {T<:Integer,T2<:Integer}) T(x)

@deprecate convert(dt::Type{<:Integer}, ip::IPAddr) dt(ip)

function (::Type{T})(arg) where {T}
if applicable(convert, T, arg)
sig = which(convert, (Type{T}, typeof(arg))).sig
Expand Down Expand Up @@ -680,15 +678,6 @@ end
Broadcast.dotview(A::AbstractArray{<:AbstractArray}, args::Integer...) = getindex(A, args...)
# Upon removing deprecations, also enable the @testset "scalar .=" in test/broadcast.jl

@noinline function getaddrinfo(callback::Function, host::AbstractString)
depwarn("`getaddrinfo` with a callback function is deprecated, wrap code in `@async` instead for deferred execution.", :getaddrinfo)
@async begin
r = getaddrinfo(host)
callback(r)
end
nothing
end

# indexing with A[true] will throw an argument error in the future
function to_index(i::Bool)
depwarn("indexing with Bool values is deprecated. Convert the index to an integer first with `Int(i)`.", (:getindex, :setindex!, :view))
Expand Down
31 changes: 5 additions & 26 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,8 @@ export
uppercase,

# text output
IOContext,
displaysize,
dump,
print,
println,
Expand Down Expand Up @@ -704,6 +706,7 @@ export
put!,
isready,
fetch,
bind,

# missing values
coalesce,
Expand Down Expand Up @@ -793,39 +796,20 @@ export
exit,
ntuple,

# IP address stuff
@ip_str,
IPAddr,
IPv4,
IPv6,

# I/O and events
accept,
bind,
close,
connect,
countlines,
eachline,
eof,
fd,
fdio,
flush,
getaddrinfo,
getalladdrinfo,
getnameinfo,
gethostname,
getipaddr,
getpeername,
getsockname,
htol,
hton,
IOContext,
displaysize,
ismarked,
isopen,
isreadonly,
listen,
listenany,
ltoh,
mark,
bytesavailable,
Expand All @@ -848,21 +832,18 @@ export
redirect_stderr,
redirect_stdin,
redirect_stdout,
recv,
recvfrom,
reset,
seek,
seekend,
seekstart,
send,
skip,
skipchars,
take!,
truncate,
unmark,
unsafe_read,
unsafe_write,
write,
TCPSocket,
UDPSocket,

# multimedia I/O
AbstractDisplay,
Expand Down Expand Up @@ -963,9 +944,7 @@ export
unsafe_copyto!,
unsafe_load,
unsafe_pointer_to_objref,
unsafe_read,
unsafe_store!,
unsafe_write,

# implemented in Random module
rand,
Expand Down
6 changes: 0 additions & 6 deletions base/libuv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,7 @@ end
function reinit_stdio()
global uv_jl_alloc_buf = cfunction(uv_alloc_buf, Cvoid, Tuple{Ptr{Cvoid}, Csize_t, Ptr{Cvoid}})
global uv_jl_readcb = cfunction(uv_readcb, Cvoid, Tuple{Ptr{Cvoid}, Cssize_t, Ptr{Cvoid}})
global uv_jl_connectioncb = cfunction(uv_connectioncb, Cvoid, Tuple{Ptr{Cvoid}, Cint})
global uv_jl_connectcb = cfunction(uv_connectcb, Cvoid, Tuple{Ptr{Cvoid}, Cint})
global uv_jl_writecb_task = cfunction(uv_writecb_task, Cvoid, Tuple{Ptr{Cvoid}, Cint})
global uv_jl_getaddrinfocb = cfunction(uv_getaddrinfocb, Cvoid, Tuple{Ptr{Cvoid}, Cint, Ptr{Cvoid}})
global uv_jl_getnameinfocb = cfunction(uv_getnameinfocb, Cvoid, Tuple{Ptr{Cvoid}, Cint, Cstring, Cstring})
global uv_jl_recvcb = cfunction(uv_recvcb, Cvoid, Tuple{Ptr{Cvoid}, Cssize_t, Ptr{Cvoid}, Ptr{Cvoid}, Cuint})
global uv_jl_sendcb = cfunction(uv_sendcb, Cvoid, Tuple{Ptr{Cvoid}, Cint})
global uv_jl_return_spawn = cfunction(uv_return_spawn, Cvoid, Tuple{Ptr{Cvoid}, Int64, Int32})
global uv_jl_asynccb = cfunction(uv_asynccb, Cvoid, Tuple{Ptr{Cvoid}})
global uv_jl_timercb = cfunction(uv_timercb, Cvoid, Tuple{Ptr{Cvoid}})
Expand Down
162 changes: 6 additions & 156 deletions base/stream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -148,30 +148,6 @@ function PipeEndpoint()
return pipe
end

mutable struct PipeServer <: LibuvServer
handle::Ptr{Cvoid}
status::Int
connectnotify::Condition
closenotify::Condition
function PipeServer(handle::Ptr{Cvoid}, status)
p = new(handle,
status,
Condition(),
Condition())
associate_julia_struct(p.handle, p)
finalizer(uvfinalize, p)
return p
end
end

function PipeServer()
pipe = PipeServer(Libc.malloc(_sizeof_uv_named_pipe), StatusUninit)
err = ccall(:uv_pipe_init, Cint, (Ptr{Cvoid}, Ptr{Cvoid}, Cint), eventloop(), pipe.handle, 0)
uv_error("failed to create pipe server", err)
pipe.status = StatusInit
return pipe
end

mutable struct TTY <: LibuvStream
handle::Ptr{Cvoid}
status::Int
Expand Down Expand Up @@ -240,6 +216,8 @@ unlock(s::LibuvStream) = unlock(s.lock)
rawhandle(stream::LibuvStream) = stream.handle
unsafe_convert(::Type{Ptr{Cvoid}}, s::Union{LibuvStream, LibuvServer}) = s.handle

const Sockets_mod = Ref{Module}()

function init_stdio(handle::Ptr{Cvoid})
t = ccall(:jl_uv_handle_type, Int32, (Ptr{Cvoid},), handle)
if t == UV_FILE
Expand All @@ -255,7 +233,10 @@ function init_stdio(handle::Ptr{Cvoid})
elseif t == UV_TTY
return TTY(handle, StatusOpen)
elseif t == UV_TCP
return TCPSocket(handle, StatusOpen)
if !isassigned(Sockets_mod)
Sockets_mod[] = Base.require(Base, :Sockets)
end
return Sockets_mod[].TCPSocket(handle, StatusOpen)
elseif t == UV_NAMED_PIPE
return PipeEndpoint(handle, StatusOpen)
else
Expand Down Expand Up @@ -435,35 +416,6 @@ get(::TTY, key::Symbol, default) = key === :color ? have_color : default

### Libuv callbacks ###

#from `connect`
function uv_connectcb(conn::Ptr{Cvoid}, status::Cint)
hand = ccall(:jl_uv_connect_handle, Ptr{Cvoid}, (Ptr{Cvoid},), conn)
sock = @handle_as hand LibuvStream
@assert sock.status == StatusConnecting
if status >= 0
sock.status = StatusOpen
notify(sock.connectnotify)
else
sock.status = StatusInit
err = UVError("connect", status)
notify_error(sock.connectnotify, err)
end
Libc.free(conn)
nothing
end

# from `listen`
function uv_connectioncb(stream::Ptr{Cvoid}, status::Cint)
sock = @handle_as stream LibuvServer
if status >= 0
notify(sock.connectnotify)
else
err = UVError("connection", status)
notify_error(sock.connectnotify, err)
end
nothing
end

## BUFFER ##
## Allocate space in buffer (for immediate use)
function alloc_request(buffer::IOBuffer, recommended_size::UInt)
Expand Down Expand Up @@ -942,108 +894,6 @@ function uv_writecb_task(req::Ptr{Cvoid}, status::Cint)
nothing
end

## server functions ##

function accept_nonblock(server::PipeServer, client::PipeEndpoint)
if client.status != StatusInit
error("client is already in use or has been closed")
end
err = ccall(:uv_accept, Int32, (Ptr{Cvoid}, Ptr{Cvoid}), server.handle, client.handle)
if err == 0
client.status = StatusOpen
end
return err
end

function accept_nonblock(server::PipeServer)
client = PipeEndpoint()
uv_error("accept", accept_nonblock(server, client) != 0)
return client
end

function accept(server::LibuvServer, client::LibuvStream)
if server.status != StatusActive
throw(ArgumentError("server not connected, make sure \"listen\" has been called"))
end
while isopen(server)
err = accept_nonblock(server, client)
if err == 0
return client
elseif err != UV_EAGAIN
uv_error("accept", err)
end
stream_wait(server, server.connectnotify)
end
uv_error("accept", UV_ECONNABORTED)
end

const BACKLOG_DEFAULT = 511

function listen(sock::LibuvServer; backlog::Integer=BACKLOG_DEFAULT)
uv_error("listen", trylisten(sock))
return sock
end

function trylisten(sock::LibuvServer; backlog::Integer=BACKLOG_DEFAULT)
check_open(sock)
err = ccall(:uv_listen, Cint, (Ptr{Cvoid}, Cint, Ptr{Cvoid}),
sock, backlog, uv_jl_connectioncb::Ptr{Cvoid})
sock.status = StatusActive
return err
end

function bind(server::PipeServer, name::AbstractString)
@assert server.status == StatusInit
err = ccall(:uv_pipe_bind, Int32, (Ptr{Cvoid}, Cstring),
server, name)
if err != 0
if err != UV_EADDRINUSE && err != UV_EACCES
#TODO: this codepath is currently not tested
throw(UVError("bind",err))
else
return false
end
end
server.status = StatusOpen
return true
end

"""
listen(path::AbstractString) -> PipeServer
Create and listen on a named pipe / UNIX domain socket.
"""
function listen(path::AbstractString)
sock = PipeServer()
bind(sock, path) || throw(ArgumentError("could not listen on path $path"))
return listen(sock)
end

function connect!(sock::PipeEndpoint, path::AbstractString)
@assert sock.status == StatusInit
req = Libc.malloc(_sizeof_uv_connect)
uv_req_set_data(req, C_NULL)
ccall(:uv_pipe_connect, Cvoid, (Ptr{Cvoid}, Ptr{Cvoid}, Cstring, Ptr{Cvoid}), req, sock.handle, path, uv_jl_connectcb::Ptr{Cvoid})
sock.status = StatusConnecting
return sock
end

function connect(sock::LibuvStream, args...)
connect!(sock, args...)
wait_connected(sock)
return sock
end

# Libuv will internally reset read/writability, which is uses to
# mark that this is an invalid pipe.

"""
connect(path::AbstractString) -> PipeEndpoint
Connect to the named pipe / UNIX domain socket at `path`.
"""
connect(path::AbstractString) = connect(PipeEndpoint(), path)

_fd(x::IOStream) = RawFD(fd(x))

function _fd(x::Union{LibuvStream, LibuvServer})
Expand Down
23 changes: 22 additions & 1 deletion base/sysimg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,6 @@ function randn end

# I/O
include("stream.jl")
include("socket.jl")
include("filesystem.jl")
using .Filesystem
include("process.jl")
Expand Down Expand Up @@ -527,6 +526,7 @@ let
:Markdown,
:LibGit2,
:Logging,
:Sockets,

:Printf,
:Profile,
Expand Down Expand Up @@ -891,6 +891,27 @@ end
@eval @deprecate_stdlib $(Symbol("@code_lowered")) InteractiveUtils true
@eval @deprecate_stdlib $(Symbol("@code_llvm")) InteractiveUtils true
@eval @deprecate_stdlib $(Symbol("@code_native")) InteractiveUtils true

@eval @deprecate_stdlib $(Symbol("@ip_str")) Sockets true
@deprecate_stdlib IPAddr Sockets true
@deprecate_stdlib IPv4 Sockets true
@deprecate_stdlib IPv6 Sockets true
@deprecate_stdlib accept Sockets true
@deprecate_stdlib connect Sockets true
@deprecate_stdlib getaddrinfo Sockets true
@deprecate_stdlib getalladdrinfo Sockets true
@deprecate_stdlib getnameinfo Sockets true
@deprecate_stdlib getipaddr Sockets true
@deprecate_stdlib getpeername Sockets true
@deprecate_stdlib getsockname Sockets true
@deprecate_stdlib listen Sockets true
@deprecate_stdlib listenany Sockets true
@deprecate_stdlib recv Sockets true
@deprecate_stdlib recvfrom Sockets true
@deprecate_stdlib send Sockets true
@deprecate_stdlib TCPSocket Sockets true
@deprecate_stdlib UDPSocket Sockets true

end
end

Expand Down
Loading

0 comments on commit 180111f

Please sign in to comment.