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
ada69206
Commit
ada69206
authored
Aug 21, 2021
by
dongjianhui03
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
rft(*): reorganize code with proper file name
parent
c8ebbd87
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
123 additions
and
124 deletions
+123
-124
main.go
benchmark/client/main.go
+1
-0
client.go
client.go
+7
-5
connection.go
connection.go
+35
-5
const.go
const.go
+12
-0
getty.go
getty.go
+8
-114
server.go
server.go
+22
-0
session.go
session.go
+38
-0
No files found.
benchmark/client/main.go
View file @
ada69206
...
...
@@ -31,6 +31,7 @@ import (
import
(
"github.com/dubbogo/gost/sync"
"github.com/montanaflynn/stats"
)
...
...
client.go
View file @
ada69206
...
...
@@ -35,7 +35,9 @@ import (
"github.com/dubbogo/gost/net"
gxsync
"github.com/dubbogo/gost/sync"
gxtime
"github.com/dubbogo/gost/time"
"github.com/gorilla/websocket"
perrors
"github.com/pkg/errors"
)
...
...
@@ -49,13 +51,13 @@ const (
var
(
sessionClientKey
=
"session-client-owner"
connectPingPackage
=
[]
byte
(
"connect-ping"
)
)
/////////////////////////////////////////
// getty tcp client
/////////////////////////////////////////
clientID
=
EndPointID
(
0
)
)
var
clientID
=
EndPointID
(
0
)
type
Client
interface
{
EndPoint
}
type
client
struct
{
ClientOptions
...
...
connection.go
View file @
ada69206
...
...
@@ -29,19 +29,49 @@ import (
import
(
"github.com/golang/snappy"
"github.com/gorilla/websocket"
perrors
"github.com/pkg/errors"
uatomic
"go.uber.org/atomic"
)
var
launchTime
=
time
.
Now
()
var
(
launchTime
=
time
.
Now
()
connID
uatomic
.
Uint32
)
// Connection wrap some connection params and operations
type
Connection
interface
{
ID
()
uint32
SetCompressType
(
CompressType
)
LocalAddr
()
string
RemoteAddr
()
string
incReadPkgNum
()
incWritePkgNum
()
// UpdateActive update session's active time
UpdateActive
()
// GetActive get session's active time
GetActive
()
time
.
Time
readTimeout
()
time
.
Duration
// SetReadTimeout sets deadline for the future read calls.
SetReadTimeout
(
time
.
Duration
)
writeTimeout
()
time
.
Duration
// SetWriteTimeout sets deadline for the future read calls.
SetWriteTimeout
(
time
.
Duration
)
send
(
interface
{})
(
int
,
error
)
// don't distinguish between tcp connection and websocket connection. Because
// gorilla/websocket/conn.go:(Conn)Close also invoke net.Conn.Close
close
(
int
)
// set related session
setSession
(
Session
)
}
// ///////////////////////////////////////
// getty connection
// ///////////////////////////////////////
var
connID
uatomic
.
Uint32
type
gettyConn
struct
{
id
uint32
compress
CompressType
...
...
@@ -103,7 +133,7 @@ func (c *gettyConn) setSession(ss Session) {
c
.
ss
=
ss
}
// Pls do not set read deadline for websocket connection. AlexStocks 20180310
//
SetReadTimeout
Pls do not set read deadline for websocket connection. AlexStocks 20180310
// gorilla/websocket/conn.go:NextReader will always fail when got a timeout error.
//
// Pls do not set read deadline when using compression. AlexStocks 20180314.
...
...
@@ -154,7 +184,7 @@ func newGettyTCPConn(conn net.Conn) *gettyTCPConn {
panic
(
"newGettyTCPConn(conn):@conn is nil"
)
}
var
localAddr
,
peerAddr
string
// check conn.LocalAddr or conn.Remo
et
Addr is nil to defeat panic on 2016/09/27
// check conn.LocalAddr or conn.Remo
te
Addr is nil to defeat panic on 2016/09/27
if
conn
.
LocalAddr
()
!=
nil
{
localAddr
=
conn
.
LocalAddr
()
.
String
()
}
...
...
const.go
View file @
ada69206
...
...
@@ -18,6 +18,7 @@
package
getty
import
(
"compress/flate"
"strconv"
)
...
...
@@ -67,3 +68,14 @@ func (x EndPointType) String() string {
return
strconv
.
Itoa
(
int
(
x
))
}
type
CompressType
int
const
(
CompressNone
CompressType
=
flate
.
NoCompression
// 0
CompressZip
=
flate
.
DefaultCompression
// -1
CompressBestSpeed
=
flate
.
BestSpeed
// 1
CompressBestCompression
=
flate
.
BestCompression
// 9
CompressHuffman
=
flate
.
HuffmanOnly
// -2
CompressSnappy
=
10
)
getty.go
View file @
ada69206
...
...
@@ -18,16 +18,17 @@
package
getty
import
(
"compress/flate"
"net"
"time"
)
import
(
gxsync
"github.com/dubbogo/gost/sync"
perrors
"github.com/pkg/errors"
)
var
(
ErrSessionClosed
=
perrors
.
New
(
"session Already Closed"
)
ErrSessionBlocked
=
perrors
.
New
(
"session Full Blocked"
)
ErrNullPeerAddr
=
perrors
.
New
(
"peer address is nil"
)
)
// 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.
...
...
@@ -92,91 +93,7 @@ type EventListener interface {
OnMessage
(
Session
,
interface
{})
}
type
CompressType
int
const
(
CompressNone
CompressType
=
flate
.
NoCompression
// 0
CompressZip
=
flate
.
DefaultCompression
// -1
CompressBestSpeed
=
flate
.
BestSpeed
// 1
CompressBestCompression
=
flate
.
BestCompression
// 9
CompressHuffman
=
flate
.
HuffmanOnly
// -2
CompressSnappy
=
10
)
// Connection wrap some connection params and operations
type
Connection
interface
{
ID
()
uint32
SetCompressType
(
CompressType
)
LocalAddr
()
string
RemoteAddr
()
string
incReadPkgNum
()
incWritePkgNum
()
// UpdateActive update session's active time
UpdateActive
()
// GetActive get session's active time
GetActive
()
time
.
Time
readTimeout
()
time
.
Duration
// SetReadTimeout sets deadline for the future read calls.
SetReadTimeout
(
time
.
Duration
)
writeTimeout
()
time
.
Duration
// SetWriteTimeout sets deadline for the future read calls.
SetWriteTimeout
(
time
.
Duration
)
send
(
interface
{})
(
int
,
error
)
// don't distinguish between tcp connection and websocket connection. Because
// gorilla/websocket/conn.go:(Conn)Close also invoke net.Conn.Close
close
(
int
)
// set related session
setSession
(
Session
)
}
/////////////////////////////////////////
// Session
/////////////////////////////////////////
var
(
ErrSessionClosed
=
perrors
.
New
(
"session Already Closed"
)
ErrSessionBlocked
=
perrors
.
New
(
"session Full Blocked"
)
ErrNullPeerAddr
=
perrors
.
New
(
"peer address is nil"
)
)
type
Session
interface
{
Connection
Reset
()
Conn
()
net
.
Conn
Stat
()
string
IsClosed
()
bool
// EndPoint get endpoint type
EndPoint
()
EndPoint
SetMaxMsgLen
(
int
)
SetName
(
string
)
SetEventListener
(
EventListener
)
SetPkgHandler
(
ReadWriter
)
SetReader
(
Reader
)
SetWriter
(
Writer
)
SetCronPeriod
(
int
)
SetWaitTime
(
time
.
Duration
)
GetAttribute
(
interface
{})
interface
{}
SetAttribute
(
interface
{},
interface
{})
RemoveAttribute
(
interface
{})
// WritePkg the Writer will invoke this function. Pls attention that if timeout is less than 0, WritePkg will send @pkg asap.
// for udp session, the first parameter should be UDPContext.
// totalBytesLength: @pkg stream bytes length after encoding @pkg.
// sendBytesLength: stream bytes length that sent out successfully.
// err: maybe it has illegal data, encoding error, or write out system error.
WritePkg
(
pkg
interface
{},
timeout
time
.
Duration
)
(
totalBytesLength
int
,
sendBytesLength
int
,
err
error
)
WriteBytes
([]
byte
)
(
int
,
error
)
WriteBytesArray
(
...
[]
byte
)
(
int
,
error
)
Close
()
}
/////////////////////////////////////////
// EndPoint
/////////////////////////////////////////
// EndPoint represents the identity of the client/server
type
EndPoint
interface
{
// ID get EndPoint ID
ID
()
EndPointID
...
...
@@ -191,26 +108,3 @@ type EndPoint interface {
// GetTaskPool get task pool implemented by dubbogo/gost
GetTaskPool
()
gxsync
.
GenericTaskPool
}
type
Client
interface
{
EndPoint
}
// Server interface
type
Server
interface
{
EndPoint
}
// StreamServer is like tcp/websocket/wss server
type
StreamServer
interface
{
Server
// Listener get the network listener
Listener
()
net
.
Listener
}
// PacketServer is like udp listen endpoint
type
PacketServer
interface
{
Server
// PacketConn get the network listener
PacketConn
()
net
.
PacketConn
}
server.go
View file @
ada69206
...
...
@@ -34,8 +34,11 @@ import (
gxnet
"github.com/dubbogo/gost/net"
gxsync
"github.com/dubbogo/gost/sync"
gxtime
"github.com/dubbogo/gost/time"
"github.com/gorilla/websocket"
perrors
"github.com/pkg/errors"
uatomic
"go.uber.org/atomic"
)
...
...
@@ -46,6 +49,25 @@ var (
serverID
uatomic
.
Int32
)
// Server interface
type
Server
interface
{
EndPoint
}
// StreamServer is like tcp/websocket/wss server
type
StreamServer
interface
{
Server
// Listener get the network listener
Listener
()
net
.
Listener
}
// PacketServer is like udp listen endpoint
type
PacketServer
interface
{
Server
// PacketConn get the network listener
PacketConn
()
net
.
PacketConn
}
type
server
struct
{
ServerOptions
...
...
session.go
View file @
ada69206
...
...
@@ -32,8 +32,11 @@ import (
gxbytes
"github.com/dubbogo/gost/bytes"
gxcontext
"github.com/dubbogo/gost/context"
gxtime
"github.com/dubbogo/gost/time"
"github.com/gorilla/websocket"
perrors
"github.com/pkg/errors"
uatomic
"go.uber.org/atomic"
)
...
...
@@ -60,6 +63,41 @@ func init() {
defaultTimerWheel
=
gxtime
.
GetDefaultTimerWheel
()
}
// Session wrap connection between the server and the client
type
Session
interface
{
Connection
Reset
()
Conn
()
net
.
Conn
Stat
()
string
IsClosed
()
bool
// EndPoint get endpoint type
EndPoint
()
EndPoint
SetMaxMsgLen
(
int
)
SetName
(
string
)
SetEventListener
(
EventListener
)
SetPkgHandler
(
ReadWriter
)
SetReader
(
Reader
)
SetWriter
(
Writer
)
SetCronPeriod
(
int
)
SetWaitTime
(
time
.
Duration
)
GetAttribute
(
interface
{})
interface
{}
SetAttribute
(
interface
{},
interface
{})
RemoveAttribute
(
interface
{})
// WritePkg the Writer will invoke this function. Pls attention that if timeout is less than 0, WritePkg will send @pkg asap.
// for udp session, the first parameter should be UDPContext.
// totalBytesLength: @pkg stream bytes length after encoding @pkg.
// sendBytesLength: stream bytes length that sent out successfully.
// err: maybe it has illegal data, encoding error, or write out system error.
WritePkg
(
pkg
interface
{},
timeout
time
.
Duration
)
(
totalBytesLength
int
,
sendBytesLength
int
,
err
error
)
WriteBytes
([]
byte
)
(
int
,
error
)
WriteBytesArray
(
...
[]
byte
)
(
int
,
error
)
Close
()
}
// getty base session
type
session
struct
{
name
string
...
...
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