這是一個性能與易用性兼備的C++ NIO網絡庫,支持C++14及以上版本,並且跨三大主流平台.
底層的IO模型借鑒了muduo 網絡庫的one loop per thread模型,該模型使得線程安全的API封裝更為高效且簡單.
上層提供的API接口借鑒了字節跳動開源的go 語言NIO 網絡庫netpoll,抽像出Listener和Dialer最終通過EventLoop來提供服務.
關於本庫的使用方式可以查看快速開始.
具體的使用範例可以查看examples.
已經支持:
windows/linux/macos各個平台分別用該平台最高性能的多路復用實現(IOCP實現的epoll/epoll/kqueue).EventLoop實現異步調用,如果所在操作系統支持sendfile調用,則會調用該零拷貝調用而不是調用C標準庫.echo服務器,只需要下面的代碼: struct server
{
NETPOLL_TCP_MESSAGE (conn, buffer){conn-> send (buffer-> readAll ());}
};
int main ()
{
auto loop = netpoll::NewEventLoop ();
auto listener = netpoll::tcp::Listener::New ({ 6666 });
listener. bind <server>();
loop. serve (listener);
}100^8s ,可能只需要100*8byte ) 具體可以查看博客:詳細介紹.one loop per thread模型非常匹配.lockfree_queuefuture用於同步,這在客戶端編程中可能很有用. netpoll::SignalTask::Register ({SIGINT,SIGTERM});
netpoll::SignalTask::Add ([](){
// do something
});將來會支持:
性能極高,暫時測試了asio(C++)/netty(java)/netpoll(go語言).
在Windows系統下測試了asio/netty ,而下列圖表測試基於Linux,而我不太會在Linux上部署Java程序,所以下列圖表沒有netty的性能表現.
不同並發情況下單個連接echo服務的平均延遲如下(avg):
服務端的底層模型架構:
Listener的封裝簡化了TcpServer的使用,所有的調用如下:
namespace netpoll ::tcp{
class Listener {
template < typename T>
void bind (Args &&...args); // 用于绑定任意类的对应方法到回调
template < typename T>
std::shared_ptr<T> instance () // 返回内部帮你创建的实例
static Listener New( const InetAddress &address,
const StringView &name = " tcp_listener " ,
bool reUseAddr = true , bool reUsePort = true ); // 建立Listener实例
void enableKickoffIdle ( size_t timeout); // 用于开启剔除空闲连接
}
} Dialer的封裝簡化了TcpClient的使用,所用調用如下:
namespace netpoll ::tcp{
class Dialer {
template < typename T>
void bind (Args &&...args); // 用于绑定任意类的对应方法到回调
template < typename T>
std::shared_ptr<T> instance () // 返回内部帮你创建的实例
static Dialer New( const InetAddress &address,
const StringView &name = " tcp_dialer " ); // 建立Listener实例
void enableRetry (); // 在连接失败后重试
// 以下调用方均是为了在没有C++17的if constexpr情况下的替代品,否则应该直接使用bind
template < typename T, typename ... Args>
static std::shared_ptr<T> Register (Args &&...args);
void onMessage (netpoll::RecvMessageCallback const &cb);
void onConnection (netpoll::ConnectionCallback const &cb);
void onConnectionError (netpoll::ConnectionErrorCallback const &cb);
void onWriteComplete (netpoll::WriteCompleteCallback const &cb);
}
} EventLoop的負載均衡策略沒有單獨設置選項,用的都是Round Robin .
EventLoop相關的API如下:
NewEventLoop(size_t threadNum=2,const netpoll::StringView&name="eventloop") : 建立EventLoop 實例,可以設置EventLoop 的線程數量.
serve方法: 當你New 出Dialer 或Listener 後可以通過該方法為他們提供服務.
serveAsDaemon方法: 與serve 方法的效果是一樣的,但是是開一個新的線程去serve ,不會阻塞當前線程.
enableQuit方法: 允許主動調用循環退出的方法,默認無法主動退出所有的循環線程,配合QuitAllEventLoop方法使用.
QuitAllEventLoop方法:退出所有的循環.
MessageBuffer是用於讀寫內核緩衝區數據的中間緩存,其實就是一個可變的緩衝區,實現起來也很比較簡單,關於不同種類的緩衝區實現可以看看我的這篇文章:可變長與不可變長buffer的實現
這裡就不對各種調用進行描述了,想要了解的可以直接看對應的頭文件: netpoll/util/message_buffer.h .
簡單講一下實現該緩衝區的亮點:
擴容時避免resize的副作用導致無意義的賦值,同時也可以簡化內存開闢和復制的操作.
void MessageBuffer::ensureWritableBytes ( size_t len)
{
if ( writableBytes () >= len) return ;
// move readable bytes
if (m_head + writableBytes () >= (len + kBufferOffset ))
{
std::copy ( begin () + m_head, begin () + m_tail, begin () + kBufferOffset );
m_tail = kBufferOffset + (m_tail - m_head);
m_head = kBufferOffset ;
return ;
}
// create new buffer
size_t newLen;
if ((m_buffer. size () * 2 ) > ( kBufferOffset + readableBytes () + len))
newLen = m_buffer. size () * 2 ;
else newLen = kBufferOffset + readableBytes () + len;
// Avoid the inefficiency of using resize
MessageBuffer newbuffer (newLen);
newbuffer. pushBack (* this );
swap (newbuffer);
}提供readFd 方法,該方法讀取對應fd 的讀緩衝區的數據到MessageBuffer ,每次讀取的內容都足夠的大,比如MessageBuffer可寫區域如果小於8kb,那麼就啟用8kb的備用讀取緩存.
ssize_t MessageBuffer::readFd ( int fd, int *retErrno)
{
char extBuffer[ 8192 ];
struct iovec vec[ 2 ];
size_t writable = writableBytes ();
vec[ 0 ]. iov_base = begin () + m_tail;
vec[ 0 ]. iov_len = static_cast < int >(writable);
vec[ 1 ]. iov_base = extBuffer;
vec[ 1 ]. iov_len = sizeof (extBuffer);
const int iovcnt = (writable < sizeof extBuffer) ? 2 : 1 ;
ssize_t n = :: readv (fd, vec, iovcnt);
if (n < 0 ) { *retErrno = errno; }
else if ( static_cast < size_t >(n) <= writable) { m_tail += n; }
else
{
m_tail = m_buffer. size ();
pushBack ({extBuffer, n - writable});
}
return n;
}TcpConnection類是一個抽像類,在使用時都是通過智能指針來使用.
該接口規範了下列功能:
發送數據(包括string/buffer/file/stream).
/* *
* @brief Send some data to the peer.
*
* @param msg
* @param len
*/
virtual void send (StringView const &msg) = 0;
virtual void send ( const MessageBuffer &buffer) = 0;
virtual void send (MessageBuffer &&buffer) = 0;
virtual void send ( const std::shared_ptr<MessageBuffer> &msgPtr) = 0;
/* *
* @brief Send a file to the peer.
*
* @param fileName in UTF-8
* @param offset
* @param length
*/
virtual void sendFile (StringView const &fileName, size_t offset = 0 ,
size_t length = 0 ) = 0;
/* *
* @brief Send a stream to the peer.
*
* @param callback function to retrieve the stream data (stream ends when a
* zero size is returned) the callback will be called with nullptr when the
* send is finished/interrupted, so that it cleans up any internal data (ex:
* close file).
* @warning The buffer size should be >= 10 to allow http chunked-encoding
* data stream
*/
// (buffer, buffer size) -> size
// of data put in buffer
virtual void sendStream (
std::function<std:: size_t ( char *, std:: size_t )> callback) = 0;獲得連接信息,如地址信息或連接狀態或接收數據的Buffer.
/* *
* @brief New the local address of the connection.
*
* @return const InetAddress&
*/
virtual const InetAddress & localAddr () const = 0;
/* *
* @brief New the remote address of the connection.
*
* @return const InetAddress&
*/
virtual const InetAddress & peerAddr () const = 0;
/* *
* @brief Return true if the connection is established.
*
* @return true
* @return false
*/
virtual bool connected () const = 0;
/* *
* @brief Return false if the connection is established.
*
* @return true
* @return false
*/
virtual bool disconnected () const = 0;
/* *
* @brief New the buffer in which the received data stored.
*
* @return MsgBuffer*
*/
virtual MessageBuffer * getRecvBuffer () = 0;設置連接回調或狀態(斷開連接或設置tcpNoDelay/keepAlive).
/* *
* @brief Set the high water mark callback
*
* @param cb The callback is called when the data in sending buffer is
* larger than the water mark.
* @param markLen The water mark in bytes.
*/
virtual void setHighWaterMarkCallback ( const HighWaterMarkCallback &cb,
size_t markLen) = 0;
/* *
* @brief Set the TCP_NODELAY option to the socket.
*
* @param on
*/
virtual void setTcpNoDelay ( bool on) = 0;
/* *
* @brief Shutdown the connection.
* @note This method only closes the writing direction.
*/
virtual void shutdown () = 0;
/* *
* @brief Close the connection forcefully.
*
*/
virtual void forceClose () = 0;
/* *
* @brief Call this method to avoid being kicked off by TcpServer, refer to
* the kickoffIdleConnections method in the TcpServer class.
*
*/
virtual void keepAlive () = 0;
/* *
* @brief Return true if the keepAlive() method is called.
*
* @return true
* @return false
*/
virtual bool isKeepAlive () = 0;設置該連接的上下文,用於處理該連接的專用業務邏輯.
/* *
* @brief Set the custom data on the connection.
*
* @param context
*/
void setContext ( const Any &context) { m_context = context; }
void setContext (Any &&context) { m_context = std::move (context); }
/* *
* @brief New mutable context
*
* @return Any
*/
Any & getContextRefMut () { return m_context; }
/* *
* @brief New unmutable context
*
* @return Any
*/
Any const & getContextRef () const { return m_context; }
/* *
* @brief Return true if the custom data is set by user.
*
* @return true
* @return false
*/
bool hasContext () const
{
# if __cplusplus >= 201703L
return m_context. has_value ();
# else
return m_context. empty ();
# endif
}
/* *
* @brief Clear the custom data.
*
*/
void clearContext ()
{
# if __cplusplus >= 201703L
m_context. reset ();
# else
m_context. clear ();
# endif
}該連接累計收發的數據量.
/* *
* @brief Return the number of bytes sent
*
* @return size_t
*/
virtual size_t bytesSent () const = 0;
/* *
* @brief Return the number of bytes received.
*
* @return size_t
*/
virtual size_t bytesReceived () const = 0;獲取該連接對應的Loop.
/* *
* @brief New the event loop in which the connection I/O is handled.
*
* @return EventLoop*
*/
virtual EventLoop * getLoop () = 0;暫時提供了下面兩個用例: