基于 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 !=
官方网站:点击访问创梦网络宿迁BGP高防活动方案:机房CPU内存硬盘带宽IP防护流量原价活动价开通方式宿迁BGP4vCPU4G40G+50G20Mbps1个100G不限流量299元/月 209.3元/月点击自助购买成都电信优化线路8vCPU8G40G+50G20Mbps1个100G不限流量399元/月 279.3元/月点击自助购买成都电信优化线路8vCPU16G40G+50G2...
感恩一年有你!免费领取2核4G套餐!2核4G轻量应用服务器2核 CPU 4GB内存 60G SSD云硬盘 6Mbps带宽领取地址:https://cloud.tencent.com/act/pro/lighthousethankyou活动规则活动时间2021年9月23日 ~ 2021年10月23日活动对象腾讯云官网已注册且完成实名认证的国内站用户(协作者与子用户账号除外),且符合以下活动条件:账号...
Sharktech(鲨鱼服务器商)我们还是比较懂的,有提供独立服务器和高防服务器,而且性价比都还算是不错,而且我们看到有一些主机商的服务器也是走这个商家渠道分销的。这不看到鲨鱼服务器商家洛杉矶独立服务器纷纷促销,不限制流量的独立服务器起步99美元,这个还未曾有过。第一、鲨鱼机房服务器方案洛杉矶机房,默认1Gbps带宽,不限流量,自带5个IPv4,免费60Gbps / 48Mpps DDoS防御。C...