乙個超級簡單的伺服器框架

2021-08-13 09:57:03 字數 3425 閱讀 7832

1. 本伺服器端框架採用epoll+執行緒池+任務佇列

2. epoll和sernasock是我封裝的,threadpool用的是

epoll.h

/**

* by:gzh

* 2017.12.12

* 可以改進的地方:epollin 接收資料read()處可以優化,

* 現在每次可以接收1kb的資料

*/#ifndef _gzh_epoll_h_

#define _gzh_epoll_h_

#include #include #include #include #include "net.h"

namespace gzhlib;}

#endif //_gzh_epoll_h_

epoll.cpp

#include #include #include #include #include "epoll.h"

#include "threadpool.h"

using namespace gzhlib;

gzhepoll* gzhepoll::create(socket serversocket, int maxevents, int threadcounts)

int gzhepoll::init(socket serversocket, int maxevents, int threadcounts)

void gzhepoll::run()

if(numwaitingevent == 0)

public:

//獲取埠

uint16_t getport() ;

//得到描述符

int getsock() ;

//監聽

int listen(int num) const;

//接收客戶端連線

int accept(struct sockaddr *addr, socklen_t *len) const;

//傳送資料

int send(int cli_sock, const char *szbuffer, size_t len) const;

//接收資料

int recv(int cli_sock, char *szbuffer, size_t len) const;

};}#endif //_sernasock_h_

server.cpp

/**

* cservernativesock實現

*/#include #include "sernasock.h"

using namespace gzhlib;

//建構函式

cservernativesock::cservernativesock(uint16_t port):port(port)

//埠復用,繫結位址

void cservernativesock::init()

; //開啟主迴圈

epoll->run();

return 0;

}

接下來是執行緒池,詳情請移步

這裡只貼**,不做解釋。

threadpool.h

#ifndef thread_pool_h_

#define thread_pool_h_

#include #include #include #include #include #include #include namespace netlib;}

#endif

threadpool.cpp

#include "threadpool.h"

#include #include #include #include #include #include #include using namespace netlib;

threadpool::threadpool(int threadnumber)

:threadnumber_(threadnumber),

running_(true)

threadpool::~threadpool()

}bool threadpool::start(void)

usleep(500);

//printf("執行緒池開始執行\n");

return true;

}bool threadpool::stop(void)

}return true;

}void threadpool::threadwork(void)

if(!tasks_.empty())

else

}task(); //執行任務

}}

net.h

/***********

net.h

***********/

#ifndef _net_h

#define _net_h

#include #include //epoll ways file

#include #include //block and noblock

#include #include #include #include #include #define hand_error(msg) dowhile(0)

#endif

cmakelists.txt

#1.cmake verson,指定cmake版本 

cmake_minimum_required(version 3.2)

#2.project name,指定專案的名稱,一般和專案的資料夾名稱對應

#可執行檔案的名稱

project(server)

#3.標頭檔案在當前目錄,可以不用寫

include_directories(

)#4.將需要的原始檔放到乙個變數裡如:source_file

set(source_file

epoll.cpp

server.cpp

threadpool.cpp

test.cpp

)#6.add executable file,新增要編譯的可執行檔案

add_executable($ $)

#7.add link library,新增可執行檔案所需要的庫,比如我們用到了libm.so(命名規則:lib+name+.so),就新增該庫的名稱

set(shared_lib

pthread

)target_link_libraries($ $)

編譯步驟:

mkdir build

cd build

cmake ..

make

執行:./server

乙個超級簡單的伺服器框架

1.本伺服器端框架採用epoll 執行緒池 任務佇列 2.epoll和sernasock是我封裝的,threadpool用的是 epoll.h by gzh 2017.12.12 可以改進的地方 epollin 接收資料read 處可以優化,現在每次可以接收1kb的資料 ifndef gzh epo...

乙個簡單的遊戲伺服器框架

最近一段時間不是很忙,就寫了乙個自己的遊戲伺服器框架雛形,很多地方還不夠完善,但是基本上也算是能夠跑起來了。我先從上層結構說起,一直到實現細節吧,想起什麼就寫什麼。第一部分 伺服器邏輯 伺服器這邊簡單的分為三個部分,客戶端的連線首先到達閘道器伺服器,閘道器這裡有個執行緒用來監聽來自與客戶端的連線,然...

乙個簡單的遊戲伺服器框架

最近一段時間不是很忙,就寫了乙個自己的遊戲伺服器框架雛形,很多地方還不夠完善,但是基本上也算是能夠跑起來了。我先從上層結構說起,一直到實現細節吧,想起什麼就寫什麼。第一部分 伺服器邏輯 伺服器這邊簡單的分為三個部分,客戶端的連線首先到達閘道器伺服器,閘道器這裡有個執行緒用來監聽來自與客戶端的連線,然...