Unverified Commit d2e6e6d2 authored by Dennis Jenkins's avatar Dennis Jenkins Committed by GitHub

Merge pull request #434 from knowledge4igor/tiny_code_improvements_14

Tiny code improvements
parents 7a054c5d 93713f9e
...@@ -30,8 +30,6 @@ namespace Http { ...@@ -30,8 +30,6 @@ namespace Http {
class ConnectionPool; class ConnectionPool;
class Transport; class Transport;
std::pair<StringView, StringView> splitUrl(const std::string& url);
struct Connection : public std::enable_shared_from_this<Connection> { struct Connection : public std::enable_shared_from_this<Connection> {
friend class ConnectionPool; friend class ConnectionPool;
...@@ -96,8 +94,7 @@ struct Connection : public std::enable_shared_from_this<Connection> { ...@@ -96,8 +94,7 @@ struct Connection : public std::enable_shared_from_this<Connection> {
Async::Rejection reject, Async::Rejection reject,
OnDone onDone); OnDone onDone);
Fd fd; Fd fd() const;
void handleResponsePacket(const char* buffer, size_t totalBytes); void handleResponsePacket(const char* buffer, size_t totalBytes);
void handleError(const char* error); void handleError(const char* error);
void handleTimeout(); void handleTimeout();
...@@ -124,6 +121,8 @@ private: ...@@ -124,6 +121,8 @@ private:
OnDone onDone; OnDone onDone;
}; };
Fd fd_;
struct sockaddr_in saddr; struct sockaddr_in saddr;
std::unique_ptr<RequestEntry> requestEntry; std::unique_ptr<RequestEntry> requestEntry;
std::atomic<uint32_t> state_; std::atomic<uint32_t> state_;
......
...@@ -40,8 +40,8 @@ public: ...@@ -40,8 +40,8 @@ public:
Peer(const Address& addr); Peer(const Address& addr);
~Peer() {} ~Peer() {}
Address address() const; const Address& address() const;
std::string hostname() const; const std::string& hostname() const;
void associateFd(Fd fd); void associateFd(Fd fd);
Fd fd() const; Fd fd() const;
......
...@@ -26,22 +26,25 @@ namespace Http { ...@@ -26,22 +26,25 @@ namespace Http {
static constexpr const char* UA = "pistache/0.1"; static constexpr const char* UA = "pistache/0.1";
std::pair<StringView, StringView> namespace
splitUrl(const std::string& url) { {
RawStreamBuf<char> buf(const_cast<char *>(&url[0]), url.size()); std::pair<StringView, StringView> splitUrl(const std::string& url)
StreamCursor cursor(&buf); {
RawStreamBuf<char> buf(const_cast<char *>(&url[0]), url.size());
StreamCursor cursor(&buf);
match_string("http://", std::strlen("http://"), cursor); match_string("http://", std::strlen("http://"), cursor);
match_string("www", std::strlen("www"), cursor); match_string("www", std::strlen("www"), cursor);
match_literal('.', cursor); match_literal('.', cursor);
StreamCursor::Token hostToken(cursor); StreamCursor::Token hostToken(cursor);
match_until({ '?', '/' }, cursor); match_until({ '?', '/' }, cursor);
StringView host(hostToken.rawText(), hostToken.size()); StringView host(hostToken.rawText(), hostToken.size());
StringView page(cursor.offset(), buf.endptr()); StringView page(cursor.offset(), buf.endptr());
return std::make_pair(std::move(host), std::move(page)); return std::make_pair(std::move(host), std::move(page));
}
} }
struct ExceptionPrinter { struct ExceptionPrinter {
...@@ -174,7 +177,7 @@ Transport::onReady(const Aio::FdSet& fds) { ...@@ -174,7 +177,7 @@ Transport::onReady(const Aio::FdSet& fds) {
// We are connected, we can start reading data now // We are connected, we can start reading data now
auto connection = connIt->second.connection.lock(); auto connection = connIt->second.connection.lock();
if (connection) { if (connection) {
reactor()->modifyFd(key(), connection->fd, NotifyOn::Read); reactor()->modifyFd(key(), connection->fd(), NotifyOn::Read);
} else { } else {
throw std::runtime_error("Connection error"); throw std::runtime_error("Connection error");
} }
...@@ -228,7 +231,7 @@ Transport::asyncSendRequestImpl( ...@@ -228,7 +231,7 @@ Transport::asyncSendRequestImpl(
if (!conn) if (!conn)
throw std::runtime_error("Send request error"); throw std::runtime_error("Send request error");
auto fd = conn->fd; auto fd = conn->fd();
ssize_t totalWritten = 0; ssize_t totalWritten = 0;
for (;;) { for (;;) {
...@@ -284,17 +287,17 @@ Transport::handleConnectionQueue() { ...@@ -284,17 +287,17 @@ Transport::handleConnectionQueue() {
if (!conn) { if (!conn) {
throw std::runtime_error("Connection error"); throw std::runtime_error("Connection error");
} }
int res = ::connect(conn->fd, data->getAddr(), data->addr_len); int res = ::connect(conn->fd(), data->getAddr(), data->addr_len);
if (res == -1) { if (res == -1) {
if (errno == EINPROGRESS) { if (errno == EINPROGRESS) {
reactor()->registerFdOneShot(key(), conn->fd, NotifyOn::Write | NotifyOn::Hangup | NotifyOn::Shutdown); reactor()->registerFdOneShot(key(), conn->fd(), NotifyOn::Write | NotifyOn::Hangup | NotifyOn::Shutdown);
} }
else { else {
data->reject(Error::system("Failed to connect")); data->reject(Error::system("Failed to connect"));
continue; continue;
} }
} }
connections.insert(std::make_pair(conn->fd, std::move(*data))); connections.insert(std::make_pair(conn->fd(), std::move(*data)));
} }
} }
...@@ -305,7 +308,7 @@ Transport::handleIncoming(std::shared_ptr<Connection> connection) { ...@@ -305,7 +308,7 @@ Transport::handleIncoming(std::shared_ptr<Connection> connection) {
ssize_t totalBytes = 0; ssize_t totalBytes = 0;
for (;;) { for (;;) {
ssize_t bytes = recv(connection->fd, buffer + totalBytes, Const::MaxBuffer - totalBytes, 0); ssize_t bytes = recv(connection->fd(), buffer + totalBytes, Const::MaxBuffer - totalBytes, 0);
if (bytes == -1) { if (bytes == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK) { if (errno == EAGAIN || errno == EWOULDBLOCK) {
if (totalBytes > 0) { if (totalBytes > 0) {
...@@ -322,7 +325,7 @@ Transport::handleIncoming(std::shared_ptr<Connection> connection) { ...@@ -322,7 +325,7 @@ Transport::handleIncoming(std::shared_ptr<Connection> connection) {
} else { } else {
connection->handleError("Remote closed connection"); connection->handleError("Remote closed connection");
} }
connections.erase(connection->fd); connections.erase(connection->fd());
connection->close(); connection->close();
break; break;
} }
...@@ -348,7 +351,7 @@ Transport::handleTimeout(const std::shared_ptr<Connection>& connection) { ...@@ -348,7 +351,7 @@ Transport::handleTimeout(const std::shared_ptr<Connection>& connection) {
} }
Connection::Connection() Connection::Connection()
: fd(-1) : fd_(-1)
, requestEntry(nullptr) , requestEntry(nullptr)
{ {
state_.store(static_cast<uint32_t>(State::Idle)); state_.store(static_cast<uint32_t>(State::Idle));
...@@ -382,7 +385,7 @@ Connection::connect(const Address& addr) ...@@ -382,7 +385,7 @@ Connection::connect(const Address& addr)
make_non_blocking(sfd); make_non_blocking(sfd);
connectionState_.store(Connecting); connectionState_.store(Connecting);
fd = sfd; fd_ = sfd;
transport_->asyncConnect(shared_from_this(), addr->ai_addr, addr->ai_addrlen) transport_->asyncConnect(shared_from_this(), addr->ai_addr, addr->ai_addrlen)
.then([=]() { .then([=]() {
...@@ -402,7 +405,7 @@ Connection::connect(const Address& addr) ...@@ -402,7 +405,7 @@ Connection::connect(const Address& addr)
std::string std::string
Connection::dump() const { Connection::dump() const {
std::ostringstream oss; std::ostringstream oss;
oss << "Connection(fd = " << fd << ", src_port = "; oss << "Connection(fd = " << fd_ << ", src_port = ";
oss << ntohs(saddr.sin_port) << ")"; oss << ntohs(saddr.sin_port) << ")";
return oss.str(); return oss.str();
} }
...@@ -420,7 +423,7 @@ Connection::isConnected() const { ...@@ -420,7 +423,7 @@ Connection::isConnected() const {
void void
Connection::close() { Connection::close() {
connectionState_.store(NotConnected); connectionState_.store(NotConnected);
::close(fd); ::close(fd_);
} }
void void
...@@ -436,6 +439,12 @@ Connection::hasTransport() const { ...@@ -436,6 +439,12 @@ Connection::hasTransport() const {
return transport_ != nullptr; return transport_ != nullptr;
} }
Fd Connection::fd() const
{
assert(fd_ != -1);
return fd_;
}
void void
Connection::handleResponsePacket(const char* buffer, size_t totalBytes) { Connection::handleResponsePacket(const char* buffer, size_t totalBytes) {
......
...@@ -30,13 +30,13 @@ Peer::Peer(const Address& addr) ...@@ -30,13 +30,13 @@ Peer::Peer(const Address& addr)
, ssl_(NULL) , ssl_(NULL)
{ } { }
Address const Address& Peer::address() const
Peer::address() const { {
return addr; return addr;
} }
string const std::string& Peer::hostname() const
Peer::hostname() const { {
return hostname_; return hostname_;
} }
......
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