aceld
4 years ago
10 changed files with 280 additions and 21 deletions
-
55README.md
-
63examples/zinx_client/main.go
-
9examples/zinx_server/conf/zinx.json
-
60examples/zinx_server/main.go
-
24examples/zinx_server/zrouter/hello.go
-
25examples/zinx_server/zrouter/ping.go
-
35makefile
-
8utils/globalobj.go
-
9znet/server.go
-
1ztimer/timerscheduler_test.go
@ -0,0 +1,63 @@ |
|||||
|
package main |
||||
|
|
||||
|
import ( |
||||
|
"fmt" |
||||
|
"github.com/aceld/zinx/znet" |
||||
|
"io" |
||||
|
"net" |
||||
|
"time" |
||||
|
) |
||||
|
|
||||
|
/* |
||||
|
模拟客户端 |
||||
|
*/ |
||||
|
func main() { |
||||
|
|
||||
|
conn, err := net.Dial("tcp", "127.0.0.1:8999") |
||||
|
if err != nil { |
||||
|
fmt.Println("client start err, exit!") |
||||
|
return |
||||
|
} |
||||
|
|
||||
|
for { |
||||
|
//发封包message消息
|
||||
|
dp := znet.NewDataPack() |
||||
|
msg, _ := dp.Pack(znet.NewMsgPackage(0, []byte("Zinx client Demo Test MsgID=0, [Ping]"))) |
||||
|
_, err := conn.Write(msg) |
||||
|
if err != nil { |
||||
|
fmt.Println("write error err ", err) |
||||
|
return |
||||
|
} |
||||
|
|
||||
|
//先读出流中的head部分
|
||||
|
headData := make([]byte, dp.GetHeadLen()) |
||||
|
_, err = io.ReadFull(conn, headData) //ReadFull 会把msg填充满为止
|
||||
|
if err != nil { |
||||
|
fmt.Println("read head error") |
||||
|
break |
||||
|
} |
||||
|
//将headData字节流 拆包到msg中
|
||||
|
msgHead, err := dp.Unpack(headData) |
||||
|
if err != nil { |
||||
|
fmt.Println("server unpack err:", err) |
||||
|
return |
||||
|
} |
||||
|
|
||||
|
if msgHead.GetDataLen() > 0 { |
||||
|
//msg 是有data数据的,需要再次读取data数据
|
||||
|
msg := msgHead.(*znet.Message) |
||||
|
msg.Data = make([]byte, msg.GetDataLen()) |
||||
|
|
||||
|
//根据dataLen从io中读取字节流
|
||||
|
_, err := io.ReadFull(conn, msg.Data) |
||||
|
if err != nil { |
||||
|
fmt.Println("server unpack data err:", err) |
||||
|
return |
||||
|
} |
||||
|
|
||||
|
fmt.Println("==> Test Router:[Ping] Recv Msg: ID=", msg.Id, ", len=", msg.DataLen, ", data=", string(msg.Data)) |
||||
|
} |
||||
|
|
||||
|
time.Sleep(1 * time.Second) |
||||
|
} |
||||
|
} |
@ -0,0 +1,9 @@ |
|||||
|
{ |
||||
|
"Name":"zinx server Demo", |
||||
|
"Host":"127.0.0.1", |
||||
|
"TcpPort":8999, |
||||
|
"MaxConn":3, |
||||
|
"WorkerPoolSize":10, |
||||
|
"LogDir": "./mylog", |
||||
|
"LogFile":"zinx.log" |
||||
|
} |
@ -0,0 +1,60 @@ |
|||||
|
/** |
||||
|
* @Author: Aceld |
||||
|
* @Date: 2020/12/24 00:24 |
||||
|
* @Mail: danbing.at@gmail.com |
||||
|
* zinx server demo |
||||
|
*/ |
||||
|
package main |
||||
|
|
||||
|
import ( |
||||
|
"github.com/aceld/zinx/examples/zinx_server/zrouter" |
||||
|
"github.com/aceld/zinx/ziface" |
||||
|
"github.com/aceld/zinx/zlog" |
||||
|
"github.com/aceld/zinx/znet" |
||||
|
) |
||||
|
|
||||
|
|
||||
|
//创建连接的时候执行
|
||||
|
func DoConnectionBegin(conn ziface.IConnection) { |
||||
|
zlog.Debug("DoConnecionBegin is Called ... ") |
||||
|
|
||||
|
//设置两个链接属性,在连接创建之后
|
||||
|
zlog.Debug("Set conn Name, Home done!") |
||||
|
conn.SetProperty("Name", "Aceld") |
||||
|
conn.SetProperty("Home", "https://www.jianshu.com/u/35261429b7f1") |
||||
|
|
||||
|
err := conn.SendMsg(2, []byte("DoConnection BEGIN...")) |
||||
|
if err != nil { |
||||
|
zlog.Error(err) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
//连接断开的时候执行
|
||||
|
func DoConnectionLost(conn ziface.IConnection) { |
||||
|
//在连接销毁之前,查询conn的Name,Home属性
|
||||
|
if name, err := conn.GetProperty("Name"); err == nil { |
||||
|
zlog.Error("Conn Property Name = ", name) |
||||
|
} |
||||
|
|
||||
|
if home, err := conn.GetProperty("Home"); err == nil { |
||||
|
zlog.Error("Conn Property Home = ", home) |
||||
|
} |
||||
|
|
||||
|
zlog.Debug("DoConneciotnLost is Called ... ") |
||||
|
} |
||||
|
|
||||
|
func main() { |
||||
|
//创建一个server句柄
|
||||
|
s := znet.NewServer() |
||||
|
|
||||
|
//注册链接hook回调函数
|
||||
|
s.SetOnConnStart(DoConnectionBegin) |
||||
|
s.SetOnConnStop(DoConnectionLost) |
||||
|
|
||||
|
//配置路由
|
||||
|
s.AddRouter(0, &zrouter.PingRouter{}) |
||||
|
s.AddRouter(1, &zrouter.HelloZinxRouter{}) |
||||
|
|
||||
|
//开启服务
|
||||
|
s.Serve() |
||||
|
} |
@ -0,0 +1,24 @@ |
|||||
|
package zrouter |
||||
|
|
||||
|
import ( |
||||
|
"github.com/aceld/zinx/ziface" |
||||
|
"github.com/aceld/zinx/zlog" |
||||
|
"github.com/aceld/zinx/znet" |
||||
|
) |
||||
|
|
||||
|
type HelloZinxRouter struct { |
||||
|
znet.BaseRouter |
||||
|
} |
||||
|
|
||||
|
//HelloZinxRouter Handle
|
||||
|
func (this *HelloZinxRouter) Handle(request ziface.IRequest) { |
||||
|
zlog.Debug("Call HelloZinxRouter Handle") |
||||
|
//先读取客户端的数据,再回写ping...ping...ping
|
||||
|
zlog.Debug("recv from client : msgId=", request.GetMsgID(), ", data=", string(request.GetData())) |
||||
|
|
||||
|
err := request.GetConnection().SendBuffMsg(1, []byte("Hello Zinx Router V0.10")) |
||||
|
if err != nil { |
||||
|
zlog.Error(err) |
||||
|
} |
||||
|
} |
||||
|
|
@ -0,0 +1,25 @@ |
|||||
|
package zrouter |
||||
|
|
||||
|
import ( |
||||
|
"github.com/aceld/zinx/ziface" |
||||
|
"github.com/aceld/zinx/zlog" |
||||
|
"github.com/aceld/zinx/znet" |
||||
|
) |
||||
|
|
||||
|
//ping test 自定义路由
|
||||
|
type PingRouter struct { |
||||
|
znet.BaseRouter |
||||
|
} |
||||
|
|
||||
|
//Ping Handle
|
||||
|
func (this *PingRouter) Handle(request ziface.IRequest) { |
||||
|
|
||||
|
zlog.Debug("Call PingRouter Handle") |
||||
|
//先读取客户端的数据,再回写ping...ping...ping
|
||||
|
zlog.Debug("recv from client : msgId=", request.GetMsgID(), ", data=", string(request.GetData())) |
||||
|
|
||||
|
err := request.GetConnection().SendBuffMsg(0, []byte("ping...ping...ping")) |
||||
|
if err != nil { |
||||
|
zlog.Error(err) |
||||
|
} |
||||
|
} |
@ -1,11 +1,28 @@ |
|||||
# gofmt格式化
|
|
||||
# run in terminal:
|
|
||||
# make fmt
|
|
||||
# win系统中,在git bash中如果出现make包没有找到
|
|
||||
# 管理员运行git bash,运行以下命令
|
|
||||
# choco install make
|
|
||||
|
|
||||
GOFMT_FILES?=$$(find . -name '*.go' |grep -v vendor) |
|
||||
|
.PHONY: build |
||||
|
|
||||
fmt: |
|
||||
gofmt -w $(GOFMT_FILES) |
|
||||
|
SERVICE := zinx |
||||
|
CUR_PWD := $(shell pwd) |
||||
|
|
||||
|
SERVER_DEMO_PATH := $(CUR_PWD)/examples/zinx_server |
||||
|
CLIENT_DEMO_PATH := $(CUR_PWD)/examples/zinx_client |
||||
|
SERVER_DEMO_BIN := $(SERVER_DEMO_PATH)/server |
||||
|
CLIENT_DEMO_BIN := $(CLIENT_DEMO_PATH)/client |
||||
|
|
||||
|
AUTHOR := $(shell git log --pretty=format:"%an"|head -n 1) |
||||
|
VERSION := $(shell git rev-list HEAD | head -1) |
||||
|
BUILD_INFO := $(shell git log --pretty=format:"%s" | head -1) |
||||
|
BUILD_DATE := $(shell date +%Y-%m-%d\ %H:%M:%S) |
||||
|
|
||||
|
export GO111MODULE=on |
||||
|
|
||||
|
LD_FLAGS='-X "$(SERVICE)/version.TAG=$(TAG)" -X "$(SERVICE)/version.VERSION=$(VERSION)" -X "$(SERVICE)/version.AUTHOR=$(AUTHOR)" -X "$(SERVICE)/version.BUILD_INFO=$(BUILD_INFO)" -X "$(SERVICE)/version.BUILD_DATE=$(BUILD_DATE)"' |
||||
|
|
||||
|
default: build |
||||
|
|
||||
|
build: |
||||
|
go build -ldflags $(LD_FLAGS) -gcflags "-N" -i -o $(SERVER_DEMO_BIN) $(SERVER_DEMO_PATH)/main.go |
||||
|
go build -ldflags $(LD_FLAGS) -gcflags "-N" -i -o $(CLIENT_DEMO_BIN) $(CLIENT_DEMO_PATH)/main.go |
||||
|
clean: |
||||
|
rm $(SERVER_DEMO_BIN) |
||||
|
rm $(CLIENT_DEMO_BIN) |
Write
Preview
Loading…
Cancel
Save
Reference in new issue