Skip to content

Commit

Permalink
调整日志打印
Browse files Browse the repository at this point in the history
  • Loading branch information
tangtao committed Jul 16, 2019
1 parent c701a92 commit 7db88c8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 25 deletions.
55 changes: 31 additions & 24 deletions src/server_tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ import (
)

type ServerTCP struct {
tcpListener net.Listener
ctx *ServerContext
exitChan chan int
waitGroup WaitGroupWrapper
pro Protocol
addr string
RealAddr string // 真实连接地址
tcpListener net.Listener
ctx *ServerContext
exitChan chan int
waitGroup WaitGroupWrapper
pro Protocol
addr string
RealAddr string // 真实连接地址
handshakeFnc HandshakeFnc
}

// 握手方法
type HandshakeFnc func(conn net.Conn, timeout time.Duration) (interface{},Conn,error)
type HandshakeFnc func(conn net.Conn, timeout time.Duration) (interface{}, Conn, error)

func NewServerTCP(addr string,handshakeFnc HandshakeFnc) *ServerTCP {
return &ServerTCP{addr:addr,handshakeFnc:handshakeFnc}
func NewServerTCP(addr string, handshakeFnc HandshakeFnc) *ServerTCP {
return &ServerTCP{addr: addr, handshakeFnc: handshakeFnc, exitChan: make(chan int, 0)}
}

func (s *ServerTCP) Start(context *ServerContext) error {
Expand All @@ -37,17 +37,18 @@ func (s *ServerTCP) Start(context *ServerContext) error {
return err
}
s.RealAddr = s.tcpListener.Addr().String()
log.Info("TCP Server 启动 ",zap.String("addr",s.tcpListener.Addr().String()))
s.info("启动 ", zap.String("addr", s.tcpListener.Addr().String()))
s.waitGroup.Wrap(s.connLoop)
return nil
}

func (s *ServerTCP) Stop() error {
err := s.tcpListener.Close()
if err!=nil {
if err != nil {
return err
}
s.exitChan <- 1
s.debug("退出")
return nil
}

Expand Down Expand Up @@ -76,26 +77,26 @@ func (s *ServerTCP) connLoop() {
}
}
exit:
fmt.Println("退出Server")
s.debug("TCP Server 停止监听")
}

func (s *ServerTCP) handleConn(cn net.Conn) {
func (s *ServerTCP) handleConn(cn net.Conn) {

var tgoConn Conn
var packet interface{}
var err error
if s.handshakeFnc!=nil {
packet,tgoConn,err = s.handshakeFnc(cn,10*time.Second)
if err!=nil {
log.Debug("握手失败!",zap.Error(err))
if s.handshakeFnc != nil {
packet, tgoConn, err = s.handshakeFnc(cn, 10*time.Second)
if err != nil {
log.Debug("握手失败!", zap.Error(err))
cn.Close()
return
}
}else{
tgoConn = NewStatefulConn(cn,s.ctx.tg.GenClientId(),nil)
packet,err = s.pro.DecodePacket(tgoConn)
if err!=nil {
fmt.Println("解码消息失败!-> ",err.Error())
} else {
tgoConn = NewStatefulConn(cn, s.ctx.tg.GenClientId(), nil)
packet, err = s.pro.DecodePacket(tgoConn)
if err != nil {
fmt.Println("解码消息失败!-> ", err.Error())
cn.Close()
return
}
Expand All @@ -106,7 +107,13 @@ func (s *ServerTCP) handleConn(cn net.Conn) {
return
}
pCtx := NewDefaultPacketContext(packet)
s.ctx.Accept(NewDefaultContext(pCtx,tgoConn))
s.ctx.Accept(NewDefaultContext(pCtx, tgoConn))
}

func (s *ServerTCP) debug(msg string, fields ...zap.Field) {
log.Debug(fmt.Sprintf("【TCP Server】%s", msg), fields...)
}

func (s *ServerTCP) info(msg string, fields ...zap.Field) {
log.Info(fmt.Sprintf("【TCP Server】%s", msg), fields...)
}
12 changes: 11 additions & 1 deletion src/tgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package tgo

import (
"fmt"
"github.com/tgo-team/tgo/src/log"
"go.uber.org/zap"
"sync/atomic"
)

Expand Down Expand Up @@ -50,7 +52,7 @@ func (t *TGO) Stop() {
panic(err)
}
}
fmt.Println("TGO stopped")
t.debug("退出")
}

// UseServer 指定server服务器
Expand Down Expand Up @@ -115,3 +117,11 @@ func (t *TGO) matchHandler(context Context) {
}
}
}

func (t *TGO) debug(msg string, fields ...zap.Field) {
log.Debug(fmt.Sprintf("【TGO】%s", msg), fields...)
}

func (t *TGO) info(msg string, fields ...zap.Field) {
log.Info(fmt.Sprintf("【TGO】%s", msg), fields...)
}

0 comments on commit 7db88c8

Please sign in to comment.