Browse Source

code optimization

master
Roger 5 years ago
parent
commit
0f669b5323
  1. 11
      GNUmakefile
  2. 2
      examples/zinx_version_ex/ZinxV0.10Test/Client0.go
  3. 2
      examples/zinx_version_ex/ZinxV0.10Test/Client1.go
  4. 2
      examples/zinx_version_ex/ZinxV0.11Test/Client0.go
  5. 2
      examples/zinx_version_ex/ZinxV0.11Test/Client1.go
  6. 3
      examples/zinx_version_ex/ZinxV0.1Test/Server.go
  7. 3
      examples/zinx_version_ex/ZinxV0.2Test/Server.go
  8. 1
      examples/zinx_version_ex/ZinxV0.3Test/Server.go
  9. 2
      examples/zinx_version_ex/ZinxV0.5Test/Client.go
  10. 2
      examples/zinx_version_ex/ZinxV0.6Test-V0.7Test/Client0.go
  11. 2
      examples/zinx_version_ex/ZinxV0.6Test-V0.7Test/Client1.go
  12. 2
      examples/zinx_version_ex/ZinxV0.8Test/Client0.go
  13. 2
      examples/zinx_version_ex/ZinxV0.8Test/Client1.go
  14. 2
      examples/zinx_version_ex/ZinxV0.9Test/Client0.go
  15. 2
      examples/zinx_version_ex/ZinxV0.9Test/Client1.go
  16. 4
      examples/zinx_version_ex/datapackDemo/Client.go
  17. 2
      examples/zinx_version_ex/datapackDemo/Server.go
  18. 2
      examples/zinx_version_ex/protoDemo/main.go
  19. 4
      utils/globalobj.go
  20. 3
      ziface/iconnection.go
  21. 1
      ziface/irequest.go
  22. 2
      zinx_app_demo/mmo_game/api/move.go
  23. 2
      zinx_app_demo/mmo_game/api/world_chat.go
  24. 2
      zinx_app_demo/mmo_game/client_AI_robot.go
  25. 4
      zinx_app_demo/mmo_game/core/player.go
  26. 3
      zlog/zlogger.go
  27. 8
      znet/connection.go
  28. 3
      znet/connmanager.go
  29. 3
      znet/msghandler.go
  30. 2
      znet/request.go
  31. 28
      znet/server.go
  32. 40
      znet/server_test.go
  33. 2
      ztimer/delayfunc.go
  34. 1
      ztimer/timer_test.go
  35. 3
      ztimer/timerscheduler.go
  36. 4
      ztimer/timerscheduler_test.go
  37. 2
      ztimer/timewheel.go
  38. 1
      ztimer/timewheel_test.go

11
GNUmakefile

@ -0,0 +1,11 @@
# gofmt格式化
# run in terminal:
# make fmt
# win系统中,在git bash中如果出现make包没有找到
# 管理员运行git bash,运行以下命令
# choco install make
GOFMT_FILES?=$$(find . -name '*.go' |grep -v vendor)
fmt:
gofmt -w $(GOFMT_FILES)

2
examples/zinx_version_ex/ZinxV0.10Test/Client0.go

