Muduo網絡庫詳解

一、木鐸

木鐸是一款開放源代碼C++網絡庫,它提供了高效的並發和異步網絡編程接口。它是一個基於Reactor模式的網絡庫,採用了Pimpl技術來避免頭文件的相互依賴。它是一款輕量級的網絡庫,僅依賴於Boost C++庫。

二、沐朵so奶咖減肥產品能瘦嗎

沐朵so奶咖減肥產品和Muduo網絡庫沒有直接的聯繫。

三、木鐸金聲

木鐸提供了多種組件,其中EventLoop是最為核心的組件,其創建了一個線程池和一個EventLoop對象池,將文件描述符上的IO事件通過Epoll機制轉發給對應的事件處理器(Channel)。EventLoop、Channel、Epoll都是多線程程序中非常重要的組件,Muduo網絡庫的架構非常經典。

class EventLoop : noncopyable {
public:
  typedef std::function Functor;

  EventLoop();
  ~EventLoop();

  void loop();
  void quit();

  Timestamp pollReturnTime() const { return pollReturnTime_; }
  int64_t iteration() const { return iteration_; }

  void runInLoop(Functor cb);
  void queueInLoop(Functor cb);

  TimerId runAt(const Timestamp& time, TimerCallback cb);
  TimerId runAfter(double delay, TimerCallback cb);
  TimerId runEvery(double interval, TimerCallback cb);
  void cancel(TimerId timerId);

  void wakeup();
  void updateChannel(Channel* channel);
  void removeChannel(Channel* channel);
  bool hasChannel(Channel* channel);

  void assertInLoopThread() {
    if (!isInLoopThread()) {
      abortNotInLoopThread();
    }
  }

  bool isInLoopThread() const { return threadId_ == CurrentThread::tid(); }
  bool eventHandling() const { return eventHandling_; }
  void setContext(const boost::any& context) {
    context_ = context;
  }
  const boost::any& getContext() const { return context_; }
  boost::any* getMutableContext() { return &context_; }
  static EventLoop* getEventLoopOfCurrentThread();

 private:
  void abortNotInLoopThread();
  void handleRead(); 
  void doPendingFunctors();

  void printActiveChannels() const;  // DEBUG
  typedef std::vector ChannelList;

  bool looping_; /* atomic */
  bool quit_; /* atomic */
  bool eventHandling_; /* atomic */
  bool callingPendingFunctors_; /* atomic */
  int64_t iteration_;
  const pid_t threadId_;
  Timestamp pollReturnTime_;
  std::unique_ptr poller_;
  std::unique_ptr timerQueue_;
  int wakeupFd_;
  // unlike in TimerQueue, which is an internal class,
  // we don't expose Channel to client.
  std::unique_ptr wakeupChannel_;
  boost::any context_;
  ChannelList activeChannels_;
  Channel* currentActiveChannel_;
  mutable MutexLock mutex_;
  std::vector pendingFunctors_;
};

四、母多少筆畫

「母」字筆畫為五畫,和Muduo網絡庫沒有直接關係。

五、木鐸之心

Muduo網絡庫的作者是Chen Shuo,在此之前,他開發了Hawdjon,一款聚焦並發編程對C++11標準庫進行封裝的庫,並在Hawdjon的基礎上,最終編寫了Muduo網絡庫。

六、目多讀什麼

Muduo網絡庫適合對高性能網絡編程感興趣的開發者閱讀,關注網絡編程、多線程編程實現的細節和經驗。

七、木鐸有心

Muduo網絡庫提供了很多高性能的類庫,如內存池,日誌組件,可重入包裝、時間戳和定時器等等工具類庫。在日誌庫方面,Muduo提供了對異步、基於線程的文件輸出,和基於UDP輸出日誌的兩種輸出方式,這些輸出方式都可以按照不同優先級級別掛載到Logger對象中,並且提供了對日誌文件的自動分割機制。

class Logger : noncopyable {
 public:
  enum LogLevel {
    TRACE,
    DEBUG,
    INFO,
    WARN,
    ERROR,
    FATAL,
    NUM_LOG_LEVELS,
  };

  // compile time calculation of basename of source file
  class SourceFile {
   public:
    template 
    inline SourceFile(const char (&arr)[N])
        : data_(arr),
          size_(N - 1) {
      const char* slash = strrchr(data_, '/'); // builtin function
      if (slash) {
        data_ = slash + 1;
        size_ -= static_cast(data_ - arr);
      }
    }

    explicit SourceFile(const char* filename)
        : data_(filename) {
      const char* slash = strrchr(filename, '/');
      if (slash) {
        data_ = slash + 1;
      }
      size_ = static_cast(strlen(data_));
    }

    const char* data_;
    int size_;
  };

  Logger(SourceFile file, int line);
  Logger(SourceFile file, int line, LogLevel level);
  Logger(SourceFile file, int line, LogLevel level, const char* func);
  Logger(SourceFile file, int line, bool toAbort);
  ~Logger();

  static LogLevel logLevel();
  static void setLogLevel(LogLevel level);

  static void setOutput(OutputFunc);
  static void setFlush(FlushFunc);

  static void setTimeZone(const TimeZone& tz);

  // FIXME: replace fixed array with dynamic allocation
  typedef FixedBuffer Buffer;

  void finish();

  // no copyable
  Logger(const Logger&) = delete;
  Logger& operator=(const Logger&) = delete;

  Logger(Logger&&) = default;
  Logger& operator=(Logger&&) = default;

  // Eg. LOG_INFO << "Hello world";
  static void log(SourceFile file, int line, LogLevel level, const char* fmt, ...);
  static void formatLog(const char* fmt, va_list ap, Buffer& buf, LogLevel level);

 private:
  class Impl {
   public:
    typedef Logger::LogLevel LogLevel;
    Impl(LogLevel level, int old_errno, const SourceFile& file, int line);
    void formatTime();
    void finish();

    Timestamp time_;
    LogStream stream_;
    LogLevel level_;
    int line_;
    SourceFile basename_;
  };

  Impl impl_;
};

#define LOG_TRACE if (muduo::Logger::logLevel() <= muduo::Logger::TRACE) \
  muduo::Logger(__FILE__, __LINE__, muduo::Logger::TRACE, __func__).stream()
#define LOG_DEBUG if (muduo::Logger::logLevel() <= muduo::Logger::DEBUG) \
  muduo::Logger(__FILE__, __LINE__, muduo::Logger::DEBUG, __func__).stream()
#define LOG_INFO if (muduo::Logger::logLevel() <= muduo::Logger::INFO) \
  muduo::Logger(__FILE__, __LINE__).stream()
#define LOG_WARN muduo::Logger(__FILE__, __LINE__, muduo::Logger::WARN).stream()
#define LOG_ERROR muduo::Logger(__FILE__, __LINE__, muduo::Logger::ERROR).stream()
#define LOG_FATAL muduo::Logger(__FILE__, __LINE__, muduo::Logger::FATAL).stream()
#define LOG_SYSERR muduo::Logger(__FILE__, __LINE__, false).stream()
#define LOG_SYSFATAL muduo::Logger(__FILE__, __LINE__, true).stream()

八、母多少畫

「母」字筆畫為五畫,和Muduo網絡庫沒有直接關係。

九、木鐸什麼意思

據網絡上的資料,Muduo是「基於Linux操作系統MULtiple DOor的一種高性能C++服務器開發框架」。

總體來說,Muduo網絡庫是一款經典、高性能、輕量級的網絡庫,並且提供了很多優秀的組件、工具類庫和異步網絡編程接口,它的源碼對於對多線程、網絡編程、經典網絡庫的實現感興趣的開發者來說,是一份優秀的參考和學習資料。

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/194620.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-02 14:40
下一篇 2024-12-02 14:40

相關推薦

  • 使用Netzob進行網絡協議分析

    Netzob是一款開源的網絡協議分析工具。它提供了一套完整的協議分析框架,可以支持多種數據格式的解析和可視化,方便用戶對協議數據進行分析和定製。本文將從多個方面對Netzob進行詳…

    編程 2025-04-29
  • 微軟發佈的網絡操作系統

    微軟發佈的網絡操作系統指的是Windows Server操作系統及其相關產品,它們被廣泛應用於企業級雲計算、數據庫管理、虛擬化、網絡安全等領域。下面將從多個方面對微軟發佈的網絡操作…

    編程 2025-04-28
  • 蔣介石的人際網絡

    本文將從多個方面對蔣介石的人際網絡進行詳細闡述,包括其對政治局勢的影響、與他人的關係、以及其在歷史上的地位。 一、蔣介石的政治影響 蔣介石是中國現代歷史上最具有政治影響力的人物之一…

    編程 2025-04-28
  • 基於tcifs的網絡文件共享實現

    tcifs是一種基於TCP/IP協議的文件系統,可以被視為是SMB網絡文件共享協議的衍生版本。作為一種開源協議,tcifs在Linux系統中得到廣泛應用,可以實現在不同設備之間的文…

    編程 2025-04-28
  • 如何開發一個網絡監控系統

    網絡監控系統是一種能夠實時監控網絡中各種設備狀態和流量的軟件系統,通過對網絡流量和設備狀態的記錄分析,幫助管理員快速地發現和解決網絡問題,保障整個網絡的穩定性和安全性。開發一套高效…

    編程 2025-04-27
  • 用Python爬取網絡女神頭像

    本文將從以下多個方面詳細介紹如何使用Python爬取網絡女神頭像。 一、準備工作 在進行Python爬蟲之前,需要準備以下幾個方面的工作: 1、安裝Python環境。 sudo a…

    編程 2025-04-27
  • 如何使用Charles Proxy Host實現網絡請求截取和模擬

    Charles Proxy Host是一款非常強大的網絡代理工具,它可以幫助我們截取和模擬網絡請求,方便我們進行開發和調試。接下來我們將從多個方面詳細介紹如何使用Charles P…

    編程 2025-04-27
  • 網絡拓撲圖的繪製方法

    在計算機網絡的設計和運維中,網絡拓撲圖是一個非常重要的工具。通過拓撲圖,我們可以清晰地了解網絡結構、設備分佈、鏈路情況等信息,從而方便進行故障排查、優化調整等操作。但是,要繪製一張…

    編程 2025-04-27
  • 網絡爬蟲什麼意思?

    網絡爬蟲(Web Crawler)是一種程序,可以按照制定的規則自動地瀏覽互聯網,並將獲取到的數據存儲到本地或者其他指定的地方。網絡爬蟲通常用於搜索引擎、數據採集、分析和處理等領域…

    編程 2025-04-27
  • 網絡數據爬蟲技術用法介紹

    網絡數據爬蟲技術是指通過一定的策略、方法和技術手段,獲取互聯網上的數據信息並進行處理的一種技術。本文將從以下幾個方面對網絡數據爬蟲技術做詳細的闡述。 一、爬蟲原理 網絡數據爬蟲技術…

    編程 2025-04-27

發表回復

登錄後才能評論