Skip to content

Commit

Permalink
add CMakeLists.txt
Browse files Browse the repository at this point in the history
  • Loading branch information
Dueplay committed Sep 28, 2023
1 parent 77ea519 commit 7614de8
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 101 deletions.
Binary file removed demo/echo_client
Binary file not shown.
44 changes: 0 additions & 44 deletions demo/echo_client.cpp

This file was deleted.

Binary file removed demo/echo_server
Binary file not shown.
22 changes: 0 additions & 22 deletions demo/echo_server.cpp

This file was deleted.

55 changes: 29 additions & 26 deletions src/core/buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,57 @@

namespace Next {

Buffer::Buffer(size_t initial_capacity) {
buf_.reserve(initial_capacity);
}
Buffer::Buffer(size_t initial_capacity) { buf_.reserve(initial_capacity); }

void Buffer::Append(const unsigned char *new_char_data, size_t data_size) {
buf_.insert(buf_.end(), new_char_data, new_char_data + data_size);
buf_.insert(buf_.end(), new_char_data, new_char_data + data_size);
}

void Buffer::Append(const std::string &new_string_data) {
Append(reinterpret_cast<const unsigned char *>(new_string_data.c_str()), new_string_data.size());
Append(reinterpret_cast<const unsigned char *>(new_string_data.c_str()),
new_string_data.size());
}

void Buffer::Append(std::vector<unsigned char> &&new_buf_data) {

buf_.insert(buf_.end(), std::make_move_iterator(new_buf_data.begin()), std::make_move_iterator(new_buf_data.end()));

buf_.insert(buf_.end(), std::make_move_iterator(new_buf_data.begin()),
std::make_move_iterator(new_buf_data.end()));
}

void Buffer::AppendToHead(const unsigned char *new_char_data, size_t data_size) {
buf_.insert(buf_.begin(), new_char_data, new_char_data + data_size);
void Buffer::AppendToHead(const unsigned char *new_char_data,
size_t data_size) {
buf_.insert(buf_.begin(), new_char_data, new_char_data + data_size);
}

void Buffer::AppendToHead(const std::string &new_string_data) {
AppendToHead(reinterpret_cast<const unsigned char *>(new_string_data.c_str()), new_string_data.size());
AppendToHead(reinterpret_cast<const unsigned char *>(new_string_data.c_str()),
new_string_data.size());
}

auto Buffer::FindAndPopTill(const std::string &target) -> std::optional<std::string> {
std::optional<std::string> ret = std::nullopt;
auto curr_content = ToStringView();
auto pos = curr_content.find(target);
if (pos != std::string::npos) {
ret = curr_content.substr(0, pos + target.size());
buf_.erase(buf_.begin(), buf_.begin() + pos + target.size());
}
return ret;
auto Buffer::FindAndPopTill(const std::string &target)
-> std::optional<std::string> {
std::optional<std::string> ret = std::nullopt;
auto curr_content = ToStringView();
auto pos = curr_content.find(target);
if (pos != std::string::npos) {
ret = curr_content.substr(0, pos + target.size());
buf_.erase(buf_.begin(), buf_.begin() + pos + target.size());
}
return ret;
}

auto Buffer::Size() const noexcept -> size_t { return buf_.size(); }

auto Buffer::Capacity() const noexcept -> size_t { return buf_.capacity(); };
auto Buffer::Capacity() const noexcept -> size_t { return buf_.capacity(); }

auto Buffer::Data() noexcept -> const unsigned char * { return buf_.data(); };
auto Buffer::Data() noexcept -> const unsigned char * { return buf_.data(); }

auto Buffer::ToStringView() const noexcept -> std::string_view {
// string_view只是一个字符串的视图,构造函数可以避免拷贝,做到O(1)复杂度
//std::string_view类的成员变量只包含两个:字符串指针和字符串长度。
return {reinterpret_cast<const char *>(buf_.data()), buf_.size() };
// string_view只是一个字符串的视图,构造函数可以避免拷贝,做到O(1)复杂度
// std::string_view类的成员变量只包含两个:字符串指针和字符串长度。
return {reinterpret_cast<const char *>(buf_.data()), buf_.size()};
}

void Buffer::Clear() noexcept { buf_.clear(); }
}

} // namespace Next
4 changes: 2 additions & 2 deletions src/core/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ auto Connection::Recv() -> std::pair<ssize_t, bool> {
void Connection::Send() {
ssize_t curr_write = 0;
ssize_t write;
const size_t to_write = GetWriteBufferSize();
const ssize_t to_write = GetWriteBufferSize();
const unsigned char *buf = write_buffer_->Data();
while (curr_write < to_write) {
write = send(GetFd(), buf + curr_write, to_write - curr_write, 0);
Expand All @@ -108,7 +108,7 @@ void Connection::Send() {
}
ClearWriteBuffer();
}
void Connection::ClearReadBuffer() noexcept { read_buffer_->Clear(); };
void Connection::ClearReadBuffer() noexcept { read_buffer_->Clear(); }
void Connection::ClearWriteBuffer() noexcept { write_buffer_->Clear(); }

void Connection::SetLooper(Looper *looper) noexcept { owner_looper_ = looper; }
Expand Down
2 changes: 1 addition & 1 deletion src/core/looper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void Looper::Loop() {
Connection *timer_conn = nullptr;

for (auto &conn : ready_connections) {
if (conn = timer_.GetTimerConnection()) {
if (conn == timer_.GetTimerConnection()) {
timer_conn = conn;
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion src/include/core/next_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class NextServer {
: pool_(std::make_unique<ThreadPool>(concurrency)),
listener_(std::make_unique<Looper>()) {
for (size_t i = 0; i < pool_->GetSize(); i++) {
reactors_.push_back(std::make_unique<Looper>(3000));
reactors_.push_back(std::make_unique<Looper>(TIMER_EXPIRATION));
}
for (auto &reactor : reactors_) {
pool_->SubmitTask([capture0 = reactor.get()]() { capture0->Loop(); });
Expand Down
10 changes: 5 additions & 5 deletions test/timer/timer_test.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include "../../src/include/core/timer.h"
#include <iostream>
int main() {
Next::Timer timer;
std::function<void()> f = []() {
std::cout << "this a singtimer" << endl;
};
Next::Timer timer;
std::function<void()> f = []() {
std::cout << "this a singtimer" << std::endl;
};

timer.AddSingleTimer(3, f);
timer.AddSingleTimer(3, f);
}

0 comments on commit 7614de8

Please sign in to comment.