Commit 9ff81a94 authored by alexstocks's avatar alexstocks

modify version & echo package format

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