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
35056ac3
Commit
35056ac3
authored
Mar 17, 2018
by
AlexStocks
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add EndPointType
parent
e00f376f
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
157 additions
and
83 deletions
+157
-83
change_log.md
change_log.md
+4
-0
client.go
client.go
+51
-52
conn.go
conn.go
+10
-3
const.go
const.go
+65
-0
server.go
server.go
+26
-27
session.go
session.go
+1
-1
No files found.
change_log.md
View file @
35056ac3
...
@@ -14,6 +14,10 @@
...
@@ -14,6 +14,10 @@
## develop history ##
## develop history ##
---
---
-
2018/03/17
> improvement
*
add end point type
-
2018/03/16
-
2018/03/16
> bug fix
> bug fix
*
set maxMsgLen of UDPSession from zero to 4k
*
set maxMsgLen of UDPSession from zero to 4k
...
...
client.go
View file @
35056ac3
...
@@ -33,14 +33,6 @@ const (
...
@@ -33,14 +33,6 @@ const (
maxTimes
=
10
maxTimes
=
10
)
)
const
(
CONNECTED_UDP_CLIENT
=
1
UNCONNECTED_UDP_CLIENT
=
2
TCP_CLIENT
=
3
WS_CLIENT
=
4
WSS_CLIENT
=
5
)
/////////////////////////////////////////
/////////////////////////////////////////
// getty tcp client
// getty tcp client
/////////////////////////////////////////
/////////////////////////////////////////
...
@@ -48,7 +40,7 @@ const (
...
@@ -48,7 +40,7 @@ const (
type
Client
struct
{
type
Client
struct
{
// net
// net
sync
.
Mutex
sync
.
Mutex
typ
int
endPointType
EndPointType
number
int
number
int
interval
time
.
Duration
interval
time
.
Duration
addr
string
addr
string
...
@@ -70,15 +62,15 @@ type Client struct {
...
@@ -70,15 +62,15 @@ type Client struct {
// @connInterval is reconnect sleep interval when getty fails to connect the server.
// @connInterval is reconnect sleep interval when getty fails to connect the server.
// @serverAddr is server address.
// @serverAddr is server address.
func
NewTCPClient
(
connNum
int
,
connInterval
time
.
Duration
,
serverAddr
string
)
*
Client
{
func
NewTCPClient
(
connNum
int
,
connInterval
time
.
Duration
,
serverAddr
string
)
*
Client
{
if
connNum
<=
0
{
if
connNum
<=
0
||
serverAddr
==
""
{
connNum
=
1
panic
(
fmt
.
Sprintf
(
"@connNum:%d, @serverAddr:%s"
,
connNum
,
serverAddr
))
}
}
if
connInterval
<
defaultInterval
{
if
connInterval
<
defaultInterval
{
connInterval
=
defaultInterval
connInterval
=
defaultInterval
}
}
return
&
Client
{
return
&
Client
{
typ
:
TCP_CLIENT
,
endPointType
:
TCP_CLIENT
,
number
:
connNum
,
number
:
connNum
,
interval
:
connInterval
,
interval
:
connInterval
,
addr
:
serverAddr
,
addr
:
serverAddr
,
...
@@ -88,23 +80,26 @@ func NewTCPClient(connNum int, connInterval time.Duration, serverAddr string) *C
...
@@ -88,23 +80,26 @@ func NewTCPClient(connNum int, connInterval time.Duration, serverAddr string) *C
}
}
// NewUdpClient function builds a udp client
// NewUdpClient function builds a udp client
// @connNum is connection number. If this value is non-zero, getty will build
// @connNum is connection number.
// some connected udp clients.
//
// @connInterval is reconnect sleep interval when getty fails to connect the server.
// @connInterval is reconnect sleep interval when getty fails to connect the server.
// @serverAddr is server address.
// @serverAddr is server address.
if this value is none-nil-string, getty will build some connected udp clients.
func
NewUDPClient
(
connNum
int
,
connInterval
time
.
Duration
,
serverAddr
string
)
*
Client
{
func
NewUDPClient
(
connNum
int
,
connInterval
time
.
Duration
,
serverAddr
string
)
*
Client
{
var
typ
int
=
CONNECTED_UDP_CLIENT
var
endPointType
=
UNCONNECTED_UDP_CLIENT
if
len
(
serverAddr
)
!=
0
{
if
connNum
<=
0
{
if
connNum
<=
0
{
connNum
=
1
panic
(
fmt
.
Sprintf
(
"getty will build a preconected connection by @serverAddr:%s while @connNum is %d"
,
typ
=
UNCONNECTED_UDP_CLIENT
serverAddr
,
connNum
))
}
endPointType
=
CONNECTED_UDP_CLIENT
}
}
if
connInterval
<
defaultInterval
{
if
connInterval
<
defaultInterval
{
connInterval
=
defaultInterval
connInterval
=
defaultInterval
}
}
return
&
Client
{
return
&
Client
{
typ
:
typ
,
endPointType
:
endPointType
,
number
:
connNum
,
number
:
connNum
,
interval
:
connInterval
,
interval
:
connInterval
,
addr
:
serverAddr
,
addr
:
serverAddr
,
...
@@ -130,7 +125,7 @@ func NewWSClient(connNum int, connInterval time.Duration, serverAddr string) *Cl
...
@@ -130,7 +125,7 @@ func NewWSClient(connNum int, connInterval time.Duration, serverAddr string) *Cl
}
}
return
&
Client
{
return
&
Client
{
typ
:
WS_CLIENT
,
endPointType
:
WS_CLIENT
,
number
:
connNum
,
number
:
connNum
,
interval
:
connInterval
,
interval
:
connInterval
,
addr
:
serverAddr
,
addr
:
serverAddr
,
...
@@ -160,7 +155,7 @@ func NewWSSClient(connNum int, connInterval time.Duration, serverAddr string, ce
...
@@ -160,7 +155,7 @@ func NewWSSClient(connNum int, connInterval time.Duration, serverAddr string, ce
}
}
return
&
Client
{
return
&
Client
{
typ
:
WSS_CLIENT
,
endPointType
:
WSS_CLIENT
,
number
:
connNum
,
number
:
connNum
,
interval
:
connInterval
,
interval
:
connInterval
,
addr
:
serverAddr
,
addr
:
serverAddr
,
...
@@ -170,6 +165,10 @@ func NewWSSClient(connNum int, connInterval time.Duration, serverAddr string, ce
...
@@ -170,6 +165,10 @@ func NewWSSClient(connNum int, connInterval time.Duration, serverAddr string, ce
}
}
}
}
func
(
c
Client
)
Type
()
EndPointType
{
return
c
.
endPointType
}
func
(
c
*
Client
)
dialTCP
()
Session
{
func
(
c
*
Client
)
dialTCP
()
Session
{
var
(
var
(
err
error
err
error
...
@@ -207,7 +206,7 @@ func (c *Client) dialUDP() Session {
...
@@ -207,7 +206,7 @@ func (c *Client) dialUDP() Session {
if
c
.
IsClosed
()
{
if
c
.
IsClosed
()
{
return
nil
return
nil
}
}
if
UNCONNECTED_UDP_CLIENT
==
c
.
typ
{
if
UNCONNECTED_UDP_CLIENT
==
c
.
endPointType
{
conn
,
err
=
net
.
ListenUDP
(
"udp"
,
localAddr
)
conn
,
err
=
net
.
ListenUDP
(
"udp"
,
localAddr
)
}
else
{
}
else
{
conn
,
err
=
net
.
DialUDP
(
"udp"
,
localAddr
,
peerAddr
)
conn
,
err
=
net
.
DialUDP
(
"udp"
,
localAddr
,
peerAddr
)
...
@@ -335,7 +334,7 @@ func (c *Client) dialWSS() Session {
...
@@ -335,7 +334,7 @@ func (c *Client) dialWSS() Session {
}
}
func
(
c
*
Client
)
dial
()
Session
{
func
(
c
*
Client
)
dial
()
Session
{
switch
c
.
typ
{
switch
c
.
endPointType
{
case
TCP_CLIENT
:
case
TCP_CLIENT
:
return
c
.
dialTCP
()
return
c
.
dialTCP
()
case
UNCONNECTED_UDP_CLIENT
,
CONNECTED_UDP_CLIENT
:
case
UNCONNECTED_UDP_CLIENT
,
CONNECTED_UDP_CLIENT
:
...
@@ -424,7 +423,7 @@ func (c *Client) RunEventLoop(newSession NewSessionCallback) {
...
@@ -424,7 +423,7 @@ func (c *Client) RunEventLoop(newSession NewSessionCallback) {
}
}
times
=
0
times
=
0
c
.
connect
()
c
.
connect
()
if
c
.
typ
==
UNCONNECTED_UDP_CLIENT
||
c
.
typ
==
CONNECTED_UDP_CLIENT
{
if
c
.
endPointType
==
UNCONNECTED_UDP_CLIENT
||
c
.
endPointType
==
CONNECTED_UDP_CLIENT
{
break
break
}
}
// time.Sleep(c.interval) // build c.number connections asap
// time.Sleep(c.interval) // build c.number connections asap
...
...
conn.go
View file @
35056ac3
...
@@ -20,6 +20,7 @@ import (
...
@@ -20,6 +20,7 @@ import (
)
)
import
(
import
(
"github.com/AlexStocks/goext/net"
log
"github.com/AlexStocks/log4go"
log
"github.com/AlexStocks/log4go"
"github.com/golang/snappy"
"github.com/golang/snappy"
"github.com/gorilla/websocket"
"github.com/gorilla/websocket"
...
@@ -477,11 +478,17 @@ func (u *gettyUDPConn) Write(udpCtx interface{}) (int, error) {
...
@@ -477,11 +478,17 @@ func (u *gettyUDPConn) Write(udpCtx interface{}) (int, error) {
atomic
.
AddUint32
(
&
u
.
writeCount
,
(
uint32
)(
len
(
buf
)))
atomic
.
AddUint32
(
&
u
.
writeCount
,
(
uint32
)(
len
(
buf
)))
peerAddr
=
ctx
.
PeerAddr
peerAddr
=
ctx
.
PeerAddr
if
u
.
peerAddr
!=
nil
{
if
u
.
peerAddr
!=
nil
&&
peerAddr
!=
nil
{
peerAddr
=
u
.
peerAddr
if
!
gxnet
.
IsUDPAddrEqual
(
peerAddr
,
u
.
peerAddr
)
{
return
0
,
fmt
.
Errorf
(
"use of peerAddr %s different from preconnected udp connection address %s"
,
peerAddr
,
u
.
peerAddr
)
}
}
peerAddr
=
nil
}
peerAddr
=
nil
length
,
_
,
err
=
u
.
conn
.
WriteMsgUDP
(
buf
,
nil
,
peerAddr
)
length
,
_
,
err
=
u
.
conn
.
WriteMsgUDP
(
buf
,
nil
,
peerAddr
)
log
.
Debug
(
"now:%s, length:%d, err:%
#v"
,
currentTime
,
length
,
er
r
)
log
.
Debug
(
"now:%s, length:%d, err:%
s, peerAddr:%s, session.peer:%s"
,
currentTime
,
length
,
err
,
peerAddr
,
u
.
peerAdd
r
)
return
length
,
err
return
length
,
err
}
}
...
...
const.go
0 → 100644
View file @
35056ac3
/******************************************************
# DESC : const properties
# AUTHOR : Alex Stocks
# VERSION : 1.0
# LICENCE : Apache License 2.0
# EMAIL : alexstocks@foxmail.com
# MOD : 2018-03-17 16:54
# FILE : const.go
******************************************************/
package
getty
import
(
"strconv"
)
type
EndPointType
int32
const
(
CONNECTED_UDP_CLIENT
EndPointType
=
0
UNCONNECTED_UDP_CLIENT
EndPointType
=
1
TCP_CLIENT
EndPointType
=
2
WS_CLIENT
EndPointType
=
3
WSS_CLIENT
EndPointType
=
4
UDP_SERVER
EndPointType
=
6
TCP_SERVER
EndPointType
=
7
WS_SERVER
EndPointType
=
8
WSS_SERVER
EndPointType
=
9
)
var
EndPointType_name
=
map
[
int32
]
string
{
0
:
"CONNECTED_UDP_CLIENT"
,
1
:
"UNCONNECTED_UDP_CLIENT"
,
2
:
"TCP_CLIENT"
,
3
:
"WS_CLIENT"
,
4
:
"WSS_CLIENT"
,
6
:
"UDP_SERVER"
,
7
:
"TCP_SERVER"
,
8
:
"WS_SERVER"
,
9
:
"WSS_SERVER"
,
}
var
EndPointType_value
=
map
[
string
]
int32
{
"CONNECTED_UDP_CLIENT"
:
0
,
"UNCONNECTED_UDP_CLIENT"
:
1
,
"TCP_CLIENT"
:
2
,
"WS_CLIENT"
:
3
,
"WSS_CLIENT"
:
4
,
"UDP_SERVER"
:
6
,
"TCP_SERVER"
:
7
,
"WS_SERVER"
:
8
,
"WSS_SERVER"
:
9
,
}
func
(
x
EndPointType
)
String
()
string
{
s
,
ok
:=
EndPointType_name
[
int32
(
x
)]
if
ok
{
return
s
}
return
strconv
.
Itoa
(
int
(
x
))
}
server.go
View file @
35056ac3
...
@@ -34,20 +34,13 @@ var (
...
@@ -34,20 +34,13 @@ var (
serverFastFailTimeout
=
gxtime
.
TimeSecondDuration
(
1
)
serverFastFailTimeout
=
gxtime
.
TimeSecondDuration
(
1
)
)
)
const
(
UDP_SERVER
=
1
TCP_SERVER
=
2
WS_SERVER
=
3
WSS_SERVER
=
4
)
type
Server
struct
{
type
Server
struct
{
// net
// net
addr
string
addr
string
pktListener
net
.
PacketConn
pktListener
net
.
PacketConn
streamListener
net
.
Listener
streamListener
net
.
Listener
lock
sync
.
Mutex
// for server
lock
sync
.
Mutex
// for server
typ
int
endPointType
EndPointType
server
*
http
.
Server
// for ws or wss server
server
*
http
.
Server
// for ws or wss server
// websocket
// websocket
...
@@ -65,7 +58,7 @@ type Server struct {
...
@@ -65,7 +58,7 @@ type Server struct {
// @addr server listen address.
// @addr server listen address.
func
NewTCPServer
(
addr
string
)
*
Server
{
func
NewTCPServer
(
addr
string
)
*
Server
{
return
&
Server
{
return
&
Server
{
typ
:
TCP_SERVER
,
endPointType
:
TCP_SERVER
,
done
:
make
(
chan
gxsync
.
Empty
),
done
:
make
(
chan
gxsync
.
Empty
),
addr
:
addr
,
addr
:
addr
,
}
}
...
@@ -75,7 +68,7 @@ func NewTCPServer(addr string) *Server {
...
@@ -75,7 +68,7 @@ func NewTCPServer(addr string) *Server {
// @addr server listen address.
// @addr server listen address.
func
NewUDPPServer
(
addr
string
)
*
Server
{
func
NewUDPPServer
(
addr
string
)
*
Server
{
return
&
Server
{
return
&
Server
{
typ
:
UDP_SERVER
,
endPointType
:
UDP_SERVER
,
done
:
make
(
chan
gxsync
.
Empty
),
done
:
make
(
chan
gxsync
.
Empty
),
addr
:
addr
,
addr
:
addr
,
}
}
...
@@ -86,7 +79,7 @@ func NewUDPPServer(addr string) *Server {
...
@@ -86,7 +79,7 @@ func NewUDPPServer(addr string) *Server {
// @path: websocket request url path
// @path: websocket request url path
func
NewWSServer
(
addr
string
,
path
string
)
*
Server
{
func
NewWSServer
(
addr
string
,
path
string
)
*
Server
{
return
&
Server
{
return
&
Server
{
typ
:
WS_SERVER
,
endPointType
:
WS_SERVER
,
done
:
make
(
chan
gxsync
.
Empty
),
done
:
make
(
chan
gxsync
.
Empty
),
addr
:
addr
,
addr
:
addr
,
path
:
path
,
path
:
path
,
...
@@ -101,7 +94,7 @@ func NewWSServer(addr string, path string) *Server {
...
@@ -101,7 +94,7 @@ func NewWSServer(addr string, path string) *Server {
// @caCert: root certificate file. to verify the legitimacy of client. it can be nil.
// @caCert: root certificate file. to verify the legitimacy of client. it can be nil.
func
NewWSSServer
(
addr
,
path
,
cert
,
privateKey
,
caCert
string
)
*
Server
{
func
NewWSSServer
(
addr
,
path
,
cert
,
privateKey
,
caCert
string
)
*
Server
{
return
&
Server
{
return
&
Server
{
typ
:
WSS_SERVER
,
endPointType
:
WSS_SERVER
,
done
:
make
(
chan
gxsync
.
Empty
),
done
:
make
(
chan
gxsync
.
Empty
),
addr
:
addr
,
addr
:
addr
,
path
:
path
,
path
:
path
,
...
@@ -111,6 +104,10 @@ func NewWSSServer(addr, path, cert, privateKey, caCert string) *Server {
...
@@ -111,6 +104,10 @@ func NewWSSServer(addr, path, cert, privateKey, caCert string) *Server {
}
}
}
}
func
(
s
Server
)
Type
()
EndPointType
{
return
s
.
endPointType
}
func
(
s
*
Server
)
stop
()
{
func
(
s
*
Server
)
stop
()
{
var
(
var
(
err
error
err
error
...
@@ -202,7 +199,7 @@ func (s *Server) listenUDP() error {
...
@@ -202,7 +199,7 @@ func (s *Server) listenUDP() error {
// Listen announces on the local network address.
// Listen announces on the local network address.
func
(
s
*
Server
)
listen
()
error
{
func
(
s
*
Server
)
listen
()
error
{
switch
s
.
typ
{
switch
s
.
endPointType
{
case
TCP_SERVER
,
WS_SERVER
,
WSS_SERVER
:
case
TCP_SERVER
,
WS_SERVER
,
WSS_SERVER
:
return
s
.
listenTCP
()
return
s
.
listenTCP
()
case
UDP_SERVER
:
case
UDP_SERVER
:
...
@@ -445,7 +442,7 @@ func (s *Server) RunEventloop(newSession NewSessionCallback) {
...
@@ -445,7 +442,7 @@ func (s *Server) RunEventloop(newSession NewSessionCallback) {
panic
(
fmt
.
Errorf
(
"Server.listen() = error:%#v"
,
err
))
panic
(
fmt
.
Errorf
(
"Server.listen() = error:%#v"
,
err
))
}
}
switch
s
.
typ
{
switch
s
.
endPointType
{
case
TCP_SERVER
:
case
TCP_SERVER
:
s
.
runTcpEventloop
(
newSession
)
s
.
runTcpEventloop
(
newSession
)
case
UDP_SERVER
:
case
UDP_SERVER
:
...
@@ -454,6 +451,8 @@ func (s *Server) RunEventloop(newSession NewSessionCallback) {
...
@@ -454,6 +451,8 @@ func (s *Server) RunEventloop(newSession NewSessionCallback) {
s
.
runWSEventLoop
(
newSession
)
s
.
runWSEventLoop
(
newSession
)
case
WSS_SERVER
:
case
WSS_SERVER
:
s
.
runWSSEventLoop
(
newSession
)
s
.
runWSSEventLoop
(
newSession
)
default
:
panic
(
fmt
.
Sprintf
(
"illegal server type %s"
,
s
.
endPointType
.
String
()))
}
}
}
}
...
...
session.go
View file @
35056ac3
...
@@ -524,7 +524,7 @@ LOOP:
...
@@ -524,7 +524,7 @@ LOOP:
case
outPkg
=
<-
s
.
wQ
:
case
outPkg
=
<-
s
.
wQ
:
if
flag
{
if
flag
{
if
err
=
s
.
writer
.
Write
(
s
,
outPkg
);
err
!=
nil
{
if
err
=
s
.
writer
.
Write
(
s
,
outPkg
);
err
!=
nil
{
log
.
Error
(
"%s, [session.handleLoop] = error{%
#
v}"
,
s
.
sessionToken
(),
err
)
log
.
Error
(
"%s, [session.handleLoop] = error{%
+
v}"
,
s
.
sessionToken
(),
err
)
s
.
stop
()
s
.
stop
()
flag
=
false
flag
=
false
// break LOOP
// break LOOP
...
...
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