Skip to content

Commit

Permalink
design: def protocol stack
Browse files Browse the repository at this point in the history
  • Loading branch information
lesismal committed Sep 21, 2024
1 parent 788b091 commit 3737403
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions protocol_stack.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package nbio

import (
"net"
)

type Protocol interface {
Parse(c net.Conn, b []byte, ps *ProtocolStack) (net.Conn, []byte, error)
Write(b []byte) (int, error)
}

type ProtocolStack struct {
stack []Protocol
}

func (ps *ProtocolStack) Add(p Protocol) {
ps.stack = append(ps.stack, p)
}

func (ps *ProtocolStack) Delete(p Protocol) {
for i, v := range ps.stack {
if v == p {
ps.stack[i] = nil
return
}
}
}

func (ps *ProtocolStack) Parse(c net.Conn, b []byte, ps_ ProtocolStack) (net.Conn, []byte, error) {
var err error
for _, p := range ps.stack {
if p == nil {
continue
}
c, b, err = p.Parse(c, b, ps)
if err != nil {
break
}
}
return c, b, err
}

func (ps *ProtocolStack) Write(b []byte) (int, error) {
return -1, ErrUnsupported
}

0 comments on commit 3737403

Please sign in to comment.