@ -2,10 +2,10 @@ package main
import (
"fmt"
"github.com/aceld/zinx/znet"
"io"
"net"
"time"
"github.com/aceld/zinx/znet"
)
/*

2
examples/zinx_version_ex/ZinxV0.10Test/Client1.go

@ -2,10 +2,10 @@ package main
import (
"fmt"
"github.com/aceld/zinx/znet"
"io"
"net"
"time"
"github.com/aceld/zinx/znet"
)
/*

2
examples/zinx_version_ex/ZinxV0.11Test/Client0.go

@ -2,10 +2,10 @@ package main
import (
"fmt"
"github.com/aceld/zinx/znet"
"io"
"net"
"time"
"github.com/aceld/zinx/znet"
)
/*

2
examples/zinx_version_ex/ZinxV0.11Test/Client1.go

@ -2,10 +2,10 @@ package main
import (
"fmt"
"github.com/aceld/zinx/znet"
"io"
"net"
"time"
"github.com/aceld/zinx/znet"
)
/*

3
examples/zinx_version_ex/ZinxV0.1Test/Server.go

@ -4,9 +4,6 @@ import (
"github.com/aceld/zinx/znet"
)
//Server 模块的测试函数
func main() {

3
examples/zinx_version_ex/ZinxV0.2Test/Server.go

@ -4,9 +4,6 @@ import (
"github.com/aceld/zinx/znet"
)
//Server 模块的测试函数
func main() {

1
examples/zinx_version_ex/ZinxV0.3Test/Server.go

@ -19,6 +19,7 @@ func (this *PingRouter) PreHandle(request ziface.IRequest) {
fmt.Println("call back ping ping ping error")
}
}
//Test Handle
func (this *PingRouter) Handle(request ziface.IRequest) {
fmt.Println("Call PingRouter Handle")

2
examples/zinx_version_ex/ZinxV0.5Test/Client.go

@ -2,10 +2,10 @@ package main
import (
"fmt"
"github.com/aceld/zinx/znet"
"io"
"net"
"time"
"github.com/aceld/zinx/znet"
)
/*

2
examples/zinx_version_ex/ZinxV0.6Test-V0.7Test/Client0.go

@ -2,10 +2,10 @@ package main
import (
"fmt"
"github.com/aceld/zinx/znet"
"io"
"net"
"time"
"github.com/aceld/zinx/znet"
)
/*

2
examples/zinx_version_ex/ZinxV0.6Test-V0.7Test/Client1.go

@ -2,10 +2,10 @@ package main
import (
"fmt"
"github.com/aceld/zinx/znet"
"io"
"net"
"time"
"github.com/aceld/zinx/znet"
)
/*

2
examples/zinx_version_ex/ZinxV0.8Test/Client0.go

@ -2,10 +2,10 @@ package main
import (
"fmt"
"github.com/aceld/zinx/znet"
"io"
"net"
"time"
"github.com/aceld/zinx/znet"
)
/*

2
examples/zinx_version_ex/ZinxV0.8Test/Client1.go

@ -2,10 +2,10 @@ package main
import (
"fmt"
"github.com/aceld/zinx/znet"
"io"
"net"
"time"
"github.com/aceld/zinx/znet"
)
/*

2
examples/zinx_version_ex/ZinxV0.9Test/Client0.go

@ -2,10 +2,10 @@ package main
import (
"fmt"
"github.com/aceld/zinx/znet"
"io"
"net"
"time"
"github.com/aceld/zinx/znet"
)
/*

2
examples/zinx_version_ex/ZinxV0.9Test/Client1.go

@ -2,10 +2,10 @@ package main
import (
"fmt"
"github.com/aceld/zinx/znet"
"io"
"net"
"time"
"github.com/aceld/zinx/znet"
)
/*

4
examples/zinx_version_ex/datapackDemo/Client.go

@ -2,8 +2,8 @@ package main
import (
"fmt"
"net"
"github.com/aceld/zinx/znet"
"net"
)
func main() {
@ -50,5 +50,3 @@ func main() {
//客户端阻塞
select {}
}

2
examples/zinx_version_ex/datapackDemo/Server.go

@ -2,9 +2,9 @@ package main
import (
"fmt"
"github.com/aceld/zinx/znet"
"io"
"net"
"github.com/aceld/zinx/znet"
)
//只是负责测试datapack拆包,封包功能

2
examples/zinx_version_ex/protoDemo/main.go

@ -2,8 +2,8 @@ package main
import (
"fmt"
"github.com/golang/protobuf/proto"
"github.com/aceld/zinx/examples/zinx_version_ex/protoDemo/pb"
"github.com/golang/protobuf/proto"
)
func main() {

4
utils/globalobj.go

@ -2,10 +2,10 @@ package utils
import (
"encoding/json"
"io/ioutil"
"os"
"github.com/aceld/zinx/ziface"
"github.com/aceld/zinx/zlog"
"io/ioutil"
"os"
)
/*

3
ziface/iconnection.go

@ -27,7 +27,4 @@ type IConnection interface {
GetProperty(key string) (interface{}, error)
//移除链接属性
RemoveProperty(key string)
}

1
ziface/irequest.go

@ -9,4 +9,3 @@ type IRequest interface{
GetData() []byte //获取请求消息的数据
GetMsgID() uint32 //获取请求的消息ID
}

2
zinx_app_demo/mmo_game/api/move.go

@ -2,11 +2,11 @@ package api
import (
"fmt"
"github.com/golang/protobuf/proto"
"github.com/aceld/zinx/ziface"
"github.com/aceld/zinx/zinx_app_demo/mmo_game/core"
"github.com/aceld/zinx/zinx_app_demo/mmo_game/pb"
"github.com/aceld/zinx/znet"
"github.com/golang/protobuf/proto"
)
//玩家移动

2
zinx_app_demo/mmo_game/api/world_chat.go

@ -2,11 +2,11 @@ package api
import (
"fmt"
"github.com/golang/protobuf/proto"
"github.com/aceld/zinx/ziface"
"github.com/aceld/zinx/zinx_app_demo/mmo_game/core"
"github.com/aceld/zinx/zinx_app_demo/mmo_game/pb"
"github.com/aceld/zinx/znet"
"github.com/golang/protobuf/proto"
)
//世界聊天 路由业务

2
zinx_app_demo/mmo_game/client_AI_robot.go

@ -4,6 +4,7 @@ import (
"bytes"
"encoding/binary"
"fmt"
"github.com/aceld/zinx/zinx_app_demo/mmo_game/pb"
"github.com/golang/protobuf/proto"
"io"
"math/rand"
@ -11,7 +12,6 @@ import (
"os"
"os/signal"
"time"
"github.com/aceld/zinx/zinx_app_demo/mmo_game/pb"
)
type Message struct {

4
zinx_app_demo/mmo_game/core/player.go

@ -2,12 +2,12 @@ package core
import (
"fmt"
"github.com/aceld/zinx/ziface"
"github.com/aceld/zinx/zinx_app_demo/mmo_game/pb"
"github.com/golang/protobuf/proto"
"math/rand"
"sync"
"time"
"github.com/aceld/zinx/ziface"
"github.com/aceld/zinx/zinx_app_demo/mmo_game/pb"
)
//玩家对象

3
zlog/zlogger.go

@ -93,7 +93,6 @@ func NewZinxLog(out io.Writer, prefix string, flag int) *ZinxLogger {
log.closeFile()
}
/*
制作当条日志数据的 格式头信息
*/
@ -305,7 +304,6 @@ func (log *ZinxLogger) SetPrefix(prefix string){
log.prefix = prefix
}
//设置日志文件输出
func (log *ZinxLogger) SetLogFile(fileDir string, fileName string) {
var file *os.File
@ -396,4 +394,3 @@ func itoa(buf *bytes.Buffer, i int, wid int) {
bp++
}
}

