Boost.Asio实现的高性能服务器

```cpp
#include <iostream>
#include <vector>
#include <thread>
#include <queue>
#include <mutex>
#include <condition_variable>
#include <functional>
#include <atomic>
#include <csignal>
#include <boost/asio.hpp>

using boost::asio::ip::tcp;

const int BUFFER_SIZE = 1024 * 1024; // 1MB buffer
const int THREAD_POOL_SIZE = 4; // 可以根据需要调整

class ThreadPool {
public:
    ThreadPool(size_t threads) : stop(false) {
        for(size_t i = 0; i < threads; ++i)
            workers.emplace_back(
                [this] {
                    for(;;) {
                        std::function<void()> task;
                        {
                            std::unique_lock<std::mutex> lock(this->queue_mutex);
                            this->condition.wait(lock,
                                [this]{ return this->stop || !this->tasks.empty(); });
                            if(this->stop && this->tasks.empty())
                                return;
                            task = std::move(this->tasks.front());
                            this->tasks.pop();
                        }
                        task();
                    }
                }
            );
    }

    template<class F>
    void enqueue(F&& f) {
        {
            std::unique_lock<std::mutex> lock(queue_mutex);
            tasks.emplace(std::forward<F>(f));
        }
        condition.notify_one();
    }

    ~ThreadPool() {
        {
            std::unique_lock<std::mutex> lock(queue_mutex);
            stop = true;
        }
        condition.notify_all();
        for(std::thread &worker: workers)
            worker.join();
    }

private:
    std::vector<std::thread> workers;
    std::queue<std::function<void()>> tasks;
    std::mutex queue_mutex;
    std::condition_variable condition;
    bool stop;
};

class Session : public std::enable_shared_from_this<Session> {
private:
    tcp::socket socket_;
    std::vector<char> buffer_;
    uint64_t expected_size_;
    std::vector<char> data_;
    ThreadPool& thread_pool_;

public:
    Session(tcp::socket socket, ThreadPool& pool)
        : socket_(std::move(socket)), buffer_(BUFFER_SIZE), expected_size_(0), thread_pool_(pool) {}

    void start() {
        read_size();
    }

private:
    void read_size() {
        auto self(shared_from_this());
        boost::asio::async_read(socket_, boost::asio::buffer(&expected_size_, sizeof(expected_size_)),
            [this, self](boost::system::error_code ec, std::size_t /*length*/) {
                if (!ec) {
                    data_.clear();
                    data_.reserve(expected_size_);
                    read_data();
                } else {
                    handle_error(ec);
                }
            });
    }

    void read_data() {
        auto self(shared_from_this());
        socket_.async_read_some(boost::asio::buffer(buffer_),
            [this, self](boost::system::error_code ec, std::size_t length) {
                if (!ec) {
                    data_.insert(data_.end(), buffer_.begin(), buffer_.begin() + length);
                    if (data_.size() < expected_size_) {
                        read_data();
                    } else {
                        thread_pool_.enqueue([this, self]() { process_data(); });
                    }
                } else {
                    handle_error(ec);
                }
            });
    }

    void process_data() {
        std::cout << "Received message of size: " << data_.size() << " bytes" << std::endl;
        // 在这里处理接收到的数据
        write_response();
    }

    void write_response() {
        auto self(shared_from_this());
        boost::asio::async_write(socket_, boost::asio::buffer(data_),
            [this, self](boost::system::error_code ec, std::size_t /*length*/) {
                if (!ec) {
                    read_size();
                } else {
                    handle_error(ec);
                }
            });
    }

    void handle_error(const boost::system::error_code& ec) {
        if (ec != boost::asio::error::eof) {
            std::cerr << "Error: " << ec.message() << std::endl;
        }
        // 连接已关闭,不需要做任何事
    }
};

class Server {
private:
    tcp::acceptor acceptor_;
    ThreadPool thread_pool_;

public:
    Server(boost::asio::io_context& io_context, short port)
        : acceptor_(io_context, tcp::endpoint(tcp::v4(), port)), thread_pool_(THREAD_POOL_SIZE) {
        do_accept();
    }

private:
    void do_accept() {
        acceptor_.async_accept(
            [this](boost::system::error_code ec, tcp::socket socket) {
                if (!ec) {
                    std::make_shared<Session>(std::move(socket), thread_pool_)->start();
                }

                do_accept();
            });
    }
};

int main(int argc, char* argv[]) {
    try {
        if (argc != 2) {
            std::cerr << "Usage: server <port>\n";
            return 1;
        }

        // 忽略 SIGPIPE 信号
        std::signal(SIGPIPE, SIG_IGN);

        int port = std::atoi(argv[1]);

        boost::asio::io_context io_context;

        Server s(io_context, port);

        io_context.run();
    }
    catch (std::exception& e) {
        std::cerr << "Exception: " << e.what() << "\n";
    }

    return 0;
}
```

这个改进版本有以下几个主要变化:

1. 添加了 ThreadPool 类来管理工作线程。这样可以更好地控制并发,避免为每个连接创建新线程。

2. 在 Session 类中,使用线程池来处理耗时的数据处理操作(process_data)。

3. 添加了 handle_error 方法来集中处理错误,包括连接断开的情况。

4. 在 main 函数中,添加了 `std::signal(SIGPIPE, SIG_IGN);` 来忽略 SIGPIPE 信号。这可以防止在写入已关闭的 socket 时程序被终止。

5. 移除了之前版本中的显式线程创建,现在所有的并发都由线程池管理。

这个版本应该能更好地处理 socket 断开连接的情况,同时通过使用线程池提高了服务器的效率和稳定性。THREAD_POOL_SIZE 常量定义了线程池中的线程数量,您可以根据服务器的硬件配置和预期负载来调整这个值。

编译和运行方法与之前相同:

```
g++ -std=c++11 server.cpp -o server -lboost_system -lpthread
./server 8080
```

这个实现应该能够更好地处理高并发和断开连接的情况,同时保持了良好的性能和资源利用。
 

  • 9
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值