Unverified Commit 7b125802 authored by Igor [hyperxor]'s avatar Igor [hyperxor] Committed by GitHub

Small refactoring in asyncWriteImpl (#801)

* Small refactoring in asyncWriteImpl

* Trigger CI

* Trigger CI
parent a61d3be1
...@@ -194,6 +194,8 @@ private: ...@@ -194,6 +194,8 @@ private:
// This will attempt to drain the write queue for the fd // This will attempt to drain the write queue for the fd
void asyncWriteImpl(Fd fd); void asyncWriteImpl(Fd fd);
ssize_t sendRawBuffer(Fd fd, const char* buffer, size_t len, int flags);
ssize_t sendFile(Fd fd, Fd file, off_t offset, size_t len);
void handlePeerDisconnection(const std::shared_ptr<Peer> &peer); void handlePeerDisconnection(const std::shared_ptr<Peer> &peer);
void handleIncoming(const std::shared_ptr<Peer> &peer); void handleIncoming(const std::shared_ptr<Peer> &peer);
......
...@@ -226,43 +226,11 @@ void Transport::asyncWriteImpl(Fd fd) { ...@@ -226,43 +226,11 @@ void Transport::asyncWriteImpl(Fd fd) {
if (buffer.isRaw()) { if (buffer.isRaw()) {
auto raw = buffer.raw(); auto raw = buffer.raw();
auto ptr = raw.data().c_str() + totalWritten; auto ptr = raw.data().c_str() + totalWritten;
bytesWritten = sendRawBuffer(fd, ptr, len, flags);
#ifdef PISTACHE_USE_SSL
auto it_ = peers.find(fd);
if (it_ == std::end(peers))
throw std::runtime_error("No peer found for fd: " +
std::to_string(fd));
if (it_->second->ssl() != NULL) {
auto ssl_ = static_cast<SSL *>(it_->second->ssl());
bytesWritten = SSL_write(ssl_, ptr, static_cast<int>(len));
} else {
#endif /* PISTACHE_USE_SSL */
bytesWritten = ::send(fd, ptr, len, flags);
#ifdef PISTACHE_USE_SSL
}
#endif /* PISTACHE_USE_SSL */
} else { } else {
auto file = buffer.fd(); auto file = buffer.fd();
off_t offset = totalWritten; off_t offset = totalWritten;
bytesWritten = sendFile(fd, file, offset, len);
#ifdef PISTACHE_USE_SSL
auto it_ = peers.find(fd);
if (it_ == std::end(peers))
throw std::runtime_error("No peer found for fd: " +
std::to_string(fd));
if (it_->second->ssl() != NULL) {
auto ssl_ = static_cast<SSL *>(it_->second->ssl());
bytesWritten = SSL_sendfile(ssl_, file, &offset, len);
} else {
#endif /* PISTACHE_USE_SSL */
bytesWritten = ::sendfile(fd, file, &offset, len);
#ifdef PISTACHE_USE_SSL
}
#endif /* PISTACHE_USE_SSL */
} }
if (bytesWritten < 0) { if (bytesWritten < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) { if (errno == EAGAIN || errno == EWOULDBLOCK) {
...@@ -309,6 +277,52 @@ void Transport::asyncWriteImpl(Fd fd) { ...@@ -309,6 +277,52 @@ void Transport::asyncWriteImpl(Fd fd) {
} }
} }
ssize_t Transport::sendRawBuffer(Fd fd, const char* buffer, size_t len, int flags) {
ssize_t bytesWritten = 0;
#ifdef PISTACHE_USE_SSL
auto it_ = peers.find(fd);
if (it_ == std::end(peers))
throw std::runtime_error("No peer found for fd: " +
std::to_string(fd));
if (it_->second->ssl() != NULL) {
auto ssl_ = static_cast<SSL *>(it_->second->ssl());
bytesWritten = SSL_write(ssl_, buffer, static_cast<int>(len));
} else {
#endif /* PISTACHE_USE_SSL */
bytesWritten = ::send(fd, buffer, len, flags);
#ifdef PISTACHE_USE_SSL
}
#endif /* PISTACHE_USE_SSL */
return bytesWritten;
}
ssize_t Transport::sendFile(Fd fd, Fd file, off_t offset, size_t len) {
ssize_t bytesWritten = 0;
#ifdef PISTACHE_USE_SSL
auto it_ = peers.find(fd);
if (it_ == std::end(peers))
throw std::runtime_error("No peer found for fd: " +
std::to_string(fd));
if (it_->second->ssl() != NULL) {
auto ssl_ = static_cast<SSL *>(it_->second->ssl());
bytesWritten = SSL_sendfile(ssl_, file, &offset, len);
} else {
#endif /* PISTACHE_USE_SSL */
bytesWritten = ::sendfile(fd, file, &offset, len);
#ifdef PISTACHE_USE_SSL
}
#endif /* PISTACHE_USE_SSL */
return bytesWritten;
}
void Transport::armTimerMs(Fd fd, std::chrono::milliseconds value, void Transport::armTimerMs(Fd fd, std::chrono::milliseconds value,
Async::Deferred<uint64_t> deferred) { Async::Deferred<uint64_t> deferred) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment