Fix `sendfile` behavior on BSD/MacOSX.
This commit is contained in:
parent
8a68c66caa
commit
774269f2f0
|
@ -10,7 +10,7 @@
|
||||||
## This module provides cross-platform wrapper for ``sendfile()`` syscall.
|
## This module provides cross-platform wrapper for ``sendfile()`` syscall.
|
||||||
|
|
||||||
when defined(nimdoc):
|
when defined(nimdoc):
|
||||||
proc sendfile*(outfd, infd: int, offset: int, count: int): int =
|
proc sendfile*(outfd, infd: int, offset: int, count: var int): int =
|
||||||
## Copies data between file descriptor ``infd`` and ``outfd``. Because this
|
## Copies data between file descriptor ``infd`` and ``outfd``. Because this
|
||||||
## copying is done within the kernel, ``sendfile()`` is more efficient than
|
## copying is done within the kernel, ``sendfile()`` is more efficient than
|
||||||
## the combination of ``read(2)`` and ``write(2)``, which would require
|
## the combination of ``read(2)`` and ``write(2)``, which would require
|
||||||
|
@ -26,11 +26,13 @@ when defined(nimdoc):
|
||||||
## data from ``infd``.
|
## data from ``infd``.
|
||||||
##
|
##
|
||||||
## ``count`` is the number of bytes to copy between the file descriptors.
|
## ``count`` is the number of bytes to copy between the file descriptors.
|
||||||
|
## On exit ``count`` will hold number of bytes actually transferred between
|
||||||
|
## file descriptors.
|
||||||
##
|
##
|
||||||
## If the transfer was successful, the number of bytes written to ``outfd``
|
## If the transfer was successful, the number of bytes written to ``outfd``
|
||||||
## is returned. Note that a successful call to ``sendfile()`` may write
|
## is stored in ``count``, and ``0`` returned. Note that a successful call to
|
||||||
## fewer bytes than requested; the caller should be prepared to retry the
|
## ``sendfile()`` may write fewer bytes than requested; the caller should
|
||||||
## call if there were unsent bytes.
|
## be prepared to retry the call if there were unsent bytes.
|
||||||
##
|
##
|
||||||
## On error, ``-1`` is returned.
|
## On error, ``-1`` is returned.
|
||||||
|
|
||||||
|
@ -39,13 +41,16 @@ when defined(linux) or defined(android):
|
||||||
proc osSendFile*(outfd, infd: cint, offset: ptr int, count: int): int
|
proc osSendFile*(outfd, infd: cint, offset: ptr int, count: int): int
|
||||||
{.importc: "sendfile", header: "<sys/sendfile.h>".}
|
{.importc: "sendfile", header: "<sys/sendfile.h>".}
|
||||||
|
|
||||||
proc sendfile*(outfd, infd: int, offset: int, count: int): int =
|
proc sendfile*(outfd, infd: int, offset: int, count: var int): int =
|
||||||
var o = offset
|
var o = offset
|
||||||
result = osSendFile(cint(outfd), cint(infd), addr o, count)
|
result = osSendFile(cint(outfd), cint(infd), addr o, count)
|
||||||
|
if result >= 0:
|
||||||
|
count = result
|
||||||
|
result = 0
|
||||||
|
|
||||||
elif defined(freebsd) or defined(openbsd) or defined(netbsd) or
|
elif defined(freebsd) or defined(openbsd) or defined(netbsd) or
|
||||||
defined(dragonflybsd):
|
defined(dragonflybsd):
|
||||||
|
import posix, os
|
||||||
type
|
type
|
||||||
SendfileHeader* = object {.importc: "sf_hdtr",
|
SendfileHeader* = object {.importc: "sf_hdtr",
|
||||||
header: """#include <sys/types.h>
|
header: """#include <sys/types.h>
|
||||||
|
@ -60,16 +65,23 @@ elif defined(freebsd) or defined(openbsd) or defined(netbsd) or
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <sys/uio.h>""".}
|
#include <sys/uio.h>""".}
|
||||||
|
|
||||||
proc sendfile*(outfd, infd: int, offset: int, count: int): int =
|
proc sendfile*(outfd, infd: int, offset: int, count: var int): int =
|
||||||
var o = 0'u
|
var o = 0'u
|
||||||
if osSendFile(cint(infd), cint(outfd), uint(offset), uint(count), nil,
|
result = osSendFile(cint(infd), cint(outfd), uint(offset), uint(count), nil,
|
||||||
addr o, 0) == 0:
|
addr o, 0)
|
||||||
result = int(o)
|
if result >= 0:
|
||||||
|
count = int(o)
|
||||||
|
result = 0
|
||||||
else:
|
else:
|
||||||
result = -1
|
let err = osLastError()
|
||||||
|
if int(err) == EAGAIN:
|
||||||
|
count = int(o)
|
||||||
|
result = 0
|
||||||
|
else:
|
||||||
|
result = -1
|
||||||
|
|
||||||
elif defined(macosx):
|
elif defined(macosx):
|
||||||
import posix
|
import posix, os
|
||||||
type
|
type
|
||||||
SendfileHeader* = object {.importc: "sf_hdtr",
|
SendfileHeader* = object {.importc: "sf_hdtr",
|
||||||
header: """#include <sys/types.h>
|
header: """#include <sys/types.h>
|
||||||
|
@ -84,9 +96,16 @@ elif defined(macosx):
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <sys/uio.h>""".}
|
#include <sys/uio.h>""".}
|
||||||
|
|
||||||
proc sendfile*(outfd, infd: int, offset: int, count: int): int =
|
proc sendfile*(outfd, infd: int, offset: int, count: var int): int =
|
||||||
var o = count
|
var o = count
|
||||||
if osSendFile(cint(infd), cint(outfd), offset, addr o, nil, 0) == 0:
|
result = osSendFile(cint(infd), cint(outfd), offset, addr o, nil, 0)
|
||||||
result = o
|
if result >= 0:
|
||||||
|
count = int(o)
|
||||||
|
result = 0
|
||||||
else:
|
else:
|
||||||
result = -1
|
let err = osLastError()
|
||||||
|
if int(err) == EAGAIN:
|
||||||
|
count = int(o)
|
||||||
|
result = 0
|
||||||
|
else:
|
||||||
|
result = -1
|
||||||
|
|
|
@ -28,6 +28,7 @@ type
|
||||||
buf: pointer # Writer buffer pointer
|
buf: pointer # Writer buffer pointer
|
||||||
buflen: int # Writer buffer size
|
buflen: int # Writer buffer size
|
||||||
offset: uint # Writer vector offset
|
offset: uint # Writer vector offset
|
||||||
|
size: int # Original size
|
||||||
writer: Future[int] # Writer vector completion Future
|
writer: Future[int] # Writer vector completion Future
|
||||||
|
|
||||||
TransportKind* {.pure.} = enum
|
TransportKind* {.pure.} = enum
|
||||||
|
@ -752,12 +753,6 @@ when defined(windows):
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|
||||||
template getVectorBuffer(v: untyped): pointer =
|
|
||||||
cast[pointer](cast[uint]((v).buf) + uint((v).boffset))
|
|
||||||
|
|
||||||
template getVectorLength(v: untyped): int =
|
|
||||||
cast[int]((v).buflen - int((v).boffset))
|
|
||||||
|
|
||||||
template initBufferStreamVector(v, p, n, t: untyped) =
|
template initBufferStreamVector(v, p, n, t: untyped) =
|
||||||
(v).kind = DataBuffer
|
(v).kind = DataBuffer
|
||||||
(v).buf = cast[pointer]((p))
|
(v).buf = cast[pointer]((p))
|
||||||
|
@ -793,14 +788,17 @@ else:
|
||||||
else:
|
else:
|
||||||
vector.writer.fail(getTransportOsError(err))
|
vector.writer.fail(getTransportOsError(err))
|
||||||
else:
|
else:
|
||||||
|
var nbytes = cast[int](vector.buf)
|
||||||
let res = sendfile(int(fd), cast[int](vector.buflen),
|
let res = sendfile(int(fd), cast[int](vector.buflen),
|
||||||
int(vector.offset),
|
int(vector.offset),
|
||||||
cast[int](vector.buf))
|
nbytes)
|
||||||
if res >= 0:
|
if res >= 0:
|
||||||
if cast[int](vector.buf) - res == 0:
|
if cast[int](vector.buf) - nbytes == 0:
|
||||||
vector.writer.complete(cast[int](vector.buf))
|
vector.size += nbytes
|
||||||
|
vector.writer.complete(vector.size)
|
||||||
else:
|
else:
|
||||||
vector.shiftVectorFile(res)
|
vector.size += nbytes
|
||||||
|
vector.shiftVectorFile(nbytes)
|
||||||
transp.queue.addFirst(vector)
|
transp.queue.addFirst(vector)
|
||||||
else:
|
else:
|
||||||
let err = osLastError()
|
let err = osLastError()
|
||||||
|
@ -948,7 +946,7 @@ else:
|
||||||
else:
|
else:
|
||||||
asyncCheck server.function(server,
|
asyncCheck server.function(server,
|
||||||
newStreamSocketTransport(sock, server.bufferSize, nil))
|
newStreamSocketTransport(sock, server.bufferSize, nil))
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
let err = osLastError()
|
let err = osLastError()
|
||||||
if int(err) == EINTR:
|
if int(err) == EINTR:
|
||||||
|
|
|
@ -684,8 +684,7 @@ when isMainModule:
|
||||||
else:
|
else:
|
||||||
discard
|
discard
|
||||||
else:
|
else:
|
||||||
test prefixes[i] & m4:
|
check waitFor(testSendFile(addresses[i])) == FilesCount
|
||||||
check waitFor(testSendFile(addresses[i])) == FilesCount
|
|
||||||
test prefixes[i] & m15:
|
test prefixes[i] & m15:
|
||||||
var address: TransportAddress
|
var address: TransportAddress
|
||||||
if addresses[i].family == AddressFamily.Unix:
|
if addresses[i].family == AddressFamily.Unix:
|
||||||
|
|
Loading…
Reference in New Issue