-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
688 additions
and
538 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#include <sys/socket.h> | ||
#include <unistd.h> | ||
|
||
#include <memory> | ||
|
||
#include "core/connection.h" | ||
#include "core/net_addr.h" | ||
#include "core/socket.h" | ||
#include "core/thread_pool.h" | ||
|
||
#define BUF_SIZE 2048 | ||
namespace Next { | ||
class EchoClient { | ||
public: | ||
explicit EchoClient(NetAddress server_address) { | ||
auto client_sock = std::make_unique<Socket>(); | ||
client_sock->Connect(server_address); | ||
client_connection = std::make_unique<Connection>(std::move(client_sock)); | ||
} | ||
|
||
void Begin() { | ||
char buf[BUF_SIZE + 1]; | ||
memset(buf, 0, sizeof(buf)); | ||
int fd = client_connection->GetFd(); | ||
while (true) { | ||
auto actual_read = read(STDIN_FILENO, buf, BUF_SIZE); | ||
send(fd, buf, actual_read, 0); | ||
memset(buf, 0, sizeof(buf)); | ||
auto actual_recv = recv(fd, buf, BUF_SIZE, 0); | ||
write(STDOUT_FILENO, buf, actual_recv); | ||
memset(buf, 0, sizeof(buf)); | ||
} | ||
} | ||
|
||
private: | ||
std::unique_ptr<Connection> client_connection; | ||
}; | ||
} // namespace Next | ||
int main() { | ||
Next::NetAddress local_address("127.0.0.1", 20080); | ||
Next::EchoClient echo_client(local_address); | ||
echo_client.Begin(); | ||
return 0; | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#include "core/next_server.h" | ||
|
||
int main() { | ||
Next::NetAddress local_address("127.0.0.1", 20080); | ||
Next::NextServer echo_server(local_address); | ||
echo_server | ||
.OnHandle([&](Next::Connection *client_conn) { | ||
int from_fd = client_conn->GetFd(); | ||
std::pair<size_t, bool> ret = client_conn->Recv(); | ||
if (ret.second) { | ||
client_conn->GetLooper()->DeleteConnection(from_fd); | ||
return; | ||
} | ||
if (ret.first) { | ||
client_conn->WriteToWriteBuffer(client_conn->ReadAsString()); | ||
client_conn->Send(); | ||
client_conn->ClearReadBuffer(); | ||
} | ||
}) | ||
.Begin(); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,78 @@ | ||
#include "core/acceptor.h" | ||
#include "core/net_addr.h" | ||
#include "core/connection.h" | ||
#include "core/socket.h" | ||
#include "core/looper.h" | ||
#include "core/net_addr.h" | ||
#include "core/poller.h" | ||
#include "core/socket.h" | ||
#include "log/logger.h" | ||
#include "core/looper.h" | ||
|
||
namespace Next { | ||
|
||
Acceptor::Acceptor(Looper *listener, std::vector<Looper *> reactors, NetAddress server_address) | ||
: reactors_(std::move(reactors)) { | ||
auto acceptor_sock = std::make_unique<Socket>(); | ||
acceptor_sock->Bind(server_address, SocketFlag::set_reusable); | ||
acceptor_sock->Listen(); | ||
acceptor_conn_ = std::make_unique<Connection>(std::move(acceptor_sock)); | ||
acceptor_conn_->SetEvents(POLL_READ); | ||
acceptor_conn_->SetLooper(listener); | ||
listener->AddAcceptor(acceptor_conn_.get()); | ||
SetCustomAcceptCallback([](Connection *) {}); | ||
SetCustomHandleCallback([](Connection *) {}); | ||
Acceptor::Acceptor(Looper *listener, std::vector<Looper *> reactors, | ||
NetAddress server_address) | ||
: reactors_(std::move(reactors)) { | ||
auto acceptor_sock = std::make_unique<Socket>(); | ||
acceptor_sock->Bind(server_address, SocketFlag::set_reusable); | ||
acceptor_sock->Listen(); | ||
acceptor_conn_ = std::make_unique<Connection>(std::move(acceptor_sock)); | ||
acceptor_conn_->SetEvents(POLL_READ); | ||
acceptor_conn_->SetLooper(listener); | ||
listener->AddAcceptor(acceptor_conn_.get()); | ||
SetCustomAcceptCallback([](Connection *) {}); | ||
SetCustomHandleCallback([](Connection *) {}); | ||
} | ||
/** | ||
* accept a connect,and create Connection for client | ||
*/ | ||
*/ | ||
void Acceptor::BaseAcceptCallback(Connection *server_conn) { | ||
NetAddress client_address; | ||
int accept_fd = server_conn->GetSocket()->Accept(client_address); | ||
if (accept_fd == -1) { | ||
return; | ||
} | ||
auto client_sock = std::make_unique<Socket>(accept_fd); | ||
client_sock->SetNonBlocking(); | ||
auto client_conn = std::make_unique<Connection>(std::move(client_sock)); | ||
client_conn->SetEvents(POLL_READ | POLL_ET); | ||
client_conn->SetCallback(GetCustomHandleCallback()); | ||
|
||
int idx = rand() % reactors_.size(); | ||
LOG_INFO("new client fd=" + std::to_string(client_conn->GetFd()) + " maps to reactor " + std::to_string(idx)); | ||
client_conn->SetLooper(reactors_[idx]); | ||
reactors_[idx]->AddConnection(std::move(client_conn)); | ||
NetAddress client_address; | ||
int accept_fd = server_conn->GetSocket()->Accept(client_address); | ||
if (accept_fd == -1) { | ||
return; | ||
} | ||
auto client_sock = std::make_unique<Socket>(accept_fd); | ||
client_sock->SetNonBlocking(); | ||
auto client_conn = std::make_unique<Connection>(std::move(client_sock)); | ||
client_conn->SetEvents(POLL_READ | POLL_ET); | ||
client_conn->SetCallback(GetCustomHandleCallback()); | ||
|
||
int idx = rand() % reactors_.size(); | ||
LOG_INFO("new client fd=" + std::to_string(client_conn->GetFd()) + | ||
" maps to reactor " + std::to_string(idx)); | ||
client_conn->SetLooper(reactors_[idx]); | ||
reactors_[idx]->AddConnection(std::move(client_conn)); | ||
} | ||
void Acceptor::BaseHandleCallback(Connection *client_conn) { | ||
int fd = client_conn->GetFd(); | ||
if (client_conn->GetLooper()) { | ||
client_conn->GetLooper()->RefreshConnection(fd); | ||
} | ||
int fd = client_conn->GetFd(); | ||
if (client_conn->GetLooper()) { | ||
client_conn->GetLooper()->RefreshConnection(fd); | ||
} | ||
} | ||
void Acceptor::SetCustomAcceptCallback(std::function<void(Connection *)> custom_accept_callback) { | ||
custom_accept_callback = std::move(custom_accept_callback); | ||
acceptor_conn_->SetCallback([this](auto &&PH1) { | ||
BaseAcceptCallback(std::forward<decltype(PH1)>(PH1)); | ||
custom_accept_callback_(std::forward<decltype(PH1)>(PH1)); | ||
}); | ||
void Acceptor::SetCustomAcceptCallback( | ||
std::function<void(Connection *)> custom_accept_callback) { | ||
custom_accept_callback = std::move(custom_accept_callback); | ||
acceptor_conn_->SetCallback([this](auto &&PH1) { | ||
BaseAcceptCallback(std::forward<decltype(PH1)>(PH1)); | ||
custom_accept_callback_(std::forward<decltype(PH1)>(PH1)); | ||
}); | ||
} | ||
void Acceptor::SetCustomHandleCallback(std::function<void(Connection *)> custom_handle_callback) { | ||
custom_handle_callback_ = [this, callback = std::move(custom_handle_callback)](auto &&PH1) { | ||
void Acceptor::SetCustomHandleCallback( | ||
std::function<void(Connection *)> custom_handle_callback) { | ||
custom_handle_callback_ = | ||
[this, callback = std::move(custom_handle_callback)](auto &&PH1) { | ||
BaseHandleCallback(std::forward<decltype(PH1)>(PH1)); | ||
callback(std::forward<decltype(PH1)>(PH1)); | ||
}; | ||
}; | ||
} | ||
auto Acceptor::GetCustomAcceptCallback() const noexcept | ||
-> std::function<void(Connection *)> { | ||
return custom_accept_callback_; | ||
} | ||
auto Acceptor::GetCustomHandleCallback() const noexcept | ||
-> std::function<void(Connection *)> { | ||
return custom_handle_callback_; | ||
} | ||
auto Acceptor::GetAcceptorConnection() noexcept -> Connection * { | ||
return acceptor_conn_.get(); | ||
} | ||
auto Acceptor::GetCustomHandleCallback() const noexcept -> std::function<void(Connection *)> { return custom_accept_callback_; } | ||
auto Acceptor::GetCustomHandleCallback() const noexcept-> std::function<void(Connection *)> { return custom_handle_callback_; } | ||
auto Acceptor::GetAcceptorConnection() noexcept -> Connection * { return acceptor_conn_.get(); } | ||
} | ||
} // namespace Next |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.