Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
G
getty
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wei.xuan
getty
Commits
e3f213ef
Commit
e3f213ef
authored
Mar 09, 2018
by
AlexStocks
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add udp server close
parent
55a91868
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
36 additions
and
25 deletions
+36
-25
change_log.md
change_log.md
+2
-0
server.go
server.go
+34
-25
No files found.
change_log.md
View file @
e3f213ef
...
...
@@ -15,6 +15,8 @@
> feature
*
1 add udp client and udp server
> version: 0.8.01
-
2017/05/02
> feature
*
1 set read/write deadline for every read/write action refers to fasthttp
...
...
server.go
View file @
e3f213ef
...
...
@@ -43,12 +43,12 @@ const (
type
Server
struct
{
// net
addr
string
udpConn
*
net
.
UDPConn
// for udp server
l
istener
net
.
Listener
lock
sync
.
Mutex
// for server
typ
int
server
*
http
.
Server
// for ws or wss server
addr
string
pktListener
net
.
PacketConn
streamL
istener
net
.
Listener
lock
sync
.
Mutex
// for server
typ
int
server
*
http
.
Server
// for ws or wss server
// websocket
path
string
...
...
@@ -116,6 +116,7 @@ func (s *Server) stop() {
err
error
ctx
context
.
Context
)
select
{
case
<-
s
.
done
:
return
...
...
@@ -131,10 +132,18 @@ func (s *Server) stop() {
log
.
Error
(
"server shutdown ctx:%#v"
,
err
)
}
}
s
.
server
=
nil
s
.
lock
.
Unlock
()
// 把listener.Close放在这里,既能防止多次关闭调用,
// 又能及时让Server因accept返回错误而从RunEventloop退出
s
.
listener
.
Close
()
if
s
.
streamListener
!=
nil
{
// 把streamListener.Close放在这里,既能防止多次关闭调用,
// 又能及时让Server因accept返回错误而从RunEventloop退出
s
.
streamListener
.
Close
()
s
.
streamListener
=
nil
}
if
s
.
pktListener
==
nil
{
s
.
pktListener
.
Close
()
s
.
pktListener
=
nil
}
})
}
}
...
...
@@ -153,40 +162,40 @@ func (s *Server) IsClosed() bool {
// net.ipv4.tcp_tw_recycle
func
(
s
*
Server
)
listenTCP
()
error
{
var
(
err
error
l
istener
net
.
Listener
err
error
streamL
istener
net
.
Listener
)
l
istener
,
err
=
net
.
Listen
(
"tcp"
,
s
.
addr
)
streamL
istener
,
err
=
net
.
Listen
(
"tcp"
,
s
.
addr
)
if
err
!=
nil
{
return
errors
.
Wrapf
(
err
,
"net.Listen(tcp, addr:%s))"
,
s
.
addr
)
}
s
.
listener
=
l
istener
s
.
streamListener
=
streamL
istener
return
nil
}
func
(
s
*
Server
)
listenUDP
()
error
{
var
(
err
error
localAddr
*
net
.
UDPAddr
udpConn
*
net
.
UDPConn
err
error
localAddr
*
net
.
UDPAddr
pktListener
*
net
.
UDPConn
)
localAddr
,
err
=
net
.
ResolveUDPAddr
(
"udp"
,
s
.
addr
)
if
err
!=
nil
{
return
errors
.
Wrapf
(
err
,
"net.ResolveUDPAddr(udp, addr:%s)"
,
s
.
addr
)
}
udpConn
,
err
=
net
.
ListenUDP
(
"udp"
,
localAddr
)
pktListener
,
err
=
net
.
ListenUDP
(
"udp"
,
localAddr
)
if
err
!=
nil
{
return
errors
.
Wrapf
(
err
,
"net.ListenUDP((udp, localAddr:%#v)"
,
localAddr
)
}
if
err
=
setUDPSocketOptions
(
udpConn
);
err
!=
nil
{
return
errors
.
Wrapf
(
err
,
"setUDPSocketOptions(
udpConn:%#v)"
,
udpConn
)
if
err
=
setUDPSocketOptions
(
pktListener
);
err
!=
nil
{
return
errors
.
Wrapf
(
err
,
"setUDPSocketOptions(
pktListener:%#v)"
,
pktListener
)
}
s
.
udpConn
=
udpConn
s
.
pktListener
=
pktListener
return
nil
}
...
...
@@ -204,7 +213,7 @@ func (s *Server) listen() error {
}
func
(
s
*
Server
)
accept
(
newSession
NewSessionCallback
)
(
Session
,
error
)
{
conn
,
err
:=
s
.
l
istener
.
Accept
()
conn
,
err
:=
s
.
streamL
istener
.
Accept
()
if
err
!=
nil
{
return
nil
,
err
}
...
...
@@ -268,7 +277,7 @@ func (s *Server) runUDPEventloop(newSession NewSessionCallback) {
ss
Session
)
ss
=
NewUDPSession
(
s
.
udpConn
,
nil
)
ss
=
NewUDPSession
(
s
.
pktListener
.
(
*
net
.
UDPConn
)
,
nil
)
if
err
:=
newSession
(
ss
);
err
!=
nil
{
panic
(
err
.
Error
())
}
...
...
@@ -355,7 +364,7 @@ func (s *Server) runWSEventLoop(newSession NewSessionCallback) {
s
.
lock
.
Lock
()
s
.
server
=
server
s
.
lock
.
Unlock
()
err
=
server
.
Serve
(
s
.
l
istener
)
err
=
server
.
Serve
(
s
.
streamL
istener
)
if
err
!=
nil
{
log
.
Error
(
"http.Server.Serve(addr{%s}) = err{%#v}"
,
s
.
addr
,
err
)
// panic(err)
...
...
@@ -421,7 +430,7 @@ func (s *Server) runWSSEventLoop(newSession NewSessionCallback) {
s
.
lock
.
Lock
()
s
.
server
=
server
s
.
lock
.
Unlock
()
err
=
server
.
Serve
(
tls
.
NewListener
(
s
.
l
istener
,
config
))
err
=
server
.
Serve
(
tls
.
NewListener
(
s
.
streamL
istener
,
config
))
if
err
!=
nil
{
log
.
Error
(
"http.Server.Serve(addr{%s}) = err{%#v}"
,
s
.
addr
,
err
)
panic
(
err
)
...
...
@@ -449,7 +458,7 @@ func (s *Server) RunEventloop(newSession NewSessionCallback) {
}
func
(
s
*
Server
)
Listener
()
net
.
Listener
{
return
s
.
l
istener
return
s
.
streamL
istener
}
func
(
s
*
Server
)
Close
()
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment