Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
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
8abb0aa7
Commit
8abb0aa7
authored
Feb 03, 2017
by
AlexStocks
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add Session interface
parent
f7505d10
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
205 additions
and
210 deletions
+205
-210
change_log.md
change_log.md
+5
-0
client.go
client.go
+20
-19
codec.go
codec.go
+8
-8
conn.go
conn.go
+32
-28
server.go
server.go
+9
-8
session.go
session.go
+127
-143
version.go
version.go
+4
-4
No files found.
change_log.md
View file @
8abb0aa7
...
...
@@ -11,6 +11,11 @@
## develop history ##
---
-
2017/02/03
> 1 Session struct -> session struct and add Session interface
>
> 2 version: 0.7.0
-
2016/11/19
> 1 add conn.go:(*gettyWSConn) setCompressType to add zip compress feature for ws connection
>
...
...
client.go
View file @
8abb0aa7
...
...
@@ -21,6 +21,7 @@ import (
)
import
(
"github.com/AlexStocks/goext/sync"
log
"github.com/AlexStocks/log4go"
"github.com/gorilla/websocket"
)
...
...
@@ -42,10 +43,10 @@ type Client struct {
interval
time
.
Duration
addr
string
newSession
NewSessionCallback
sessionMap
map
[
*
Session
]
e
mpty
sessionMap
map
[
Session
]
gxsync
.
E
mpty
sync
.
Once
done
chan
e
mpty
done
chan
gxsync
.
E
mpty
wg
sync
.
WaitGroup
// for wss client
...
...
@@ -68,8 +69,8 @@ func NewClient(connNum int, connInterval time.Duration, serverAddr string) *Clie
number
:
connNum
,
interval
:
connInterval
,
addr
:
serverAddr
,
sessionMap
:
make
(
map
[
*
Session
]
e
mpty
,
connNum
),
done
:
make
(
chan
e
mpty
),
sessionMap
:
make
(
map
[
Session
]
gxsync
.
E
mpty
,
connNum
),
done
:
make
(
chan
gxsync
.
E
mpty
),
}
}
...
...
@@ -90,13 +91,13 @@ func NewWSSClient(connNum int, connInterval time.Duration, serverAddr string, ce
number
:
connNum
,
interval
:
connInterval
,
addr
:
serverAddr
,
sessionMap
:
make
(
map
[
*
Session
]
e
mpty
,
connNum
),
done
:
make
(
chan
e
mpty
),
sessionMap
:
make
(
map
[
Session
]
gxsync
.
E
mpty
,
connNum
),
done
:
make
(
chan
gxsync
.
E
mpty
),
certFile
:
cert
,
}
}
func
(
this
*
Client
)
dialTCP
()
*
Session
{
func
(
this
*
Client
)
dialTCP
()
Session
{
var
(
err
error
conn
net
.
Conn
...
...
@@ -120,12 +121,12 @@ func (this *Client) dialTCP() *Session {
}
}
func
(
this
*
Client
)
dialWS
()
*
Session
{
func
(
this
*
Client
)
dialWS
()
Session
{
var
(
err
error
dialer
websocket
.
Dialer
conn
*
websocket
.
Conn
session
*
Session
session
Session
)
dialer
.
EnableCompression
=
true
...
...
@@ -139,8 +140,8 @@ func (this *Client) dialWS() *Session {
}
if
err
==
nil
{
session
=
NewWSSession
(
conn
)
if
session
.
maxMsgLen
>
0
{
conn
.
SetReadLimit
(
int64
(
session
.
maxMsgLen
))
if
session
.
(
*
session
)
.
maxMsgLen
>
0
{
conn
.
SetReadLimit
(
int64
(
session
.
(
*
session
)
.
maxMsgLen
))
}
return
session
...
...
@@ -152,14 +153,14 @@ func (this *Client) dialWS() *Session {
}
}
func
(
this
*
Client
)
dialWSS
()
*
Session
{
func
(
this
*
Client
)
dialWSS
()
Session
{
var
(
err
error
certPem
[]
byte
certPool
*
x509
.
CertPool
dialer
websocket
.
Dialer
conn
*
websocket
.
Conn
session
*
Session
session
Session
)
dialer
.
EnableCompression
=
true
...
...
@@ -184,8 +185,8 @@ func (this *Client) dialWSS() *Session {
}
if
err
==
nil
{
session
=
NewWSSession
(
conn
)
if
session
.
maxMsgLen
>
0
{
conn
.
SetReadLimit
(
int64
(
session
.
maxMsgLen
))
if
session
.
(
*
session
)
.
maxMsgLen
>
0
{
conn
.
SetReadLimit
(
int64
(
session
.
(
*
session
)
.
maxMsgLen
))
}
return
session
...
...
@@ -197,7 +198,7 @@ func (this *Client) dialWSS() *Session {
}
}
func
(
this
*
Client
)
dial
()
*
Session
{
func
(
this
*
Client
)
dial
()
Session
{
if
strings
.
HasPrefix
(
this
.
addr
,
"ws"
)
{
return
this
.
dialWS
()
}
else
if
strings
.
HasPrefix
(
this
.
addr
,
"wss"
)
{
...
...
@@ -225,7 +226,7 @@ func (this *Client) sessionNum() int {
func
(
this
*
Client
)
connect
()
{
var
(
err
error
session
*
Session
session
Session
)
for
{
...
...
@@ -237,9 +238,9 @@ func (this *Client) connect() {
err
=
this
.
newSession
(
session
)
if
err
==
nil
{
// session.RunEventLoop()
session
.
run
()
session
.
(
*
session
)
.
run
()
this
.
Lock
()
this
.
sessionMap
[
session
]
=
e
mpty
{}
this
.
sessionMap
[
session
]
=
gxsync
.
E
mpty
{}
this
.
Unlock
()
break
}
...
...
codec.go
View file @
8abb0aa7
...
...
@@ -12,19 +12,19 @@ package getty
// NewSessionCallback will be invoked when server accepts a new client connection or client connects to server successfully.
// if there are too many client connections or u do not want to connect a server again, u can return non-nil error. And
// then getty will close the new session.
type
NewSessionCallback
func
(
*
Session
)
error
type
NewSessionCallback
func
(
Session
)
error
// Reader is used to unmarshal a complete pkg from buffer
type
Reader
interface
{
// Parse tcp pkg from buffer and if possible return a complete pkg
// If length of buf is not long enough, u should return {nil,0, nil}
// The second return value is the length of the pkg.
Read
(
*
Session
,
[]
byte
)
(
interface
{},
int
,
error
)
Read
(
Session
,
[]
byte
)
(
interface
{},
int
,
error
)
}
// Writer is used to marshal pkg and write to session
type
Writer
interface
{
Write
(
*
Session
,
interface
{})
error
Write
(
Session
,
interface
{})
error
}
// tcp package handler interface
...
...
@@ -37,19 +37,19 @@ type ReadWriter interface {
type
EventListener
interface
{
// invoked when session opened
// If the return error is not nil, @Session will be closed.
OnOpen
(
*
Session
)
error
OnOpen
(
Session
)
error
// invoked when session closed.
OnClose
(
*
Session
)
OnClose
(
Session
)
// invoked when got error.
OnError
(
*
Session
,
error
)
OnError
(
Session
,
error
)
// invoked periodically, its period can be set by (Session)SetCronPeriod
OnCron
(
*
Session
)
OnCron
(
Session
)
// invoked when receive packge. Pls attention that do not handle long time logic processing in this func.
// Y'd better set the package's maximum length. If the message's length is greater than it, u should
// should return err in Reader{Read} and getty will close this connection soon.
OnMessage
(
*
Session
,
interface
{})
OnMessage
(
Session
,
interface
{})
}
conn.go
View file @
8abb0aa7
...
...
@@ -48,20 +48,24 @@ const (
// connection interfacke
/////////////////////////////////////////
type
iCon
n
interface
{
id
()
uint32
s
etCompressType
(
t
CompressType
)
l
ocalAddr
()
string
r
emoteAddr
()
string
type
Connectio
n
interface
{
ID
()
uint32
S
etCompressType
(
t
CompressType
)
L
ocalAddr
()
string
R
emoteAddr
()
string
incReadPkgCount
()
incWritePkgCount
()
updateActive
()
getActive
()
time
.
Time
// update session's active time
UpdateActive
()
// get session's active time
GetActive
()
time
.
Time
readDeadline
()
time
.
Duration
setReadDeadline
(
time
.
Duration
)
// SetReadDeadline sets deadline for the future read calls.
SetReadDeadline
(
time
.
Duration
)
writeDeadline
()
time
.
Duration
setWriteDeadline
(
time
.
Duration
)
write
(
p
[]
byte
)
error
// SetWriteDeadlile sets deadline for the future read calls.
SetWriteDeadline
(
time
.
Duration
)
Write
(
p
[]
byte
)
error
// don't distinguish between tcp connection and websocket connection. Because
// gorilla/websocket/conn.go:(Conn)Close also invoke net.Conn.Close
close
(
int
)
...
...
@@ -76,7 +80,7 @@ var (
)
type
gettyConn
struct
{
ID
uint32
id
uint32
compress
CompressType
padding1
uint8
padding2
uint16
...
...
@@ -91,15 +95,15 @@ type gettyConn struct {
peer
string
// peer address
}
func
(
this
*
gettyConn
)
id
()
uint32
{
return
this
.
ID
func
(
this
*
gettyConn
)
ID
()
uint32
{
return
this
.
id
}
func
(
this
*
gettyConn
)
l
ocalAddr
()
string
{
func
(
this
*
gettyConn
)
L
ocalAddr
()
string
{
return
this
.
local
}
func
(
this
*
gettyConn
)
r
emoteAddr
()
string
{
func
(
this
*
gettyConn
)
R
emoteAddr
()
string
{
return
this
.
peer
}
...
...
@@ -111,15 +115,15 @@ func (this *gettyConn) incWritePkgCount() {
atomic
.
AddUint32
(
&
this
.
writePkgCount
,
1
)
}
func
(
this
*
gettyConn
)
u
pdateActive
()
{
func
(
this
*
gettyConn
)
U
pdateActive
()
{
atomic
.
StoreInt64
(
&
(
this
.
active
),
int64
(
time
.
Since
(
launchTime
)))
}
func
(
this
*
gettyConn
)
g
etActive
()
time
.
Time
{
func
(
this
*
gettyConn
)
G
etActive
()
time
.
Time
{
return
launchTime
.
Add
(
time
.
Duration
(
atomic
.
LoadInt64
(
&
(
this
.
active
))))
}
func
(
this
*
gettyConn
)
w
rite
([]
byte
)
error
{
func
(
this
*
gettyConn
)
W
rite
([]
byte
)
error
{
return
nil
}
...
...
@@ -129,7 +133,7 @@ func (this gettyConn) readDeadline() time.Duration {
return
this
.
rDeadline
}
func
(
this
*
gettyConn
)
s
etReadDeadline
(
rDeadline
time
.
Duration
)
{
func
(
this
*
gettyConn
)
S
etReadDeadline
(
rDeadline
time
.
Duration
)
{
if
rDeadline
<
1
{
panic
(
"@rDeadline < 1"
)
}
...
...
@@ -144,7 +148,7 @@ func (this gettyConn) writeDeadline() time.Duration {
return
this
.
wDeadline
}
func
(
this
*
gettyConn
)
s
etWriteDeadline
(
wDeadline
time
.
Duration
)
{
func
(
this
*
gettyConn
)
S
etWriteDeadline
(
wDeadline
time
.
Duration
)
{
if
wDeadline
<
1
{
panic
(
"@wDeadline < 1"
)
}
...
...
@@ -182,7 +186,7 @@ func newGettyTCPConn(conn net.Conn) *gettyTCPConn {
reader
:
io
.
Reader
(
conn
),
writer
:
io
.
Writer
(
conn
),
gettyConn
:
gettyConn
{
ID
:
atomic
.
AddUint32
(
&
connID
,
1
),
id
:
atomic
.
AddUint32
(
&
connID
,
1
),
local
:
localAddr
,
peer
:
peerAddr
,
compress
:
CompressNone
,
...
...
@@ -213,7 +217,7 @@ func (this *writeFlusher) Write(p []byte) (int, error) {
}
// set compress type(tcp: zip/snappy, websocket:zip)
func
(
this
*
gettyTCPConn
)
s
etCompressType
(
t
CompressType
)
{
func
(
this
*
gettyTCPConn
)
S
etCompressType
(
t
CompressType
)
{
switch
{
case
t
==
CompressZip
:
this
.
reader
=
flate
.
NewReader
(
this
.
conn
)
...
...
@@ -244,7 +248,7 @@ func (this *gettyTCPConn) read(p []byte) (int, error) {
}
// tcp connection write
func
(
this
*
gettyTCPConn
)
w
rite
(
p
[]
byte
)
error
{
func
(
this
*
gettyTCPConn
)
W
rite
(
p
[]
byte
)
error
{
// if this.conn == nil {
// return 0, ErrInvalidConnection
// }
...
...
@@ -296,7 +300,7 @@ func newGettyWSConn(conn *websocket.Conn) *gettyWSConn {
gettyWSConn
:=
&
gettyWSConn
{
conn
:
conn
,
gettyConn
:
gettyConn
{
ID
:
atomic
.
AddUint32
(
&
connID
,
1
),
id
:
atomic
.
AddUint32
(
&
connID
,
1
),
local
:
localAddr
,
peer
:
peerAddr
,
},
...
...
@@ -309,7 +313,7 @@ func newGettyWSConn(conn *websocket.Conn) *gettyWSConn {
}
// set compress type(tcp: zip/snappy, websocket:zip)
func
(
this
*
gettyWSConn
)
s
etCompressType
(
t
CompressType
)
{
func
(
this
*
gettyWSConn
)
S
etCompressType
(
t
CompressType
)
{
switch
{
case
t
==
CompressZip
:
this
.
conn
.
EnableWriteCompression
(
true
)
...
...
@@ -328,14 +332,14 @@ func (this *gettyWSConn) handlePing(message string) error {
err
=
nil
}
if
err
==
nil
{
this
.
u
pdateActive
()
this
.
U
pdateActive
()
}
return
err
}
func
(
this
*
gettyWSConn
)
handlePong
(
string
)
error
{
this
.
u
pdateActive
()
this
.
U
pdateActive
()
return
nil
}
...
...
@@ -356,7 +360,7 @@ func (this *gettyWSConn) read() ([]byte, error) {
}
// websocket connection write
func
(
this
*
gettyWSConn
)
w
rite
(
p
[]
byte
)
error
{
func
(
this
*
gettyWSConn
)
W
rite
(
p
[]
byte
)
error
{
// atomic.AddUint32(&this.writeCount, 1)
atomic
.
AddUint32
(
&
this
.
writeCount
,
(
uint32
)(
len
(
p
)))
// this.conn.SetWriteDeadline(time.Now().Add(this.wDeadline))
...
...
server.go
View file @
8abb0aa7
...
...
@@ -20,6 +20,7 @@ import (
import
(
"github.com/AlexStocks/goext/net"
"github.com/AlexStocks/goext/sync"
log
"github.com/AlexStocks/log4go"
"github.com/gorilla/websocket"
)
...
...
@@ -34,12 +35,12 @@ type Server struct {
listener
net
.
Listener
sync
.
Once
done
chan
e
mpty
done
chan
gxsync
.
E
mpty
wg
sync
.
WaitGroup
}
func
NewServer
()
*
Server
{
return
&
Server
{
done
:
make
(
chan
e
mpty
)}
return
&
Server
{
done
:
make
(
chan
gxsync
.
E
mpty
)}
}
func
(
this
*
Server
)
stop
()
{
...
...
@@ -94,7 +95,7 @@ func (this *Server) RunEventloop(newSession NewSessionCallback) {
defer
this
.
wg
.
Done
()
var
(
err
error
client
*
Session
client
Session
delay
time
.
Duration
)
for
{
...
...
@@ -123,7 +124,7 @@ func (this *Server) RunEventloop(newSession NewSessionCallback) {
}
delay
=
0
// client.RunEventLoop()
client
.
run
()
client
.
(
*
session
)
.
run
()
}
}()
}
...
...
@@ -178,11 +179,11 @@ func (this *wsHandler) serveWSRequest(w http.ResponseWriter, r *http.Request) {
log
.
Warn
(
"Server{%s}.newSession(session{%#v}) = err {%#v}"
,
this
.
server
.
addr
,
session
,
err
)
return
}
if
session
.
maxMsgLen
>
0
{
conn
.
SetReadLimit
(
int64
(
session
.
maxMsgLen
))
if
session
.
(
*
session
)
.
maxMsgLen
>
0
{
conn
.
SetReadLimit
(
int64
(
session
.
(
*
session
)
.
maxMsgLen
))
}
// session.RunEventLoop()
session
.
run
()
session
.
(
*
session
)
.
run
()
}
// RunWSEventLoop serve websocket client request
...
...
@@ -253,7 +254,7 @@ func (this *Server) Listener() net.Listener {
return
this
.
listener
}
func
(
this
*
Server
)
accept
(
newSession
NewSessionCallback
)
(
*
Session
,
error
)
{
func
(
this
*
Server
)
accept
(
newSession
NewSessionCallback
)
(
Session
,
error
)
{
conn
,
err
:=
this
.
listener
.
Accept
()
if
err
!=
nil
{
return
nil
,
err
...
...
session.go
View file @
8abb0aa7
...
...
@@ -21,6 +21,7 @@ import (
)
import
(
"github.com/AlexStocks/goext/sync"
"github.com/AlexStocks/goext/time"
log
"github.com/AlexStocks/log4go"
"github.com/gorilla/websocket"
...
...
@@ -31,7 +32,7 @@ const (
netIOTimeout
=
1e9
// 1s
period
=
60
*
1e9
// 1 minute
pendingDuration
=
3e9
defaultSessionName
=
"
S
ession"
defaultSessionName
=
"
s
ession"
outputFormat
=
"session %s, Read Count: %d, Write Count: %d, Read Pkg Count: %d, Write Pkg Count: %d"
)
...
...
@@ -40,8 +41,8 @@ const (
/////////////////////////////////////////
var
(
ErrSessionClosed
=
errors
.
New
(
"
S
ession Already Closed"
)
ErrSessionBlocked
=
errors
.
New
(
"
S
ession Full Blocked"
)
ErrSessionClosed
=
errors
.
New
(
"
s
ession Already Closed"
)
ErrSessionBlocked
=
errors
.
New
(
"
s
ession Full Blocked"
)
ErrMsgTooLong
=
errors
.
New
(
"Message Too Long"
)
)
...
...
@@ -49,20 +50,46 @@ var (
wheel
=
gxtime
.
NewWheel
(
gxtime
.
TimeMillisecondDuration
(
100
),
1200
)
// wheel longest span is 2 minute
)
type
empty
struct
{}
type
Session
interface
{
Connection
Reset
()
Conn
()
net
.
Conn
Stat
()
string
IsClosed
()
bool
SetMaxMsgLen
(
int
)
SetName
(
string
)
SetEventListener
(
EventListener
)
SetPkgHandler
(
ReadWriter
)
SetReader
(
Reader
)
SetWriter
(
Writer
)
SetCronPeriod
(
int
)
SetRQLen
(
int
)
SetWQLen
(
int
)
SetWaitTime
(
time
.
Duration
)
GetAttribute
(
string
)
interface
{}
SetAttribute
(
string
,
interface
{})
RemoveAttribute
(
string
)
WritePkg
(
interface
{})
error
WriteBytes
([]
byte
)
error
WriteBytesArray
(
...
[]
byte
)
error
Close
()
}
// getty base session
type
S
ession
struct
{
type
s
ession
struct
{
name
string
maxMsgLen
int32
// net read
w
rite
iCon
n
// net read
W
rite
Connectio
n
// pkgHandler ReadWriter
reader
Reader
// @reader should be nil when @conn is a gettyWSConn object.
writer
Writer
listener
EventListener
once
sync
.
Once
done
chan
e
mpty
done
chan
gxsync
.
E
mpty
// errFlag bool
period
time
.
Duration
...
...
@@ -77,113 +104,94 @@ type Session struct {
lock
sync
.
RWMutex
}
func
NewSession
()
*
Session
{
session
:=
&
S
ession
{
func
NewSession
()
Session
{
session
:=
&
s
ession
{
name
:
defaultSessionName
,
done
:
make
(
chan
e
mpty
),
done
:
make
(
chan
gxsync
.
E
mpty
),
period
:
period
,
wait
:
pendingDuration
,
attrs
:
make
(
map
[
string
]
interface
{}),
}
session
.
s
etWriteDeadline
(
netIOTimeout
)
session
.
s
etReadDeadline
(
netIOTimeout
)
session
.
S
etWriteDeadline
(
netIOTimeout
)
session
.
S
etReadDeadline
(
netIOTimeout
)
return
session
}
func
NewTCPSession
(
conn
net
.
Conn
)
*
Session
{
session
:=
&
S
ession
{
name
:
defaultSessionName
,
iConn
:
newGettyTCPConn
(
conn
),
done
:
make
(
chan
e
mpty
),
period
:
period
,
wait
:
pendingDuration
,
attrs
:
make
(
map
[
string
]
interface
{}),
func
NewTCPSession
(
conn
net
.
Conn
)
Session
{
session
:=
&
s
ession
{
name
:
defaultSessionName
,
Connection
:
newGettyTCPConn
(
conn
),
done
:
make
(
chan
gxsync
.
E
mpty
),
period
:
period
,
wait
:
pendingDuration
,
attrs
:
make
(
map
[
string
]
interface
{}),
}
session
.
s
etWriteDeadline
(
netIOTimeout
)
session
.
s
etReadDeadline
(
netIOTimeout
)
session
.
S
etWriteDeadline
(
netIOTimeout
)
session
.
S
etReadDeadline
(
netIOTimeout
)
return
session
}
func
NewWSSession
(
conn
*
websocket
.
Conn
)
*
Session
{
session
:=
&
S
ession
{
name
:
defaultSessionName
,
iConn
:
newGettyWSConn
(
conn
),
done
:
make
(
chan
e
mpty
),
period
:
period
,
wait
:
pendingDuration
,
attrs
:
make
(
map
[
string
]
interface
{}),
func
NewWSSession
(
conn
*
websocket
.
Conn
)
Session
{
session
:=
&
s
ession
{
name
:
defaultSessionName
,
Connection
:
newGettyWSConn
(
conn
),
done
:
make
(
chan
gxsync
.
E
mpty
),
period
:
period
,
wait
:
pendingDuration
,
attrs
:
make
(
map
[
string
]
interface
{}),
}
session
.
s
etWriteDeadline
(
netIOTimeout
)
session
.
s
etReadDeadline
(
netIOTimeout
)
session
.
S
etWriteDeadline
(
netIOTimeout
)
session
.
S
etReadDeadline
(
netIOTimeout
)
return
session
}
func
(
this
*
S
ession
)
Reset
()
{
func
(
this
*
s
ession
)
Reset
()
{
this
.
name
=
defaultSessionName
this
.
once
=
sync
.
Once
{}
this
.
done
=
make
(
chan
e
mpty
)
this
.
done
=
make
(
chan
gxsync
.
E
mpty
)
// this.errFlag = false
this
.
period
=
period
this
.
wait
=
pendingDuration
this
.
attrs
=
make
(
map
[
string
]
interface
{})
this
.
grNum
=
0
this
.
s
etWriteDeadline
(
netIOTimeout
)
this
.
s
etReadDeadline
(
netIOTimeout
)
this
.
S
etWriteDeadline
(
netIOTimeout
)
this
.
S
etReadDeadline
(
netIOTimeout
)
}
// func (this *
S
ession) SetConn(conn net.Conn) { this.gettyConn = newGettyConn(conn) }
func
(
this
*
S
ession
)
Conn
()
net
.
Conn
{
if
tc
,
ok
:=
this
.
iCon
n
.
(
*
gettyTCPConn
);
ok
{
// func (this *
s
ession) SetConn(conn net.Conn) { this.gettyConn = newGettyConn(conn) }
func
(
this
*
s
ession
)
Conn
()
net
.
Conn
{
if
tc
,
ok
:=
this
.
Connectio
n
.
(
*
gettyTCPConn
);
ok
{
return
tc
.
conn
}
if
wc
,
ok
:=
this
.
iCon
n
.
(
*
gettyWSConn
);
ok
{
if
wc
,
ok
:=
this
.
Connectio
n
.
(
*
gettyWSConn
);
ok
{
return
wc
.
conn
.
UnderlyingConn
()
}
return
nil
}
func
(
this
*
S
ession
)
gettyConn
()
*
gettyConn
{
if
tc
,
ok
:=
this
.
iCon
n
.
(
*
gettyTCPConn
);
ok
{
func
(
this
*
s
ession
)
gettyConn
()
*
gettyConn
{
if
tc
,
ok
:=
this
.
Connectio
n
.
(
*
gettyTCPConn
);
ok
{
return
&
(
tc
.
gettyConn
)
}
if
wc
,
ok
:=
this
.
iCon
n
.
(
*
gettyWSConn
);
ok
{
if
wc
,
ok
:=
this
.
Connectio
n
.
(
*
gettyWSConn
);
ok
{
return
&
(
wc
.
gettyConn
)
}
return
nil
}
// get session ID
func
(
this
*
Session
)
ID
()
uint32
{
return
this
.
iConn
.
id
()
}
func
(
this
*
Session
)
SetCompressType
(
t
CompressType
)
{
this
.
iConn
.
setCompressType
(
t
)
}
// get local address
func
(
this
*
Session
)
LocalAddr
()
string
{
return
this
.
iConn
.
localAddr
()
}
// get peer address
func
(
this
*
Session
)
RemoteAddr
()
string
{
return
this
.
iConn
.
remoteAddr
()
}
// return the connect statistic data
func
(
this
*
S
ession
)
Stat
()
string
{
func
(
this
*
s
ession
)
Stat
()
string
{
var
conn
*
gettyConn
if
conn
=
this
.
gettyConn
();
conn
==
nil
{
return
""
...
...
@@ -199,7 +207,7 @@ func (this *Session) Stat() string {
}
// check whether the session has been closed.
func
(
this
*
S
ession
)
IsClosed
()
bool
{
func
(
this
*
s
ession
)
IsClosed
()
bool
{
select
{
case
<-
this
.
done
:
return
true
...
...
@@ -210,35 +218,35 @@ func (this *Session) IsClosed() bool {
}
// set maximum pacakge length of every pacakge in (EventListener)OnMessage(@pkgs)
func
(
this
*
S
ession
)
SetMaxMsgLen
(
length
int
)
{
this
.
maxMsgLen
=
int32
(
length
)
}
func
(
this
*
s
ession
)
SetMaxMsgLen
(
length
int
)
{
this
.
maxMsgLen
=
int32
(
length
)
}
// set session name
func
(
this
*
S
ession
)
SetName
(
name
string
)
{
this
.
name
=
name
}
func
(
this
*
s
ession
)
SetName
(
name
string
)
{
this
.
name
=
name
}
// set EventListener
func
(
this
*
S
ession
)
SetEventListener
(
listener
EventListener
)
{
func
(
this
*
s
ession
)
SetEventListener
(
listener
EventListener
)
{
this
.
listener
=
listener
}
// set package handler
func
(
this
*
S
ession
)
SetPkgHandler
(
handler
ReadWriter
)
{
func
(
this
*
s
ession
)
SetPkgHandler
(
handler
ReadWriter
)
{
this
.
reader
=
handler
this
.
writer
=
handler
// this.pkgHandler = handler
}
// set Reader
func
(
this
*
S
ession
)
SetReader
(
reader
Reader
)
{
func
(
this
*
s
ession
)
SetReader
(
reader
Reader
)
{
this
.
reader
=
reader
}
// set Writer
func
(
this
*
S
ession
)
SetWriter
(
writer
Writer
)
{
func
(
this
*
s
ession
)
SetWriter
(
writer
Writer
)
{
this
.
writer
=
writer
}
// period is in millisecond. Websocket session will send ping frame automatically every peroid.
func
(
this
*
S
ession
)
SetCronPeriod
(
period
int
)
{
func
(
this
*
s
ession
)
SetCronPeriod
(
period
int
)
{
if
period
<
1
{
panic
(
"@period < 1"
)
}
...
...
@@ -248,8 +256,8 @@ func (this *Session) SetCronPeriod(period int) {
this
.
lock
.
Unlock
()
}
// set @
S
ession's read queue size
func
(
this
*
S
ession
)
SetRQLen
(
readQLen
int
)
{
// set @
s
ession's read queue size
func
(
this
*
s
ession
)
SetRQLen
(
readQLen
int
)
{
if
readQLen
<
1
{
panic
(
"@readQLen < 1"
)
}
...
...
@@ -260,8 +268,8 @@ func (this *Session) SetRQLen(readQLen int) {
this
.
lock
.
Unlock
()
}
// set @
Session's w
rite queue size
func
(
this
*
S
ession
)
SetWQLen
(
writeQLen
int
)
{
// set @
session's W
rite queue size
func
(
this
*
s
ession
)
SetWQLen
(
writeQLen
int
)
{
if
writeQLen
<
1
{
panic
(
"@writeQLen < 1"
)
}
...
...
@@ -272,22 +280,8 @@ func (this *Session) SetWQLen(writeQLen int) {
this
.
lock
.
Unlock
()
}
// SetReadDeadline sets deadline for the future read calls.
func
(
this
*
Session
)
SetReadDeadline
(
rDeadline
time
.
Duration
)
{
this
.
lock
.
Lock
()
this
.
setReadDeadline
(
rDeadline
)
this
.
lock
.
Unlock
()
}
// SetWriteDeadlile sets deadline for the future read calls.
func
(
this
*
Session
)
SetWriteDeadline
(
wDeadline
time
.
Duration
)
{
this
.
lock
.
Lock
()
this
.
setWriteDeadline
(
wDeadline
)
this
.
lock
.
Unlock
()
}
// set maximum wait time when session got error or got exit signal
func
(
this
*
S
ession
)
SetWaitTime
(
waitTime
time
.
Duration
)
{
func
(
this
*
s
ession
)
SetWaitTime
(
waitTime
time
.
Duration
)
{
if
waitTime
<
1
{
panic
(
"@wait < 1"
)
}
...
...
@@ -298,7 +292,7 @@ func (this *Session) SetWaitTime(waitTime time.Duration) {
}
// set attribute of key @session:key
func
(
this
*
S
ession
)
GetAttribute
(
key
string
)
interface
{}
{
func
(
this
*
s
ession
)
GetAttribute
(
key
string
)
interface
{}
{
var
ret
interface
{}
this
.
lock
.
RLock
()
ret
=
this
.
attrs
[
key
]
...
...
@@ -307,35 +301,25 @@ func (this *Session) GetAttribute(key string) interface{} {
}
// get attribute of key @session:key
func
(
this
*
S
ession
)
SetAttribute
(
key
string
,
value
interface
{})
{
func
(
this
*
s
ession
)
SetAttribute
(
key
string
,
value
interface
{})
{
this
.
lock
.
Lock
()
this
.
attrs
[
key
]
=
value
this
.
lock
.
Unlock
()
}
// delete attribute of key @session:key
func
(
this
*
S
ession
)
RemoveAttribute
(
key
string
)
{
func
(
this
*
s
ession
)
RemoveAttribute
(
key
string
)
{
this
.
lock
.
Lock
()
delete
(
this
.
attrs
,
key
)
this
.
lock
.
Unlock
()
}
// update session's active time
func
(
this
*
Session
)
UpdateActive
()
{
this
.
updateActive
()
}
// get session's active time
func
(
this
*
Session
)
GetActive
()
time
.
Time
{
return
this
.
getActive
()
}
func
(
this
*
Session
)
sessionToken
()
string
{
return
fmt
.
Sprintf
(
"{%s:%d:%s<->%s}"
,
this
.
name
,
this
.
iConn
.
id
(),
this
.
iConn
.
localAddr
(),
this
.
iConn
.
remoteAddr
())
func
(
this
*
session
)
sessionToken
()
string
{
return
fmt
.
Sprintf
(
"{%s:%d:%s<->%s}"
,
this
.
name
,
this
.
ID
(),
this
.
LocalAddr
(),
this
.
RemoteAddr
())
}
// Queued
w
rite, for handler
func
(
this
*
S
ession
)
WritePkg
(
pkg
interface
{})
error
{
// Queued
W
rite, for handler
func
(
this
*
s
ession
)
WritePkg
(
pkg
interface
{})
error
{
if
this
.
IsClosed
()
{
return
ErrSessionClosed
}
...
...
@@ -369,24 +353,24 @@ func (this *Session) WritePkg(pkg interface{}) error {
}
// for codecs
func
(
this
*
S
ession
)
WriteBytes
(
pkg
[]
byte
)
error
{
func
(
this
*
s
ession
)
WriteBytes
(
pkg
[]
byte
)
error
{
if
this
.
IsClosed
()
{
return
ErrSessionClosed
}
// this.conn.SetWriteDeadline(time.Now().Add(this.wDeadline))
return
this
.
iConn
.
w
rite
(
pkg
)
return
this
.
Connection
.
W
rite
(
pkg
)
}
//
w
rite multiple packages at once
func
(
this
*
S
ession
)
WriteBytesArray
(
pkgs
...
[]
byte
)
error
{
//
W
rite multiple packages at once
func
(
this
*
s
ession
)
WriteBytesArray
(
pkgs
...
[]
byte
)
error
{
if
this
.
IsClosed
()
{
return
ErrSessionClosed
}
// this.conn.SetWriteDeadline(time.Now().Add(this.wDeadline))
if
len
(
pkgs
)
==
1
{
return
this
.
iConn
.
w
rite
(
pkgs
[
0
])
return
this
.
Connection
.
W
rite
(
pkgs
[
0
])
}
// get len
...
...
@@ -408,27 +392,27 @@ func (this *Session) WriteBytesArray(pkgs ...[]byte) error {
l
+=
len
(
pkgs
[
i
])
}
// return this.
iConn.w
rite(arr)
// return this.
Connection.W
rite(arr)
return
this
.
WriteBytes
(
arr
)
}
// func (this *
S
ession) RunEventLoop() {
func
(
this
*
S
ession
)
run
()
{
// func (this *
s
ession) RunEventLoop() {
func
(
this
*
s
ession
)
run
()
{
if
this
.
rQ
==
nil
||
this
.
wQ
==
nil
{
errStr
:=
fmt
.
Sprintf
(
"
S
ession{name:%s, rQ:%#v, wQ:%#v}"
,
errStr
:=
fmt
.
Sprintf
(
"
s
ession{name:%s, rQ:%#v, wQ:%#v}"
,
this
.
name
,
this
.
rQ
,
this
.
wQ
)
log
.
Error
(
errStr
)
panic
(
errStr
)
}
if
this
.
iCon
n
==
nil
||
this
.
listener
==
nil
||
this
.
writer
==
nil
{
errStr
:=
fmt
.
Sprintf
(
"
S
ession{name:%s, conn:%#v, listener:%#v, writer:%#v}"
,
this
.
name
,
this
.
iCon
n
,
this
.
listener
,
this
.
writer
)
if
this
.
Connectio
n
==
nil
||
this
.
listener
==
nil
||
this
.
writer
==
nil
{
errStr
:=
fmt
.
Sprintf
(
"
s
ession{name:%s, conn:%#v, listener:%#v, writer:%#v}"
,
this
.
name
,
this
.
Connectio
n
,
this
.
listener
,
this
.
writer
)
log
.
Error
(
errStr
)
panic
(
errStr
)
}
// call session opened
this
.
u
pdateActive
()
this
.
U
pdateActive
()
if
err
:=
this
.
listener
.
OnOpen
(
this
);
err
!=
nil
{
this
.
Close
()
return
...
...
@@ -439,7 +423,7 @@ func (this *Session) run() {
go
this
.
handlePackage
()
}
func
(
this
*
S
ession
)
handleLoop
()
{
func
(
this
*
s
ession
)
handleLoop
()
{
var
(
err
error
flag
bool
...
...
@@ -471,8 +455,8 @@ func (this *Session) handleLoop() {
this
.
gc
()
}()
wsConn
,
wsFlag
=
this
.
iCon
n
.
(
*
gettyWSConn
)
flag
=
true
// do not do any read/
write/cron operation while got w
rite error
wsConn
,
wsFlag
=
this
.
Connectio
n
.
(
*
gettyWSConn
)
flag
=
true
// do not do any read/
Write/cron operation while got W
rite error
// ticker = time.NewTicker(this.period) // use wheel instead, 2016/09/26
LOOP
:
for
{
...
...
@@ -480,9 +464,9 @@ LOOP:
// It choose one at random if multiple are ready. Otherwise it choose default branch if none is ready.
select
{
case
<-
this
.
done
:
// 这个分支确保(
Session)handleLoop gr在(S
ession)handlePackage gr之后退出
// 这个分支确保(
session)handleLoop gr在(s
ession)handlePackage gr之后退出
// once.Do(func() { ticker.Stop() }) // use wheel instead, 2016/09/26
if
atomic
.
LoadInt32
(
&
(
this
.
grNum
))
==
1
{
// make sure @(
S
ession)handlePackage goroutine has been closed.
if
atomic
.
LoadInt32
(
&
(
this
.
grNum
))
==
1
{
// make sure @(
s
ession)handlePackage goroutine has been closed.
if
len
(
this
.
rQ
)
==
0
&&
len
(
this
.
wQ
)
==
0
{
log
.
Info
(
"%s, [session.handleLoop] got done signal. Both rQ and wQ are nil."
,
this
.
Stat
())
break
LOOP
...
...
@@ -496,7 +480,7 @@ LOOP:
}
case
inPkg
=
<-
this
.
rQ
:
// 这个条件分支通过(
Session)rQ排空确保(Session)handlePackage gr不会阻塞在(S
ession)rQ上
// 这个条件分支通过(
session)rQ排空确保(session)handlePackage gr不会阻塞在(s
ession)rQ上
if
flag
{
this
.
listener
.
OnMessage
(
this
,
inPkg
)
this
.
incReadPkgCount
()
...
...
@@ -534,7 +518,7 @@ LOOP:
// once.Do(func() { ticker.Stop() }) // use wheel instead, 2016/09/26
}
func
(
this
*
S
ession
)
handlePackage
()
{
func
(
this
*
s
ession
)
handlePackage
()
{
var
(
err
error
)
...
...
@@ -559,21 +543,21 @@ func (this *Session) handlePackage() {
}
}()
if
_
,
ok
:=
this
.
iCon
n
.
(
*
gettyTCPConn
);
ok
{
if
_
,
ok
:=
this
.
Connectio
n
.
(
*
gettyTCPConn
);
ok
{
if
this
.
reader
==
nil
{
errStr
:=
fmt
.
Sprintf
(
"
Session{name:%s, conn:%#v, reader:%#v}"
,
this
.
name
,
this
.
iCon
n
,
this
.
reader
)
errStr
:=
fmt
.
Sprintf
(
"
session{name:%s, conn:%#v, reader:%#v}"
,
this
.
name
,
this
.
Connectio
n
,
this
.
reader
)
log
.
Error
(
errStr
)
panic
(
errStr
)
}
err
=
this
.
handleTCPPackage
()
}
else
if
_
,
ok
:=
this
.
iCon
n
.
(
*
gettyWSConn
);
ok
{
}
else
if
_
,
ok
:=
this
.
Connectio
n
.
(
*
gettyWSConn
);
ok
{
err
=
this
.
handleWSPackage
()
}
}
// get package from tcp stream(packet)
func
(
this
*
S
ession
)
handleTCPPackage
()
error
{
func
(
this
*
s
ession
)
handleTCPPackage
()
error
{
var
(
ok
bool
err
error
...
...
@@ -589,7 +573,7 @@ func (this *Session) handleTCPPackage() error {
buf
=
make
([]
byte
,
maxReadBufLen
)
pktBuf
=
new
(
bytes
.
Buffer
)
conn
=
this
.
iCon
n
.
(
*
gettyTCPConn
)
conn
=
this
.
Connectio
n
.
(
*
gettyTCPConn
)
for
{
if
this
.
IsClosed
()
{
err
=
nil
...
...
@@ -638,7 +622,7 @@ func (this *Session) handleTCPPackage() error {
if
pkg
==
nil
{
break
}
this
.
u
pdateActive
()
this
.
U
pdateActive
()
this
.
rQ
<-
pkg
pktBuf
.
Next
(
pkgLen
)
}
...
...
@@ -651,7 +635,7 @@ func (this *Session) handleTCPPackage() error {
}
// get package from websocket stream
func
(
this
*
S
ession
)
handleWSPackage
()
error
{
func
(
this
*
s
ession
)
handleWSPackage
()
error
{
var
(
ok
bool
err
error
...
...
@@ -662,7 +646,7 @@ func (this *Session) handleWSPackage() error {
unmarshalPkg
interface
{}
)
conn
=
this
.
iCon
n
.
(
*
gettyWSConn
)
conn
=
this
.
Connectio
n
.
(
*
gettyWSConn
)
for
{
if
this
.
IsClosed
()
{
break
...
...
@@ -676,7 +660,7 @@ func (this *Session) handleWSPackage() error {
// this.errFlag = true
return
err
}
this
.
u
pdateActive
()
this
.
U
pdateActive
()
if
this
.
reader
!=
nil
{
unmarshalPkg
,
length
,
err
=
this
.
reader
.
Read
(
this
,
pkg
)
if
err
==
nil
&&
this
.
maxMsgLen
>
0
&&
length
>
int
(
this
.
maxMsgLen
)
{
...
...
@@ -695,14 +679,14 @@ func (this *Session) handleWSPackage() error {
return
nil
}
func
(
this
*
S
ession
)
stop
()
{
func
(
this
*
s
ession
)
stop
()
{
select
{
case
<-
this
.
done
:
// this.done is a blocked channel. if it has not been closed, the default branch will be invoked.
return
default
:
this
.
once
.
Do
(
func
()
{
// let read/
w
rite timeout asap
// let read/
W
rite timeout asap
if
conn
:=
this
.
Conn
();
conn
!=
nil
{
conn
.
SetReadDeadline
(
time
.
Now
()
.
Add
(
this
.
readDeadline
()))
conn
.
SetWriteDeadline
(
time
.
Now
()
.
Add
(
this
.
writeDeadline
()))
...
...
@@ -712,7 +696,7 @@ func (this *Session) stop() {
}
}
func
(
this
*
S
ession
)
gc
()
{
func
(
this
*
s
ession
)
gc
()
{
this
.
lock
.
Lock
()
if
this
.
attrs
!=
nil
{
this
.
attrs
=
nil
...
...
@@ -720,14 +704,14 @@ func (this *Session) gc() {
this
.
wQ
=
nil
close
(
this
.
rQ
)
this
.
rQ
=
nil
this
.
iCon
n
.
close
((
int
)((
int64
)(
this
.
wait
)))
this
.
Connectio
n
.
close
((
int
)((
int64
)(
this
.
wait
)))
}
this
.
lock
.
Unlock
()
}
// this function will be invoked by NewSessionCallback(if return error is not nil) or (
S
ession)handleLoop automatically.
// this function will be invoked by NewSessionCallback(if return error is not nil) or (
s
ession)handleLoop automatically.
// It is goroutine-safe to be invoked many times.
func
(
this
*
S
ession
)
Close
()
{
func
(
this
*
s
ession
)
Close
()
{
this
.
stop
()
log
.
Info
(
"%s closed now, its current gr num %d"
,
this
.
sessionToken
(),
atomic
.
LoadInt32
(
&
(
this
.
grNum
)))
}
version.go
View file @
8abb0aa7
...
...
@@ -10,9 +10,9 @@
package
getty
const
(
Version
=
"0.
6.2
"
DATE
=
"201
6/11/19
"
Version
=
"0.
7.0
"
DATE
=
"201
7/02/03
"
GETTY_MAJOR
=
0
GETTY_MINOR
=
6
GETTY_BUILD
=
2
GETTY_MINOR
=
7
GETTY_BUILD
=
0
)
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