Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connection は server/client の Endpoint を持つ #84

Merged
merged 2 commits into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .idea/editor.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion src/lib/transport/connection.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "connection.hpp"
#include "utils/logger.hpp"

Connection::Connection(const int fd) : clientFd_(fd), fdReader_(clientFd_), buffer_(fdReader_) {}
Connection::Connection(const int fd, const Endpoint &listenerEndpoint, const Endpoint &clientEndpoint)
: clientFd_(fd), listenerEndpoint_(listenerEndpoint), clientEndpoint_(clientEndpoint), fdReader_(clientFd_),
buffer_(fdReader_) {}

Connection::~Connection() {
LOG_DEBUG("Connection: destruct");
Expand All @@ -11,6 +13,14 @@ int Connection::getFd() const {
return clientFd_.get();
}

const Endpoint &Connection::getListenerEndpoint() const {
return listenerEndpoint_;
}

const Endpoint &Connection::getClientEndpoint() const {
return clientEndpoint_;
}

ReadBuffer &Connection::getReadBuffer() {
return buffer_;
}
8 changes: 6 additions & 2 deletions src/lib/transport/connection.hpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
#ifndef SRC_LIB_CONNECTION_HPP
#define SRC_LIB_CONNECTION_HPP

#include "endpoint.hpp"
#include "utils/auto_fd.hpp"
#include "utils/io/read_buffer.hpp"
#include "utils/io/reader.hpp"
#include <memory>

// クライアントソケットの抽象
class Connection {
public:
explicit Connection(int fd);
Connection(int fd, const Endpoint &listenerEndpoint, const Endpoint &clientEndpoint);
~Connection();

int getFd() const;
const Endpoint &getListenerEndpoint() const;
const Endpoint &getClientEndpoint() const;
ReadBuffer &getReadBuffer();

private:
AutoFd clientFd_;
Endpoint listenerEndpoint_;
Endpoint clientEndpoint_;
io::FdReader fdReader_; // ReadBuffer に渡す IReader & の参照先として必要
ReadBuffer buffer_;
};
Expand Down
7 changes: 3 additions & 4 deletions src/lib/transport/listener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,17 @@ Listener::AcceptConnectionResult Listener::acceptConnection() const {
LOG_WARNF("failed to accept connection: %s", std::strerror(errno));
return Err<std::string>("failed to accept connection");
}
const Endpoint clientEndpoint(inet_ntoa(clientAddr.sin_addr), ntohs(clientAddr.sin_port));

const Result<void, error::AppError> result = utils::setNonBlocking(fd);
if (result.isErr()) {
LOG_ERROR("failed to set non-blocking fd");
return Err<std::string>("failed to set non-blocking fd");
}

LOG_INFOF(
"connection established from %s:%u (fd: %d)", inet_ntoa(clientAddr.sin_addr), ntohs(clientAddr.sin_port), fd
);
LOG_INFOF("connection established from %s (fd: %d)", clientEndpoint.toString().c_str(), fd);

return Ok(new Connection(fd));
return Ok(new Connection(fd, this->getEndpoint(), clientEndpoint));
}

int Listener::setupSocket(const std::string &ip, const unsigned short port, const int backlog) {
Expand Down