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
2fdf2ab2
Commit
2fdf2ab2
authored
Mar 16, 2018
by
AlexStocks
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
use udpContext for server
parent
bfb3fc6a
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
42 additions
and
17 deletions
+42
-17
handler.go
echo/tcp-echo/server/app/handler.go
+1
-0
handler.go
echo/udp-echo/server/app/handler.go
+27
-11
readwriter.go
echo/udp-echo/server/app/readwriter.go
+12
-4
handler.go
echo/ws-echo/server/app/handler.go
+1
-1
handler.go
echo/wss-echo/server/app/handler.go
+1
-1
No files found.
echo/tcp-echo/server/app/handler.go
View file @
2fdf2ab2
...
...
@@ -57,6 +57,7 @@ type MessageHandler struct{}
func
(
this
*
MessageHandler
)
Handle
(
session
getty
.
Session
,
pkg
*
EchoPackage
)
error
{
log
.
Debug
(
"get echo package{%s}"
,
pkg
)
// write echo message handle logic here.
return
session
.
WritePkg
(
pkg
,
WritePkgTimeout
)
}
...
...
echo/udp-echo/server/app/handler.go
View file @
2fdf2ab2
...
...
@@ -16,6 +16,7 @@ import (
)
import
(
"fmt"
"github.com/AlexStocks/getty"
log
"github.com/AlexStocks/log4go"
)
...
...
@@ -29,7 +30,7 @@ var (
)
type
PackageHandler
interface
{
Handle
(
getty
.
Session
,
*
EchoPackage
)
error
Handle
(
getty
.
Session
,
getty
.
UDPContext
)
error
}
////////////////////////////////////////////
...
...
@@ -38,15 +39,23 @@ type PackageHandler interface {
type
HeartbeatHandler
struct
{}
func
(
this
*
HeartbeatHandler
)
Handle
(
session
getty
.
Session
,
pkg
*
EchoPackage
)
error
{
log
.
Debug
(
"get echo heartbeat package{%s}"
,
pkg
)
func
(
this
*
HeartbeatHandler
)
Handle
(
session
getty
.
Session
,
ctx
getty
.
UDPContext
)
error
{
var
(
pkg
*
EchoPackage
ok
bool
)
log
.
Debug
(
"get echo heartbeat udp context{%#v}"
,
ctx
)
if
pkg
,
ok
=
ctx
.
Pkg
.
(
*
EchoPackage
);
!
ok
{
return
fmt
.
Errorf
(
"illegal @ctx.Pkg:%#v"
,
ctx
.
Pkg
)
}
var
rspPkg
EchoPackage
rspPkg
.
H
=
pkg
.
H
rspPkg
.
B
=
echoHeartbeatResponseString
rspPkg
.
H
.
Len
=
uint16
(
len
(
rspPkg
.
B
)
+
1
)
return
session
.
WritePkg
(
&
rspPkg
,
WritePkgTimeout
)
return
session
.
WritePkg
(
getty
.
UDPContext
{
Pkg
:
&
rspPkg
,
PeerAddr
:
ctx
.
PeerAddr
}
,
WritePkgTimeout
)
}
////////////////////////////////////////////
...
...
@@ -55,9 +64,10 @@ func (this *HeartbeatHandler) Handle(session getty.Session, pkg *EchoPackage) er
type
MessageHandler
struct
{}
func
(
this
*
MessageHandler
)
Handle
(
session
getty
.
Session
,
pkg
*
EchoPackage
)
error
{
log
.
Debug
(
"get echo package{%s}"
,
pkg
)
return
session
.
WritePkg
(
pkg
,
WritePkgTimeout
)
func
(
this
*
MessageHandler
)
Handle
(
session
getty
.
Session
,
ctx
getty
.
UDPContext
)
error
{
log
.
Debug
(
"get echo ctx{%#v}"
,
ctx
)
// write echo message handle logic here.
return
session
.
WritePkg
(
ctx
,
WritePkgTimeout
)
}
////////////////////////////////////////////
...
...
@@ -120,10 +130,16 @@ func (this *EchoMessageHandler) OnClose(session getty.Session) {
this
.
rwlock
.
Unlock
()
}
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 UDPContext{%#v}"
,
udpCtx
)
return
}
p
,
ok
:=
ctx
.
Pkg
.
(
*
EchoPackage
)
if
!
ok
{
log
.
Error
(
"illegal p
ackge{%#v}"
,
p
kg
)
log
.
Error
(
"illegal p
kg{%#v}"
,
ctx
.
P
kg
)
return
}
...
...
@@ -132,7 +148,7 @@ func (this *EchoMessageHandler) OnMessage(session getty.Session, pkg interface{}
log
.
Error
(
"illegal command{%d}"
,
p
.
H
.
Command
)
return
}
err
:=
handler
.
Handle
(
session
,
p
)
err
:=
handler
.
Handle
(
session
,
ctx
)
if
err
!=
nil
{
this
.
rwlock
.
Lock
()
if
_
,
ok
:=
this
.
sessionMap
[
session
];
ok
{
...
...
echo/udp-echo/server/app/readwriter.go
View file @
2fdf2ab2
...
...
@@ -16,6 +16,7 @@ import (
)
import
(
"fmt"
"github.com/AlexStocks/getty"
log
"github.com/AlexStocks/log4go"
)
...
...
@@ -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/ws-echo/server/app/handler.go
View file @
2fdf2ab2
...
...
@@ -37,7 +37,7 @@ type MessageHandler struct{}
func
(
this
*
MessageHandler
)
Handle
(
session
getty
.
Session
,
pkg
*
EchoPackage
)
error
{
log
.
Debug
(
"get echo package{%s}"
,
pkg
)
fmt
.
Printf
(
"get echo package{%s}
\n
"
,
pkg
)
// write echo message handle logic here.
return
session
.
WritePkg
(
pkg
,
conf
.
GettySessionParam
.
waitTimeout
)
}
...
...
echo/wss-echo/server/app/handler.go
View file @
2fdf2ab2
...
...
@@ -37,7 +37,7 @@ type MessageHandler struct{}
func
(
this
*
MessageHandler
)
Handle
(
session
getty
.
Session
,
pkg
*
EchoPackage
)
error
{
log
.
Debug
(
"get echo package{%s}"
,
pkg
)
fmt
.
Printf
(
"get echo package{%s}
\n
"
,
pkg
)
// write echo message handle logic here.
return
session
.
WritePkg
(
pkg
,
conf
.
GettySessionParam
.
waitTimeout
)
}
...
...
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