Skip to content

Commit

Permalink
Optimize sendfile
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed Aug 22, 2024
1 parent 912e6bc commit e692bad
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 20 deletions.
4 changes: 2 additions & 2 deletions include/swoole_socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ static bool IN_IS_ADDR_LOOPBACK(struct in_addr *a) {

// OS Feature
#if defined(HAVE_KQUEUE) || !defined(HAVE_SENDFILE)
int swoole_sendfile(int out_fd, int in_fd, off_t *offset, size_t size);
ssize_t swoole_sendfile(int out_fd, int in_fd, off_t *offset, size_t size);
#else
#include <sys/sendfile.h>
#define swoole_sendfile(out_fd, in_fd, offset, limit) sendfile(out_fd, in_fd, offset, limit)
Expand Down Expand Up @@ -387,7 +387,7 @@ struct Socket {
ssize_t ssl_send(const void *__buf, size_t __n);
ssize_t ssl_readv(IOVector *io_vector);
ssize_t ssl_writev(IOVector *io_vector);
int ssl_sendfile(const File &fp, off_t *offset, size_t size);
ssize_t ssl_sendfile(const File &fp, off_t *offset, size_t size);
STACK_OF(X509) * ssl_get_peer_cert_chain();
std::vector<std::string> ssl_get_peer_cert_chain(int limit);
X509 *ssl_get_peer_certificate();
Expand Down
10 changes: 5 additions & 5 deletions src/coroutine/socket.cc
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ bool Socket::connect(std::string _host, int _port, int flags) {
}
socket->info.addr.un.sun_family = AF_UNIX;
memcpy(&socket->info.addr.un.sun_path, connect_host.c_str(), connect_host.size());
socket->info.len = (socklen_t)(offsetof(struct sockaddr_un, sun_path) + connect_host.size());
socket->info.len = (socklen_t) (offsetof(struct sockaddr_un, sun_path) + connect_host.size());
_target_addr = (struct sockaddr *) &socket->info.addr.un;
break;
} else {
Expand Down Expand Up @@ -1387,16 +1387,16 @@ bool Socket::sendfile(const char *filename, off_t offset, size_t length) {
}

TimerController timer(&write_timer, write_timeout, this, timer_callback);
int n, sendn;
ssize_t n, sent_bytes;
while ((size_t) offset < length) {
sendn = (length - offset > SW_SENDFILE_CHUNK_SIZE) ? SW_SENDFILE_CHUNK_SIZE : length - offset;
sent_bytes = (length - offset > SW_SENDFILE_CHUNK_SIZE) ? SW_SENDFILE_CHUNK_SIZE : length - offset;
#ifdef SW_USE_OPENSSL
if (socket->ssl) {
n = socket->ssl_sendfile(file, &offset, sendn);
n = socket->ssl_sendfile(file, &offset, sent_bytes);
} else
#endif
{
n = ::swoole_sendfile(sock_fd, file.get_fd(), &offset, sendn);
n = ::swoole_sendfile(sock_fd, file.get_fd(), &offset, sent_bytes);
}
if (n > 0) {
continue;
Expand Down
19 changes: 8 additions & 11 deletions src/network/socket.cc
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,16 @@ int Socket::sendfile_blocking(const char *filename, off_t offset, size_t length,
length = offset + length;
}

int n, sendn;
ssize_t n, sent_bytes;
while (offset < (off_t) length) {
if (wait_event(timeout_ms, SW_EVENT_WRITE) < 0) {
return SW_ERR;
} else {
sendn = (length - offset > SW_SENDFILE_CHUNK_SIZE) ? SW_SENDFILE_CHUNK_SIZE : length - offset;
n = ::swoole_sendfile(fd, file.get_fd(), &offset, sendn);
if (n <= 0) {
swoole_sys_warning("sendfile(%d, %s) failed", fd, filename);
return SW_ERR;
} else {
continue;
}
}
sent_bytes = (length - offset > SW_SENDFILE_CHUNK_SIZE) ? SW_SENDFILE_CHUNK_SIZE : length - offset;
n = ::swoole_sendfile(fd, file.get_fd(), &offset, sent_bytes);
if (n <= 0) {
swoole_sys_warning("sendfile(%d, %s) failed", fd, filename);
return SW_ERR;
}
}
return SW_OK;
Expand Down Expand Up @@ -1190,7 +1187,7 @@ int Socket::ssl_connect() {
return SW_ERR;
}

int Socket::ssl_sendfile(const File &fp, off_t *_offset, size_t _size) {
ssize_t Socket::ssl_sendfile(const File &fp, off_t *_offset, size_t _size) {
char buf[SW_BUFFER_SIZE_BIG];
ssize_t readn = _size > sizeof(buf) ? sizeof(buf) : _size;

Expand Down
4 changes: 2 additions & 2 deletions src/os/sendfile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include <sys/socket.h>
#include <sys/uio.h>

int swoole_sendfile(int out_fd, int in_fd, off_t *offset, size_t size) {
ssize_t swoole_sendfile(int out_fd, int in_fd, off_t *offset, size_t size) {
ssize_t ret;

#ifdef __MACH__
Expand Down Expand Up @@ -64,7 +64,7 @@ int swoole_sendfile(int out_fd, int in_fd, off_t *offset, size_t size) {
return SW_OK;
}
#elif !defined(HAVE_SENDFILE)
int swoole_sendfile(int out_fd, int in_fd, off_t *offset, size_t size) {
ssize_t swoole_sendfile(int out_fd, int in_fd, off_t *offset, size_t size) {
char buf[SW_BUFFER_SIZE_BIG];
size_t readn = size > sizeof(buf) ? sizeof(buf) : size;
ssize_t n = pread(in_fd, buf, readn, *offset);
Expand Down

0 comments on commit e692bad

Please sign in to comment.