基于 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 !=
ZJI原名维翔主机,是原来Wordpress圈知名主机商家,成立于2011年,2018年9月更名为ZJI,提供香港、日本、美国独立服务器(自营/数据中心直营)租用及VDS、虚拟主机空间、域名注册业务。ZJI今年全新上架了台湾CN2线路服务器,本月针对香港高主频服务器和台湾CN2服务器提供7折优惠码,其他机房及产品提供8折优惠码,优惠后台湾CN2线路E5服务器月付595元起。台湾一型CPU:Inte...
从介绍看啊,新增的HostYun 俄罗斯机房采用的是双向CN2线路,其他的像香港和日本机房,均为国内直连线路,访问质量不错。HostYun商家通用九折优惠码:HostYun内存CPUSSD流量带宽价格(原价)购买地址1G1核10G300G/月200M28元/月购买链接1G1核10G500G/月200M38元/月购买链接1G1核20G900G/月200M68元/月购买链接2G1核30G1500G/月...
特网云为您提供高速、稳定、安全、弹性的云计算服务计算、存储、监控、安全,完善的云产品满足您的一切所需,深耕云计算领域10余年;我们拥有前沿的核心技术,始终致力于为政府机构、企业组织和个人开发者提供稳定、安全、可靠、高性价比的云计算产品与服务。公司名:珠海市特网科技有限公司官方网站:https://www.56dr.com特网云为您提供高速、稳定、安全、弹性的云计算服务 计算、存储、监控、安全,完善...