C++基于protobuf实现仿RabbitMQ消息队列---接口介绍

这里只展示了接口内容具体,介绍看我的其他文章哦;

muduo::net::TcpServer 类

cpp 复制代码
typedef std::shared_ptr<TcpConnection> TcpConnectionPtr;
typedef std::function<void (const TcpConnectionPtr&)> ConnectionCallback;
typedef std::function<void (const TcpConnectionPtr&, Buffer*, Timestamp)> MessageCallback;

class InetAddress : public muduo::copyable
{
public:
    InetAddress(StringArg ip, uint16_t port, bool ipv6 = false);
};

class TcpServer : noncopyable
{
public:
    enum Option
    {
        kNoReusePort,
        kReusePort,
    };

    TcpServer(EventLoop* loop, const InetAddress& listenAddr, const string& nameArg, Option option = kNoReusePort);

    void setThreadNum(int numThreads);
    void start();
    
    // 当一个新连接建立成功的时候被调用
    void setConnectionCallback(const ConnectionCallback& cb)
    { connectionCallback_ = cb; }
    
    // 消息的业务处理回调函数---这是收到新连接消息的时候被调用的函数
    void setMessageCallback(const MessageCallback& cb)
    { messageCallback_ = cb; }
};

muduo::net::EventLoop 类

cpp 复制代码
class EventLoop : noncopyable
{
public:
    /// Loops forever.
    /// Must be called in the same thread as creation of the object.
    void loop();
    /// Quits loop.
    /// This is not 100% thread safe, if you call through a raw pointer,
    /// better to call through shared_ptr<EventLoop> for 100% safety.
    void quit();
    TimerId runAt(Timestamp time, TimerCallback cb);
    /// Runs callback after @c delay seconds.
    /// Safe to call from other threads.
    TimerId runAfter(double delay, TimerCallback cb);
    /// Runs callback every @c interval seconds.
    /// Safe to call from other threads.
    TimerId runEvery(double interval, TimerCallback cb);
    /// Cancels the timer.
    /// Safe to call from other threads.
    void cancel(TimerId timerId);
private:
    std::atomic<bool> quit_;
    std::unique_ptr<Poller> poller_;
    mutable MutexLock mutex_;
};

muduo::net::TcpConnection 类

cpp 复制代码
class TcpConnection : noncopyable,
    public std::enable_shared_from_this<TcpConnection>
{
public:
    /// Constructs a TcpConnection with a connected sockfd
    ///
    /// User should not create this object.
    TcpConnection(EventLoop* loop,
        const string& name,
        int sockfd,
        const InetAddress& localAddr,
        const InetAddress& peerAddr);
        
    bool connected() const { return state_ == kConnected; }
    bool disconnected() const { return state_ == kDisconnected; }

    void send(string&& message); // C++11
    void send(const void* message, int len);
    void send(const StringPiece& message);
    // void send(Buffer&& message); // C++11
    void send(Buffer* message); // this one will swap data
    void shutdown(); // NOT thread safe, no simultaneous calling

    void setContext(const boost::any& context)
    { context_ = context; }

    const boost::any& getContext() const
    { return context_; }

    boost::any* getMutableContext()
    { return &context_; }

    void setConnectionCallback(const ConnectionCallback& cb)
    { connectionCallback_ = cb; }

    void setMessageCallback(const MessageCallback& cb)
    { messageCallback_ = cb; }
    
private:
    enum StateE { kDisconnected, kConnecting, kConnected, kDisconnecting };
    EventLoop* loop_; 
    ConnectionCallback connectionCallback_; 
    MessageCallback messageCallback_; 
    WriteCompleteCallback writeCompleteCallback_; 
    boost::any context_;
};

muduo::net::TcpClient 类

cpp 复制代码
class TcpClient : noncopyable
{
public:
    // TcpClient(EventLoop* loop);
    // TcpClient(EventLoop* loop, const string& host, uint16_t port);
    TcpClient(EventLoop* loop,
        const InetAddress& serverAddr,
        const string& nameArg);

    ~TcpClient(); // force out-line dtor, for std::unique_ptr members.
    
    void connect();//连接服务器
    void disconnect();//关闭连接
    void stop();
    
    //获取客户端对应的通信连接 Connection 对象的接口,发起 Connect 后,有可能还没有连接建立成功
    TcpConnectionPtr connection() const
    {
        MutexLockGuard lock(mutex_);
        return connection_;
    }

    /// 连接服务器成功的回调函数
    void setConnectionCallback(ConnectionCallback cb)
    { connectionCallback_ = std::move(cb); }
    
    /// 收到服务器发送的消息时的回调函数
    void setMessageCallback(MessageCallback cb)
    { messageCallback_ = std::move(cb); }
    
private:
    EventLoop* loop_;
    ConnectionCallback connectionCallback_;
    MessageCallback messageCallback_;
    WriteCompleteCallback writeCompleteCallback_;
    TcpConnectionPtr connection_ GUARDED_BY(mutex_);
};

// 需要注意的是,因为 muduo 库不管是服务器还是客户端都是异步操作,
// 对于客户端来说如果我们在连接还没有完全建立成功的时候发送数据,这是不允许的。
// 因此我们可以使用内置的 CountDownLatch 类进行同步控制

class CountDownLatch : noncopyable
{
public:
    explicit CountDownLatch(int count);
    
    void wait()
    {
        MutexLockGuard lock(mutex_);
        while (count_ > 0)
        {
            condition_.wait();
        }
    }

    void countDown()
    {
        MutexLockGuard lock(mutex_);
        --count_;
        if (count_ == 0)
        {
            condition_.notifyAll();
        }
    }

    int getCount() const;
    
private:
    mutable MutexLock mutex_;
    Condition condition_ GUARDED_BY(mutex_);
    int count_ GUARDED_BY(mutex_);
};

muduo::net::Buffer 类

cpp 复制代码
public:
    static const size_t kCheapPrepend = 8;
    static const size_t kInitialSize = 1024;
    
    explicit Buffer(size_t initialSize = kInitialSize)
        : buffer_(kCheapPrepend + initialSize),
          readerIndex_(kCheapPrepend),
          writerIndex_(kCheapPrepend)
    { }
    
    void swap(Buffer& rhs);
    
    size_t readableBytes() const;
    size_t writableBytes() const;
    const char* peek() const;
    
    const char* findEOL() const;
    const char* findEOL(const char* start) const;
    
    void retrieve(size_t len);
    void retrieveInt64();
    void retrieveInt32();
    void retrieveInt16();
    void retrieveInt8();
    string retrieveAllAsString();
    string retrieveAsString(size_t len);
    
    void append(const StringPiece& str);
    void append(const char* /*restrict*/ data, size_t len);
    void append(const void* /*restrict*/ data, size_t len);
    
    char* beginWrite();
    const char* beginWrite() const;
    void hasWritten(size_t len);
    
    void appendInt64(int64_t x);
    void appendInt32(int32_t x);
    void appendInt16(int16_t x);
    void appendInt8(int8_t x);
    
    int64_t readInt64();
    int32_t readInt32();
    int16_t readInt16();
    int8_t readInt8();
    
    int64_t peekInt64() const;
    int32_t peekInt32() const;
    int16_t peekInt16() const;
    int8_t peekInt8() const;
    
    void prependInt64(int64_t x);
    void prependInt32(int32_t x);
    void prependInt16(int16_t x);
    void prependInt8(int8_t x);
    void prepend(const void* /*restrict*/ data, size_t len);

private:
    std::vector<char> buffer_;
    size_t readerIndex_;
    size_t writerIndex_;
    static const char kCRLF[];
相关推荐
看我干嘛!几秒前
python第四次作业
开发语言·python
Coder_preston几秒前
Java集合框架详解
java·开发语言
多多*5 分钟前
2026年最新 测试开发工程师相关 Linux相关知识点
java·开发语言·javascript·算法·spring·java-ee·maven
Laurence6 分钟前
从零到一构建 C++ 项目(IDE / 命令行双轨实现)
前端·c++·ide
我在人间贩卖青春12 分钟前
cout语句和cin语句
c++·cin·输入输出流·cout
Jiu-yuan16 分钟前
C++文件操作
c++
2301_7634725830 分钟前
实时系统下的C++编程
开发语言·c++·算法
阿猿收手吧!37 分钟前
【C++】深入理解C++ Atomic内存序:解决什么问题?怎么用?
开发语言·c++
小白学大数据41 分钟前
Python爬虫实现无限滚动页面的自动点击与内容抓取
开发语言·爬虫·python·pandas
Andy Dennis41 分钟前
一文漫谈设计模式之创建型模式(一)
java·开发语言·设计模式