Skip to content

Commit

Permalink
move Sockets out of Base
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Feb 11, 2018
1 parent 80a8175 commit 34c85c5
Show file tree
Hide file tree
Showing 23 changed files with 379 additions and 418 deletions.
11 changes: 0 additions & 11 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -455,8 +455,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 @@ -678,15 +676,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 @@ -622,6 +622,8 @@ export
uppercase,

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

# missing values
coalesce,
Expand Down Expand Up @@ -800,39 +803,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 @@ -855,21 +839,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 @@ -972,9 +953,7 @@ export
unsafe_copyto!,
unsafe_load,
unsafe_pointer_to_objref,
unsafe_read,
unsafe_store!,
unsafe_write,

# implemented in Random module
rand,
Expand Down
4 changes: 0 additions & 4 deletions base/libuv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,6 @@ function reinit_stdio()
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
103 changes: 0 additions & 103 deletions base/precompile.jl

Large diffs are not rendered by default.

133 changes: 6 additions & 127 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 @@ -942,108 +923,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 @@ -342,7 +342,6 @@ function randn end

# I/O
include("stream.jl")
include("socket.jl")
include("filesystem.jl")
using .Filesystem
include("process.jl")
Expand Down Expand Up @@ -501,6 +500,7 @@ Base.require(Base, :Base64)
Base.require(Base, :CRC32c)
Base.require(Base, :SHA)
Base.require(Base, :Dates)
Base.require(Base, :Sockets)
Base.require(Base, :DelimitedFiles)
Base.require(Base, :Serialization)
Base.require(Base, :Distributed)
Expand Down Expand Up @@ -855,6 +855,27 @@ Base.require(Base, :Markdown)
@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

empty!(DEPOT_PATH)
Expand Down
20 changes: 0 additions & 20 deletions doc/src/base/io-network.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,27 +126,7 @@ Base.Multimedia.istextmime
## Network I/O

```@docs
Base.connect(::TCPSocket, ::Integer)
Base.connect(::AbstractString)
Base.listen(::Any)
Base.listen(::AbstractString)
Base.getaddrinfo
Base.getalladdrinfo
Base.getnameinfo
Base.getsockname
Base.getpeername
Base.IPv4
Base.IPv6
Base.TCPSocket
Base.UDPSocket
Base.bytesavailable
Base.accept
Base.listenany
Base.bind
Base.send
Base.recv
Base.recvfrom
Base.setopt
Base.ntoh
Base.hton
Base.ltoh
Expand Down
2 changes: 1 addition & 1 deletion examples/clustermanager/simple/UnixDomainCM.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This file is a part of Julia. License is MIT: https://julialang.org/license

using Distributed
using Sockets, Distributed
import Distributed: launch, manage, connect, exit

mutable struct UnixDomainCM <: ClusterManager
Expand Down
1 change: 1 addition & 0 deletions stdlib/Distributed/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Sockets = "6462fe0b-24de-5631-8697-dd941f90decc"
5 changes: 3 additions & 2 deletions stdlib/Distributed/src/Distributed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module Distributed

# imports for extension
import Base: getindex, wait, put!, take!, fetch, isready, push!, length,
hash, ==, connect, kill, close, showerror
hash, ==, kill, close, showerror

# imports for use
using Base: Process, Semaphore, JLOptions, AnyDict, buffer_writes, wait_connected,
Expand All @@ -18,8 +18,9 @@ using Base: Process, Semaphore, JLOptions, AnyDict, buffer_writes, wait_connecte
AsyncGenerator, acquire, release, invokelatest,
shell_escape_posixly, uv_error, coalesce, notnothing

using Serialization
using Serialization, Sockets
import Serialization: serialize, deserialize
import Sockets: connect

# NOTE: clusterserialize.jl imports additional symbols from Serialization for use

Expand Down
Loading

0 comments on commit 34c85c5

Please sign in to comment.