Skip to content

Commit

Permalink
Merge pull request #565 from capnproto/feat/arena-server
Browse files Browse the repository at this point in the history
Allow users to specify the Arena implementation used by server.
  • Loading branch information
lthibault authored May 20, 2024
2 parents f68dd6e + 3274c13 commit e31b23c
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ type Server struct {

// Handler for custom behavior of unknown methods
HandleUnknownMethod func(m capnp.Method) *Method

// Arena implementation
NewArena func() capnp.Arena
}

func (s *Server) String() string {
Expand Down Expand Up @@ -126,7 +129,7 @@ func (srv *Server) Send(ctx context.Context, s capnp.Send) (*capnp.Answer, capnp
if mm == nil {
return capnp.ErrorAnswer(s.Method, capnp.Unimplemented("unimplemented")), func() {}
}
args, err := sendArgsToStruct(s)
args, err := srv.sendArgsToStruct(s)
if err != nil {
return capnp.ErrorAnswer(mm.Method, err), func() {}
}
Expand Down Expand Up @@ -234,11 +237,22 @@ type serverBrand struct {
x any
}

func sendArgsToStruct(s capnp.Send) (capnp.Struct, error) {
func (srv *Server) sendArgsToStruct(s capnp.Send) (capnp.Struct, error) {
if s.PlaceArgs == nil {
return capnp.Struct{}, nil
}
_, seg := capnp.NewMultiSegmentMessage(nil)

if srv.NewArena == nil {
srv.NewArena = func() capnp.Arena {
// TODO: change to single segment?
return capnp.MultiSegment(nil)
}
}

_, seg, err := capnp.NewMessage(srv.NewArena())
if err != nil {
return capnp.Struct{}, err
}
st, err := capnp.NewRootStruct(seg, s.ArgsSize)
if err != nil {
return capnp.Struct{}, err
Expand Down

0 comments on commit e31b23c

Please sign in to comment.