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/protocol/pipeline_nnet.go

78 lines
2.0 KiB
Go

2 years ago
package protocol
import (
"encoding/json"
2 years ago
"errors"
2 years ago
"fmt"
"git.noahlan.cn/noahlan/nnet/core"
"git.noahlan.cn/noahlan/nnet/entity"
"git.noahlan.cn/noahlan/ntools-go/core/nlog"
)
type (
HandshakeValidatorFunc func(*HandshakeReq) error
HandshakeAckPayloadFunc func() interface{}
2 years ago
)
func WithNNetPipeline(
handshakeResp *HandshakeResp,
validator HandshakeValidatorFunc,
) core.RunOption {
2 years ago
packer := NewNNetPacker()
return func(server *core.NNet) {
2 years ago
server.Pipeline().Inbound().PushFront(func(entity entity.NetworkEntity, v interface{}) error {
pkg, ok := v.(*NNetPacket)
if !ok {
return ErrWrongPacketType
}
conn, _ := entity.Conn()
switch pkg.PacketType {
case Handshake:
var handshakeData HandshakeReq
err := json.Unmarshal(pkg.Data, &handshakeData)
nlog.Must(err)
if err := validator(&handshakeData); err != nil {
2 years ago
return err
}
handshakeResp.Payload = handshakeData.Payload
data, err := json.Marshal(handshakeResp)
2 years ago
nlog.Must(err)
hrd, _ := packer.Pack(Header{
PacketType: Handshake,
MessageHeader: MessageHeader{},
}, data)
2 years ago
if err := entity.SendBytes(hrd); err != nil {
return err
}
entity.SetStatus(core.StatusPrepare)
nlog.Debugf("connection handshake Id=%d, Remote=%s", entity.Session().ID(), conn.RemoteAddr())
case HandshakeAck:
entity.SetStatus(core.StatusPending)
nlog.Debugf("receive handshake ACK Id=%d, Remote=%s", entity.Session().ID(), conn.RemoteAddr())
case Data:
if entity.Status() < core.StatusPending {
return errors.New(fmt.Sprintf("receive data on socket which not yet ACK, session will be closed immediately, remote=%s",
conn.RemoteAddr()))
}
entity.SetStatus(core.StatusWorking)
var lastMid uint64
switch pkg.MsgType {
case Request:
lastMid = pkg.ID
case Notify:
lastMid = 0
default:
return fmt.Errorf("Invalid message type: %s ", pkg.MsgType.String())
}
entity.SetLastMID(lastMid)
}
return nil
})
}
}