|
@ -2,12 +2,16 @@ package znet |
|
|
|
|
|
|
|
|
import ( |
|
|
import ( |
|
|
"fmt" |
|
|
"fmt" |
|
|
|
|
|
"github.com/aceld/zinx/ziface" |
|
|
|
|
|
"io" |
|
|
"net" |
|
|
"net" |
|
|
"testing" |
|
|
"testing" |
|
|
"time" |
|
|
"time" |
|
|
"github.com/aceld/zinx/ziface" |
|
|
|
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
// run in terminal:
|
|
|
|
|
|
// go test -v ./znet -run=TestServer
|
|
|
|
|
|
|
|
|
/* |
|
|
/* |
|
|
模拟客户端 |
|
|
模拟客户端 |
|
|
*/ |
|
|
*/ |
|
@ -17,47 +21,57 @@ func ClientTest() { |
|
|
//3秒之后发起测试请求,给服务端开启服务的机会
|
|
|
//3秒之后发起测试请求,给服务端开启服务的机会
|
|
|
time.Sleep(3 * time.Second) |
|
|
time.Sleep(3 * time.Second) |
|
|
|
|
|
|
|
|
conn, err := net.Dial("tcp", "127.0.0.1:7777") |
|
|
|
|
|
|
|
|
conn, err := net.Dial("tcp", "127.0.0.1:8999") |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
fmt.Println("client start err, exit!") |
|
|
fmt.Println("client start err, exit!") |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
for { |
|
|
for { |
|
|
_, err := conn.Write([]byte("Zinx V0.2 test")) |
|
|
|
|
|
|
|
|
dp := NewDataPack() |
|
|
|
|
|
msg, _ := dp.Pack(NewMsgPackage(1, []byte("client test message"))) |
|
|
|
|
|
_, err := conn.Write(msg) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
fmt.Println("write error err ", err) |
|
|
|
|
|
|
|
|
fmt.Println("client write err: ", err) |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
buf := make([]byte, 512) |
|
|
|
|
|
cnt, err := conn.Read(buf) |
|
|
|
|
|
|
|
|
//先读出流中的head部分
|
|
|
|
|
|
headData := make([]byte, dp.GetHeadLen()) |
|
|
|
|
|
_, err = io.ReadFull(conn, headData) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
fmt.Println("read buf error ") |
|
|
|
|
|
|
|
|
fmt.Println("client read head err: ", err) |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
fmt.Printf(" server call back : %s, cnt = %d\n", buf, cnt) |
|
|
|
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* |
|
|
|
|
|
//Server 模块的测试函数
|
|
|
|
|
|
func TestServer(t *testing.T) { |
|
|
|
|
|
|
|
|
// 将headData字节流 拆包到msg中
|
|
|
|
|
|
msgHead, err := dp.Unpack(headData) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
fmt.Println("client unpack head err: ", err) |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if msgHead.GetDataLen() > 0 { |
|
|
|
|
|
//msg 是有data数据的,需要再次读取data数据
|
|
|
|
|
|
msg := msgHead.(*Message) |
|
|
|
|
|
msg.Data = make([]byte, msg.GetDataLen()) |
|
|
|
|
|
|
|
|
// 服务端测试
|
|
|
|
|
|
//1 创建一个server 句柄 s
|
|
|
|
|
|
s := NewServer("[zinx V0.1]") |
|
|
|
|
|
|
|
|
//根据dataLen从io中读取字节流
|
|
|
|
|
|
_, err := io.ReadFull(conn, msg.Data) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
fmt.Println("client unpack data err") |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// 客户端测试
|
|
|
|
|
|
go ClientTest() |
|
|
|
|
|
|
|
|
fmt.Printf("==> Client receive Msg: Id = %d, len = %d , data = %s\n", msg.Id, msg.DataLen, msg.Data) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
//2 开启服务
|
|
|
|
|
|
s.Serve() |
|
|
|
|
|
|
|
|
time.Sleep(time.Second) |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* |
|
|
|
|
|
模拟服务器端 |
|
|
*/ |
|
|
*/ |
|
|
|
|
|
|
|
|
//ping test 自定义路由
|
|
|
//ping test 自定义路由
|
|
@ -65,43 +79,50 @@ type PingRouter struct { |
|
|
BaseRouter |
|
|
BaseRouter |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Test PreHandle
|
|
|
//Test PreHandle
|
|
|
func (this *PingRouter) PreHandle(request ziface.IRequest) { |
|
|
func (this *PingRouter) PreHandle(request ziface.IRequest) { |
|
|
fmt.Println("Call Router PreHandle") |
|
|
fmt.Println("Call Router PreHandle") |
|
|
_, err := request.GetConnection().GetTCPConnection().Write([]byte("before ping ....\n")) |
|
|
|
|
|
|
|
|
err := request.GetConnection().SendMsg(1, []byte("before ping ....\n")) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
fmt.Println("call back ping ping ping error") |
|
|
|
|
|
|
|
|
fmt.Println("preHandle SendMsg err: ", err) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//Test Handle
|
|
|
//Test Handle
|
|
|
func (this *PingRouter) Handle(request ziface.IRequest) { |
|
|
func (this *PingRouter) Handle(request ziface.IRequest) { |
|
|
fmt.Println("Call PingRouter Handle") |
|
|
fmt.Println("Call PingRouter Handle") |
|
|
_, err := request.GetConnection().GetTCPConnection().Write([]byte("ping...ping...ping\n")) |
|
|
|
|
|
|
|
|
//先读取客户端的数据,再回写ping...ping...ping
|
|
|
|
|
|
fmt.Println("recv from client : msgId=", request.GetMsgID(), ", data=", string(request.GetData())) |
|
|
|
|
|
|
|
|
|
|
|
err := request.GetConnection().SendMsg(1, []byte("ping...ping...ping\n")) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
fmt.Println("call back ping ping ping error") |
|
|
|
|
|
|
|
|
fmt.Println("Handle SendMsg err: ", err) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//Test PostHandle
|
|
|
//Test PostHandle
|
|
|
func (this *PingRouter) PostHandle(request ziface.IRequest) { |
|
|
func (this *PingRouter) PostHandle(request ziface.IRequest) { |
|
|
fmt.Println("Call Router PostHandle") |
|
|
fmt.Println("Call Router PostHandle") |
|
|
_, err := request.GetConnection().GetTCPConnection().Write([]byte("After ping .....\n")) |
|
|
|
|
|
|
|
|
err := request.GetConnection().SendMsg(1, []byte("After ping .....\n")) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
fmt.Println("call back ping ping ping error") |
|
|
|
|
|
|
|
|
fmt.Println("Post SendMsg err: ", err) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func TestServerV0_3(t *testing.T) { |
|
|
|
|
|
|
|
|
func TestServer(t *testing.T) { |
|
|
//创建一个server句柄
|
|
|
//创建一个server句柄
|
|
|
s := NewServer() |
|
|
s := NewServer() |
|
|
|
|
|
|
|
|
s.AddRouter(&PingRouter{}) |
|
|
|
|
|
|
|
|
s.AddRouter(1, &PingRouter{}) |
|
|
|
|
|
|
|
|
// 客户端测试
|
|
|
// 客户端测试
|
|
|
go ClientTest() |
|
|
go ClientTest() |
|
|
|
|
|
|
|
|
//2 开启服务
|
|
|
//2 开启服务
|
|
|
s.Serve() |
|
|
|
|
|
|
|
|
go s.Serve() |
|
|
|
|
|
|
|
|
|
|
|
select { |
|
|
|
|
|
case <-time.After(time.Second * 10): |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
} |
|
|
} |