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.
118 lines
2.5 KiB
Go
118 lines
2.5 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"git.noahlan.cn/noahlan/nnet/config"
|
|
"git.noahlan.cn/noahlan/nnet/core"
|
|
"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"
|
|
"time"
|
|
)
|
|
|
|
func runServer(addr string) {
|
|
server := core.NewServer(config.EngineConf{
|
|
ServerConf: config.ServerConf{
|
|
Protocol: "tcp",
|
|
Addr: addr,
|
|
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.AddRoutes([]core.Route{
|
|
{
|
|
Matches: protocol.Match{
|
|
Route: "ping",
|
|
Code: 1,
|
|
},
|
|
Handler: func(et entity.NetworkEntity, pkg packet.IPacket) {
|
|
nlog.Info("client ping, server pong -> ")
|
|
err := et.Send(protocol.Header{
|
|
PacketType: protocol.Data,
|
|
MessageHeader: protocol.MessageHeader{
|
|
MsgType: protocol.Request,
|
|
ID: 1,
|
|
Route: "pong",
|
|
},
|
|
}, []byte("1"))
|
|
nlog.Must(err)
|
|
},
|
|
},
|
|
})
|
|
|
|
defer server.Stop()
|
|
server.Start()
|
|
}
|
|
|
|
func runClient(addr string) (client *core.Client, et entity.NetworkEntity) {
|
|
chReady := make(chan struct{})
|
|
client = core.NewClient(config.EngineConf{
|
|
Pool: pool.Config{
|
|
PoolSize: math.MaxInt32,
|
|
ExpiryDuration: time.Second,
|
|
PreAlloc: false,
|
|
MaxBlockingTasks: 0,
|
|
Nonblocking: false,
|
|
DisablePurge: false,
|
|
},
|
|
}, protocol.WithNNetClientProtocol(func() {
|
|
chReady <- struct{}{}
|
|
})...)
|
|
|
|
client.AddRoutes([]core.Route{
|
|
{
|
|
Matches: protocol.Match{
|
|
Route: "test.client",
|
|
Code: 1,
|
|
},
|
|
Handler: func(et entity.NetworkEntity, pkg packet.IPacket) {
|
|
nlog.Info("client hahaha")
|
|
},
|
|
},
|
|
})
|
|
et = client.Dial(addr)
|
|
|
|
handshake, err := json.Marshal(&protocol.HandshakeReq{
|
|
Version: "1.0.0",
|
|
Type: "test",
|
|
ClientId: "a",
|
|
ClientSecret: "a",
|
|
Payload: map[string]string{
|
|
"pl": "test-data",
|
|
},
|
|
})
|
|
nlog.Must(err)
|
|
|
|
packer := protocol.NewNNetPacker()
|
|
hsd, err := packer.Pack(protocol.Header{
|
|
PacketType: protocol.Handshake,
|
|
MessageHeader: protocol.MessageHeader{
|
|
MsgType: 0,
|
|
ID: 0,
|
|
Route: "",
|
|
},
|
|
}, handshake)
|
|
nlog.Must(err)
|
|
|
|
err = et.SendBytes(hsd)
|
|
nlog.Must(err)
|
|
|
|
<-chReady
|
|
return
|
|
}
|