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
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 {
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
))
}
...
...
@@ -80,20 +80,9 @@ func NewTCPClient(opts ...ClientOption) Client {
return
newClient
(
TCP_CLIENT
,
opts
...
)
}
// NewUdpClient function builds a udp client
// NewUdpClient function builds a
connected
udp client
func
NewUDPClient
(
opts
...
ClientOption
)
Client
{
c
:=
newClient
(
UNCONNECTED_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
return
newClient
(
UDP_CLIENT
,
opts
...
)
}
// NewWsClient function builds a ws client.
...
...
@@ -121,7 +110,7 @@ func NewWSSClient(opts ...ClientOption) Client {
return
c
}
func
(
c
client
)
Type
()
EndPointType
{
func
(
c
client
)
EndPoint
Type
()
EndPointType
{
return
c
.
endPointType
}
...
...
@@ -162,13 +151,9 @@ func (c *client) dialUDP() Session {
if
c
.
IsClosed
()
{
return
nil
}
if
UNCONNECTED_UDP_CLIENT
==
c
.
endPointType
{
conn
,
err
=
net
.
ListenUDP
(
"udp"
,
localAddr
)
}
else
{
conn
,
err
=
net
.
DialUDP
(
"udp"
,
localAddr
,
peerAddr
)
if
err
==
nil
&&
conn
.
LocalAddr
()
.
String
()
==
conn
.
RemoteAddr
()
.
String
()
{
err
=
errSelfConnect
}
conn
,
err
=
net
.
DialUDP
(
"udp"
,
localAddr
,
peerAddr
)
if
err
==
nil
&&
conn
.
LocalAddr
()
.
String
()
==
conn
.
RemoteAddr
()
.
String
()
{
err
=
errSelfConnect
}
if
err
==
nil
{
return
newUDPSession
(
conn
,
c
.
endPointType
)
...
...
@@ -292,7 +277,7 @@ func (c *client) dial() Session {
switch
c
.
endPointType
{
case
TCP_CLIENT
:
return
c
.
dialTCP
()
case
U
NCONNECTED_UDP_CLIENT
,
CONNECTED_U
DP_CLIENT
:
case
UDP_CLIENT
:
return
c
.
dialUDP
()
case
WS_CLIENT
:
return
c
.
dialWS
()
...
...
@@ -378,9 +363,9 @@ func (c *client) RunEventLoop(newSession NewSessionCallback) {
}
times
=
0
c
.
connect
()
if
c
.
endPointType
==
UNCONNECTED_UDP_CLIENT
||
c
.
endPointType
==
CONNECTED_
UDP_CLIENT
{
break
}
//if c.endPointType ==
UDP_CLIENT {
//
break
//
}
// 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) {
}
}
if
u
.
ss
.
Type
()
==
CONNECTED_
UDP_CLIENT
{
if
u
.
ss
.
EndPointType
()
==
UDP_CLIENT
{
length
,
err
=
u
.
conn
.
Read
(
p
)
}
else
{
length
,
addr
,
err
=
u
.
conn
.
ReadFromUDP
(
p
)
...
...
@@ -418,7 +418,7 @@ func (u *gettyUDPConn) Write(udpCtx interface{}) (int, error) {
if
buf
,
ok
=
ctx
.
Pkg
.
([]
byte
);
!
ok
{
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
if
peerAddr
==
nil
{
return
0
,
ErrNullPeerAddr
...
...
const.go
View file @
98dfd51c
...
...
@@ -17,42 +17,36 @@ import (
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
UDP_ENDPOINT
EndPointType
=
0
UDP_CLIENT
EndPointType
=
1
TCP_CLIENT
EndPointType
=
2
WS_CLIENT
EndPointType
=
3
WSS_CLIENT
EndPointType
=
4
TCP_SERVER
EndPointType
=
7
WS_SERVER
EndPointType
=
8
WSS_SERVER
EndPointType
=
9
)
var
EndPointType_name
=
map
[
int32
]
string
{
0
:
"
CONNECTED_UDP_CLIE
NT"
,
1
:
"U
NCONNECTED_U
DP_CLIENT"
,
0
:
"
UDP_ENDPOI
NT"
,
1
:
"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
,
"UDP_ENDPOINT"
:
0
,
"UDP_CLIENT"
:
1
,
"TCP_CLIENT"
:
2
,
"WS_CLIENT"
:
3
,
"WSS_CLIENT"
:
4
,
"TCP_SERVER"
:
7
,
"WS_SERVER"
:
8
,
"WSS_SERVER"
:
9
,
}
func
(
x
EndPointType
)
String
()
string
{
...
...
getty.go
View file @
98dfd51c
...
...
@@ -126,7 +126,7 @@ type Session interface {
Stat
()
string
IsClosed
()
bool
// get endpoint type
Type
()
EndPointType
EndPoint
Type
()
EndPointType
SetMaxMsgLen
(
int
)
SetName
(
string
)
...
...
@@ -157,7 +157,7 @@ type Session interface {
type
EndPoint
interface
{
// get endpoint type
Type
()
EndPointType
EndPoint
Type
()
EndPointType
// run event loop and serves client request.
RunEventLoop
(
newSession
NewSessionCallback
)
// check the endpoint has been closed
...
...
server.go
View file @
98dfd51c
...
...
@@ -75,9 +75,9 @@ func NewTCPServer(opts ...ServerOption) Server {
return
newServer
(
TCP_SERVER
,
opts
...
)
}
// NewUDP
Server
builds a unconnected udp server.
func
NewUDPP
Server
(
opts
...
ServerOption
)
Server
{
return
newServer
(
UDP_
SERVER
,
opts
...
)
// NewUDP
EndPoint
builds a unconnected udp server.
func
NewUDPP
EndPoint
(
opts
...
ServerOption
)
Server
{
return
newServer
(
UDP_
ENDPOINT
,
opts
...
)
}
// NewWSServer builds a websocket server.
...
...
@@ -97,7 +97,7 @@ func NewWSSServer(opts ...ServerOption) Server {
return
s
}
func
(
s
server
)
Type
()
EndPointType
{
func
(
s
server
)
EndPoint
Type
()
EndPointType
{
return
s
.
endPointType
}
...
...
@@ -195,7 +195,7 @@ func (s *server) listen() error {
switch
s
.
endPointType
{
case
TCP_SERVER
,
WS_SERVER
,
WSS_SERVER
:
return
s
.
listenTCP
()
case
UDP_
SERVER
:
case
UDP_
ENDPOINT
:
return
s
.
listenUDP
()
}
...
...
@@ -432,7 +432,7 @@ func (s *server) RunEventLoop(newSession NewSessionCallback) {
switch
s
.
endPointType
{
case
TCP_SERVER
:
s
.
runTcpEventLoop
(
newSession
)
case
UDP_
SERVER
:
case
UDP_
ENDPOINT
:
s
.
runUDPEventLoop
(
newSession
)
case
WS_SERVER
:
s
.
runWSEventLoop
(
newSession
)
...
...
session.go
View file @
98dfd51c
...
...
@@ -77,7 +77,7 @@ type session struct {
}
func
newSession
(
endPointType
EndPointType
,
conn
Connection
)
*
session
{
s
ession
:=
&
session
{
s
s
:=
&
session
{
name
:
defaultSessionName
,
endPointType
:
endPointType
,
maxMsgLen
:
maxReadBufLen
,
...
...
@@ -88,11 +88,11 @@ func newSession(endPointType EndPointType, conn Connection) *session {
attrs
:
gxcontext
.
NewValuesContext
(
nil
),
}
s
ession
.
Connection
.
setSession
(
session
)
s
ession
.
SetWriteTimeout
(
netIOTimeout
)
s
ession
.
SetReadTimeout
(
netIOTimeout
)
s
s
.
Connection
.
setSession
(
ss
)
s
s
.
SetWriteTimeout
(
netIOTimeout
)
s
s
.
SetReadTimeout
(
netIOTimeout
)
return
s
ession
return
s
s
}
func
newTCPSession
(
conn
net
.
Conn
,
endPointType
EndPointType
)
Session
{
...
...
@@ -150,7 +150,7 @@ func (s *session) Conn() net.Conn {
return
nil
}
func
(
s
*
session
)
Type
()
EndPointType
{
func
(
s
*
session
)
EndPoint
Type
()
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