You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nnet/core/nnet_test.go

67 lines
1.5 KiB
Go

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"
"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,
},
}, protocol.WithNNetProtocol(protocol.NNetConfig{
HeartbeatInterval: 0,
HandshakeValidator: nil,
})...)
server.AddRoute(Route{
Matches: protocol.Match{
Route: "test",
Code: 1,
},
Handler: func(entity entity.NetworkEntity, pkg packet.IPacket) {
fmt.Println(pkg)
p, ok := pkg.(*protocol.NNetPacket)
if !ok {
nlog.Error("wrong packet type")
return
}
bd := []byte("服务器接收到数据为: " + string(p.GetBody()))
// 注Response类型数据不需要Route原地返回客户端需等待
_ = entity.Send(protocol.Header{
PacketType: protocol.Data,
Length: uint32(len(bd)),
MessageHeader: protocol.MessageHeader{
MsgType: protocol.Response,
ID: p.ID,
Route: p.Route,
},
}, bd)
},
})
defer server.Stop()
server.Start()
}