TChannel和TChanSwitch结构体
此文件内的函数基本上分成两类。一类的输入参数中包含TChannel结构体变量,另一类的输入参数中包含另一个结构体变量TChanSwitch。初看后觉得二者的差别很小,细看后发现差异在vtbl属性。vtbl这个结构体属性有很多函数指针属性。类似于监听函数(listen)、接受函数(accept)、读函数(read)和写函数(write)等等。TChannel包含读写等函数指针。TChanSwitch包含监听和接受函数指针。因此可以断定TChannel是用来代表一个连接上服务端的socket连接,TChanSwitch用来代表一个服务端socket。即,本文档内的函数分成两类:操作连接上服务端socket的函数,和操作服务端socket的函数。
struct TChannelVtbl {
ChannelDestroyImpl * destroy;
ChannelWriteImpl * write;
ChannelReadImpl * read;
ChannelWaitImpl * wait;
ChannelInterruptImpl * interrupt;
ChannelFormatPeerInfoImpl * formatPeerInfo;
};
struct _TChannel {
uint signature;
/* With both background and foreground use of sockets, and
background being both fork and pthread, it is very easy to
screw up socket lifetime and try to destroy twice. We use
this signature to help catch such bugs.
*/
void * implP;
struct TChannelVtbl vtbl;
};
struct TChanSwitchVtbl {
SwitchDestroyImpl * destroy;
SwitchListenImpl * listen;
SwitchAcceptImpl * accept;
SwitchInterruptImpl * interrupt;
};
struct _TChanSwitch {
uint signature;
/* With both background and foreground use of switches, and
background being both fork and pthread, it is very easy to
screw up switch lifetime and try to destroy twice. We use
this signature to help catch such bugs.
*/
void * implP;
struct TChanSwitchVtbl vtbl;
};
TChannel
channelVtbl是个静态变量,保存了本文档内所有操作TChannel结构体的函数指针。
static struct TChannelVtbl const ch