Correct way to perform direct socket io, i.e no memory copy

currently, socket read/write are through stream interface, and it’s buffered. and to read that, there is an extra memory copy with take!.

what is the proper way to do direct socket io, without memory copy?(or is direct socket bad, thus designed to be buffered? or such copy should not be a major concern)

should i replace those buffer?

# for read 
mybuffer = IOBuffer()
socket.buffer = mybuffer
Base.wait_readnb(socket)
# then check out my buffer
mybuffer.data

# for write
mybuffer = IOBuffer()
# write something to mybuffer
socket.sendbuf = nothing  # perform flush if necessary
Base.unsafe_write(socket, pointer(mybuffer.data), nb)

this does not look good/correct to me. is there simple way to do this?

ideally i want something like

# for read 
buffer = zeros(UInt8, maxsize)
while should_continue 
  n_read = readbytes!(socket, buffer, maxsize; all = false)  # fill at most maxsize
  # do something with buffer 
  # clean up if necessary
end

# for write 
buffer = zeros(UInt8, maxsize)
while should_continue
  # write something to buffer
  n_written = write(socket, buffer, nbytes)
  # clear buffer if necessary
end
2 Likes

I don’t think there is one in Julia. Anyhow, zero copy socket is hard to debug and needs more care when implementing, if you want to implement one in linux check out vmsplice and sockmap with which you can implement zero copy socks(these are linux os APIs, so you will encounter some form of explicit memory management).