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
2ec937f9
Commit
2ec937f9
authored
Mar 16, 2018
by
AlexStocks
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
use UDPPackage for udp client
parent
2fdf2ab2
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
46 additions
and
14 deletions
+46
-14
client.go
echo/udp-echo/client/app/client.go
+10
-3
config.go
echo/udp-echo/client/app/config.go
+5
-0
handler.go
echo/udp-echo/client/app/handler.go
+8
-3
main.go
echo/udp-echo/client/app/main.go
+10
-3
readwriter.go
echo/udp-echo/client/app/readwriter.go
+12
-4
readwriter.go
echo/udp-echo/server/app/readwriter.go
+1
-1
No files found.
echo/udp-echo/client/app/client.go
View file @
2ec937f9
...
...
@@ -146,7 +146,11 @@ func (this *EchoClient) getClientEchoSession(session getty.Session) (clientEchoS
}
func
(
this
*
EchoClient
)
heartbeat
(
session
getty
.
Session
)
{
var
pkg
EchoPackage
var
(
pkg
EchoPackage
ctx
getty
.
UDPContext
)
pkg
.
H
.
Magic
=
echoPkgMagic
pkg
.
H
.
LogID
=
(
uint32
)(
src
.
Int63
())
pkg
.
H
.
Sequence
=
atomic
.
AddUint32
(
&
reqID
,
1
)
...
...
@@ -155,8 +159,11 @@ func (this *EchoClient) heartbeat(session getty.Session) {
pkg
.
B
=
echoHeartbeatRequestString
pkg
.
H
.
Len
=
(
uint16
)(
len
(
pkg
.
B
)
+
1
)
if
err
:=
session
.
WritePkg
(
&
pkg
,
WritePkgTimeout
);
err
!=
nil
{
log
.
Warn
(
"session.WritePkg(session{%s}, pkg{%s}) = error{%v}"
,
session
.
Stat
(),
pkg
,
err
)
ctx
.
Pkg
=
&
pkg
ctx
.
PeerAddr
=
&
(
conf
.
serverAddr
)
if
err
:=
session
.
WritePkg
(
ctx
,
WritePkgTimeout
);
err
!=
nil
{
log
.
Warn
(
"session.WritePkg(session{%s}, context{%#v}) = error{%v}"
,
session
.
Stat
(),
ctx
,
err
)
session
.
Close
()
this
.
removeSession
(
session
)
...
...
echo/udp-echo/client/app/config.go
View file @
2ec937f9
...
...
@@ -19,6 +19,7 @@ import (
import
(
log
"github.com/AlexStocks/log4go"
config
"github.com/koding/multiconfig"
"net"
)
const
(
...
...
@@ -56,6 +57,7 @@ type (
ServerHost
string
`default:"127.0.0.1"`
ServerPort
int
`default:"10000"`
ProfilePort
int
`default:"10086"`
serverAddr
net
.
UDPAddr
// session pool
ConnectionNum
int
`default:"16"`
...
...
@@ -101,6 +103,9 @@ func initConf() {
}
conf
=
new
(
Config
)
config
.
MustLoadWithPath
(
confFile
,
conf
)
conf
.
serverAddr
=
net
.
UDPAddr
{
IP
:
net
.
ParseIP
(
conf
.
ServerHost
),
Port
:
conf
.
ServerPort
}
conf
.
connectInterval
,
err
=
time
.
ParseDuration
(
conf
.
ConnectInterval
)
if
err
!=
nil
{
panic
(
fmt
.
Sprintf
(
"time.ParseDuration(ConnectionInterval{%#v}) = error{%v}"
,
conf
.
ConnectInterval
,
err
))
...
...
echo/udp-echo/client/app/handler.go
View file @
2ec937f9
...
...
@@ -55,10 +55,15 @@ func (this *EchoMessageHandler) OnClose(session getty.Session) {
client
.
removeSession
(
session
)
}
func
(
this
*
EchoMessageHandler
)
OnMessage
(
session
getty
.
Session
,
pkg
interface
{})
{
p
,
ok
:=
pkg
.
(
*
EchoPackage
)
func
(
this
*
EchoMessageHandler
)
OnMessage
(
session
getty
.
Session
,
udpCtx
interface
{})
{
ctx
,
ok
:=
udpCtx
.
(
getty
.
UDPContext
)
if
!
ok
{
log
.
Error
(
"illegal packge{%#v}"
,
pkg
)
log
.
Error
(
"illegal UDPContext{%#v}"
,
udpCtx
)
return
}
p
,
ok
:=
ctx
.
Pkg
.
(
*
EchoPackage
)
if
!
ok
{
log
.
Error
(
"illegal packge{%#v}"
,
ctx
.
Pkg
)
return
}
...
...
echo/udp-echo/client/app/main.go
View file @
2ec937f9
...
...
@@ -147,7 +147,11 @@ func initSignal() {
}
func
echo
()
{
var
pkg
EchoPackage
var
(
pkg
EchoPackage
ctx
getty
.
UDPContext
)
pkg
.
H
.
Magic
=
echoPkgMagic
pkg
.
H
.
LogID
=
(
uint32
)(
src
.
Int63
())
pkg
.
H
.
Sequence
=
atomic
.
AddUint32
(
&
reqID
,
1
)
...
...
@@ -156,10 +160,13 @@ func echo() {
pkg
.
B
=
conf
.
EchoString
pkg
.
H
.
Len
=
(
uint16
)(
len
(
pkg
.
B
))
+
1
ctx
.
Pkg
=
&
pkg
ctx
.
PeerAddr
=
&
(
conf
.
serverAddr
)
if
session
:=
client
.
selectSession
();
session
!=
nil
{
err
:=
session
.
WritePkg
(
&
pkg
,
WritePkgTimeout
)
err
:=
session
.
WritePkg
(
ctx
,
WritePkgTimeout
)
if
err
!=
nil
{
log
.
Warn
(
"session.WritePkg(session{%s},
pkg{%s}) = error{%v}"
,
session
.
Stat
(),
pkg
,
err
)
log
.
Warn
(
"session.WritePkg(session{%s},
UDPContext{%#v}) = error{%v}"
,
session
.
Stat
(),
ctx
,
err
)
session
.
Close
()
client
.
removeSession
(
session
)
}
...
...
echo/udp-echo/client/app/readwriter.go
View file @
2ec937f9
...
...
@@ -12,6 +12,7 @@ package main
import
(
"bytes"
"errors"
"fmt"
"time"
)
...
...
@@ -48,18 +49,25 @@ func (this *EchoPackageHandler) Read(ss getty.Session, data []byte) (interface{}
return
&
pkg
,
len
,
nil
}
func
(
this
*
EchoPackageHandler
)
Write
(
ss
getty
.
Session
,
pkg
interface
{})
error
{
func
(
this
*
EchoPackageHandler
)
Write
(
ss
getty
.
Session
,
udpCtx
interface
{})
error
{
var
(
ok
bool
err
error
startTime
time
.
Time
echoPkg
*
EchoPackage
buf
*
bytes
.
Buffer
ctx
getty
.
UDPContext
)
ctx
,
ok
=
udpCtx
.
(
getty
.
UDPContext
)
if
!
ok
{
log
.
Error
(
"illegal UDPContext{%#v}"
,
udpCtx
)
return
fmt
.
Errorf
(
"illegal @udpCtx{%#v}"
,
udpCtx
)
}
startTime
=
time
.
Now
()
if
echoPkg
,
ok
=
p
kg
.
(
*
EchoPackage
);
!
ok
{
log
.
Error
(
"illegal pkg:%+v
\n
"
,
p
kg
)
if
echoPkg
,
ok
=
ctx
.
P
kg
.
(
*
EchoPackage
);
!
ok
{
log
.
Error
(
"illegal pkg:%+v
\n
"
,
ctx
.
P
kg
)
return
errors
.
New
(
"invalid echo package!"
)
}
...
...
@@ -69,7 +77,7 @@ func (this *EchoPackageHandler) Write(ss getty.Session, pkg interface{}) error {
return
err
}
err
=
ss
.
WriteBytes
(
buf
.
Bytes
()
)
_
,
err
=
ss
.
Write
(
getty
.
UDPContext
{
Pkg
:
buf
.
Bytes
(),
PeerAddr
:
ctx
.
PeerAddr
}
)
log
.
Info
(
"WriteEchoPkgTimeMs = %s"
,
time
.
Since
(
startTime
)
.
String
())
return
err
...
...
echo/udp-echo/server/app/readwriter.go
View file @
2ec937f9
...
...
@@ -12,11 +12,11 @@ package main
import
(
"bytes"
"errors"
"fmt"
"time"
)
import
(
"fmt"
"github.com/AlexStocks/getty"
log
"github.com/AlexStocks/log4go"
)
...
...
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