Commit e7956e69 authored by AlexStocks's avatar AlexStocks

update udp echo server by getty v0.8.2

parent 9a265ed1
...@@ -10,6 +10,10 @@ ...@@ -10,6 +10,10 @@
## develop history ## ## develop history ##
--- ---
- 2018/03/17
> improvement
* use getty v0.8.2
- 2018/03/16 - 2018/03/16
> bug fix > bug fix
* forbid close udp session in OnCron * forbid close udp session in OnCron
......
...@@ -19,6 +19,7 @@ import ( ...@@ -19,6 +19,7 @@ import (
import ( import (
"github.com/AlexStocks/getty" "github.com/AlexStocks/getty"
log "github.com/AlexStocks/log4go" log "github.com/AlexStocks/log4go"
"net"
) )
var ( var (
...@@ -38,6 +39,7 @@ type EchoClient struct { ...@@ -38,6 +39,7 @@ type EchoClient struct {
lock sync.RWMutex lock sync.RWMutex
sessions []*clientEchoSession sessions []*clientEchoSession
gettyClient *getty.Client gettyClient *getty.Client
serverAddr net.UDPAddr
} }
func (this *EchoClient) isAvailable() bool { func (this *EchoClient) isAvailable() bool {
...@@ -160,7 +162,7 @@ func (this *EchoClient) heartbeat(session getty.Session) { ...@@ -160,7 +162,7 @@ func (this *EchoClient) heartbeat(session getty.Session) {
pkg.H.Len = (uint16)(len(pkg.B) + 1) pkg.H.Len = (uint16)(len(pkg.B) + 1)
ctx.Pkg = &pkg ctx.Pkg = &pkg
ctx.PeerAddr = &(conf.serverAddr) ctx.PeerAddr = &(this.serverAddr)
if err := session.WritePkg(ctx, WritePkgTimeout); err != nil { if err := session.WritePkg(ctx, WritePkgTimeout); err != nil {
log.Warn("session.WritePkg(session{%s}, context{%#v}) = error{%v}", session.Stat(), ctx, err) log.Warn("session.WritePkg(session{%s}, context{%#v}) = error{%v}", session.Stat(), ctx, err)
......
...@@ -19,7 +19,6 @@ import ( ...@@ -19,7 +19,6 @@ import (
import ( import (
log "github.com/AlexStocks/log4go" log "github.com/AlexStocks/log4go"
config "github.com/koding/multiconfig" config "github.com/koding/multiconfig"
"net"
) )
const ( const (
...@@ -57,7 +56,6 @@ type ( ...@@ -57,7 +56,6 @@ type (
ServerHost string `default:"127.0.0.1"` ServerHost string `default:"127.0.0.1"`
ServerPort int `default:"10000"` ServerPort int `default:"10000"`
ProfilePort int `default:"10086"` ProfilePort int `default:"10086"`
serverAddr net.UDPAddr
// session pool // session pool
ConnectionNum int `default:"16"` ConnectionNum int `default:"16"`
...@@ -104,8 +102,6 @@ func initConf() { ...@@ -104,8 +102,6 @@ func initConf() {
conf = new(Config) conf = new(Config)
config.MustLoadWithPath(confFile, conf) config.MustLoadWithPath(confFile, conf)
conf.serverAddr = net.UDPAddr{IP: net.ParseIP(conf.ServerHost), Port: conf.ServerPort}
conf.connectInterval, err = time.ParseDuration(conf.ConnectInterval) conf.connectInterval, err = time.ParseDuration(conf.ConnectInterval)
if err != nil { if err != nil {
panic(fmt.Sprintf("time.ParseDuration(ConnectionInterval{%#v}) = error{%v}", conf.ConnectInterval, err)) panic(fmt.Sprintf("time.ParseDuration(ConnectionInterval{%#v}) = error{%v}", conf.ConnectInterval, err))
......
...@@ -80,8 +80,8 @@ func (this *EchoMessageHandler) OnCron(session getty.Session) { ...@@ -80,8 +80,8 @@ func (this *EchoMessageHandler) OnCron(session getty.Session) {
if conf.sessionTimeout.Nanoseconds() < time.Since(session.GetActive()).Nanoseconds() { if conf.sessionTimeout.Nanoseconds() < time.Since(session.GetActive()).Nanoseconds() {
log.Warn("session{%s} timeout{%s}, reqNum{%d}", log.Warn("session{%s} timeout{%s}, reqNum{%d}",
session.Stat(), time.Since(session.GetActive()).String(), clientEchoSession.reqNum) session.Stat(), time.Since(session.GetActive()).String(), clientEchoSession.reqNum)
client.removeSession(session) // client.removeSession(session)
return // return
} }
client.heartbeat(session) client.heartbeat(session)
......
...@@ -111,6 +111,7 @@ func initClient() { ...@@ -111,6 +111,7 @@ func initClient() {
gxnet.HostAddress(conf.ServerHost, conf.ServerPort), gxnet.HostAddress(conf.ServerHost, conf.ServerPort),
) )
client.gettyClient.RunEventLoop(newSession) client.gettyClient.RunEventLoop(newSession)
client.serverAddr = net.UDPAddr{IP: net.ParseIP(conf.ServerHost), Port: conf.ServerPort}
} }
func uninitClient() { func uninitClient() {
...@@ -146,7 +147,7 @@ func initSignal() { ...@@ -146,7 +147,7 @@ func initSignal() {
} }
} }
func echo() { func echo(serverAddr *net.UDPAddr) {
var ( var (
pkg EchoPackage pkg EchoPackage
ctx getty.UDPContext ctx getty.UDPContext
...@@ -161,7 +162,7 @@ func echo() { ...@@ -161,7 +162,7 @@ func echo() {
pkg.H.Len = (uint16)(len(pkg.B)) + 1 pkg.H.Len = (uint16)(len(pkg.B)) + 1
ctx.Pkg = &pkg ctx.Pkg = &pkg
ctx.PeerAddr = &(conf.serverAddr) ctx.PeerAddr = serverAddr
if session := client.selectSession(); session != nil { if session := client.selectSession(); session != nil {
err := session.WritePkg(ctx, WritePkgTimeout) err := session.WritePkg(ctx, WritePkgTimeout)
...@@ -174,6 +175,12 @@ func echo() { ...@@ -174,6 +175,12 @@ func echo() {
} }
func test() { func test() {
var (
cost int64
serverAddr net.UDPAddr
counter gxtime.CountWatch
)
for { for {
if client.isAvailable() { if client.isAvailable() {
break break
...@@ -181,13 +188,11 @@ func test() { ...@@ -181,13 +188,11 @@ func test() {
time.Sleep(1e6) time.Sleep(1e6)
} }
var ( serverAddr = net.UDPAddr{IP: net.ParseIP(conf.ServerHost), Port: conf.ServerPort}
cost int64
counter gxtime.CountWatch
)
counter.Start() counter.Start()
for i := 0; i < conf.EchoTimes; i++ { for i := 0; i < conf.EchoTimes; i++ {
echo() echo(&serverAddr)
} }
cost = counter.Count() cost = counter.Count()
log.Info("after loop %d times, echo cost %d ms", conf.EchoTimes, cost/1e6) log.Info("after loop %d times, echo cost %d ms", conf.EchoTimes, cost/1e6)
......
...@@ -21,8 +21,7 @@ import ( ...@@ -21,8 +21,7 @@ import (
log "github.com/AlexStocks/log4go" log "github.com/AlexStocks/log4go"
) )
type EchoPackageHandler struct { type EchoPackageHandler struct{}
}
func NewEchoPackageHandler() *EchoPackageHandler { func NewEchoPackageHandler() *EchoPackageHandler {
return &EchoPackageHandler{} return &EchoPackageHandler{}
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
<!-- level is (:?FINEST|FINE|DEBUG|TRACE|INFO|WARNING|ERROR) --> <!-- level is (:?FINEST|FINE|DEBUG|TRACE|INFO|WARNING|ERROR) -->
<level>DEBUG</level> <level>DEBUG</level>
</filter> </filter>
<filter enabled="false"> <filter enabled="true">
<tag>debug_file</tag> <tag>debug_file</tag>
<type>file</type> <type>file</type>
<level>DEBUG</level> <level>DEBUG</level>
......
...@@ -39,7 +39,7 @@ var ( ...@@ -39,7 +39,7 @@ var (
) )
var ( var (
serverList []*getty.Server serverList []getty.Server
) )
func main() { func main() {
...@@ -110,7 +110,7 @@ func initServer() { ...@@ -110,7 +110,7 @@ func initServer() {
var ( var (
addr string addr string
portList []string portList []string
server *getty.Server server getty.Server
) )
// if *host == "" { // if *host == "" {
...@@ -128,9 +128,11 @@ func initServer() { ...@@ -128,9 +128,11 @@ func initServer() {
} }
for _, port := range portList { for _, port := range portList {
addr = gxnet.HostAddress2(conf.Host, port) addr = gxnet.HostAddress2(conf.Host, port)
server = getty.NewUDPPServer(addr) server = getty.NewUDPPServer(
getty.WithLocalAddress(addr),
)
// run server // run server
server.RunEventloop(newSession) server.RunEventLoop(newSession)
log.Debug("server bind addr{%s} ok!", addr) log.Debug("server bind addr{%s} ok!", addr)
serverList = append(serverList, server) serverList = append(serverList, server)
} }
......
...@@ -10,5 +10,5 @@ ...@@ -10,5 +10,5 @@
package main package main
var ( var (
Version = "0.8.1" Version = "0.8.2"
) )
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment