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
79dd7fa9
Commit
79dd7fa9
authored
Feb 08, 2017
by
AlexStocks
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
session var -> ss
parent
5d4283aa
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
55 additions
and
53 deletions
+55
-53
change_log.md
change_log.md
+4
-2
client.go
client.go
+41
-41
server.go
server.go
+10
-10
No files found.
change_log.md
View file @
79dd7fa9
...
...
@@ -14,9 +14,11 @@
-
2017/02/08
> improvement
>
> 1 server shutdown in server.go:(Server) stop()
> 1 server shutdown in server.go:(Server) stop()
>
> 2 version: 0.7.01
> 2 session variable -> ss
>
> 3 version: 0.7.01
-
2017/02/03
> 1 Session struct -> session struct and add Session interface
...
...
client.go
View file @
79dd7fa9
...
...
@@ -43,7 +43,7 @@ type Client struct {
interval
time
.
Duration
addr
string
newSession
NewSessionCallback
s
essionMap
map
[
Session
]
gxsync
.
Empty
s
sMap
map
[
Session
]
gxsync
.
Empty
sync
.
Once
done
chan
gxsync
.
Empty
...
...
@@ -66,11 +66,11 @@ func NewClient(connNum int, connInterval time.Duration, serverAddr string) *Clie
}
return
&
Client
{
number
:
connNum
,
interval
:
connInterval
,
addr
:
serverAddr
,
s
essionMap
:
make
(
map
[
Session
]
gxsync
.
Empty
,
connNum
),
done
:
make
(
chan
gxsync
.
Empty
),
number
:
connNum
,
interval
:
connInterval
,
addr
:
serverAddr
,
s
sMap
:
make
(
map
[
Session
]
gxsync
.
Empty
,
connNum
),
done
:
make
(
chan
gxsync
.
Empty
),
}
}
...
...
@@ -88,12 +88,12 @@ func NewWSSClient(connNum int, connInterval time.Duration, serverAddr string, ce
}
return
&
Client
{
number
:
connNum
,
interval
:
connInterval
,
addr
:
serverAddr
,
s
essionMap
:
make
(
map
[
Session
]
gxsync
.
Empty
,
connNum
),
done
:
make
(
chan
gxsync
.
Empty
),
certFile
:
cert
,
number
:
connNum
,
interval
:
connInterval
,
addr
:
serverAddr
,
s
sMap
:
make
(
map
[
Session
]
gxsync
.
Empty
,
connNum
),
done
:
make
(
chan
gxsync
.
Empty
),
certFile
:
cert
,
}
}
...
...
@@ -123,10 +123,10 @@ func (c *Client) dialTCP() Session {
func
(
c
*
Client
)
dialWS
()
Session
{
var
(
err
error
dialer
websocket
.
Dialer
conn
*
websocket
.
Conn
s
ession
Session
err
error
dialer
websocket
.
Dialer
conn
*
websocket
.
Conn
s
s
Session
)
dialer
.
EnableCompression
=
true
...
...
@@ -139,12 +139,12 @@ func (c *Client) dialWS() Session {
err
=
errSelfConnect
}
if
err
==
nil
{
s
ession
=
NewWSSession
(
conn
)
if
s
ession
.
(
*
session
)
.
maxMsgLen
>
0
{
conn
.
SetReadLimit
(
int64
(
s
ession
.
(
*
session
)
.
maxMsgLen
))
s
s
=
NewWSSession
(
conn
)
if
s
s
.
(
*
session
)
.
maxMsgLen
>
0
{
conn
.
SetReadLimit
(
int64
(
s
s
.
(
*
session
)
.
maxMsgLen
))
}
return
s
ession
return
s
s
}
log
.
Info
(
"websocket.dialer.Dial(addr:%s) = error{%v}"
,
c
.
addr
,
err
)
...
...
@@ -160,7 +160,7 @@ func (c *Client) dialWSS() Session {
certPool
*
x509
.
CertPool
dialer
websocket
.
Dialer
conn
*
websocket
.
Conn
s
ession
Session
s
s
Session
)
dialer
.
EnableCompression
=
true
...
...
@@ -184,12 +184,12 @@ func (c *Client) dialWSS() Session {
err
=
errSelfConnect
}
if
err
==
nil
{
s
ession
=
NewWSSession
(
conn
)
if
s
ession
.
(
*
session
)
.
maxMsgLen
>
0
{
conn
.
SetReadLimit
(
int64
(
s
ession
.
(
*
session
)
.
maxMsgLen
))
s
s
=
NewWSSession
(
conn
)
if
s
s
.
(
*
session
)
.
maxMsgLen
>
0
{
conn
.
SetReadLimit
(
int64
(
s
s
.
(
*
session
)
.
maxMsgLen
))
}
return
s
ession
return
s
s
}
log
.
Info
(
"websocket.dialer.Dial(addr:%s) = error{%v}"
,
c
.
addr
,
err
)
...
...
@@ -208,16 +208,16 @@ func (c *Client) dial() Session {
return
c
.
dialTCP
()
}
func
(
c
*
Client
)
s
ession
Num
()
int
{
func
(
c
*
Client
)
s
s
Num
()
int
{
var
num
int
c
.
Lock
()
for
s
:=
range
c
.
s
ession
Map
{
for
s
:=
range
c
.
s
s
Map
{
if
s
.
IsClosed
()
{
delete
(
c
.
s
ession
Map
,
s
)
delete
(
c
.
s
s
Map
,
s
)
}
}
num
=
len
(
c
.
s
ession
Map
)
num
=
len
(
c
.
s
s
Map
)
c
.
Unlock
()
return
num
...
...
@@ -225,28 +225,28 @@ func (c *Client) sessionNum() int {
func
(
c
*
Client
)
connect
()
{
var
(
err
error
s
ession
Session
err
error
s
s
Session
)
for
{
s
ession
=
c
.
dial
()
if
s
ession
==
nil
{
s
s
=
c
.
dial
()
if
s
s
==
nil
{
// client has been closed
break
}
err
=
c
.
newSession
(
s
ession
)
err
=
c
.
newSession
(
s
s
)
if
err
==
nil
{
// s
ession
.RunEventLoop()
s
ession
.
(
*
session
)
.
run
()
// s
s
.RunEventLoop()
s
s
.
(
*
session
)
.
run
()
c
.
Lock
()
c
.
s
essionMap
[
session
]
=
gxsync
.
Empty
{}
c
.
s
sMap
[
ss
]
=
gxsync
.
Empty
{}
c
.
Unlock
()
break
}
// don't distinguish between tcp connection and websocket connection. Because
// gorilla/websocket/conn.go:(Conn)Close also invoke net.Conn.Close()
s
ession
.
Conn
()
.
Close
()
s
s
.
Conn
()
.
Close
()
}
}
...
...
@@ -270,7 +270,7 @@ func (c *Client) RunEventLoop(newSession NewSessionCallback) {
break
}
num
=
c
.
s
ession
Num
()
num
=
c
.
s
s
Num
()
// log.Info("current client connction number:%d", num)
if
max
<=
num
{
times
++
...
...
@@ -295,10 +295,10 @@ func (c *Client) stop() {
c
.
Once
.
Do
(
func
()
{
close
(
c
.
done
)
c
.
Lock
()
for
s
:=
range
c
.
s
ession
Map
{
for
s
:=
range
c
.
s
s
Map
{
s
.
Close
()
}
c
.
s
ession
Map
=
nil
c
.
s
s
Map
=
nil
c
.
Unlock
()
})
}
...
...
server.go
View file @
79dd7fa9
...
...
@@ -191,18 +191,18 @@ func (s *wsHandler) serveWSRequest(w http.ResponseWriter, r *http.Request) {
return
}
// conn.SetReadLimit(int64(handler.maxMsgLen))
s
ession
:=
NewWSSession
(
conn
)
err
=
s
.
newSession
(
s
ession
)
s
s
:=
NewWSSession
(
conn
)
err
=
s
.
newSession
(
s
s
)
if
err
!=
nil
{
conn
.
Close
()
log
.
Warn
(
"Server{%s}.newSession(s
ession{%#v}) = err {%#v}"
,
s
.
server
.
addr
,
session
,
err
)
log
.
Warn
(
"Server{%s}.newSession(s
s{%#v}) = err {%#v}"
,
s
.
server
.
addr
,
ss
,
err
)
return
}
if
s
ession
.
(
*
session
)
.
maxMsgLen
>
0
{
conn
.
SetReadLimit
(
int64
(
s
ession
.
(
*
session
)
.
maxMsgLen
))
if
s
s
.
(
*
session
)
.
maxMsgLen
>
0
{
conn
.
SetReadLimit
(
int64
(
s
s
.
(
*
session
)
.
maxMsgLen
))
}
// s
ession
.RunEventLoop()
s
ession
.
(
*
session
)
.
run
()
// s
s
.RunEventLoop()
s
s
.
(
*
session
)
.
run
()
}
// RunWSEventLoop serve websocket client request
...
...
@@ -291,14 +291,14 @@ func (s *Server) accept(newSession NewSessionCallback) (Session, error) {
return
nil
,
errSelfConnect
}
s
ession
:=
NewTCPSession
(
conn
)
err
=
newSession
(
s
ession
)
s
s
:=
NewTCPSession
(
conn
)
err
=
newSession
(
s
s
)
if
err
!=
nil
{
conn
.
Close
()
return
nil
,
err
}
return
s
ession
,
nil
return
s
s
,
nil
}
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