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
98dfd51c
Commit
98dfd51c
authored
Mar 18, 2018
by
AlexStocks
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
delete udp_server
parent
d1e123b3
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
45 additions
and
66 deletions
+45
-66
client.go
client.go
+11
-26
conn.go
conn.go
+2
-2
const.go
const.go
+18
-24
getty.go
getty.go
+2
-2
server.go
server.go
+6
-6
session.go
session.go
+6
-6
No files found.
client.go
View file @
98dfd51c
...
@@ -66,7 +66,7 @@ func newClient(t EndPointType, opts ...ClientOption) *client {
...
@@ -66,7 +66,7 @@ func newClient(t EndPointType, opts ...ClientOption) *client {
c
.
init
(
opts
...
)
c
.
init
(
opts
...
)
if
t
!=
UNCONNECTED_UDP_CLIENT
&&
c
.
number
<=
0
||
c
.
addr
==
""
{
if
c
.
number
<=
0
||
c
.
addr
==
""
{
panic
(
fmt
.
Sprintf
(
"@connNum:%d, @serverAddr:%s"
,
c
.
number
,
c
.
addr
))
panic
(
fmt
.
Sprintf
(
"@connNum:%d, @serverAddr:%s"
,
c
.
number
,
c
.
addr
))
}
}
...
@@ -80,20 +80,9 @@ func NewTCPClient(opts ...ClientOption) Client {
...
@@ -80,20 +80,9 @@ func NewTCPClient(opts ...ClientOption) Client {
return
newClient
(
TCP_CLIENT
,
opts
...
)
return
newClient
(
TCP_CLIENT
,
opts
...
)
}
}
// NewUdpClient function builds a udp client
// NewUdpClient function builds a
connected
udp client
func
NewUDPClient
(
opts
...
ClientOption
)
Client
{
func
NewUDPClient
(
opts
...
ClientOption
)
Client
{
c
:=
newClient
(
UNCONNECTED_UDP_CLIENT
,
opts
...
)
return
newClient
(
UDP_CLIENT
,
opts
...
)
if
len
(
c
.
addr
)
!=
0
{
if
c
.
number
<=
0
{
panic
(
fmt
.
Sprintf
(
"getty will build a preconected connection by @serverAddr:%s while @connNum is %d"
,
c
.
addr
,
c
.
number
))
}
c
.
endPointType
=
CONNECTED_UDP_CLIENT
}
return
c
}
}
// NewWsClient function builds a ws client.
// NewWsClient function builds a ws client.
...
@@ -121,7 +110,7 @@ func NewWSSClient(opts ...ClientOption) Client {
...
@@ -121,7 +110,7 @@ func NewWSSClient(opts ...ClientOption) Client {
return
c
return
c
}
}
func
(
c
client
)
Type
()
EndPointType
{
func
(
c
client
)
EndPoint
Type
()
EndPointType
{
return
c
.
endPointType
return
c
.
endPointType
}
}
...
@@ -162,13 +151,9 @@ func (c *client) dialUDP() Session {
...
@@ -162,13 +151,9 @@ func (c *client) dialUDP() Session {
if
c
.
IsClosed
()
{
if
c
.
IsClosed
()
{
return
nil
return
nil
}
}
if
UNCONNECTED_UDP_CLIENT
==
c
.
endPointType
{
conn
,
err
=
net
.
DialUDP
(
"udp"
,
localAddr
,
peerAddr
)
conn
,
err
=
net
.
ListenUDP
(
"udp"
,
localAddr
)
if
err
==
nil
&&
conn
.
LocalAddr
()
.
String
()
==
conn
.
RemoteAddr
()
.
String
()
{
}
else
{
err
=
errSelfConnect
conn
,
err
=
net
.
DialUDP
(
"udp"
,
localAddr
,
peerAddr
)
if
err
==
nil
&&
conn
.
LocalAddr
()
.
String
()
==
conn
.
RemoteAddr
()
.
String
()
{
err
=
errSelfConnect
}
}
}
if
err
==
nil
{
if
err
==
nil
{
return
newUDPSession
(
conn
,
c
.
endPointType
)
return
newUDPSession
(
conn
,
c
.
endPointType
)
...
@@ -292,7 +277,7 @@ func (c *client) dial() Session {
...
@@ -292,7 +277,7 @@ func (c *client) dial() Session {
switch
c
.
endPointType
{
switch
c
.
endPointType
{
case
TCP_CLIENT
:
case
TCP_CLIENT
:
return
c
.
dialTCP
()
return
c
.
dialTCP
()
case
U
NCONNECTED_UDP_CLIENT
,
CONNECTED_U
DP_CLIENT
:
case
UDP_CLIENT
:
return
c
.
dialUDP
()
return
c
.
dialUDP
()
case
WS_CLIENT
:
case
WS_CLIENT
:
return
c
.
dialWS
()
return
c
.
dialWS
()
...
@@ -378,9 +363,9 @@ func (c *client) RunEventLoop(newSession NewSessionCallback) {
...
@@ -378,9 +363,9 @@ func (c *client) RunEventLoop(newSession NewSessionCallback) {
}
}
times
=
0
times
=
0
c
.
connect
()
c
.
connect
()
if
c
.
endPointType
==
UNCONNECTED_UDP_CLIENT
||
c
.
endPointType
==
CONNECTED_
UDP_CLIENT
{
//if c.endPointType ==
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 @
98dfd51c
...
@@ -387,7 +387,7 @@ func (u *gettyUDPConn) read(p []byte) (int, *net.UDPAddr, error) {
...
@@ -387,7 +387,7 @@ func (u *gettyUDPConn) read(p []byte) (int, *net.UDPAddr, error) {
}
}
}
}
if
u
.
ss
.
Type
()
==
CONNECTED_
UDP_CLIENT
{
if
u
.
ss
.
EndPointType
()
==
UDP_CLIENT
{
length
,
err
=
u
.
conn
.
Read
(
p
)
length
,
err
=
u
.
conn
.
Read
(
p
)
}
else
{
}
else
{
length
,
addr
,
err
=
u
.
conn
.
ReadFromUDP
(
p
)
length
,
addr
,
err
=
u
.
conn
.
ReadFromUDP
(
p
)
...
@@ -418,7 +418,7 @@ func (u *gettyUDPConn) Write(udpCtx interface{}) (int, error) {
...
@@ -418,7 +418,7 @@ func (u *gettyUDPConn) Write(udpCtx interface{}) (int, error) {
if
buf
,
ok
=
ctx
.
Pkg
.
([]
byte
);
!
ok
{
if
buf
,
ok
=
ctx
.
Pkg
.
([]
byte
);
!
ok
{
return
0
,
fmt
.
Errorf
(
"illegal @udpCtx.Pkg{%#v} type"
,
udpCtx
)
return
0
,
fmt
.
Errorf
(
"illegal @udpCtx.Pkg{%#v} type"
,
udpCtx
)
}
}
if
u
.
ss
.
Type
()
==
UDP_SERVER
||
u
.
ss
.
Type
()
==
UNCONNECTED_
UDP_CLIENT
{
if
u
.
ss
.
EndPointType
()
==
UDP_CLIENT
{
peerAddr
=
ctx
.
PeerAddr
peerAddr
=
ctx
.
PeerAddr
if
peerAddr
==
nil
{
if
peerAddr
==
nil
{
return
0
,
ErrNullPeerAddr
return
0
,
ErrNullPeerAddr
...
...
const.go
View file @
98dfd51c
...
@@ -17,42 +17,36 @@ import (
...
@@ -17,42 +17,36 @@ import (
type
EndPointType
int32
type
EndPointType
int32
const
(
const
(
CONNECTED_UDP_CLIENT
EndPointType
=
0
UDP_ENDPOINT
EndPointType
=
0
UNCONNECTED_UDP_CLIENT
EndPointType
=
1
UDP_CLIENT
EndPointType
=
1
TCP_CLIENT
EndPointType
=
2
TCP_CLIENT
EndPointType
=
2
WS_CLIENT
EndPointType
=
3
WS_CLIENT
EndPointType
=
3
WSS_CLIENT
EndPointType
=
4
WSS_CLIENT
EndPointType
=
4
TCP_SERVER
EndPointType
=
7
UDP_SERVER
EndPointType
=
6
WS_SERVER
EndPointType
=
8
TCP_SERVER
EndPointType
=
7
WSS_SERVER
EndPointType
=
9
WS_SERVER
EndPointType
=
8
WSS_SERVER
EndPointType
=
9
)
)
var
EndPointType_name
=
map
[
int32
]
string
{
var
EndPointType_name
=
map
[
int32
]
string
{
0
:
"
CONNECTED_UDP_CLIE
NT"
,
0
:
"
UDP_ENDPOI
NT"
,
1
:
"U
NCONNECTED_U
DP_CLIENT"
,
1
:
"UDP_CLIENT"
,
2
:
"TCP_CLIENT"
,
2
:
"TCP_CLIENT"
,
3
:
"WS_CLIENT"
,
3
:
"WS_CLIENT"
,
4
:
"WSS_CLIENT"
,
4
:
"WSS_CLIENT"
,
6
:
"UDP_SERVER"
,
7
:
"TCP_SERVER"
,
7
:
"TCP_SERVER"
,
8
:
"WS_SERVER"
,
8
:
"WS_SERVER"
,
9
:
"WSS_SERVER"
,
9
:
"WSS_SERVER"
,
}
}
var
EndPointType_value
=
map
[
string
]
int32
{
var
EndPointType_value
=
map
[
string
]
int32
{
"CONNECTED_UDP_CLIENT"
:
0
,
"UDP_ENDPOINT"
:
0
,
"UNCONNECTED_UDP_CLIENT"
:
1
,
"UDP_CLIENT"
:
1
,
"TCP_CLIENT"
:
2
,
"TCP_CLIENT"
:
2
,
"WS_CLIENT"
:
3
,
"WS_CLIENT"
:
3
,
"WSS_CLIENT"
:
4
,
"WSS_CLIENT"
:
4
,
"TCP_SERVER"
:
7
,
"UDP_SERVER"
:
6
,
"WS_SERVER"
:
8
,
"TCP_SERVER"
:
7
,
"WSS_SERVER"
:
9
,
"WS_SERVER"
:
8
,
"WSS_SERVER"
:
9
,
}
}
func
(
x
EndPointType
)
String
()
string
{
func
(
x
EndPointType
)
String
()
string
{
...
...
getty.go
View file @
98dfd51c
...
@@ -126,7 +126,7 @@ type Session interface {
...
@@ -126,7 +126,7 @@ type Session interface {
Stat
()
string
Stat
()
string
IsClosed
()
bool
IsClosed
()
bool
// get endpoint type
// get endpoint type
Type
()
EndPointType
EndPoint
Type
()
EndPointType
SetMaxMsgLen
(
int
)
SetMaxMsgLen
(
int
)
SetName
(
string
)
SetName
(
string
)
...
@@ -157,7 +157,7 @@ type Session interface {
...
@@ -157,7 +157,7 @@ type Session interface {
type
EndPoint
interface
{
type
EndPoint
interface
{
// get endpoint type
// get endpoint type
Type
()
EndPointType
EndPoint
Type
()
EndPointType
// run event loop and serves client request.
// run event loop and serves client request.
RunEventLoop
(
newSession
NewSessionCallback
)
RunEventLoop
(
newSession
NewSessionCallback
)
// check the endpoint has been closed
// check the endpoint has been closed
...
...
server.go
View file @
98dfd51c
...
@@ -75,9 +75,9 @@ func NewTCPServer(opts ...ServerOption) Server {
...
@@ -75,9 +75,9 @@ func NewTCPServer(opts ...ServerOption) Server {
return
newServer
(
TCP_SERVER
,
opts
...
)
return
newServer
(
TCP_SERVER
,
opts
...
)
}
}
// NewUDP
Server
builds a unconnected udp server.
// NewUDP
EndPoint
builds a unconnected udp server.
func
NewUDPP
Server
(
opts
...
ServerOption
)
Server
{
func
NewUDPP
EndPoint
(
opts
...
ServerOption
)
Server
{
return
newServer
(
UDP_
SERVER
,
opts
...
)
return
newServer
(
UDP_
ENDPOINT
,
opts
...
)
}
}
// NewWSServer builds a websocket server.
// NewWSServer builds a websocket server.
...
@@ -97,7 +97,7 @@ func NewWSSServer(opts ...ServerOption) Server {
...
@@ -97,7 +97,7 @@ func NewWSSServer(opts ...ServerOption) Server {
return
s
return
s
}
}
func
(
s
server
)
Type
()
EndPointType
{
func
(
s
server
)
EndPoint
Type
()
EndPointType
{
return
s
.
endPointType
return
s
.
endPointType
}
}
...
@@ -195,7 +195,7 @@ func (s *server) listen() error {
...
@@ -195,7 +195,7 @@ func (s *server) listen() error {
switch
s
.
endPointType
{
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_
ENDPOINT
:
return
s
.
listenUDP
()
return
s
.
listenUDP
()
}
}
...
@@ -432,7 +432,7 @@ func (s *server) RunEventLoop(newSession NewSessionCallback) {
...
@@ -432,7 +432,7 @@ func (s *server) RunEventLoop(newSession NewSessionCallback) {
switch
s
.
endPointType
{
switch
s
.
endPointType
{
case
TCP_SERVER
:
case
TCP_SERVER
:
s
.
runTcpEventLoop
(
newSession
)
s
.
runTcpEventLoop
(
newSession
)
case
UDP_
SERVER
:
case
UDP_
ENDPOINT
:
s
.
runUDPEventLoop
(
newSession
)
s
.
runUDPEventLoop
(
newSession
)
case
WS_SERVER
:
case
WS_SERVER
:
s
.
runWSEventLoop
(
newSession
)
s
.
runWSEventLoop
(
newSession
)
...
...
session.go
View file @
98dfd51c
...
@@ -77,7 +77,7 @@ type session struct {
...
@@ -77,7 +77,7 @@ type session struct {
}
}
func
newSession
(
endPointType
EndPointType
,
conn
Connection
)
*
session
{
func
newSession
(
endPointType
EndPointType
,
conn
Connection
)
*
session
{
s
ession
:=
&
session
{
s
s
:=
&
session
{
name
:
defaultSessionName
,
name
:
defaultSessionName
,
endPointType
:
endPointType
,
endPointType
:
endPointType
,
maxMsgLen
:
maxReadBufLen
,
maxMsgLen
:
maxReadBufLen
,
...
@@ -88,11 +88,11 @@ func newSession(endPointType EndPointType, conn Connection) *session {
...
@@ -88,11 +88,11 @@ func newSession(endPointType EndPointType, conn Connection) *session {
attrs
:
gxcontext
.
NewValuesContext
(
nil
),
attrs
:
gxcontext
.
NewValuesContext
(
nil
),
}
}
s
ession
.
Connection
.
setSession
(
session
)
s
s
.
Connection
.
setSession
(
ss
)
s
ession
.
SetWriteTimeout
(
netIOTimeout
)
s
s
.
SetWriteTimeout
(
netIOTimeout
)
s
ession
.
SetReadTimeout
(
netIOTimeout
)
s
s
.
SetReadTimeout
(
netIOTimeout
)
return
s
ession
return
s
s
}
}
func
newTCPSession
(
conn
net
.
Conn
,
endPointType
EndPointType
)
Session
{
func
newTCPSession
(
conn
net
.
Conn
,
endPointType
EndPointType
)
Session
{
...
@@ -150,7 +150,7 @@ func (s *session) Conn() net.Conn {
...
@@ -150,7 +150,7 @@ func (s *session) Conn() net.Conn {
return
nil
return
nil
}
}
func
(
s
*
session
)
Type
()
EndPointType
{
func
(
s
*
session
)
EndPoint
Type
()
EndPointType
{
return
s
.
endPointType
return
s
.
endPointType
}
}
...
...
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