|
|
|
|
package core
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"git.noahlan.cn/noahlan/nnet/config"
|
|
|
|
|
"git.noahlan.cn/noahlan/nnet/entity"
|
|
|
|
|
"git.noahlan.cn/noahlan/nnet/packet"
|
|
|
|
|
"git.noahlan.cn/noahlan/nnet/protocol/nnet"
|
|
|
|
|
"git.noahlan.cn/noahlan/ntools-go/core/nlog"
|
|
|
|
|
"git.noahlan.cn/noahlan/ntools-go/core/pool"
|
|
|
|
|
"math"
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestServer(t *testing.T) {
|
|
|
|
|
server := NewServer(config.EngineConf{
|
|
|
|
|
ServerConf: config.ServerConf{
|
|
|
|
|
Protocol: "tcp",
|
|
|
|
|
Addr: "0.0.0.0:6666",
|
|
|
|
|
Name: "testServer",
|
|
|
|
|
Mode: "dev",
|
|
|
|
|
},
|
|
|
|
|
Pool: pool.Config{
|
|
|
|
|
PoolSize: math.MaxInt32,
|
|
|
|
|
ExpiryDuration: time.Second,
|
|
|
|
|
PreAlloc: false,
|
|
|
|
|
MaxBlockingTasks: 0,
|
|
|
|
|
Nonblocking: false,
|
|
|
|
|
DisablePurge: false,
|
|
|
|
|
},
|
|
|
|
|
}, nnet.WithNNetProtocol(nnet.Config{
|
|
|
|
|
HeartbeatInterval: 0,
|
|
|
|
|
HandshakeValidator: nil,
|
|
|
|
|
})...)
|
|
|
|
|
|
|
|
|
|
server.AddRoute(Route{
|
|
|
|
|
Matches: nnet.Match{
|
|
|
|
|
Route: "test",
|
|
|
|
|
Code: 1,
|
|
|
|
|
},
|
|
|
|
|
Handler: func(entity entity.NetworkEntity, pkg packet.IPacket) {
|
|
|
|
|
fmt.Println(pkg)
|
|
|
|
|
p, ok := pkg.(*nnet.Packet)
|
|
|
|
|
if !ok {
|
|
|
|
|
nlog.Error("wrong packet type")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bd := []byte("服务器接收到数据为: " + string(p.GetBody()))
|
|
|
|
|
// 注:Response类型数据不需要Route(原地返回,客户端需等待)
|
|
|
|
|
_ = entity.Send(nnet.Header{
|
|
|
|
|
PacketType: nnet.Data,
|
|
|
|
|
Length: uint32(len(bd)),
|
|
|
|
|
MessageHeader: nnet.MessageHeader{
|
|
|
|
|
MsgType: nnet.Response,
|
|
|
|
|
ID: p.ID,
|
|
|
|
|
Route: p.Route,
|
|
|
|
|
},
|
|
|
|
|
}, bd)
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
defer server.Stop()
|
|
|
|
|
server.Start()
|
|
|
|
|
}
|