8
znet/connection.go

@ -3,11 +3,11 @@ package znet
import (
"errors"
"fmt"
"github.com/aceld/zinx/utils"
"github.com/aceld/zinx/ziface"
"io"
"net"
"sync"
"github.com/aceld/zinx/utils"
"github.com/aceld/zinx/ziface"
)
type Connection struct {
@ -25,7 +25,7 @@ type Connection struct {
ExitBuffChan chan bool
//无缓冲管道,用于读、写两个goroutine之间的消息通信
msgChan chan []byte
//有冲管道,用于读、写两个goroutine之间的消息通信
//有冲管道,用于读、写两个goroutine之间的消息通信
msgBuffChan chan []byte
//链接属性
@ -78,8 +78,8 @@ func (c *Connection) StartWriter() {
return
}
} else {
break
fmt.Println("msgBuffChan is Closed")
break
}
case <-c.ExitBuffChan:
return

3
znet/connmanager.go

@ -3,8 +3,8 @@ package znet
import (
"errors"
"fmt"
"sync"
"github.com/aceld/zinx/ziface"
"sync"
)
/*
@ -80,6 +80,5 @@ func (connMgr *ConnManager) ClearConn() {
delete(connMgr.connections, connID)
}
fmt.Println("Clear All Connections successfully: conn num = ", connMgr.Len())
}

3
znet/msghandler.go

@ -2,9 +2,9 @@ package znet
import (
"fmt"
"strconv"
"github.com/aceld/zinx/utils"
"github.com/aceld/zinx/ziface"
"strconv"
)
type MsgHandle struct {
@ -34,7 +34,6 @@ func (mh *MsgHandle)SendMsgToTaskQueue(request ziface.IRequest) {
mh.TaskQueue[workerID] <- request
}
//马上以非阻塞方式处理消息
func (mh *MsgHandle) DoMsgHandler(request ziface.IRequest) {
handler, ok := mh.Apis[request.GetMsgID()]

2
znet/request.go

@ -6,10 +6,12 @@ type Request struct {
conn ziface.IConnection //已经和客户端建立好的 链接
msg ziface.IMessage //客户端请求的数据
}
//获取请求连接信息
func (r *Request) GetConnection() ziface.IConnection {
return r.conn
}
//获取请求消息的数据
func (r *Request) GetData() []byte {
return r.msg.GetData()

28
znet/server.go

@ -2,12 +2,12 @@ package znet
import (
"fmt"
"net"
"github.com/aceld/zinx/utils"
"github.com/aceld/zinx/ziface"
"net"
)
var zinx_logo = `
var zinxLogo = `
@ -16,9 +16,9 @@ var zinx_logo = `
`
var top_line = `┌───────────────────────────────────────────────────┐`
var border_line = ``
var bottom_line = `└───────────────────────────────────────────────────┘`
var topLine = `┌───────────────────────────────────────────────────┐`
var borderLine = ``
var bottomLine = `└───────────────────────────────────────────────────┘`
//iServer 接口实现,定义一个Server服务类
type Server struct {
@ -55,6 +55,7 @@ func NewServer () ziface.IServer {
}
return s
}
//============== 实现 ziface.IServer 里的全部接口方法 ========
//开启网络服务
@ -74,7 +75,7 @@ func (s *Server) Start() {
}
//2 监听服务器地址
listenner, err:= net.ListenTCP(s.IPVersion, addr)
listener, err := net.ListenTCP(s.IPVersion, addr)
if err != nil {
fmt.Println("listen", s.IPVersion, "err", err)
return
@ -90,7 +91,7 @@ func (s *Server) Start() {
//3 启动server网络连接业务
for {
//3.1 阻塞等待客户端建立连接请求
conn, err := listenner.AcceptTCP()
conn, err := listener.AcceptTCP()
if err != nil {
fmt.Println("Accept err ", err)
continue
@ -168,16 +169,13 @@ func (s *Server) CallOnConnStop(conn ziface.IConnection) {
}
func init() {
fmt.Println(zinx_logo)
fmt.Println(top_line)
fmt.Println(fmt.Sprintf("%s [Github] https://github.com/aceld %s", border_line, border_line))
fmt.Println(fmt.Sprintf("%s [tutorial] https://www.jianshu.com/p/23d07c0a28e5 %s", border_line, border_line))
fmt.Println(bottom_line)
fmt.Println(zinxLogo)
fmt.Println(topLine)
fmt.Println(fmt.Sprintf("%s [Github] https://github.com/aceld %s", borderLine, borderLine))
fmt.Println(fmt.Sprintf("%s [tutorial] https://www.jianshu.com/p/23d07c0a28e5 %s", borderLine, borderLine))
fmt.Println(bottomLine)
fmt.Printf("[Zinx] Version: %s, MaxConn: %d, MaxPacketSize: %d\n",
utils.GlobalObject.Version,
utils.GlobalObject.MaxConn,
utils.GlobalObject.MaxPacketSize)
}

40
znet/server_test.go

@ -15,7 +15,7 @@ import (
/*
模拟客户端
*/
func ClientTest() {
func ClientTest(i uint32) {
fmt.Println("Client Test ... start")
//3秒之后发起测试请求,给服务端开启服务的机会
@ -29,7 +29,7 @@ func ClientTest() {
for {
dp := NewDataPack()
msg, _ := dp.Pack(NewMsgPackage(1, []byte("client test message")))
msg, _ := dp.Pack(NewMsgPackage(i, []byte("client test message")))
_, err := conn.Write(msg)
if err != nil {
fmt.Println("client write err: ", err)
@ -109,14 +109,48 @@ func (this *PingRouter) PostHandle(request ziface.IRequest) {
}
}
type HelloRouter struct {
BaseRouter
}
func (this *HelloRouter) Handle(request ziface.IRequest) {
fmt.Println("call helloRouter Handle")
fmt.Printf("receive from client msgId=%d, data=%s\n", request.GetMsgID(), string(request.GetData()))
err := request.GetConnection().SendMsg(2, []byte("hello zix hello Router"))
if err != nil {
fmt.Println(err)
}
}
func DoConnectionBegin(conn ziface.IConnection) {
fmt.Println("DoConnectionBegin is Called ... ")
err := conn.SendMsg(2, []byte("DoConnection BEGIN..."))
if err != nil {
fmt.Println(err)
}
}
//连接断开的时候执行
func DoConnectionLost(conn ziface.IConnection) {
fmt.Println("DoConnectionLost is Called ... ")
}
func TestServer(t *testing.T) {
//创建一个server句柄
s := NewServer()
//注册链接hook回调函数
s.SetOnConnStart(DoConnectionBegin)
s.SetOnConnStop(DoConnectionLost)
// 多路由
s.AddRouter(1, &PingRouter{})
s.AddRouter(2, &HelloRouter{})
// 客户端测试
go ClientTest()
go ClientTest(1)
go ClientTest(2)
//2 开启服务
go s.Serve()

2
ztimer/delayfunc.go

@ -36,8 +36,6 @@ func (df *DelayFunc) String() string {
return fmt.Sprintf("{DelayFun:%s, args:%v}", reflect.TypeOf(df.f).Name(), df.args)
}
/*
执行延迟函数---如果执行失败抛出异常
*/

1
ztimer/timer_test.go

@ -20,7 +20,6 @@ func myFunc(v ...interface{}) {
func TestTimer(t *testing.T) {
for i := 0; i < 5; i++ {
go func(i int) {
NewTimerAfter(NewDelayFunc(myFunc, []interface{}{i, 2 * i}), time.Duration(2*i)*time.Second).Run()

3
ztimer/timerscheduler.go

@ -9,10 +9,10 @@
package ztimer
import (
"github.com/aceld/zinx/zlog"
"math"
"sync"
"time"
"github.com/aceld/zinx/zlog"
)
const (
@ -110,7 +110,6 @@ func (this *TimerScheduler) Start() {
this.triggerChan <- timer.delayFunc
}
time.Sleep(MAX_TIME_DELAY / 2 * time.Millisecond)
}
}()

4
ztimer/timerscheduler_test.go

@ -9,9 +9,9 @@ package ztimer
import (
"fmt"
"github.com/aceld/zinx/zlog"
"testing"
"time"
"github.com/aceld/zinx/zlog"
)
//触发函数
@ -63,5 +63,3 @@ func TestNewAutoExecTimerScheduler(t *testing.T) {
//阻塞等待
select {}
}

2
ztimer/timewheel.go

@ -8,9 +8,9 @@ package ztimer
import (
"errors"
"fmt"
"github.com/aceld/zinx/zlog"
"sync"
"time"
"github.com/aceld/zinx/zlog"
)
/*

1
ztimer/timewheel_test.go

@ -30,7 +30,6 @@ func TestTimerWheel(t *testing.T) {
fmt.Println("init timewheels done!")
//===== > 以上为初始化分层时间轮 <====
//给时间轮添加定时器

Loading…
Cancel
Save