2024-01-07 10:12:43 +00:00
|
|
|
package base
|
|
|
|
|
|
|
|
import (
|
2024-01-29 04:39:40 +00:00
|
|
|
"context"
|
2024-01-07 10:12:43 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/net"
|
2024-01-29 06:55:36 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/service"
|
2024-01-07 10:12:43 +00:00
|
|
|
"github.com/vmihailenco/msgpack/v5"
|
2024-01-29 06:55:36 +00:00
|
|
|
"go.uber.org/zap"
|
2024-01-29 04:39:40 +00:00
|
|
|
"io"
|
2024-01-07 10:12:43 +00:00
|
|
|
)
|
|
|
|
|
2024-01-29 04:39:40 +00:00
|
|
|
var (
|
|
|
|
_ msgpack.CustomDecoder = (*IncomingMessageReader)(nil)
|
|
|
|
)
|
|
|
|
|
2024-01-07 10:12:43 +00:00
|
|
|
type IncomingMessage interface {
|
2024-01-29 04:39:40 +00:00
|
|
|
HandleMessage(message IncomingMessageData) error
|
|
|
|
DecodeMessage(dec *msgpack.Decoder, message IncomingMessageData) error
|
|
|
|
HandshakeRequirer
|
|
|
|
}
|
|
|
|
|
|
|
|
type IncomingMessageData struct {
|
|
|
|
Original []byte
|
|
|
|
Data []byte
|
|
|
|
Ctx context.Context
|
2024-01-29 06:55:36 +00:00
|
|
|
Services service.Services
|
|
|
|
Logger *zap.Logger
|
2024-01-29 04:39:40 +00:00
|
|
|
Peer net.Peer
|
|
|
|
VerifyId bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type IncomingMessageReader struct {
|
|
|
|
Kind int
|
|
|
|
Data []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *IncomingMessageReader) DecodeMsgpack(dec *msgpack.Decoder) error {
|
|
|
|
kind, err := dec.DecodeInt()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
i.Kind = kind
|
|
|
|
|
|
|
|
raw, err := io.ReadAll(dec.Buffered())
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
i.Data = raw
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type HandshakeRequirer interface {
|
2024-01-13 16:22:01 +00:00
|
|
|
RequiresHandshake() bool
|
|
|
|
SetRequiresHandshake(value bool)
|
2024-01-07 10:12:43 +00:00
|
|
|
}
|
|
|
|
|
2024-01-29 04:39:40 +00:00
|
|
|
type HandshakeRequirement struct {
|
|
|
|
requiresHandshake bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hr *HandshakeRequirement) RequiresHandshake() bool {
|
|
|
|
return hr.requiresHandshake
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hr *HandshakeRequirement) SetRequiresHandshake(value bool) {
|
|
|
|
hr.requiresHandshake = value
|
2024-01-07 10:12:43 +00:00
|
|
|
}
|