good morning!!!!

Skip to content
Snippets Groups Projects
server.go 1.96 KiB
Newer Older
a's avatar
rpc
a committed
package jrpc

import (
	"context"
	"sync/atomic"

a's avatar
a committed
	"gfx.cafe/open/jrpc/codec"
a's avatar
rpc
a committed
	mapset "github.com/deckarep/golang-set"
a's avatar
a committed
	"tuxpa.in/a/zlog/log"
a's avatar
rpc
a committed
)

// Server is an RPC server.
type Server struct {
a's avatar
a committed
	services Handler
a's avatar
rpc
a committed
	run      int32
	codecs   mapset.Set
}

// NewServer creates a new server instance with no registered handlers.
a's avatar
a committed
func NewServer(r Handler) *Server {
a's avatar
rpc
a committed
	server := &Server{
		codecs: mapset.NewSet(),
		run:    1,
	}
a's avatar
a committed
	server.services = r
a's avatar
rpc
a committed
	// Register the default service providing meta information about the RPC service such
	// as the services and methods it offers.
	return server
}

// ServeCodec reads incoming requests from codec, calls the appropriate callback and writes
// the response back using the given codec. It will block until the codec is closed or the
// server is stopped. In either case the codec is closed.
a's avatar
a committed
func (s *Server) ServeCodec(codec codec.ReaderWriter) {
a's avatar
a committed
	defer codec.Close()
a's avatar
rpc
a committed

	// Don't serve if server is stopped.
	if atomic.LoadInt32(&s.run) == 0 {
		return
	}
	// Add the codec to the set so it can be closed by Stop.
	s.codecs.Add(codec)
	defer s.codecs.Remove(codec)

a's avatar
a committed
	// TODO: handle this
	// c := initClient(codec, s.services)
	// <-codec.Closed()
	// c.Close()
a's avatar
rpc
a committed
}

// Stop stops reading new requests, waits for stopPendingRequestTimeout to allow pending
// requests to finish, then closes all codecs which will cancel pending requests and
// subscriptions.
func (s *Server) Stop() {
	if atomic.CompareAndSwapInt32(&s.run, 1, 0) {
		log.Debug().Msg("RPC server shutting down")
		s.codecs.Each(func(c any) bool {
a's avatar
a committed
			c.(codec.ReaderWriter).Close()
a's avatar
rpc
a committed
			return true
		})
	}
}

type peerInfoContextKey struct{}

// PeerInfoFromContext returns information about the client's network connection.
// Use this with the context passed to RPC method handler functions.
//
// The zero value is returned if no connection info is present in ctx.
a's avatar
a committed
func PeerInfoFromContext(ctx context.Context) codec.PeerInfo {
	info, _ := ctx.Value(peerInfoContextKey{}).(codec.PeerInfo)
a's avatar
rpc
a committed
	return info
}