目錄
類實現
編譯測試
這一篇本質上是為了TcpServer而做的一層封裝,讓外界調用更加簡潔
參考上文
TcpServer服務器管理模塊(模塊十)-CSDN博客
類實現
echo.hpp
#include "../server.hpp"class EchoServer
{
private:TcpServer _server;private:void OnConnected(const PtrConnection &conn){DBG_LOG("NEW CONNECTION:%p", conn.get());}void OnClosed(const PtrConnection &conn){DBG_LOG("CLOSE CONNECTION:%p", conn.get());}void OnMessage(const PtrConnection &conn, Buffer *buf){conn->Send(buf->ReadPosition(), buf->ReadAbleSize());buf->MoveReadOffset(buf->ReadAbleSize());conn->Shutdown(); // 調用關閉接口}public:EchoServer(int port) : _server(port){_server.SetThreadCount(2);_server.EnableInactiveRelease(10);_server.SetClosedCallback(std::bind(&EchoServer::OnClosed, this, std::placeholders::_1));_server.SetConnectedCallback(std::bind(&EchoServer::OnConnected, this, std::placeholders::_1));_server.SetMessageCallback(std::bind(&EchoServer::OnMessage, this, std::placeholders::_1, std::placeholders::_2));}void Start() { _server.Start(); }
};
main.cc
#include "echo.hpp"int main()
{EchoServer server(8500);server.Start();return 0;
}
編譯測試
因為本篇是基于使用而創建的一個回顯調用,所以對文件的位置進行了修改,加之代碼中涉及到了相對文件的引用,所以特此呈現文件的分布情況
服務端顯示
客戶端回顯
符合預期
之后的文獻會對該項目進行測試性能,將圍繞性能測試來進行說明