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[];
相关推荐
JIngJaneIL1 小时前
基于Java失物招领系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·vue
豐儀麟阁贵1 小时前
9.3获取字符串信息
java·开发语言·前端·算法
kusedexingfu1 小时前
如何理解python中的闭包
开发语言·python
Protein_zmm1 小时前
第二章 应用层(套接字编程)
开发语言·计算机网络·php
by__csdn1 小时前
ES6新特性全攻略:JavaScript的现代革命
开发语言·前端·javascript·typescript·ecmascript·es6·js
foxsen_xia1 小时前
go(基础10)——错误处理
开发语言·后端·golang
robch1 小时前
Java后端优雅的实现分页搜索排序-架构2
java·开发语言·架构
她说..1 小时前
在定义Java接口参数时,遇到整数类型,到底该用int还是Integer?
java·开发语言·java-ee·springboot
Evand J1 小时前
【PSINS进阶例程】雷达三维跟踪与EKF轨迹滤波。带坐标转换,观测为斜距、方向角、俯仰角。MATLAB编写,附下载链接
开发语言·matlab·psins·雷达观测