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

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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()
}