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
69a7b1ba
Commit
69a7b1ba
authored
May 21, 2019
by
u0x01
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Mod: pkg/errors
parent
811b3d34
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
89 additions
and
86 deletions
+89
-86
client.go
client.go
+11
-11
conn.go
conn.go
+23
-23
go.mod
go.mod
+2
-1
go.sum
go.sum
+2
-0
options.go
options.go
+3
-3
server.go
server.go
+17
-17
session.go
session.go
+31
-31
No files found.
client.go
View file @
69a7b1ba
...
...
@@ -25,7 +25,7 @@ import (
"github.com/AlexStocks/goext/net"
log
"github.com/dubbogo/log4go"
"github.com/gorilla/websocket"
jerrors
"github.com/juju
/errors"
"github.com/pkg
/errors"
)
const
(
...
...
@@ -139,7 +139,7 @@ func (c *client) dialTCP() Session {
return
newTCPSession
(
conn
,
c
)
}
log
.
Info
(
"net.DialTimeout(addr:%s, timeout:%v) = error
{%s}"
,
c
.
addr
,
jerrors
.
ErrorStack
(
err
)
)
log
.
Info
(
"net.DialTimeout(addr:%s, timeout:%v) = error
:%+v"
,
c
.
addr
,
err
)
// time.Sleep(connInterval)
<-
wheel
.
After
(
connInterval
)
}
...
...
@@ -168,7 +168,7 @@ func (c *client) dialUDP() Session {
err
=
errSelfConnect
}
if
err
!=
nil
{
log
.
Warn
(
"net.DialTimeout(addr:%s, timeout:%v) = error
{%s}"
,
c
.
addr
,
jerrors
.
ErrorStack
(
err
)
)
log
.
Warn
(
"net.DialTimeout(addr:%s, timeout:%v) = error
:%+v"
,
c
.
addr
,
err
)
// time.Sleep(connInterval)
<-
wheel
.
After
(
connInterval
)
continue
...
...
@@ -178,18 +178,18 @@ func (c *client) dialUDP() Session {
conn
.
SetWriteDeadline
(
time
.
Now
()
.
Add
(
1e9
))
if
length
,
err
=
conn
.
Write
(
connectPingPackage
[
:
]);
err
!=
nil
{
conn
.
Close
()
log
.
Warn
(
"conn.Write(%s) = {length:%d, err:%
s}"
,
string
(
connectPingPackage
),
length
,
jerrors
.
ErrorStack
(
err
)
)
log
.
Warn
(
"conn.Write(%s) = {length:%d, err:%
+v}"
,
string
(
connectPingPackage
),
length
,
err
)
// time.Sleep(connInterval)
<-
wheel
.
After
(
connInterval
)
continue
}
conn
.
SetReadDeadline
(
time
.
Now
()
.
Add
(
1e9
))
length
,
err
=
conn
.
Read
(
buf
)
if
netErr
,
ok
:=
j
errors
.
Cause
(
err
)
.
(
net
.
Error
);
ok
&&
netErr
.
Timeout
()
{
if
netErr
,
ok
:=
errors
.
Cause
(
err
)
.
(
net
.
Error
);
ok
&&
netErr
.
Timeout
()
{
err
=
nil
}
if
err
!=
nil
{
log
.
Info
(
"conn{%#v}.Read() = {length:%d, err:%
s}"
,
conn
,
length
,
jerrors
.
ErrorStack
(
err
)
)
log
.
Info
(
"conn{%#v}.Read() = {length:%d, err:%
+v}"
,
conn
,
length
,
err
)
conn
.
Close
()
// time.Sleep(connInterval)
<-
wheel
.
After
(
connInterval
)
...
...
@@ -215,7 +215,7 @@ func (c *client) dialWS() Session {
return
nil
}
conn
,
_
,
err
=
dialer
.
Dial
(
c
.
addr
,
nil
)
log
.
Info
(
"websocket.dialer.Dial(addr:%s) = error:%
s"
,
c
.
addr
,
jerrors
.
ErrorStack
(
err
)
)
log
.
Info
(
"websocket.dialer.Dial(addr:%s) = error:%
+v"
,
c
.
addr
,
err
)
if
err
==
nil
&&
gxnet
.
IsSameAddr
(
conn
.
RemoteAddr
(),
conn
.
LocalAddr
())
{
conn
.
Close
()
err
=
errSelfConnect
...
...
@@ -229,7 +229,7 @@ func (c *client) dialWS() Session {
return
ss
}
log
.
Info
(
"websocket.dialer.Dial(addr:%s) = error:%
s"
,
c
.
addr
,
jerrors
.
ErrorStack
(
err
)
)
log
.
Info
(
"websocket.dialer.Dial(addr:%s) = error:%
+v"
,
c
.
addr
,
err
)
// time.Sleep(connInterval)
<-
wheel
.
After
(
connInterval
)
}
...
...
@@ -256,7 +256,7 @@ func (c *client) dialWSS() Session {
if
c
.
cert
!=
""
{
certPEMBlock
,
err
:=
ioutil
.
ReadFile
(
c
.
cert
)
if
err
!=
nil
{
panic
(
fmt
.
Sprintf
(
"ioutil.ReadFile(cert:%s) = error
{%s}"
,
c
.
cert
,
jerrors
.
ErrorStack
(
err
)
))
panic
(
fmt
.
Sprintf
(
"ioutil.ReadFile(cert:%s) = error
:%+v"
,
c
.
cert
,
err
))
}
var
cert
tls
.
Certificate
...
...
@@ -278,7 +278,7 @@ func (c *client) dialWSS() Session {
for
_
,
c
:=
range
config
.
Certificates
{
roots
,
err
=
x509
.
ParseCertificates
(
c
.
Certificate
[
len
(
c
.
Certificate
)
-
1
])
if
err
!=
nil
{
panic
(
fmt
.
Sprintf
(
"error parsing server's root cert: %
s
\n
"
,
jerrors
.
ErrorStack
(
err
)
))
panic
(
fmt
.
Sprintf
(
"error parsing server's root cert: %
+v
\n
"
,
err
))
}
for
_
,
root
=
range
roots
{
certPool
.
AddCert
(
root
)
...
...
@@ -308,7 +308,7 @@ func (c *client) dialWSS() Session {
return
ss
}
log
.
Info
(
"websocket.dialer.Dial(addr:%s) = error
{%s}"
,
c
.
addr
,
jerrors
.
ErrorStack
(
err
)
)
log
.
Info
(
"websocket.dialer.Dial(addr:%s) = error
:%+v"
,
c
.
addr
,
err
)
// time.Sleep(connInterval)
<-
wheel
.
After
(
connInterval
)
}
...
...
conn.go
View file @
69a7b1ba
...
...
@@ -24,7 +24,7 @@ import (
log
"github.com/dubbogo/log4go"
"github.com/golang/snappy"
"github.com/gorilla/websocket"
jerrors
"github.com/juju
/errors"
"github.com/pkg
/errors"
"golang.org/x/net/ipv4"
"golang.org/x/net/ipv6"
)
...
...
@@ -193,10 +193,10 @@ func (t *writeFlusher) Write(p []byte) (int, error) {
defer
t
.
lock
.
Unlock
()
n
,
err
=
t
.
flusher
.
Write
(
p
)
if
err
!=
nil
{
return
n
,
jerrors
.
Trace
(
err
)
return
n
,
errors
.
WithStack
(
err
)
}
if
err
:=
t
.
flusher
.
Flush
();
err
!=
nil
{
return
0
,
jerrors
.
Trace
(
err
)
return
0
,
errors
.
WithStack
(
err
)
}
return
n
,
nil
...
...
@@ -243,7 +243,7 @@ func (t *gettyTCPConn) read(p []byte) (int, error) {
currentTime
=
time
.
Now
()
if
currentTime
.
Sub
(
t
.
rLastDeadline
)
>
(
t
.
rTimeout
>>
2
)
{
if
err
=
t
.
conn
.
SetReadDeadline
(
currentTime
.
Add
(
t
.
rTimeout
));
err
!=
nil
{
return
0
,
jerrors
.
Trace
(
err
)
return
0
,
errors
.
WithStack
(
err
)
}
t
.
rLastDeadline
=
currentTime
}
...
...
@@ -252,7 +252,7 @@ func (t *gettyTCPConn) read(p []byte) (int, error) {
length
,
err
=
t
.
reader
.
Read
(
p
)
log
.
Debug
(
"now:%s, length:%d, err:%s"
,
currentTime
,
length
,
err
)
atomic
.
AddUint32
(
&
t
.
readBytes
,
uint32
(
length
))
return
length
,
jerrors
.
Trace
(
err
)
return
length
,
errors
.
WithStack
(
err
)
//return length, err
}
...
...
@@ -267,7 +267,7 @@ func (t *gettyTCPConn) Write(pkg interface{}) (int, error) {
)
if
p
,
ok
=
pkg
.
([]
byte
);
!
ok
{
return
0
,
j
errors
.
Errorf
(
"illegal @pkg{%#v} type"
,
pkg
)
return
0
,
errors
.
Errorf
(
"illegal @pkg{%#v} type"
,
pkg
)
}
if
t
.
compress
==
CompressNone
&&
t
.
wTimeout
>
0
{
// Optimization: update write deadline only if more than 25%
...
...
@@ -276,7 +276,7 @@ func (t *gettyTCPConn) Write(pkg interface{}) (int, error) {
currentTime
=
time
.
Now
()
if
currentTime
.
Sub
(
t
.
wLastDeadline
)
>
(
t
.
wTimeout
>>
2
)
{
if
err
=
t
.
conn
.
SetWriteDeadline
(
currentTime
.
Add
(
t
.
wTimeout
));
err
!=
nil
{
return
0
,
jerrors
.
Trace
(
err
)
return
0
,
errors
.
WithStack
(
err
)
}
t
.
wLastDeadline
=
currentTime
}
...
...
@@ -286,7 +286,7 @@ func (t *gettyTCPConn) Write(pkg interface{}) (int, error) {
atomic
.
AddUint32
(
&
t
.
writeBytes
,
(
uint32
)(
len
(
p
)))
}
log
.
Debug
(
"now:%s, length:%d, err:%s"
,
currentTime
,
length
,
err
)
return
length
,
jerrors
.
Trace
(
err
)
return
length
,
errors
.
WithStack
(
err
)
//return length, err
}
...
...
@@ -299,7 +299,7 @@ func (t *gettyTCPConn) close(waitSec int) {
if
t
.
conn
!=
nil
{
if
writer
,
ok
:=
t
.
writer
.
(
*
snappy
.
Writer
);
ok
{
if
err
:=
writer
.
Close
();
err
!=
nil
{
log
.
Error
(
"snappy.Writer.Close() = error
{%s}"
,
jerrors
.
ErrorStack
(
err
)
)
log
.
Error
(
"snappy.Writer.Close() = error
:%+v"
,
err
)
}
}
t
.
conn
.
(
*
net
.
TCPConn
)
.
SetLinger
(
waitSec
)
...
...
@@ -333,7 +333,7 @@ func setUDPSocketOptions(conn *net.UDPConn) error {
err6
:=
ipv6
.
NewPacketConn
(
conn
)
.
SetControlMessage
(
ipv6
.
FlagDst
|
ipv6
.
FlagInterface
,
true
)
err4
:=
ipv4
.
NewPacketConn
(
conn
)
.
SetControlMessage
(
ipv4
.
FlagDst
|
ipv4
.
FlagInterface
,
true
)
if
err6
!=
nil
&&
err4
!=
nil
{
return
jerrors
.
Trace
(
err4
)
return
errors
.
WithStack
(
err4
)
}
return
nil
}
...
...
@@ -393,7 +393,7 @@ func (u *gettyUDPConn) read(p []byte) (int, *net.UDPAddr, error) {
currentTime
=
time
.
Now
()
if
currentTime
.
Sub
(
u
.
rLastDeadline
)
>
(
u
.
rTimeout
>>
2
)
{
if
err
=
u
.
conn
.
SetReadDeadline
(
currentTime
.
Add
(
u
.
rTimeout
));
err
!=
nil
{
return
0
,
nil
,
jerrors
.
Trace
(
err
)
return
0
,
nil
,
errors
.
WithStack
(
err
)
}
u
.
rLastDeadline
=
currentTime
}
...
...
@@ -406,7 +406,7 @@ func (u *gettyUDPConn) read(p []byte) (int, *net.UDPAddr, error) {
}
//return length, addr, err
return
length
,
addr
,
jerrors
.
Trace
(
err
)
return
length
,
addr
,
errors
.
WithStack
(
err
)
}
// write udp packet, @ctx should be of type UDPContext
...
...
@@ -422,10 +422,10 @@ func (u *gettyUDPConn) Write(udpCtx interface{}) (int, error) {
)
if
ctx
,
ok
=
udpCtx
.
(
UDPContext
);
!
ok
{
return
0
,
j
errors
.
Errorf
(
"illegal @udpCtx{%s} type, @udpCtx type:%T"
,
udpCtx
,
udpCtx
)
return
0
,
errors
.
Errorf
(
"illegal @udpCtx{%s} type, @udpCtx type:%T"
,
udpCtx
,
udpCtx
)
}
if
buf
,
ok
=
ctx
.
Pkg
.
([]
byte
);
!
ok
{
return
0
,
j
errors
.
Errorf
(
"illegal @udpCtx.Pkg{%#v} type"
,
udpCtx
)
return
0
,
errors
.
Errorf
(
"illegal @udpCtx.Pkg{%#v} type"
,
udpCtx
)
}
if
u
.
ss
.
EndPoint
()
.
EndPointType
()
==
UDP_ENDPOINT
{
peerAddr
=
ctx
.
PeerAddr
...
...
@@ -441,7 +441,7 @@ func (u *gettyUDPConn) Write(udpCtx interface{}) (int, error) {
currentTime
=
time
.
Now
()
if
currentTime
.
Sub
(
u
.
wLastDeadline
)
>
(
u
.
wTimeout
>>
2
)
{
if
err
=
u
.
conn
.
SetWriteDeadline
(
currentTime
.
Add
(
u
.
wTimeout
));
err
!=
nil
{
return
0
,
jerrors
.
Trace
(
err
)
return
0
,
errors
.
WithStack
(
err
)
}
u
.
wLastDeadline
=
currentTime
}
...
...
@@ -452,7 +452,7 @@ func (u *gettyUDPConn) Write(udpCtx interface{}) (int, error) {
}
log
.
Debug
(
"WriteMsgUDP(peerAddr:%s) = {length:%d, error:%s}"
,
peerAddr
,
length
,
err
)
return
length
,
jerrors
.
Trace
(
err
)
return
length
,
errors
.
WithStack
(
err
)
//return length, err
}
...
...
@@ -529,7 +529,7 @@ func (w *gettyWSConn) handlePing(message string) error {
w
.
UpdateActive
()
}
return
jerrors
.
Trace
(
err
)
return
errors
.
WithStack
(
err
)
}
func
(
w
*
gettyWSConn
)
handlePong
(
string
)
error
{
...
...
@@ -550,7 +550,7 @@ func (w *gettyWSConn) read() ([]byte, error) {
}
}
return
b
,
jerrors
.
Trace
(
e
)
return
b
,
errors
.
WithStack
(
e
)
//return b, e
}
...
...
@@ -567,7 +567,7 @@ func (w *gettyWSConn) updateWriteDeadline() error {
currentTime
=
time
.
Now
()
if
currentTime
.
Sub
(
w
.
wLastDeadline
)
>
(
w
.
wTimeout
>>
2
)
{
if
err
=
w
.
conn
.
SetWriteDeadline
(
currentTime
.
Add
(
w
.
wTimeout
));
err
!=
nil
{
return
jerrors
.
Trace
(
err
)
return
errors
.
WithStack
(
err
)
}
w
.
wLastDeadline
=
currentTime
}
...
...
@@ -585,25 +585,25 @@ func (w *gettyWSConn) Write(pkg interface{}) (int, error) {
)
if
p
,
ok
=
pkg
.
([]
byte
);
!
ok
{
return
0
,
j
errors
.
Errorf
(
"illegal @pkg{%#v} type"
,
pkg
)
return
0
,
errors
.
Errorf
(
"illegal @pkg{%#v} type"
,
pkg
)
}
w
.
updateWriteDeadline
()
if
err
=
w
.
conn
.
WriteMessage
(
websocket
.
BinaryMessage
,
p
);
err
==
nil
{
atomic
.
AddUint32
(
&
w
.
writeBytes
,
(
uint32
)(
len
(
p
)))
}
return
len
(
p
),
jerrors
.
Trace
(
err
)
return
len
(
p
),
errors
.
WithStack
(
err
)
//return len(p), err
}
func
(
w
*
gettyWSConn
)
writePing
()
error
{
w
.
updateWriteDeadline
()
return
jerrors
.
Trace
(
w
.
conn
.
WriteMessage
(
websocket
.
PingMessage
,
[]
byte
{}))
return
errors
.
WithStack
(
w
.
conn
.
WriteMessage
(
websocket
.
PingMessage
,
[]
byte
{}))
}
func
(
w
*
gettyWSConn
)
writePong
(
message
[]
byte
)
error
{
w
.
updateWriteDeadline
()
return
jerrors
.
Trace
(
w
.
conn
.
WriteMessage
(
websocket
.
PongMessage
,
message
))
return
errors
.
WithStack
(
w
.
conn
.
WriteMessage
(
websocket
.
PongMessage
,
message
))
}
// close websocket connection
...
...
go.mod
View file @
69a7b1ba
...
...
@@ -7,13 +7,14 @@ require (
github.com/dubbogo/log4go v0.0.0-20190406152735-41c57e1073e9
github.com/golang/snappy v0.0.1
github.com/gorilla/websocket v1.4.0
github.com/juju/errors v0.0.0-20190207033735-e65537c515d7
github.com/juju/errors v0.0.0-20190207033735-e65537c515d7
// indirect
github.com/juju/loggo v0.0.0-20190212223446-d976af380377 // indirect
github.com/juju/testing v0.0.0-20180920084828-472a3e8b2073 // indirect
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 // indirect
github.com/k0kubun/pp v3.0.1+incompatible // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/mattn/go-colorable v0.1.1 // indirect
github.com/pkg/errors v0.8.1
github.com/stretchr/testify v1.3.0 // indirect
golang.org/x/net v0.0.0-20190320064053-1272bf9dcd53
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
...
...
go.sum
View file @
69a7b1ba
...
...
@@ -33,6 +33,8 @@ github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcncea
github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.7 h1:UvyT9uN+3r7yLEYSlJsbQGdsaB/a0DlgWP3pql6iwOc=
github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
...
...
options.go
View file @
69a7b1ba
...
...
@@ -68,9 +68,9 @@ func WithWebsocketServerRootCert(cert string) ServerOption {
type
ClientOption
func
(
*
ClientOptions
)
type
ClientOptions
struct
{
addr
string
number
int
reconnectInterval
int
// reConnect Interval
addr
string
number
int
reconnectInterval
int
// reConnect Interval
// the cert file of wss server which may contain server domain, server ip, the starting effective date, effective
// duration, the hash alg, the len of the private key.
...
...
server.go
View file @
69a7b1ba
...
...
@@ -26,11 +26,11 @@ import (
"github.com/AlexStocks/goext/time"
log
"github.com/dubbogo/log4go"
"github.com/gorilla/websocket"
jerrors
"github.com/juju
/errors"
"github.com/pkg
/errors"
)
var
(
errSelfConnect
=
j
errors
.
New
(
"connect self!"
)
errSelfConnect
=
errors
.
New
(
"connect self!"
)
serverFastFailTimeout
=
gxtime
.
TimeSecondDuration
(
1
)
)
...
...
@@ -157,7 +157,7 @@ func (s *server) listenTCP() error {
streamListener
,
err
=
net
.
Listen
(
"tcp"
,
s
.
addr
)
if
err
!=
nil
{
return
jerrors
.
Annotate
f
(
err
,
"net.Listen(tcp, addr:%s))"
,
s
.
addr
)
return
errors
.
Wrap
f
(
err
,
"net.Listen(tcp, addr:%s))"
,
s
.
addr
)
}
s
.
streamListener
=
streamListener
...
...
@@ -174,11 +174,11 @@ func (s *server) listenUDP() error {
localAddr
,
err
=
net
.
ResolveUDPAddr
(
"udp"
,
s
.
addr
)
if
err
!=
nil
{
return
jerrors
.
Annotate
f
(
err
,
"net.ResolveUDPAddr(udp, addr:%s)"
,
s
.
addr
)
return
errors
.
Wrap
f
(
err
,
"net.ResolveUDPAddr(udp, addr:%s)"
,
s
.
addr
)
}
pktListener
,
err
=
net
.
ListenUDP
(
"udp"
,
localAddr
)
if
err
!=
nil
{
return
jerrors
.
Annotate
f
(
err
,
"net.ListenUDP((udp, localAddr:%#v)"
,
localAddr
)
return
errors
.
Wrap
f
(
err
,
"net.ListenUDP((udp, localAddr:%#v)"
,
localAddr
)
}
// if err = setUDPSocketOptions(pktListener); err != nil {
// return errors.Wrapf(err, "setUDPSocketOptions(pktListener:%#v)", pktListener)
...
...
@@ -193,9 +193,9 @@ func (s *server) listenUDP() error {
func
(
s
*
server
)
listen
()
error
{
switch
s
.
endPointType
{
case
TCP_SERVER
,
WS_SERVER
,
WSS_SERVER
:
return
jerrors
.
Trace
(
s
.
listenTCP
())
return
errors
.
WithStack
(
s
.
listenTCP
())
case
UDP_ENDPOINT
:
return
jerrors
.
Trace
(
s
.
listenUDP
())
return
errors
.
WithStack
(
s
.
listenUDP
())
}
return
nil
...
...
@@ -204,7 +204,7 @@ func (s *server) listen() error {
func
(
s
*
server
)
accept
(
newSession
NewSessionCallback
)
(
Session
,
error
)
{
conn
,
err
:=
s
.
streamListener
.
Accept
()
if
err
!=
nil
{
return
nil
,
jerrors
.
Trace
(
err
)
return
nil
,
errors
.
WithStack
(
err
)
}
if
gxnet
.
IsSameAddr
(
conn
.
RemoteAddr
(),
conn
.
LocalAddr
())
{
log
.
Warn
(
"conn.localAddr{%s} == conn.RemoteAddr"
,
conn
.
LocalAddr
()
.
String
(),
conn
.
RemoteAddr
()
.
String
())
...
...
@@ -215,7 +215,7 @@ func (s *server) accept(newSession NewSessionCallback) (Session, error) {
err
=
newSession
(
ss
)
if
err
!=
nil
{
conn
.
Close
()
return
nil
,
jerrors
.
Trace
(
err
)
return
nil
,
errors
.
WithStack
(
err
)
}
return
ss
,
nil
...
...
@@ -252,7 +252,7 @@ func (s *server) runTcpEventLoop(newSession NewSessionCallback) {
}
continue
}
log
.
Warn
(
"server{%s}.Accept() = err {%
#v}"
,
s
.
addr
,
jerrors
.
ErrorStack
(
err
)
)
log
.
Warn
(
"server{%s}.Accept() = err {%
+v}"
,
s
.
addr
,
err
)
continue
}
delay
=
0
...
...
@@ -309,7 +309,7 @@ func (s *wsHandler) serveWSRequest(w http.ResponseWriter, r *http.Request) {
conn
,
err
:=
s
.
upgrader
.
Upgrade
(
w
,
r
,
nil
)
if
err
!=
nil
{
log
.
Warn
(
"upgrader.Upgrader(http.Request{%#v}) = error
{%s}
"
,
r
,
err
)
log
.
Warn
(
"upgrader.Upgrader(http.Request{%#v}) = error
:%+v
"
,
r
,
err
)
return
}
if
conn
.
RemoteAddr
()
.
String
()
==
conn
.
LocalAddr
()
.
String
()
{
...
...
@@ -355,7 +355,7 @@ func (s *server) runWSEventLoop(newSession NewSessionCallback) {
s
.
lock
.
Unlock
()
err
=
server
.
Serve
(
s
.
streamListener
)
if
err
!=
nil
{
log
.
Error
(
"http.server.Serve(addr{%s}) = err{%
s}"
,
s
.
addr
,
jerrors
.
ErrorStack
(
err
)
)
log
.
Error
(
"http.server.Serve(addr{%s}) = err{%
+v}"
,
s
.
addr
,
err
)
// panic(err)
}
}()
...
...
@@ -378,8 +378,8 @@ func (s *server) runWSSEventLoop(newSession NewSessionCallback) {
defer
s
.
wg
.
Done
()
if
certificate
,
err
=
tls
.
LoadX509KeyPair
(
s
.
cert
,
s
.
privateKey
);
err
!=
nil
{
panic
(
fmt
.
Sprintf
(
"tls.LoadX509KeyPair(cert{%s}, privateKey{%s}) = err{%
s
}"
,
s
.
cert
,
s
.
privateKey
,
jerrors
.
ErrorStack
(
err
)
))
panic
(
fmt
.
Sprintf
(
"tls.LoadX509KeyPair(cert{%s}, privateKey{%s}) = err{%
+v
}"
,
s
.
cert
,
s
.
privateKey
,
err
))
return
}
config
=
&
tls
.
Config
{
...
...
@@ -392,7 +392,7 @@ func (s *server) runWSSEventLoop(newSession NewSessionCallback) {
if
s
.
caCert
!=
""
{
certPem
,
err
=
ioutil
.
ReadFile
(
s
.
caCert
)
if
err
!=
nil
{
panic
(
fmt
.
Errorf
(
"ioutil.ReadFile(certFile{%s}) = err{%
s}"
,
s
.
caCert
,
jerrors
.
ErrorStack
(
err
)
))
panic
(
fmt
.
Errorf
(
"ioutil.ReadFile(certFile{%s}) = err{%
+v}"
,
s
.
caCert
,
err
))
}
certPool
=
x509
.
NewCertPool
()
if
ok
:=
certPool
.
AppendCertsFromPEM
(
certPem
);
!
ok
{
...
...
@@ -417,7 +417,7 @@ func (s *server) runWSSEventLoop(newSession NewSessionCallback) {
s
.
lock
.
Unlock
()
err
=
server
.
Serve
(
tls
.
NewListener
(
s
.
streamListener
,
config
))
if
err
!=
nil
{
log
.
Error
(
"http.server.Serve(addr{%s}) = err{%
s}"
,
s
.
addr
,
jerrors
.
ErrorStack
(
err
)
)
log
.
Error
(
"http.server.Serve(addr{%s}) = err{%
+v}"
,
s
.
addr
,
err
)
panic
(
err
)
}
}()
...
...
@@ -427,7 +427,7 @@ func (s *server) runWSSEventLoop(newSession NewSessionCallback) {
// @newSession: new connection callback
func
(
s
*
server
)
RunEventLoop
(
newSession
NewSessionCallback
)
{
if
err
:=
s
.
listen
();
err
!=
nil
{
panic
(
fmt
.
Errorf
(
"server.listen() = error:%
s"
,
jerrors
.
ErrorStack
(
err
)
))
panic
(
fmt
.
Errorf
(
"server.listen() = error:%
+v"
,
err
))
}
switch
s
.
endPointType
{
...
...
session.go
View file @
69a7b1ba
...
...
@@ -22,7 +22,7 @@ import (
import
(
log
"github.com/dubbogo/log4go"
"github.com/gorilla/websocket"
jerrors
"github.com/juju
/errors"
"github.com/pkg
/errors"
)
import
(
...
...
@@ -337,7 +337,7 @@ func (s *session) WritePkg(pkg interface{}, timeout time.Duration) error {
s
.
incWritePkgNum
()
// gxlog.CError("after incWritePkgNum, ss:%s", s.Stat())
}
return
jerrors
.
Trace
(
err
)
return
errors
.
WithStack
(
err
)
}
select
{
case
s
.
wQ
<-
pkg
:
...
...
@@ -359,7 +359,7 @@ func (s *session) WriteBytes(pkg []byte) error {
// s.conn.SetWriteTimeout(time.Now().Add(s.wTimeout))
if
_
,
err
:=
s
.
Connection
.
Write
(
pkg
);
err
!=
nil
{
return
jerrors
.
Annotate
f
(
err
,
"s.Connection.Write(pkg len:%d)"
,
len
(
pkg
))
return
errors
.
Wrap
f
(
err
,
"s.Connection.Write(pkg len:%d)"
,
len
(
pkg
))
}
s
.
incWritePkgNum
()
...
...
@@ -400,7 +400,7 @@ func (s *session) WriteBytesArray(pkgs ...[]byte) error {
// return s.Connection.Write(arr)
if
err
=
s
.
WriteBytes
(
arr
);
err
!=
nil
{
return
jerrors
.
Trace
(
err
)
return
errors
.
WithStack
(
err
)
}
num
:=
len
(
pkgs
)
-
1
...
...
@@ -506,7 +506,7 @@ LOOP:
case
outPkg
=
<-
s
.
wQ
:
if
flag
{
if
err
=
s
.
writer
.
Write
(
s
,
outPkg
);
err
!=
nil
{
log
.
Error
(
"%s, [session.handleLoop] = error
{%s}"
,
s
.
sessionToken
(),
jerrors
.
ErrorStack
(
err
)
)
log
.
Error
(
"%s, [session.handleLoop] = error
:%+v"
,
s
.
sessionToken
(),
err
)
s
.
stop
()
flag
=
false
// break LOOP
...
...
@@ -549,7 +549,7 @@ func (s *session) handlePackage() {
log
.
Info
(
"%s, [session.handlePackage] gr will exit now, left gr num %d"
,
s
.
sessionToken
(),
grNum
)
s
.
stop
()
if
err
!=
nil
{
log
.
Error
(
"%s, [session.handlePackage] error
{%s}"
,
s
.
sessionToken
(),
jerrors
.
ErrorStack
(
err
)
)
log
.
Error
(
"%s, [session.handlePackage] error
:%+v"
,
s
.
sessionToken
(),
err
)
s
.
listener
.
OnError
(
s
,
err
)
}
}()
...
...
@@ -603,10 +603,10 @@ func (s *session) handleTCPPackage() error {
// s.conn.SetReadTimeout(time.Now().Add(s.rTimeout))
bufLen
,
err
=
conn
.
read
(
buf
)
if
err
!=
nil
{
if
netError
,
ok
=
j
errors
.
Cause
(
err
)
.
(
net
.
Error
);
ok
&&
netError
.
Timeout
()
{
if
netError
,
ok
=
errors
.
Cause
(
err
)
.
(
net
.
Error
);
ok
&&
netError
.
Timeout
()
{
break
}
log
.
Error
(
"%s, [session.conn.read] = error
{%s}"
,
s
.
sessionToken
(),
jerrors
.
ErrorStack
(
err
)
)
log
.
Error
(
"%s, [session.conn.read] = error
:%+v"
,
s
.
sessionToken
(),
err
)
// for (Codec)OnErr
// s.errFlag = true
exit
=
true
...
...
@@ -627,11 +627,11 @@ func (s *session) handleTCPPackage() error {
// pkg, err = s.pkgHandler.Read(s, pktBuf)
pkg
,
pkgLen
,
err
=
s
.
reader
.
Read
(
s
,
pktBuf
.
Bytes
())
if
err
==
nil
&&
s
.
maxMsgLen
>
0
&&
pkgLen
>
int
(
s
.
maxMsgLen
)
{
err
=
j
errors
.
Errorf
(
"pkgLen %d > session max message len %d"
,
pkgLen
,
s
.
maxMsgLen
)
err
=
errors
.
Errorf
(
"pkgLen %d > session max message len %d"
,
pkgLen
,
s
.
maxMsgLen
)
}
if
err
!=
nil
{
log
.
Warn
(
"%s, [session.handleTCPPackage] = len{%d}, error
{%s}
"
,
s
.
sessionToken
(),
pkgLen
,
jerrors
.
ErrorStack
(
err
)
)
log
.
Warn
(
"%s, [session.handleTCPPackage] = len{%d}, error
:%+v
"
,
s
.
sessionToken
(),
pkgLen
,
err
)
// for (Codec)OnErr
// s.errFlag = true
exit
=
true
...
...
@@ -649,7 +649,7 @@ func (s *session) handleTCPPackage() error {
}
}
return
jerrors
.
Trace
(
err
)
return
errors
.
WithStack
(
err
)
}
// get package from udp packet
...
...
@@ -678,19 +678,19 @@ func (s *session) handleUDPPackage() error {
}
bufLen
,
addr
,
err
=
conn
.
read
(
buf
)
log
.
Debug
(
"conn.read() = bufLen:%d, addr:%#v, err:%
s"
,
bufLen
,
addr
,
jerrors
.
ErrorStack
(
err
)
)
if
netError
,
ok
=
j
errors
.
Cause
(
err
)
.
(
net
.
Error
);
ok
&&
netError
.
Timeout
()
{
log
.
Debug
(
"conn.read() = bufLen:%d, addr:%#v, err:%
+v"
,
bufLen
,
addr
,
err
)
if
netError
,
ok
=
errors
.
Cause
(
err
)
.
(
net
.
Error
);
ok
&&
netError
.
Timeout
()
{
continue
}
if
err
!=
nil
{
log
.
Error
(
"%s, [session.handleUDPPackage] = len{%d}, error{%s}"
,
s
.
sessionToken
(),
bufLen
,
jerrors
.
ErrorStack
(
err
)
)
err
=
jerrors
.
Annotate
f
(
err
,
"conn.read()"
)
log
.
Error
(
"%s, [session.handleUDPPackage] = len{%d}, error{%
+
s}"
,
s
.
sessionToken
(),
bufLen
,
err
)
err
=
errors
.
Wrap
f
(
err
,
"conn.read()"
)
break
}
if
bufLen
==
0
{
log
.
Error
(
"conn.read() = bufLen:%d, addr:%s, err:%
s"
,
bufLen
,
addr
,
jerrors
.
ErrorStack
(
err
)
)
log
.
Error
(
"conn.read() = bufLen:%d, addr:%s, err:%
+v"
,
bufLen
,
addr
,
err
)
continue
}
...
...
@@ -700,17 +700,17 @@ func (s *session) handleUDPPackage() error {
}
pkg
,
pkgLen
,
err
=
s
.
reader
.
Read
(
s
,
buf
[
:
bufLen
])
log
.
Debug
(
"s.reader.Read() = pkg:%#v, pkgLen:%d, err:%
s"
,
pkg
,
pkgLen
,
jerrors
.
ErrorStack
(
err
)
)
log
.
Debug
(
"s.reader.Read() = pkg:%#v, pkgLen:%d, err:%
+v"
,
pkg
,
pkgLen
,
err
)
if
err
==
nil
&&
s
.
maxMsgLen
>
0
&&
bufLen
>
int
(
s
.
maxMsgLen
)
{
err
=
j
errors
.
Errorf
(
"Message Too Long, bufLen %d, session max message len %d"
,
bufLen
,
s
.
maxMsgLen
)
err
=
errors
.
Errorf
(
"Message Too Long, bufLen %d, session max message len %d"
,
bufLen
,
s
.
maxMsgLen
)
}
if
err
!=
nil
{
log
.
Warn
(
"%s, [session.handleUDPPackage] = len{%d}, error
{%s}
"
,
s
.
sessionToken
(),
pkgLen
,
jerrors
.
ErrorStack
(
err
)
)
log
.
Warn
(
"%s, [session.handleUDPPackage] = len{%d}, error
:%+v
"
,
s
.
sessionToken
(),
pkgLen
,
err
)
continue
}
if
pkgLen
==
0
{
log
.
Error
(
"s.reader.Read() = pkg:%#v, pkgLen:%d, err:%
s"
,
pkg
,
pkgLen
,
jerrors
.
ErrorStack
(
err
)
)
log
.
Error
(
"s.reader.Read() = pkg:%#v, pkgLen:%d, err:%
+v"
,
pkg
,
pkgLen
,
err
)
continue
}
...
...
@@ -718,7 +718,7 @@ func (s *session) handleUDPPackage() error {
s
.
rQ
<-
UDPContext
{
Pkg
:
pkg
,
PeerAddr
:
addr
}
}
return
jerrors
.
Trace
(
err
)
return
errors
.
WithStack
(
err
)
}
// get package from websocket stream
...
...
@@ -739,24 +739,24 @@ func (s *session) handleWSPackage() error {
break
}
pkg
,
err
=
conn
.
read
()
if
netError
,
ok
=
j
errors
.
Cause
(
err
)
.
(
net
.
Error
);
ok
&&
netError
.
Timeout
()
{
if
netError
,
ok
=
errors
.
Cause
(
err
)
.
(
net
.
Error
);
ok
&&
netError
.
Timeout
()
{
continue
}
if
err
!=
nil
{
log
.
Warn
(
"%s, [session.handleWSPackage] = error{%s}"
,
s
.
sessionToken
(),
jerrors
.
ErrorStack
(
err
)
)
log
.
Warn
(
"%s, [session.handleWSPackage] = error{%
+
s}"
,
s
.
sessionToken
(),
err
)
// s.errFlag = true
return
jerrors
.
Trace
(
err
)
return
errors
.
WithStack
(
err
)
}
s
.
UpdateActive
()
if
s
.
reader
!=
nil
{
unmarshalPkg
,
length
,
err
=
s
.
reader
.
Read
(
s
,
pkg
)
if
err
==
nil
&&
s
.
maxMsgLen
>
0
&&
length
>
int
(
s
.
maxMsgLen
)
{
err
=
j
errors
.
Errorf
(
"Message Too Long, length %d, session max message len %d"
,
length
,
s
.
maxMsgLen
)
err
=
errors
.
Errorf
(
"Message Too Long, length %d, session max message len %d"
,
length
,
s
.
maxMsgLen
)
}
if
err
!=
nil
{
log
.
Warn
(
"%s, [session.handleWSPackage] = len{%d}, error
{%s}
"
,
s
.
sessionToken
(),
length
,
jerrors
.
ErrorStack
(
err
)
)
log
.
Warn
(
"%s, [session.handleWSPackage] = len{%d}, error
:%+v
"
,
s
.
sessionToken
(),
length
,
err
)
continue
}
s
.
rQ
<-
unmarshalPkg
...
...
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