基于 tlink的内核态与用户态异步并发消息处理模型
用户态采用sl c模型初始化时建立多个 linksocket,绑定完成之后,向内核发送握手消息,这样内核可以将已经建立的连接记住,以便后续选择可用的连接发送数据。初始化和握手完成之后,由内核主动向用户态发送数据,用户态主线程在各个so cet句柄上面等待读事件的到来,当检测到读事件时,向线程池提交数据读取和处理任务。这样模拟一个连接池和事件分发模型,保证内核数据及时被读取到用户态程序并处理,能做到并发。
而内核态的etlink在接收数据时本身就是以系统调用的方式提供给业务层的发送接口,因此本身就是异步的,性能不是问题。内核态收到数据时,只需要提交给一个内核线程去处理即可。
原型代码如下:共用头文件
#dfneE TLINK_TET
#efineAX_D AA_LEN(76 #defn
AX_PROCSS_COUNT 10#efie MAX_PYL
D1024
d ineAX_PID_CUNT MAX_PROSSCOUN
#dfine MAX_R_DALEN 102deine S
TIC_PEOD 1 24*5 0#e ine MSG_CO
1000#d ine TRE 1用户态
#include
#incl e
nlude
#ncld e
#iclue
#in ude
#iclud
#inlude asm/tpe .h
#include
#include
lu de
inc ude
#include "conf.h"struct ndpo it
{unsigned nt pi;
trct socka r_l s c_add ;stru c sockadd_nl d taddr;int ok_f
st t mgd mg;
} *endoits=N; nt is en_s (voi);
nt handwitknl(vid)it lose_u er id);int doslect st uct timeal wai );
oid* hreadPoc(oid*ed);v * sendTreadProc(vid*arg);void fluhcount n ln)void stt csut(void);vi tart dThread oi);
tat ntaf =0
tati fd_set r et;
ta c strct t evl tout;unsined ch readbffer;sta ic struct time l tno;s ti r timevl oldtimes a ic lon ccunt = ptheadmute_tmtex;sta i long abtes= 0;stat c lo radnum0;static long endby e 0;int main(in rgc,chr*arg[]
po = struc endpoi *)mlloc
sizeof( ruct ndpoint) *AXPD_CNT);reabfer (unsiged har*)mallo c(AX_E_DATA_LEN ; / rate an netlink ocet andbnd.listenuser( if(thred_utex_n t(&mut ,NUL) =0)
pr ntf Init mtu err \");r rn -1 ;
} / en a han hae msg o thenl. let the knl to ee int.
ndwit l( ; / eep(6) // art SenThead ; get imef y(&oldtime,NUL)tmou .t_sec=1
m t.tv_usec=0
/ ait or v t fom th knl to d ach.
h e (1)
{
o s le ct(tmout); f( ab
tes>STATIC_PROID)
{stti ut );
} // le (2);
} /close the soc .
lo _u ser(return 0;
}int lis en_ser(void)
{int pidont;
truct edp nt*ed =NULL;
t uct lmgdr* hNLL;unsign int pid = tpid ); o idcunt= pi d unt MAX_PD_CONT i dcut+)
{
endpoits+ i dont;memst((vo d* e, sizef(str t edoin )); d-> ckd =socket(AF_NETLIN,SOC_RW,TLIN_TST);e-id= +pid unt;i (0=ed-> d)
{pi = ++ed->pid;
} e d->sr c_ddr.lfamil y
AF_NE TINK;e d->sr c_addr.nl_groups = ;ed->src_addr.l_p ed->pid; /
TDO my e th srcddr.nl_i i alredyin used, te bid w l eturn a onzr vau .b d(ed->so ckfd, (struc s kad )&ed->sr _adr, si o(ed-> r_ddr ;ed->de stdr.l_fm y AFE TINK;e->dest_a r.lp d= 0;ed>dest_dd l_goups= ;
/ F ll in the netlink messge pyload /ed>msg.msg_name= (voi *) ->dest_ddr
ed->m . _namlen s eof( d>estad ; printf(" it the cket%d o p d%d s ces l.\n" ed->soc_ded->pid)u l ep(10000);
} retrn 0 }int ha w thknl vo d{it pdont;st ct ndpoint*ed=NULs ruct nlsghdr l NU; or(idcont=0;pi dcount ! AX_P _CTpidcount++)
{
endoits+ idcount;
d>msg.mg_iovle =1 ;ed>msg.msgiv=mllo (sizef(strutio c )
d->mg.msg_iov->ov_base= mal o(NLMS G_SPAE MAX_PAYLOA));nlh ( truct nlms gdr*e-m .ms giov->iovas ;
lh->nlmsg_le =LMS_SPA(A_PAY LO AD);nl-nl mg_p =e d->pi;
lh->nlms g_flag 0;e->g.msg_ ov->iov_ln=nlh->nlmsg_ en;
snprintf( chr)NM S_T(lh ,MAX_PAYO AD 1, "ell kl! Thi i s%d!"ed->pid); printf " ning messaero. . .\n", d>pi ;se msg(ed>sok_f,&ed->ms,0); f (e>soc_d>mxfd)
{
xfde-sock_d;
} F D_SE T ed->oc_fd &set);// hrea_t i
f(0 =pthr ea dcrae(&ti,NLL &th aProc, (void*) )
// {
// rint("cre ate thrad % successfl or th d: %. n", id,ed->pid);
//
e urn ;
}in l e_user vid
nt picout; for(idcount 0;pidot =MAXP OUN; icont++)
{
o e(enpoits id nt]. ok_f);
} r etr
int o e et(struc ie it)
{int picunt;int selcount0;stru ed it*e d NL;
trut lmshdr*nlh =NLL; l cout=se ct(mxd+1,& t NLL,NLL, &wai ) if se count=0)
{ret n 0
}els if sel on
{printf("se eted error!\n");re urn-1
}e se
{for(p d u = ; pid nt !=
melbicom从2015年就开始运作了,在国内也是有一定的粉丝群,站长最早是从2017年开始介绍melbicom。上一次测评melbicom是在2018年,由于期间有不少人持续关注这个品牌,而且站长貌似也听说过路由什么的有变动的迹象。为此,今天重新对莫斯科数据中心的VPS进行一次简单测评,数据仅供参考。官方网站: https://melbicom.net比特币、信用卡、PayPal、支付宝、银联...
触摸云触摸云(cmzi.com),国人商家,有IDC/ISP正规资质,主营香港线路VPS、物理机等产品。本次为大家带上的是美国高防2区的套餐。去程普通线路,回程cn2 gia,均衡防御速度与防御,防御值为200G,无视UDP攻击,可选择性是否开启CC防御策略,超过峰值黑洞1-2小时。最低套餐20M起,多数套餐为50M,适合有防御型建站需求使用。美国高防2区 弹性云[大宽带]· 配置:1-16核· ...
提速啦(www.tisula.com)是赣州王成璟网络科技有限公司旗下云服务器品牌,目前拥有在籍员工40人左右,社保在籍员工30人+,是正规的国内拥有IDC ICP ISP CDN 云牌照资质商家,2018-2021年连续4年获得CTG机房顶级金牌代理商荣誉 2021年赣州市于都县创业大赛三等奖,2020年于都电子商务示范企业,2021年于都县电子商务融合推广大使。资源优势介绍:Ceranetwo...