基于 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 !=
paypal贝宝可撸$10的代金券!这两天paypal出了活动,本次并没有其他的限制,只要注册国区的paypal,使用国内的手机号和62开头的银联卡,就可以获得10美元的代金券,这个代金券购买产品需要大于10.1美元,站长给大家推荐几个方式,可以白嫖一年的VPS,有需要的朋友可以看看比较简单。PayPal送10美元活动:点击直达活动sfz与绑定卡的号码可以重复用 注册的邮箱,手机号与绑的银联卡必须...
昨天有在"盘点2021年主流云服务器商家618年中大促活动"文章中整理到当前年中大促618活动期间的一些国内国外的云服务商的促销活动,相对来说每年年中和年末的活动力度还是蛮大的,唯独就是活动太过于密集,而且商家比较多,导致我们很多新人不懂如何选择,当然对于我们这些老油条还是会选择的,估计没有比我们更聪明的进行薅爆款新人活动。有网友提到,是否可以整理一篇当前的这些活动商家中的促销产品。哪些商家哪款产...
Megalayer 商家主营业务是以独立服务器和站群服务器的,后来也陆续的有新增香港、菲律宾数据中心的VPS主机产品。由于其线路的丰富,还是深受一些用户喜欢的,有CN2优化直连线路,有全向国际线路,以及针对欧美的国际线路。这次有看到商家也有新增美国机房的VPS主机,也有包括15M带宽CN2优化带宽以及30M带宽的全向线路。Megalayer 商家提供的美国机房VPS产品,提供的配置方案也是比较多,...