11# netpoll
22
3+ socket,connect,listen,getsocketopt 都有一个全局函数变量来表示。
4+
35hook_unix.go :
46
57``` go
@@ -30,11 +32,38 @@ func installTestHooks() {
3032用这种全局函数 hook,或者叫注册表的方式,可以实现类似于面向对象中的 interface 功能。不过因为不同平台提供的网络编程函数差别有些大,所以这里这些全局网络函数也就只是用来方便测试。
3133
3234``` go
35+ // Integrated network poller (platform-independent part).
36+ // A particular implementation (epoll/kqueue) must define the following functions:
37+ // func netpollinit() // to initialize the poller
38+ // func netpollopen(fd uintptr, pd *pollDesc) int32 // to arm edge-triggered notifications
39+ // and associate fd with pd.
40+ // An implementation must call the following function to denote that the pd is ready.
41+ // func netpollready(gpp **g, pd *pollDesc, mode int32)
42+
43+ // pollDesc contains 2 binary semaphores, rg and wg, to park reader and writer
44+ // goroutines respectively. The semaphore can be in the following states:
45+ // pdReady - io readiness notification is pending;
46+ // a goroutine consumes the notification by changing the state to nil.
47+ // pdWait - a goroutine prepares to park on the semaphore, but not yet parked;
48+ // the goroutine commits to park by changing the state to G pointer,
49+ // or, alternatively, concurrent io notification changes the state to READY,
50+ // or, alternatively, concurrent timeout/close changes the state to nil.
51+ // G pointer - the goroutine is blocked on the semaphore;
52+ // io notification or timeout/close changes the state to READY or nil respectively
53+ // and unparks the goroutine.
54+ // nil - nothing of the above.
55+ const (
56+ pdReady uintptr = 1
57+ pdWait uintptr = 2
58+ )
59+
60+ const pollBlockSize = 4 * 1024
61+
3362// Network file descriptor.
3463type netFD struct {
3564 pfd poll.FD
3665
37- // immutable until Close
66+ // 下面这些元素在 Close 之前都是不可变的
3867 family int
3968 sotype int
4069 isConnected bool
@@ -43,13 +72,13 @@ type netFD struct {
4372 raddr Addr
4473}
4574
46- // FD is a file descriptor. The net and os packages use this type as a
47- // field of a larger type representing a network connection or OS file.
75+ // FD 是对 file descriptor 的一个包装,内部的 Sysfd 就是 linux 下的
76+ // file descriptor。net 和 os 包中使用这个类型来代表一个网络连接或者一个 OS 文件
4877type FD struct {
49- // Lock sysfd and serialize access to Read and Write methods.
78+ // 对 sysfd 加锁,以使 Read 和 Write 方法串行执行
5079 fdmu fdMutex
5180
52- // System file descriptor. Immutable until Close.
81+ // 操作系统的 file descriptor。在关闭之前是不可变的
5382 Sysfd int
5483
5584 // I/O poller.
@@ -61,18 +90,18 @@ type FD struct {
6190 // Semaphore signaled when file is closed.
6291 csema uint32
6392
64- // Whether this is a streaming descriptor, as opposed to a
65- // packet-based descriptor like a UDP socket. Immutable.
93+ // 不可变。表示当前这个 fd 是否是一个流,或者是一个基于包的 fd
94+ // 用来区分是 TCP 还是 UDP
6695 IsStream bool
6796
6897 // Whether a zero byte read indicates EOF. This is false for a
6998 // message based socket connection.
7099 ZeroReadIsEOF bool
71100
72- // Whether this is a file rather than a network socket.
101+ // 这个 fd 表示的是不是一个文件,如果 false 的话就是一个 network socket
73102 isFile bool
74103
75- // Whether this file has been set to blocking mode.
104+ // 这个文件是否被设置为了 blocking 模式
76105 isBlocking bool
77106}
78107
0 commit comments