Commit ec48a400 authored by alexstocks's avatar alexstocks

fix bugs

parent 2161daec
......@@ -156,8 +156,10 @@ func (this *EchoClient) heartbeat(session *getty.Session) {
pkg.B = echoHeartbeatRequestString
pkg.H.Len = (uint16)(len(pkg.B))
if err := session.WritePkg(pkg); err != nil {
if err := session.WritePkg(&pkg); err != nil {
log.Warn("session.WritePkg(session{%s}, pkg{%s}) = error{%v}", session.Stat(), pkg, err)
session.Close()
this.removeSession(session)
}
}
......@@ -25,6 +25,7 @@ import (
import (
"github.com/AlexStocks/getty"
"github.com/AlexStocks/gocolor"
log "github.com/AlexStocks/log4go"
)
......@@ -159,6 +160,7 @@ func echo() {
if err != nil {
log.Warn("session.WritePkg(session{%s}, pkg{%s}) = error{%v}", session.Stat(), pkg, err)
session.Close()
client.removeSession(session)
}
}
}
......@@ -171,7 +173,14 @@ func test() {
time.Sleep(1e9)
}
var (
cost int64
counter getty.CountWatch
)
counter.Start()
for i := 0; i < conf.EchoTimes; i++ {
echo()
}
cost = counter.Count()
gocolor.Info("after loop %d times, echo cost %d ms", conf.EchoTimes, cost/1e6)
}
......@@ -142,5 +142,5 @@ func (this *EchoPackage) Unmarshal(buf *bytes.Buffer) (int, error) {
}
this.B = (string)(buf.Next((int)(len)))
return (int)(this.H.Len) + echoPkgHeaderLen, nil
return (int)(this.H.Len) + 1 + echoPkgHeaderLen, nil
}
......@@ -2,7 +2,6 @@
LocalHost = "127.0.0.1"
# ServerHost = "192.168.35.3"
ServerHost = "192.168.35.1"
ServerPort = 10000
ProfilePort = 10080
......@@ -18,4 +17,4 @@ HeartbeatPeriod = "10s"
SessionTimeout = "20s"
# 发送echo请求次数
EchoTimes = 100
EchoTimes = 100000
......@@ -25,7 +25,7 @@ var (
)
type PackageHandler interface {
Handle(*getty.Session, *EchoPackage)
Handle(*getty.Session, *EchoPackage) error
}
////////////////////////////////////////////
......@@ -34,7 +34,7 @@ type PackageHandler interface {
type HeartbeatHandler struct{}
func (this *HeartbeatHandler) Handle(session *getty.Session, pkg *EchoPackage) {
func (this *HeartbeatHandler) Handle(session *getty.Session, pkg *EchoPackage) error {
log.Debug("get echo heartbeat package{%s}", pkg)
var rspPkg EchoPackage
......@@ -42,7 +42,7 @@ func (this *HeartbeatHandler) Handle(session *getty.Session, pkg *EchoPackage) {
rspPkg.B = echoHeartbeatResponseString
rspPkg.H.Len = uint16(len(rspPkg.B))
session.WritePkg(&rspPkg)
return session.WritePkg(&rspPkg)
}
////////////////////////////////////////////
......@@ -51,9 +51,9 @@ func (this *HeartbeatHandler) Handle(session *getty.Session, pkg *EchoPackage) {
type MessageHandler struct{}
func (this *MessageHandler) Handle(session *getty.Session, pkg *EchoPackage) {
func (this *MessageHandler) Handle(session *getty.Session, pkg *EchoPackage) error {
log.Debug("get echo package{%s}", pkg)
session.WritePkg(pkg)
return session.WritePkg(pkg)
}
////////////////////////////////////////////
......@@ -128,13 +128,15 @@ func (this *EchoMessageHandler) OnMessage(session *getty.Session, pkg interface{
log.Error("illegal command{%d}", p.H.Command)
return
}
handler.Handle(session, p)
this.rwlock.Lock()
if _, ok := this.sessionMap[session]; ok {
this.sessionMap[session].active = time.Now()
this.sessionMap[session].reqNum++
err := handler.Handle(session, p)
if err != nil {
this.rwlock.Lock()
if _, ok := this.sessionMap[session]; ok {
this.sessionMap[session].active = time.Now()
this.sessionMap[session].reqNum++
}
this.rwlock.Unlock()
}
this.rwlock.Unlock()
}
func (this *EchoMessageHandler) OnCron(session *getty.Session) {
......
......@@ -142,5 +142,5 @@ func (this *EchoPackage) Unmarshal(buf *bytes.Buffer) (int, error) {
}
this.B = (string)(buf.Next((int)(len)))
return (int)(this.H.Len) + echoPkgHeaderLen, nil
return (int)(this.H.Len) + 1 + echoPkgHeaderLen, nil
}
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