muduo/base/timestamp.cc
muduo/base/timestamp.h
muduo/base/copyable.h
muduo/base/types.h
其中timestamp.cc和timestamp.h 是類的檔案。
copyable.**件中包括的空類是乙個標識類,表示繼承該類的所有類都是可複製的,是值語義。
types.**件主要使用了該檔案中對string類的包含。
#ifndef muduo_base_timestamp_h
#define muduo_base_timestamp_h
#include #include #include namespace muduo
////// constucts a timestamp at specific time
////// @param microsecondssinceepoch
explicit timestamp(int64_t microsecondssinceepoch);
void swap(timestamp& that)
// default copy/assignment/dtor are okay
string tostring() const;
string toformattedstring() const;
bool valid() const
// for internal usage.
int64_t microsecondssinceepoch() const
time_t secondssinceepoch() const
////// get time of now.
///static timestamp now();
static timestamp invalid();
static const int kmicrosecondspersecond = 1000 * 1000;
private:
int64_t microsecondssinceepoch_;
};inline bool operator
inline bool operator==(timestamp lhs, timestamp rhs)
////// gets time difference of two timestamps, result in seconds.
////// @param high, low
/// @return (high-low) in seconds
/// @c double has 52-bit precision, enough for one-microseciond
/// resolution for next 100 years.
inline double timedifference(timestamp high, timestamp low)
////// add @c seconds to given timestamp.
////// @return timestamp+seconds as timestamp
///inline timestamp addtime(timestamp timestamp, double seconds)
}#endif // muduo_base_timestamp_h
boost::less_than_comparable<>是模版類,繼承這個類,表明要求實現=這些比較運算子。這種程式設計是一種模版元的程式設計思想。
可以減少實現的**。
#include #include #include #define __stdc_format_macros
#include #undef __stdc_format_macros
#include using namespace muduo;
boost_static_assert(sizeof(timestamp) == sizeof(int64_t));
timestamp::timestamp(int64_t microseconds)
: microsecondssinceepoch_(microseconds)
string timestamp::tostring() const
; int64_t seconds = microsecondssinceepoch_ / kmicrosecondspersecond;
int64_t microseconds = microsecondssinceepoch_ % kmicrosecondspersecond;
snprintf(buf, sizeof(buf)-1, "%" prid64 ".%06" prid64 "", seconds, microseconds);
return buf;
}string timestamp::toformattedstring() const
; time_t seconds = static_cast(microsecondssinceepoch_ / kmicrosecondspersecond);
int microseconds = static_cast(microsecondssinceepoch_ % kmicrosecondspersecond);
struct tm tm_time;
gmtime_r(&seconds, &tm_time);//thread safe function
snprintf(buf, sizeof(buf), "%4d%02d%02d %02d:%02d:%02d.%06d",
tm_time.tm_year + 1900, tm_time.tm_mon + 1, tm_time.tm_mday,
tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec,
microseconds);
return buf;
}timestamp timestamp::now()
timestamp timestamp::invalid()
是標頭檔案/usr/include/inttypes.h中定義的乙個巨集。可以自動根據作業系統的版本,來表示是"ld"還是"lld"。
gmtime(執行緒不安全的)是把日期和時間轉換為格林威治(gmt)時間的函式。將引數timep 所指的time_t 結構中的資訊轉換成真實世界所使用的時間日期表示方法,然後將結果由結構tm返回。使用gmtime後要立即處理結果,否則返回的指標指向的內容可能會被覆蓋。
乙個好的方法是使用gmtime_r(執行緒安全的),gmtime_r()函式功能與此相同,但是它可以將資料儲存到使用者提供的結構體中,由於使用了使用者分配的記憶體,是不會出錯的。
muduo/base/tests/timestamp_unittest.cc檔案是該類的測試檔案。
#include #include #include using muduo::timestamp;
void passbyconstreference(const timestamp& x)
void passbyvalue(timestamp x)
void benchmark()
printf("front: %s\n", stamps.front().tostring().c_str());
printf("back: %s\n", stamps.back().tostring().c_str());
printf("back-front: %f\n", timedifference(stamps.back(), stamps.front()));
int increments[100] = ;
int64_t start = stamps.front().microsecondssinceepoch();
for (int i = 1; i < knumber; ++i)
else if (inc < 100)
else
} for (int i = 0; i < 100; ++i) }
int main()
muduo網路庫使用
之前粗略看過一段時間的nginx原始碼,對基於c實現的web server有了初步的理解。現在結合muduo網路庫來學習下c 的web server。首先是muduo網路庫的安裝和環境搭建 可參見muduo網路庫是給出了很多的例子,我首先是閱讀了最簡單的echo的原始碼,然後自己編寫makefile...
muduo網路庫編譯安裝
sudo apt get install cmake sudo apt get install libboost dev tar zxf muduo 0.9.1 beta.tar.gz cd muduo build.shcentos系統使用yum install boost devel來安裝libb...
muduo網路庫編譯安裝
2 安裝依賴 sudo apt get install cmake sudo apt get install libboost dev tar zxf muduo 0.9.1 beta.tar.gz cd muduo build.sh centos系統使用yum install boost deve...