owl icon indicating copy to clipboard operation
owl copied to clipboard

讨论一下config_reader.h文件的作用和TCP粘包的处理在哪个文件中

Open Arewethere opened this issue 4 years ago • 4 comments

Arewethere avatar Apr 09 '20 12:04 Arewethere

msg_head.h中的commu_head的length记录了要发送的数据包的大小,通过send_data将要发送的数据大小写入head.length, 当可读事件发生,数据从内核缓冲区读入内存池,此时读出包头的head.length,将包头大小加上这个length的内存大小数据从内存池中清除,即将这个数据包从内存池中取出.将数据包数据大小传给客端注册的回调函数,执行回调函数.

xunshuidezhu avatar Apr 09 '20 13:04 xunshuidezhu

config_reader是一个全局的单例

xunshuidezhu avatar Apr 09 '20 13:04 xunshuidezhu

谢谢回答,能大概讲一下msg_dispatcher的作用吗^O^

Arewethere avatar Apr 09 '20 14:04 Arewethere

msg_dispatcher是一个分发器,客端调用add_msg_cb,注册回调函数,这个回调函数执行的时机在主线程将连接的活跃socketfd分发给线程池中的某个工作线程,这个工作线程被eventfd唤醒,开始将内核缓冲区数据读到用户态缓冲内存池,此时注册的回调函数开始执行,以一个pingpong程序为例,此时服务端的回调函数会将用户态缓冲区数据(从客户端读到)发送到客户端(依然是拷贝到内核缓冲区然后拷贝到内核协议栈,从网卡缓冲区发送到网路中).add_msg_cb除了注册将来要发生的回调函数之外,还注册了一个id,这个id与回调函数作为hashmap的k和v,当用户态缓冲区有数据之后,根据这个id来执行相应的回调函数,这种写法可以避免大量if/else来判断状态机

xunshuidezhu avatar Apr 09 '20 14:04 xunshuidezhu