-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathserver.go
208 lines (182 loc) · 3.92 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// +build linux
package main
import (
"fmt"
"net"
"sync"
"syscall"
)
func NewEpollM() *EpollM {
return &EpollM{
conn: make(map[int]*ServerConn),
}
}
type EpollM struct {
look sync.RWMutex
conn map[int]*ServerConn
socketFd int //监听socket的fd
epollFd int //epoll的fd
}
//关闭所有的链接
func (e *EpollM) Close() {
syscall.Close(e.socketFd)
syscall.Close(e.epollFd)
for _, con := range e.conn {
con.Close()
}
}
//获取一个链接
func (e *EpollM) GetConn(fd int) *ServerConn {
e.look.RLock()
defer e.look.RUnlock()
return e.conn[fd]
}
//添加一个链接
func (e *EpollM) AddConn(conn *ServerConn) {
e.look.Lock()
e.conn[conn.fd] = conn
e.look.Unlock()
}
//删除一个链接
func (e *EpollM) DelConn(fd int) {
e.look.Lock()
delete(e.conn, fd)
e.look.Unlock()
}
//开启监听
func (e *EpollM) Listen(ipAddr string, port int) error {
//使用系统调用,打开一个socket
fd, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_STREAM, syscall.IPPROTO_TCP)
if err != nil {
return err
}
//ip地址转换
var addr [4]byte
copy(addr[:], net.ParseIP(ipAddr).To4())
net.ParseIP(ipAddr).To4()
err = syscall.Bind(fd, &syscall.SockaddrInet4{
Port: port,
Addr: addr,
})
if err != nil {
return err
}
//开启监听
err = syscall.Listen(fd, 10)
if err != nil {
return err
}
e.socketFd = fd
return nil
}
//在死循环中等待client发来的链接
func (e *EpollM) Accept() error {
for {
nfd, _, err := syscall.Accept(e.socketFd)
if err != nil {
return err
}
err = e.EpollAddEvent(nfd)
if err != nil {
return nil
}
e.AddConn(&ServerConn{
fd: nfd,
})
}
}
//关闭指定的链接
func (e *EpollM) CloseConn(fd int) error {
conn := e.GetConn(fd)
if conn == nil {
return nil
}
if err := e.EpollRemoveEvent(fd); err != nil {
return err
}
conn.Close()
e.DelConn(fd)
return nil
}
//创建一个epoll
func (e *EpollM) CreateEpoll() error {
//通过系统调用,创建一个epoll
fd, err := syscall.EpollCreate(1)
if err != nil {
return err
}
e.epollFd = fd
return nil
}
//处理epoll
func (e *EpollM) HandlerEpoll() error {
events := make([]syscall.EpollEvent, 100)
//在死循环中处理epoll
for {
//msec -1,会一直阻塞,直到有事件可以处理才会返回, n 事件个数
n, err := syscall.EpollWait(e.epollFd, events, -1)
if err != nil {
return err
}
for i := 0; i < n; i++ {
//先在map中是否有这个链接
conn := e.GetConn(int(events[i].Fd))
if conn == nil { //没有这个链接,忽略
continue
}
if events[i].Events&syscall.EPOLLHUP == syscall.EPOLLHUP || events[i].Events&syscall.EPOLLERR == syscall.EPOLLERR {
//断开||出错
if err := e.CloseConn(int(events[i].Fd)); err != nil {
return err
}
} else if events[i].Events == syscall.EPOLLIN {
//可读事件
conn.Read()
}
}
}
}
//向epoll中加事件
func (e *EpollM) EpollAddEvent(fd int) error {
return syscall.EpollCtl(e.epollFd, syscall.EPOLL_CTL_ADD, fd, &syscall.EpollEvent{
Events: syscall.EPOLLIN,
Fd: int32(fd),
Pad: 0,
})
}
//从epoll中删除事件
func (e *EpollM) EpollRemoveEvent(fd int) error {
return syscall.EpollCtl(e.epollFd, syscall.EPOLL_CTL_DEL, fd, nil)
}
type ServerConn struct {
fd int
}
//读取数据
func (s *ServerConn) Read() {
data := make([]byte, 100)
//通过系统调用,读取数据,n是读到的长度
n, err := syscall.Read(s.fd, data)
if n == 0 {
return
}
if err != nil {
fmt.Printf("fd %d read error:%s\n", s.fd, err.Error())
} else {
fmt.Printf("%d say: %s \n", s.fd, data[:n])
s.Write([]byte(fmt.Sprintf("hello %d", s.fd)))
}
}
//向这个链接中写数据
func (s *ServerConn) Write(data []byte) {
_, err := syscall.Write(s.fd, data)
if err != nil {
fmt.Printf("fd %d write error:%s\n", s.fd, err.Error())
}
}
//关闭这个链接
func (s *ServerConn) Close() {
err := syscall.Close(s.fd)
if err != nil {
fmt.Printf("fd%d close error:%s\n", s.fd, err.Error())
}
}