Commit 9ff81a94 authored by alexstocks's avatar alexstocks

modify version & echo package format

parent 9de187bb
......@@ -53,7 +53,7 @@ func (this *EchoClient) close() {
if client.gettyClient != nil {
for _, s := range this.sessions {
log.Info("close client session{%s, last active:%s, request number:%d}",
s.session.Stat(), s.active.String(), s.reqNum)
s.session.Stat(), s.session.GetActive().String(), s.reqNum)
s.session.Close()
}
client.gettyClient.Close()
......@@ -83,7 +83,7 @@ func (this *EchoClient) addSession(session *getty.Session) {
}
this.lock.Lock()
this.sessions = append(this.sessions, &clientEchoSession{session: session, active: time.Now()})
this.sessions = append(this.sessions, &clientEchoSession{session: session})
this.lock.Unlock()
}
......@@ -115,7 +115,6 @@ func (this *EchoClient) updateSession(session *getty.Session) {
for i, s := range this.sessions {
if s.session == session {
this.sessions[i].active = time.Now()
this.sessions[i].reqNum++
break
}
......@@ -154,7 +153,7 @@ func (this *EchoClient) heartbeat(session *getty.Session) {
// pkg.H.ServiceID = 0
pkg.H.Command = heartbeatCmd
pkg.B = echoHeartbeatRequestString
pkg.H.Len = (uint16)(len(pkg.B))
pkg.H.Len = (uint16)(len(pkg.B) + 1)
if err := session.WritePkg(&pkg); err != nil {
log.Warn("session.WritePkg(session{%s}, pkg{%s}) = error{%v}", session.Stat(), pkg, err)
......
......@@ -4,7 +4,7 @@
# LICENCE : Apache License 2.0
# EMAIL : alexstocks@foxmail.com
# MOD : 2016-08-22 17:44
# FILE : utils.go
# FILE : echo.go
******************************************************/
package main
......@@ -131,7 +131,7 @@ func (this *EchoPackage) Unmarshal(buf *bytes.Buffer) (int, error) {
if buf.Len() < (int)(this.H.Len) {
return 0, ErrNotEnoughSteam
}
if maxEchoStringLen < this.H.Len {
if maxEchoStringLen < this.H.Len-1 {
return 0, ErrTooLargePackage
}
......@@ -141,5 +141,5 @@ func (this *EchoPackage) Unmarshal(buf *bytes.Buffer) (int, error) {
}
this.B = (string)(buf.Next((int)(len)))
return (int)(this.H.Len) + 1 + echoPkgHeaderLen, nil
return (int)(this.H.Len) + echoPkgHeaderLen, nil
}
......@@ -29,7 +29,6 @@ var (
type clientEchoSession struct {
session *getty.Session
active time.Time
reqNum int32
}
......@@ -73,9 +72,9 @@ func (this *EchoMessageHandler) OnCron(session *getty.Session) {
log.Error("client.getClientSession(session{%s}) = error{%#v}", session.Stat(), err)
return
}
if conf.sessionTimeout.Nanoseconds() < time.Since(clientEchoSession.active).Nanoseconds() {
if conf.sessionTimeout.Nanoseconds() < time.Since(session.GetActive()).Nanoseconds() {
log.Warn("session{%s} timeout{%s}, reqNum{%d}",
session.Stat(), time.Since(clientEchoSession.active).String(), clientEchoSession.reqNum)
session.Stat(), time.Since(session.GetActive()).String(), clientEchoSession.reqNum)
client.removeSession(session)
return
}
......
......@@ -146,7 +146,7 @@ func echo() {
// pkg.H.ServiceID = 0
pkg.H.Command = echoCmd
pkg.B = conf.EchoString
pkg.H.Len = (uint16)(len(pkg.B))
pkg.H.Len = (uint16)(len(pkg.B)) + 1
if session := client.selectSession(); session != nil {
err := session.WritePkg(&pkg)
......
......@@ -10,5 +10,5 @@
package main
var (
Version = "0.3.07"
Version = "0.4.04"
)
......@@ -4,7 +4,7 @@
# LICENCE : Apache License 2.0
# EMAIL : alexstocks@foxmail.com
# MOD : 2016-08-22 17:44
# FILE : utils.go
# FILE : echo.go
******************************************************/
package main
......@@ -51,7 +51,6 @@ const (
echoHeartbeatRequestString = "ping"
echoHeartbeatResponseString = "pong"
echoMessage = "Hello, getty!"
)
var (
......@@ -133,7 +132,7 @@ func (this *EchoPackage) Unmarshal(buf *bytes.Buffer) (int, error) {
return 0, ErrNotEnoughSteam
}
// 防止恶意客户端把这个字段设置过大导致服务端死等或者服务端在准备对应的缓冲区时内存崩溃
if maxEchoStringLen < this.H.Len {
if maxEchoStringLen < this.H.Len-1 {
return 0, ErrTooLargePackage
}
......@@ -143,5 +142,5 @@ func (this *EchoPackage) Unmarshal(buf *bytes.Buffer) (int, error) {
}
this.B = (string)(buf.Next((int)(len)))
return (int)(this.H.Len) + 1 + echoPkgHeaderLen, nil
return (int)(this.H.Len) + echoPkgHeaderLen, nil
}
......@@ -40,7 +40,7 @@ func (this *HeartbeatHandler) Handle(session *getty.Session, pkg *EchoPackage) e
var rspPkg EchoPackage
rspPkg.H = pkg.H
rspPkg.B = echoHeartbeatResponseString
rspPkg.H.Len = uint16(len(rspPkg.B))
rspPkg.H.Len = uint16(len(rspPkg.B) + 1)
return session.WritePkg(&rspPkg)
}
......@@ -97,7 +97,7 @@ func (this *EchoMessageHandler) OnOpen(session *getty.Session) error {
log.Info("got session:%s", session.Stat())
this.rwlock.Lock()
this.sessionMap[session] = &clientEchoSession{session: session, active: time.Now()}
this.sessionMap[session] = &clientEchoSession{session: session}
this.rwlock.Unlock()
return nil
}
......@@ -140,13 +140,17 @@ func (this *EchoMessageHandler) OnMessage(session *getty.Session, pkg interface{
}
func (this *EchoMessageHandler) OnCron(session *getty.Session) {
var flag bool
var (
flag bool
active time.Time
)
this.rwlock.RLock()
if _, ok := this.sessionMap[session]; ok {
if conf.sessionTimeout.Nanoseconds() < time.Since(this.sessionMap[session].active).Nanoseconds() {
active = session.GetActive()
if conf.sessionTimeout.Nanoseconds() < time.Since(active).Nanoseconds() {
flag = true
log.Warn("session{%s} timeout{%s}, reqNum{%d}",
session.Stat(), time.Since(this.sessionMap[session].active).String(), this.sessionMap[session].reqNum)
session.Stat(), time.Since(active).String(), this.sessionMap[session].reqNum)
}
}
this.rwlock.RUnlock()
......
......@@ -10,5 +10,5 @@
package main
var (
Version = "0.3.07"
Version = "0.4.04"
)
......@@ -10,5 +10,5 @@
package main
var (
Version = "0.3.07"
Version = "0.4.04"
)
......@@ -7,7 +7,7 @@ AppName = "ECHO-CLIENT"
LocalHost = "127.0.0.1"
# server
ServerHost = "192.168.35.3"
ServerHost = "192.168.35.1"
ServerPort = 10000
ServerPath = "/echo"
ProfilePort = 10080
......
......@@ -10,5 +10,5 @@
package main
var (
Version = "0.3.07"
Version = "0.4.04"
)
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