'Does some magic with splice() and tee() to move stuff from disk to
network without ever touching userspace.
:param wsockfd: file descriptor (integer) of the socket out which to
send data'

def zero_copy_send(self, wsockfd):

    self._started_at_0 = True
    rfd = self._fp.fileno()
    (client_rpipe, client_wpipe) = os.pipe()
    (hash_rpipe, hash_wpipe) = os.pipe()
    md5_sockfd = get_md5_socket()
    pipe_size = fcntl.fcntl(client_rpipe, F_SETPIPE_SZ, self._pipe_size)
    fcntl.fcntl(hash_rpipe, F_SETPIPE_SZ, pipe_size)
    dropped_cache = 0
    self._bytes_read = 0
    try:
        while True:
            (bytes_in_pipe, _1, _2) = splice(rfd, None, client_wpipe, None, pipe_size, 0)
            if (bytes_in_pipe == 0):
                self._read_to_eof = True
                self._drop_cache(rfd, dropped_cache, (self._bytes_read - dropped_cache))
                break
            self._bytes_read += bytes_in_pipe
            bytes_copied = tee(client_rpipe, hash_wpipe, bytes_in_pipe, 0)
            if (bytes_copied != bytes_in_pipe):
                raise Exception(('tee()    failed:    tried    to    move    %d    bytes,    but    only    moved    %d' % (bytes_in_pipe, bytes_copied)))
            (hashed, _1, _2) = splice(hash_rpipe, None, md5_sockfd, None, bytes_in_pipe, splice.SPLICE_F_MORE)
            if (hashed != bytes_in_pipe):
                raise Exception(("md5    socket    didn't    take    all    the    data?    (tried    to    write    %d,    but    wrote    %d)" % (bytes_in_pipe, hashed)))
            while (bytes_in_pipe > 0):
                try:
                    res = splice(client_rpipe, None, wsockfd, None, bytes_in_pipe, 0)
                    bytes_in_pipe -= res[0]
                except IOError as exc:
                    if (exc.errno == errno.EWOULDBLOCK):
                        trampoline(wsockfd, write=True)
                    else:
                        raise
            if ((self._bytes_read - dropped_cache) > DROP_CACHE_WINDOW):
                self._drop_cache(rfd, dropped_cache, (self._bytes_read - dropped_cache))
                dropped_cache = self._bytes_read
    finally:
        if (self._bytes_read > 0):
            bin_checksum = os.read(md5_sockfd, 16)
            hex_checksum = ''.join((('%02x' % ord(c)) for c in bin_checksum))
        else:
            hex_checksum = MD5_OF_EMPTY_STRING
        self._md5_of_sent_bytes = hex_checksum
        os.close(client_rpipe)
        os.close(client_wpipe)
        os.close(hash_rpipe)
        os.close(hash_wpipe)
        os.close(md5_sockfd)
        self.close()
