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
4f0a9de6
Unverified
Commit
4f0a9de6
authored
Apr 27, 2020
by
Xin.Zh
Committed by
GitHub
Apr 27, 2020
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #38 from divebomb/master
Add: StreamServer & PacketServer
parents
076a8069
5cc2deb9
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
89 additions
and
22 deletions
+89
-22
config.go
demo/hello/tcp/config.go
+1
-1
getty.go
getty.go
+13
-0
go.mod
go.mod
+3
-5
go.sum
go.sum
+40
-0
server.go
server.go
+25
-10
session.go
session.go
+7
-6
No files found.
demo/hello/tcp/config.go
View file @
4f0a9de6
...
...
@@ -27,7 +27,7 @@ var (
)
func
InitialSession
(
session
getty
.
Session
)
(
err
error
)
{
session
.
SetCompressType
(
getty
.
CompressZip
)
//
session.SetCompressType(getty.CompressZip)
tcpConn
,
ok
:=
session
.
Conn
()
.
(
*
net
.
TCPConn
)
if
!
ok
{
...
...
getty.go
View file @
4f0a9de6
...
...
@@ -195,8 +195,21 @@ type Client interface {
EndPoint
}
// Server interface
type
Server
interface
{
EndPoint
}
// StreamServer is like tcp/websocket/wss server
type
StreamServer
interface
{
Server
// get the network listener
Listener
()
net
.
Listener
}
// PacketServer is like udp listen endpoint
type
PacketServer
interface
{
Server
// get the network listener
PacketConn
()
net
.
PacketConn
}
go.mod
View file @
4f0a9de6
...
...
@@ -3,13 +3,11 @@ module github.com/dubbogo/getty
require (
github.com/dubbogo/gost v1.9.0
github.com/golang/snappy v0.0.1
github.com/gorilla/websocket v1.4.
0
github.com/gorilla/websocket v1.4.
2
github.com/juju/errors v0.0.0-20190930114154-d42613fe1ab9
github.com/pkg/errors v0.
8
.1
github.com/pkg/errors v0.
9
.1
github.com/stretchr/testify v1.5.1
go.uber.org/atomic v1.4.0 // indirect
go.uber.org/multierr v1.1.0 // indirect
go.uber.org/zap v1.10.0
go.uber.org/zap v1.15.0
)
go 1.13
go.sum
View file @
4f0a9de6
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dubbogo/gost v1.5.2 h1:ri/03971hdpnn3QeCU+4UZgnRNGDXLDGDucR/iozZm8=
github.com/dubbogo/gost v1.5.2/go.mod h1:pPTjVyoJan3aPxBPNUX0ADkXjPibLo+/Ib0/fADXSG8=
github.com/dubbogo/gost v1.9.0 h1:UT+dWwvLyJiDotxJERO75jB3Yxgsdy10KztR5ycxRAk=
github.com/dubbogo/gost v1.9.0/go.mod h1:pPTjVyoJan3aPxBPNUX0ADkXjPibLo+/Ib0/fADXSG8=
github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4=
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q=
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/juju/errors v0.0.0-20190930114154-d42613fe1ab9 h1:hJix6idebFclqlfZCHE7EUX7uqLCyb70nHNHH1XKGBg=
github.com/juju/errors v0.0.0-20190930114154-d42613fe1ab9/go.mod h1:W54LbzXuIE0boCoNJfwqpmkKJ1O4TCTZMetAt6jGk7Q=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
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/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.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/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
go.uber.org/atomic v1.4.0 h1:cxzIVoETapQEqDhQu3QfnvXAV4AlzcvUCxkVUFw3+EU=
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/atomic v1.6.0 h1:Ezj3JGmsOnG1MoRWQkPBsKLe9DwWD9QeXzTRzzldNVk=
go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
go.uber.org/multierr v1.1.0 h1:HoEmRHQPVSqub6w2z2d2EOVs2fjyFRGyofhKuyDq0QI=
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
go.uber.org/multierr v1.5.0 h1:KCa4XfM8CWFCpxXRGok+Q0SS/0XBhMDbHHGABQLvD2A=
go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU=
go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA=
go.uber.org/zap v1.10.0 h1:ORx85nbTijNz8ljznvCMR1ZBIPKFn3jQrag10X2AsuM=
go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
go.uber.org/zap v1.15.0 h1:ZZCA22JRF2gQE5FoNmhmrf7jeJJ2uhqDUNRYKm8dvmM=
go.uber.org/zap v1.15.0/go.mod h1:Mb2vm2krFEG5DV0W9qcHBYFtp/Wku1cvYaqPsS/WYfc=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
server.go
View file @
4f0a9de6
...
...
@@ -24,8 +24,9 @@ import (
)
import
(
"github.com/dubbogo/gost/net"
gxnet
"github.com/dubbogo/gost/net"
"github.com/gorilla/websocket"
perrors
"github.com/pkg/errors"
)
...
...
@@ -173,6 +174,7 @@ func (s *server) listenTCP() error {
}
s
.
streamListener
=
streamListener
s
.
addr
=
s
.
streamListener
.
Addr
()
.
String
()
return
nil
}
...
...
@@ -201,6 +203,7 @@ func (s *server) listenUDP() error {
}
s
.
pktListener
=
pktListener
s
.
addr
=
s
.
pktListener
.
LocalAddr
()
.
String
()
return
nil
}
...
...
@@ -256,7 +259,7 @@ func (s *server) runTcpEventLoop(newSession NewSessionCallback) {
}
client
,
err
=
s
.
accept
(
newSession
)
if
err
!=
nil
{
if
netErr
,
ok
:=
err
.
(
net
.
Error
);
ok
&&
netErr
.
Temporary
()
{
if
netErr
,
ok
:=
perrors
.
Cause
(
err
)
.
(
net
.
Error
);
ok
&&
netErr
.
Temporary
()
{
if
delay
==
0
{
delay
=
5
*
time
.
Millisecond
}
else
{
...
...
@@ -277,15 +280,23 @@ func (s *server) runTcpEventLoop(newSession NewSessionCallback) {
}
func
(
s
*
server
)
runUDPEventLoop
(
newSession
NewSessionCallback
)
{
var
(
ss
Session
)
s
.
wg
.
Add
(
1
)
go
func
()
{
defer
s
.
wg
.
Done
()
var
(
err
error
conn
*
net
.
UDPConn
ss
Session
)
ss
=
newUDPSession
(
s
.
pktListener
.
(
*
net
.
UDPConn
),
s
)
if
err
:=
newSession
(
ss
);
err
!=
nil
{
panic
(
err
.
Error
())
}
ss
.
(
*
session
)
.
run
()
conn
=
s
.
pktListener
.
(
*
net
.
UDPConn
)
ss
=
newUDPSession
(
conn
,
s
)
if
err
=
newSession
(
ss
);
err
!=
nil
{
conn
.
Close
()
panic
(
err
.
Error
())
}
ss
.
(
*
session
)
.
run
()
}()
}
type
wsHandler
struct
{
...
...
@@ -460,6 +471,10 @@ func (s *server) Listener() net.Listener {
return
s
.
streamListener
}
func
(
s
*
server
)
PacketConn
()
net
.
PacketConn
{
return
s
.
pktListener
}
func
(
s
*
server
)
Close
()
{
s
.
stop
()
s
.
wg
.
Wait
()
...
...
session.go
View file @
4f0a9de6
...
...
@@ -26,6 +26,7 @@ import (
gxcontext
"github.com/dubbogo/gost/context"
gxsync
"github.com/dubbogo/gost/sync"
gxtime
"github.com/dubbogo/gost/time"
"github.com/gorilla/websocket"
perrors
"github.com/pkg/errors"
)
...
...
@@ -479,7 +480,7 @@ func (s *session) WriteBytesArray(pkgs ...[]byte) error {
}
if
err
=
s
.
WriteBytes
(
arr
);
err
!=
nil
{
return
jerrors
.
Trace
(
err
)
return
perrors
.
WithStack
(
err
)
}
num
:=
len
(
pkgs
)
-
1
...
...
@@ -579,7 +580,7 @@ LOOP:
if
udpFlag
||
wsFlag
{
err
=
s
.
WritePkg
(
outPkg
,
0
)
if
err
!=
nil
{
log
.
Errorf
(
"%s, [session.handleLoop] = error:%+v"
,
s
.
sessionToken
(),
err
)
log
.
Errorf
(
"%s, [session.handleLoop] = error:%+v"
,
s
.
sessionToken
(),
perrors
.
WithStack
(
err
)
)
s
.
stop
()
// break LOOP
flag
=
false
...
...
@@ -592,7 +593,7 @@ LOOP:
for
idx
:=
0
;
idx
<
maxIovecNum
;
idx
++
{
pkgBytes
,
err
=
s
.
writer
.
Write
(
s
,
outPkg
)
if
err
!=
nil
{
log
.
Errorf
(
"%s, [session.handleLoop] = error:%+v"
,
s
.
sessionToken
(),
jerrors
.
Error
Stack
(
err
))
log
.
Errorf
(
"%s, [session.handleLoop] = error:%+v"
,
s
.
sessionToken
(),
perrors
.
With
Stack
(
err
))
s
.
stop
()
// break LOOP
flag
=
false
...
...
@@ -813,7 +814,7 @@ func (s *session) handleUDPPackage() error {
if
int
(
s
.
maxMsgLen
<<
1
)
<
bufLen
{
maxBufLen
=
int
(
s
.
maxMsgLen
<<
1
)
}
bufp
=
gxbytes
.
GetBytes
(
maxBufLen
)
//make([]byte, maxBufLen)
bufp
=
gxbytes
.
GetBytes
(
maxBufLen
)
defer
gxbytes
.
PutBytes
(
bufp
)
buf
=
*
bufp
for
{
...
...
@@ -827,8 +828,8 @@ func (s *session) handleUDPPackage() error {
continue
}
if
err
!=
nil
{
log
.
Errorf
(
"%s, [session.handleUDPPackage] = len
{%d}, error{%+s}
"
,
s
.
sessionToken
(),
bufLen
,
err
)
log
.
Errorf
(
"%s, [session.handleUDPPackage] = len
:%d, error:%+v
"
,
s
.
sessionToken
(),
bufLen
,
perrors
.
WithStack
(
err
)
)
err
=
perrors
.
Wrapf
(
err
,
"conn.read()"
)
break
}
...
...
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