基于 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 !=
无忧云怎么样?无忧云服务器好不好?无忧云值不值得购买?无忧云,无忧云是一家成立于2017年的老牌商家旗下的服务器销售品牌,现由深圳市云上无忧网络科技有限公司运营,是正规持证IDC/ISP/IRCS商家,自营有国内雅安高防、洛阳BGP企业线路、香港CN2线路、国外服务器产品等,非常适合需要稳定的线路的用户,如游戏、企业建站业务需求和各种负载较高的项目,同时还有自营的高性能、高配置的BGP线路高防物理...
萤光云怎么样?萤光云是一家国人云厂商,总部位于福建福州。其成立于2002年,主打高防云服务器产品,主要提供福州、北京、上海BGP和香港CN2节点。萤光云的高防云服务器自带50G防御,适合高防建站、游戏高防等业务。目前萤光云推出北京云服务器优惠活动,机房为北京BGP机房,购买北京云服务器可享受6.5折优惠+51元代金券(折扣和代金券可叠加使用)。活动期间还支持申请免费试用,需提交工单开通免费试用体验...
华纳云(HNCloud Limited)是一家专业的全球数据中心基础服务提供商,总部在香港,隶属于香港联合通讯国际有限公司,拥有香港政府颁发的商业登记证明,保证用户的安全性和合规性。 华纳云是APNIC 和 ARIN 会员单位。主要提供数据中心基础服务、互联网业务解决方案, 以及香港服务器租用、香港服务器托管、香港云服务器、美国云服务器,云计算、云安全技术研发等产品和服务。其中云服务器基于成熟的 ...