Commit e7956e69 authored by AlexStocks's avatar AlexStocks

update udp echo server by getty v0.8.2

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