mirror of
https://github.com/soypat/lneto.git
synced 2026-09-10 16:49:37 +00:00
Add round-robin implementation (#66)
* add handler.encapsulateNode * implement round robin handler approach * simplify round robin implementation * leave TODO * internet.node touch up * fix conflict resolve f-up * replace certain ErrShortBuffer with ErrTruncatedFrame error
This commit is contained in:
+4
-4
@@ -8,13 +8,13 @@ import (
|
||||
"github.com/soypat/lneto"
|
||||
)
|
||||
|
||||
// NewIPv4Frame returns a new IPv4Frame with data set to buf.
|
||||
// NewFrame returns a new [Frame] with data set to buf.
|
||||
// An error is returned if the buffer size is smaller than 20.
|
||||
// Users should still call [IPv4Frame.ValidateSize] before working
|
||||
// Users should still call [Frame.ValidateSize] before working
|
||||
// with payload/options of frames to avoid panics.
|
||||
func NewFrame(buf []byte) (Frame, error) {
|
||||
if len(buf) < sizeHeader {
|
||||
return Frame{buf: nil}, lneto.ErrShortBuffer
|
||||
return Frame{buf: nil}, lneto.ErrTruncatedFrame
|
||||
}
|
||||
return Frame{buf: buf}, nil
|
||||
}
|
||||
@@ -218,7 +218,7 @@ func (ifrm Frame) ValidateSize(v *lneto.Validator) {
|
||||
v.AddError(lneto.ErrInvalidLengthField)
|
||||
}
|
||||
if int(tl) > len(ifrm.RawData()) {
|
||||
v.AddError(lneto.ErrShortBuffer)
|
||||
v.AddError(lneto.ErrTruncatedFrame)
|
||||
}
|
||||
if ihl < 5 || uint16(ihl)*4 > tl {
|
||||
v.AddError(lneto.ErrInvalidLengthField)
|
||||
|
||||
@@ -54,6 +54,7 @@ func (client *Client) Configure(cfg ClientConfig) error {
|
||||
}
|
||||
client.connid++
|
||||
internal.SliceReuse(&client.outgoingEcho, cfg.ResponseQueueLimit)
|
||||
internal.SliceReuse(&client.incomingEcho, cfg.ResponseQueueLimit)
|
||||
client.responseRing = internal.Ring{Buf: cfg.ResponseQueueBuffer}
|
||||
client.magic = cfg.HashSeed
|
||||
client.id = cfg.ID
|
||||
@@ -101,6 +102,10 @@ func (client *Client) Demux(carrierData []byte, frameOffset int) error {
|
||||
}
|
||||
switch tp {
|
||||
case TypeEcho:
|
||||
free := cap(client.incomingEcho) - len(client.incomingEcho)
|
||||
if free == 0 {
|
||||
return lneto.ErrExhausted
|
||||
}
|
||||
// We received a ping request; not handled client-side.
|
||||
efrm := FrameEcho{Frame: ifrm}
|
||||
data := efrm.Data()
|
||||
@@ -227,6 +232,10 @@ func (client *Client) PingStart(remoteAddr [4]byte, pattern []byte, size uint16)
|
||||
} else if remoteAddr == [4]byte{} {
|
||||
return 0, lneto.ErrZeroDestination
|
||||
}
|
||||
free := cap(client.outgoingEcho) - len(client.outgoingEcho)
|
||||
if free == 0 {
|
||||
return 0, lneto.ErrExhausted
|
||||
}
|
||||
key = client.magichash(pattern, int(size)) & keyHashBits
|
||||
v := internal.SliceReclaim(&client.outgoingEcho)
|
||||
v.key = key
|
||||
|
||||
@@ -59,7 +59,7 @@ const (
|
||||
|
||||
func NewFrame(buf []byte) (Frame, error) {
|
||||
if len(buf) < sizeHeader {
|
||||
return Frame{}, lneto.ErrShortBuffer
|
||||
return Frame{}, lneto.ErrTruncatedFrame
|
||||
}
|
||||
return Frame{buf: buf}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user