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
e7d1b729
Commit
e7d1b729
authored
Mar 10, 2018
by
AlexStocks
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add updateWriteDeadline
parent
d708dcdd
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
30 deletions
+29
-30
change_log.md
change_log.md
+3
-2
conn.go
conn.go
+26
-28
No files found.
change_log.md
View file @
e7d1b729
...
...
@@ -15,8 +15,9 @@
> improvement
*
1 rDeadline -> rTimeout
*
2 wDeadline -> wTimeout
*
3 disable readtimeout in func (w
*
gettyWSConn) read()
*
3 disable readtimeout in gettyWSConn:read
*
4 add gettyWSConn:updateWriteDeadline
-
2018/03/08
> feature
*
1 add udp client and udp server
...
...
conn.go
View file @
e7d1b729
...
...
@@ -375,24 +375,7 @@ func (w *gettyWSConn) SetCompressType(c CompressType) {
}
func
(
w
*
gettyWSConn
)
handlePing
(
message
string
)
error
{
var
(
err
error
currentTime
time
.
Time
)
if
w
.
wTimeout
>
0
{
// Optimization: update write deadline only if more than 25%
// of the last write deadline exceeded.
// See https://github.com/golang/go/issues/15133 for details.
currentTime
=
wheel
.
Now
()
if
currentTime
.
Sub
(
w
.
wLastDeadline
)
>
(
w
.
wTimeout
>>
2
)
{
if
err
=
w
.
conn
.
SetWriteDeadline
(
currentTime
.
Add
(
w
.
wTimeout
));
err
!=
nil
{
return
err
}
w
.
wLastDeadline
=
currentTime
}
}
err
=
w
.
conn
.
WriteMessage
(
websocket
.
PongMessage
,
[]
byte
(
message
))
err
:=
w
.
writePong
([]
byte
(
message
))
if
err
==
websocket
.
ErrCloseSent
{
err
=
nil
}
else
if
e
,
ok
:=
err
.
(
net
.
Error
);
ok
&&
e
.
Temporary
()
{
...
...
@@ -426,18 +409,12 @@ func (w *gettyWSConn) read() ([]byte, error) {
return
b
,
e
}
// websocket connection write
func
(
w
*
gettyWSConn
)
Write
(
pkg
interface
{})
(
int
,
error
)
{
func
(
w
*
gettyWSConn
)
updateWriteDeadline
()
error
{
var
(
err
error
currentTime
time
.
Time
ok
bool
p
[]
byte
)
if
p
,
ok
=
pkg
.
([]
byte
);
!
ok
{
return
0
,
fmt
.
Errorf
(
"illegal @pkg{%#v} type"
,
pkg
)
}
if
w
.
wTimeout
>
0
{
// Optimization: update write deadline only if more than 25%
// of the last write deadline exceeded.
...
...
@@ -445,24 +422,45 @@ func (w *gettyWSConn) Write(pkg interface{}) (int, error) {
currentTime
=
wheel
.
Now
()
if
currentTime
.
Sub
(
w
.
wLastDeadline
)
>
(
w
.
wTimeout
>>
2
)
{
if
err
=
w
.
conn
.
SetWriteDeadline
(
currentTime
.
Add
(
w
.
wTimeout
));
err
!=
nil
{
return
0
,
err
return
err
}
w
.
wLastDeadline
=
currentTime
}
}
return
nil
}
// websocket connection write
func
(
w
*
gettyWSConn
)
Write
(
pkg
interface
{})
(
int
,
error
)
{
var
(
ok
bool
p
[]
byte
)
if
p
,
ok
=
pkg
.
([]
byte
);
!
ok
{
return
0
,
fmt
.
Errorf
(
"illegal @pkg{%#v} type"
,
pkg
)
}
// atomic.AddUint32(&w.writeCount, 1)
atomic
.
AddUint32
(
&
w
.
writeCount
,
(
uint32
)(
len
(
p
)))
// w.conn.SetWriteTimeout(time.Now().Add(w.wTimeout)
)
w
.
updateWriteDeadline
(
)
return
len
(
p
),
w
.
conn
.
WriteMessage
(
websocket
.
BinaryMessage
,
p
)
}
func
(
w
*
gettyWSConn
)
writePing
()
error
{
w
.
updateWriteDeadline
()
return
w
.
conn
.
WriteMessage
(
websocket
.
PingMessage
,
[]
byte
{})
}
func
(
w
*
gettyWSConn
)
writePong
(
message
[]
byte
)
error
{
w
.
updateWriteDeadline
()
return
w
.
conn
.
WriteMessage
(
websocket
.
PongMessage
,
message
)
}
// close websocket connection
func
(
w
*
gettyWSConn
)
close
(
waitSec
int
)
{
w
.
updateWriteDeadline
()
w
.
conn
.
WriteMessage
(
websocket
.
CloseMessage
,
[]
byte
(
"bye-bye!!!"
))
conn
:=
w
.
conn
.
UnderlyingConn
()
if
tcpConn
,
ok
:=
conn
.
(
*
net
.
TCPConn
);
ok
{
...
...
@@ -503,7 +501,7 @@ func setUDPSocketOptions(conn *net.UDPConn) error {
// create gettyUDPConn
func
newGettyUDPConn
(
conn
*
net
.
UDPConn
,
peerUDPAddr
*
net
.
UDPAddr
)
*
gettyUDPConn
{
if
conn
==
nil
{
panic
(
"newGetty
WS
Conn(conn):@conn is nil"
)
panic
(
"newGetty
UDP
Conn(conn):@conn is nil"
)
}
var
localAddr
,
peerAddr
string
...
...
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