Documentation ¶
Index ¶
- Variables
- func AcceptMuxedListener(ctx context.Context, lis net.Listener, srv *Server) error
- func NewMuxedConn(conn net.Conn, outbound bool) (network.MuxedConn, error)
- func NewMuxedConnWithRwc(ctx context.Context, rwc io.ReadWriteCloser, outbound bool) (network.MuxedConn, error)
- func NewPipeStream(ctx context.Context) (Stream, Stream)
- type CallData
- func (*CallData) Descriptor() ([]byte, []int)deprecated
- func (this *CallData) EqualVT(that *CallData) bool
- func (x *CallData) GetComplete() bool
- func (x *CallData) GetData() []byte
- func (x *CallData) GetDataIsZero() bool
- func (x *CallData) GetError() string
- func (m *CallData) MarshalToSizedBufferVT(dAtA []byte) (int, error)
- func (m *CallData) MarshalToVT(dAtA []byte) (int, error)
- func (m *CallData) MarshalVT() (dAtA []byte, err error)
- func (*CallData) ProtoMessage()
- func (x *CallData) ProtoReflect() protoreflect.Message
- func (x *CallData) Reset()
- func (m *CallData) SizeVT() (n int)
- func (x *CallData) String() string
- func (m *CallData) UnmarshalVT(dAtA []byte) error
- func (p *CallData) Validate() error
- type CallStart
- func (*CallStart) Descriptor() ([]byte, []int)deprecated
- func (this *CallStart) EqualVT(that *CallStart) bool
- func (x *CallStart) GetData() []byte
- func (x *CallStart) GetDataIsZero() bool
- func (x *CallStart) GetRpcMethod() string
- func (x *CallStart) GetRpcService() string
- func (m *CallStart) MarshalToSizedBufferVT(dAtA []byte) (int, error)
- func (m *CallStart) MarshalToVT(dAtA []byte) (int, error)
- func (m *CallStart) MarshalVT() (dAtA []byte, err error)
- func (*CallStart) ProtoMessage()
- func (x *CallStart) ProtoReflect() protoreflect.Message
- func (x *CallStart) Reset()
- func (m *CallStart) SizeVT() (n int)
- func (x *CallStart) String() string
- func (m *CallStart) UnmarshalVT(dAtA []byte) error
- func (p *CallStart) Validate() error
- type Client
- type ClientRPC
- func (r *ClientRPC) Close()
- func (r *ClientRPC) Context() context.Context
- func (r *ClientRPC) HandleCallData(pkt *CallData) error
- func (r *ClientRPC) HandleCallStart(pkt *CallStart) error
- func (r *ClientRPC) HandlePacket(msg *Packet) error
- func (r *ClientRPC) HandlePacketData(data []byte) error
- func (r *ClientRPC) HandleStreamClose(closeErr error)
- func (r *ClientRPC) ReadAll() ([][]byte, error)
- func (r *ClientRPC) ReadOne() ([]byte, error)
- func (r *ClientRPC) Start(writer Writer, writeFirstMsg bool, firstMsg []byte) error
- type CloseHandler
- type HTTPServer
- type Handler
- type Invoker
- type Message
- type MsgStream
- type Mux
- type OpenStreamFunc
- type Packet
- func (*Packet) Descriptor() ([]byte, []int)deprecated
- func (this *Packet) EqualVT(that *Packet) bool
- func (m *Packet) GetBody() isPacket_Body
- func (x *Packet) GetCallData() *CallData
- func (x *Packet) GetCallStart() *CallStart
- func (m *Packet) MarshalToSizedBufferVT(dAtA []byte) (int, error)
- func (m *Packet) MarshalToVT(dAtA []byte) (int, error)
- func (m *Packet) MarshalVT() (dAtA []byte, err error)
- func (*Packet) ProtoMessage()
- func (x *Packet) ProtoReflect() protoreflect.Message
- func (x *Packet) Reset()
- func (m *Packet) SizeVT() (n int)
- func (x *Packet) String() string
- func (m *Packet) UnmarshalVT(dAtA []byte) error
- func (p *Packet) Validate() error
- type PacketHandler
- type PacketReaderWriter
- type Packet_CallData
- type Packet_CallStart
- type RwcConn
- func (p *RwcConn) Close() error
- func (p *RwcConn) LocalAddr() net.Addr
- func (p *RwcConn) Read(b []byte) (n int, err error)
- func (p *RwcConn) RemoteAddr() net.Addr
- func (p *RwcConn) SetDeadline(t time.Time) error
- func (p *RwcConn) SetReadDeadline(t time.Time) error
- func (p *RwcConn) SetWriteDeadline(t time.Time) error
- func (p *RwcConn) Write(pkt []byte) (n int, err error)
- type Server
- type ServerRPC
- func (r *ServerRPC) Close()
- func (r *ServerRPC) Context() context.Context
- func (r *ServerRPC) HandleCallData(pkt *CallData) error
- func (r *ServerRPC) HandleCallStart(pkt *CallStart) error
- func (r *ServerRPC) HandlePacket(msg *Packet) error
- func (r *ServerRPC) HandleStreamClose(closeErr error)
- func (r *ServerRPC) SetWriter(w Writer)
- func (r *ServerRPC) Wait(ctx context.Context) error
- type Stream
- type WebSocketConn
- func (w *WebSocketConn) AcceptStream() (io.ReadWriteCloser, error)
- func (w *WebSocketConn) Close() error
- func (w *WebSocketConn) GetOpenStreamFunc() OpenStreamFunc
- func (w *WebSocketConn) GetWebSocket() *websocket.Conn
- func (w *WebSocketConn) OpenStream(ctx context.Context, msgHandler PacketHandler, closeHandler CloseHandler) (Writer, error)
- type Writer
Constants ¶
This section is empty.
Variables ¶
var ( // ErrUnimplemented is returned if the RPC method was not implemented. ErrUnimplemented = errors.New("unimplemented") // ErrCompleted is returned if a message is received after the rpc was completed. ErrCompleted = errors.New("unexpected packet after rpc was completed") // ErrUnrecognizedPacket is returned if the packet type was not recognized. ErrUnrecognizedPacket = errors.New("unrecognized packet type") // ErrEmptyPacket is returned if nothing is specified in a packet. ErrEmptyPacket = errors.New("invalid empty packet") // ErrInvalidMessage indicates the message failed to parse. ErrInvalidMessage = errors.New("invalid message") // ErrEmptyMethodID is returned if the method id was empty. ErrEmptyMethodID = errors.New("method id empty") // ErrEmptyServiceID is returned if the service id was empty. ErrEmptyServiceID = errors.New("service id empty") )
var ( ErrInvalidLength = fmt.Errorf("proto: negative length found during unmarshaling") ErrIntOverflow = fmt.Errorf("proto: integer overflow") ErrUnexpectedEndOfGroup = fmt.Errorf("proto: unexpected end of group") )
var File_github_com_aperturerobotics_starpc_srpc_rpcproto_proto protoreflect.FileDescriptor
Functions ¶
func AcceptMuxedListener ¶ added in v0.8.3
AcceptMuxedListener accepts incoming connections from a net.Listener.
Uses the default mplex muxer.
func NewMuxedConn ¶ added in v0.8.2
NewMuxedConn constructs a new MuxedConn from a net.Conn.
func NewMuxedConnWithRwc ¶ added in v0.10.8
func NewMuxedConnWithRwc(ctx context.Context, rwc io.ReadWriteCloser, outbound bool) (network.MuxedConn, error)
NewMuxedConnWithRwc builds a new MuxedConn with a io.ReadWriteCloser.
Types ¶
type CallData ¶
type CallData struct { // Data contains the packet in the sequence. Data []byte `protobuf:"bytes,1,opt,name=data,proto3" json:"data,omitempty"` // DataIsZero indicates Data is set with an empty message. DataIsZero bool `protobuf:"varint,2,opt,name=data_is_zero,json=dataIsZero,proto3" json:"data_is_zero,omitempty"` // Complete indicates the RPC call is completed. Complete bool `protobuf:"varint,3,opt,name=complete,proto3" json:"complete,omitempty"` // Error contains any error that caused the RPC to fail. // If set, implies complete=true. Error string `protobuf:"bytes,4,opt,name=error,proto3" json:"error,omitempty"` // contains filtered or unexported fields }
CallData contains a message in a streaming RPC sequence.
func (*CallData) Descriptor
deprecated
func (*CallData) GetComplete ¶
func (*CallData) GetDataIsZero ¶ added in v0.1.8
func (*CallData) MarshalToSizedBufferVT ¶
func (*CallData) ProtoMessage ¶
func (*CallData) ProtoMessage()
func (*CallData) ProtoReflect ¶
func (x *CallData) ProtoReflect() protoreflect.Message
func (*CallData) UnmarshalVT ¶
type CallStart ¶
type CallStart struct { // RpcService is the service to contact. // Must be set. RpcService string `protobuf:"bytes,1,opt,name=rpc_service,json=rpcService,proto3" json:"rpc_service,omitempty"` // RpcMethod is the RPC method to call. // Must be set. RpcMethod string `protobuf:"bytes,2,opt,name=rpc_method,json=rpcMethod,proto3" json:"rpc_method,omitempty"` // Data contains the request or the first message in the stream. // Optional if streaming. Data []byte `protobuf:"bytes,3,opt,name=data,proto3" json:"data,omitempty"` // DataIsZero indicates Data is set with an empty message. DataIsZero bool `protobuf:"varint,4,opt,name=data_is_zero,json=dataIsZero,proto3" json:"data_is_zero,omitempty"` // contains filtered or unexported fields }
CallStart requests starting a new RPC call.
func (*CallStart) Descriptor
deprecated
func (*CallStart) GetDataIsZero ¶ added in v0.1.8
func (*CallStart) GetRpcMethod ¶
func (*CallStart) GetRpcService ¶
func (*CallStart) MarshalToSizedBufferVT ¶
func (*CallStart) ProtoMessage ¶
func (*CallStart) ProtoMessage()
func (*CallStart) ProtoReflect ¶
func (x *CallStart) ProtoReflect() protoreflect.Message
func (*CallStart) UnmarshalVT ¶
type Client ¶
type Client interface { // Invoke executes a unary RPC with the remote. Invoke(ctx context.Context, service, method string, in, out Message) error // NewStream starts a streaming RPC with the remote & returns the stream. // firstMsg is optional. NewStream(ctx context.Context, service, method string, firstMsg Message) (Stream, error) }
Client implements a SRPC client which can initiate RPC streams.
func NewClient ¶
func NewClient(openStream OpenStreamFunc) Client
NewClient constructs a client with a OpenStreamFunc.
func NewClientWithConn ¶ added in v0.8.2
NewClientWithConn constructs the muxer and the client.
uses libp2p mplex
func NewClientWithMuxedConn ¶ added in v0.3.6
NewClientWithMuxedConn constructs a new client with a MuxedConn.
type ClientRPC ¶
type ClientRPC struct {
// contains filtered or unexported fields
}
ClientRPC represents the client side of an on-going RPC call message stream. Not concurrency safe: use a mutex if calling concurrently.
func NewClientRPC ¶
NewClientRPC constructs a new ClientRPC session and writes CallStart. the writer will be closed when the ClientRPC completes. service and method must be specified. must call Start after creating the RPC object.
func (*ClientRPC) Close ¶
func (r *ClientRPC) Close()
Close releases any resources held by the ClientRPC. not concurrency safe with HandlePacket.
func (*ClientRPC) HandleCallData ¶
HandleCallData handles the call data packet.
func (*ClientRPC) HandleCallStart ¶
HandleCallStart handles the call start packet.
func (*ClientRPC) HandlePacket ¶
HandlePacket handles an incoming parsed message packet. Not concurrency safe: use a mutex if calling concurrently.
func (*ClientRPC) HandlePacketData ¶
HandlePacketData handles an incoming unparsed message packet. Not concurrency safe: use a mutex if calling concurrently.
func (*ClientRPC) HandleStreamClose ¶ added in v0.8.0
HandleStreamClose handles the incoming stream closing w/ optional error.
func (*ClientRPC) ReadAll ¶
ReadAll reads all returned Data packets and returns any error. intended for use with unary rpcs.
type CloseHandler ¶ added in v0.8.0
type CloseHandler = func(closeErr error)
CloseHandler handles the stream closing with an optional error.
type HTTPServer ¶
type HTTPServer struct {
// contains filtered or unexported fields
}
HTTPServer implements the SRPC server.
func NewHTTPServer ¶
func NewHTTPServer(mux Mux, path string) (*HTTPServer, error)
NewHTTPServer builds a http server / handler. if path is empty, serves on all routes.
func (*HTTPServer) ServeHTTP ¶
func (s *HTTPServer) ServeHTTP(w http.ResponseWriter, r *http.Request)
type Handler ¶
type Handler interface { // Invoker invokes the methods. Invoker // GetServiceID returns the ID of the service. GetServiceID() string // GetMethodIDs returns the list of methods for the service. GetMethodIDs() []string }
Handler describes a SRPC call handler implementation.
type Invoker ¶ added in v0.10.7
type Invoker interface { // InvokeMethod invokes the method matching the service & method ID. // Returns false, nil if not found. // If service string is empty, ignore it. InvokeMethod(serviceID, methodID string, strm Stream) (bool, error) }
Invoker describes a SRPC service invoker.
type MsgStream ¶ added in v0.4.0
type MsgStream struct {
// contains filtered or unexported fields
}
MsgStream implements the stream interface passed to implementations.
func NewMsgStream ¶ added in v0.4.0
NewMsgStream constructs a new Stream with a ClientRPC. dataCh should be closed when no more messages will arrive.
func (*MsgStream) CloseSend ¶ added in v0.4.0
CloseSend signals to the remote that we will no longer send any messages.
type Mux ¶
type Mux interface { // Invoker invokes the methods. Invoker // Register registers a new RPC method handler (service). Register(handler Handler) error // HasService checks if the service ID exists in the handlers. HasService(serviceID string) bool // HasServiceMethod checks if <service-id, method-id> exists in the handlers. HasServiceMethod(serviceID, methodID string) bool }
Mux contains a set of <service, method> handlers.
type OpenStreamFunc ¶
type OpenStreamFunc = func( ctx context.Context, msgHandler PacketHandler, closeHandler CloseHandler, ) (Writer, error)
OpenStreamFunc opens a stream with a remote. msgHandler must not be called concurrently.
func NewOpenStreamWithMuxedConn ¶ added in v0.3.6
func NewOpenStreamWithMuxedConn(conn network.MuxedConn) OpenStreamFunc
NewOpenStreamWithMuxedConn constructs a OpenStream func with a MuxedConn.
func NewServerPipe ¶
func NewServerPipe(server *Server) OpenStreamFunc
NewServerPipe constructs a open stream func which creates an in-memory Pipe Stream with the given Server. Starts read pumps for both. Starts the HandleStream function on the server in a separate goroutine.
type Packet ¶
type Packet struct { // Body is the packet body. // // Types that are assignable to Body: // // *Packet_CallStart // *Packet_CallData Body isPacket_Body `protobuf_oneof:"body"` // contains filtered or unexported fields }
Packet is a message sent over a srpc packet connection.
func NewCallDataPacket ¶
NewCallDataPacket constructs a new CallData packet.
func NewCallStartPacket ¶
NewCallStartPacket constructs a new CallStart packet.
func (*Packet) Descriptor
deprecated
func (*Packet) GetCallData ¶
func (*Packet) GetCallStart ¶
func (*Packet) MarshalToSizedBufferVT ¶
func (*Packet) ProtoMessage ¶
func (*Packet) ProtoMessage()
func (*Packet) ProtoReflect ¶
func (x *Packet) ProtoReflect() protoreflect.Message
func (*Packet) UnmarshalVT ¶
type PacketHandler ¶
PacketHandler handles a packet.
pkt is optional (can be nil) if closeErr is set, the stream is closed after pkt.
type PacketReaderWriter ¶
type PacketReaderWriter struct {
// contains filtered or unexported fields
}
PacketReaderWriter reads and writes packets from a io.ReadWriter. Uses a LittleEndian uint32 length prefix.
func NewPacketReadWriter ¶
func NewPacketReadWriter(rw io.ReadWriteCloser) *PacketReaderWriter
NewPacketReadWriter constructs a new read/writer.
func (*PacketReaderWriter) Close ¶
func (r *PacketReaderWriter) Close() error
Close closes the packet rw.
func (*PacketReaderWriter) ReadPump ¶
func (r *PacketReaderWriter) ReadPump(cb PacketHandler, closed CloseHandler)
ReadPump executes the read pump in a goroutine.
calls the handler when closed or returning an error
func (*PacketReaderWriter) ReadToHandler ¶ added in v0.8.0
func (r *PacketReaderWriter) ReadToHandler(cb PacketHandler) error
ReadToHandler reads data to the given handler. Does not handle closing the stream, use ReadPump instead.
func (*PacketReaderWriter) WritePacket ¶
func (r *PacketReaderWriter) WritePacket(p *Packet) error
WritePacket writes a packet to the writer.
type Packet_CallData ¶
type Packet_CallData struct { // CallData is a message in a streaming RPC sequence. CallData *CallData `protobuf:"bytes,2,opt,name=call_data,json=callData,proto3,oneof"` }
func (*Packet_CallData) EqualVT ¶ added in v0.10.0
func (this *Packet_CallData) EqualVT(thatIface isPacket_Body) bool
func (*Packet_CallData) MarshalToSizedBufferVT ¶
func (m *Packet_CallData) MarshalToSizedBufferVT(dAtA []byte) (int, error)
func (*Packet_CallData) MarshalToVT ¶
func (m *Packet_CallData) MarshalToVT(dAtA []byte) (int, error)
func (*Packet_CallData) SizeVT ¶
func (m *Packet_CallData) SizeVT() (n int)
type Packet_CallStart ¶
type Packet_CallStart struct { // CallStart initiates a new call. CallStart *CallStart `protobuf:"bytes,1,opt,name=call_start,json=callStart,proto3,oneof"` }
func (*Packet_CallStart) EqualVT ¶ added in v0.10.0
func (this *Packet_CallStart) EqualVT(thatIface isPacket_Body) bool
func (*Packet_CallStart) MarshalToSizedBufferVT ¶
func (m *Packet_CallStart) MarshalToSizedBufferVT(dAtA []byte) (int, error)
func (*Packet_CallStart) MarshalToVT ¶
func (m *Packet_CallStart) MarshalToVT(dAtA []byte) (int, error)
func (*Packet_CallStart) SizeVT ¶
func (m *Packet_CallStart) SizeVT() (n int)
type RwcConn ¶ added in v0.10.8
type RwcConn struct {
// contains filtered or unexported fields
}
RwcConn implements a Conn with a buffered ReadWriteCloser.
func NewRwcConn ¶ added in v0.10.8
func NewRwcConn( ctx context.Context, rwc io.ReadWriteCloser, laddr, raddr net.Addr, bufferPacketN int, ) *RwcConn
NewRwcConn constructs a new packet conn and starts the rx pump.
func (*RwcConn) Close ¶ added in v0.10.8
Close closes the connection. Any blocked ReadFrom or WriteTo operations will be unblocked and return errors.
func (*RwcConn) Read ¶ added in v0.10.8
Read reads data from the connection. Read can be made to time out and return an error after a fixed time limit; see SetDeadline and SetReadDeadline.
func (*RwcConn) RemoteAddr ¶ added in v0.10.8
RemoteAddr returns the bound remote network address.
func (*RwcConn) SetDeadline ¶ added in v0.10.8
SetDeadline sets the read and write deadlines associated with the connection. It is equivalent to calling both SetReadDeadline and SetWriteDeadline.
A deadline is an absolute time after which I/O operations fail instead of blocking. The deadline applies to all future and pending I/O, not just the immediately following call to Read or Write. After a deadline has been exceeded, the connection can be refreshed by setting a deadline in the future.
If the deadline is exceeded a call to Read or Write or to other I/O methods will return an error that wraps os.ErrDeadlineExceeded. This can be tested using errors.Is(err, os.ErrDeadlineExceeded). The error's Timeout method will return true, but note that there are other possible errors for which the Timeout method will return true even if the deadline has not been exceeded.
An idle timeout can be implemented by repeatedly extending the deadline after successful ReadFrom or WriteTo calls.
A zero value for t means I/O operations will not time out.
func (*RwcConn) SetReadDeadline ¶ added in v0.10.8
SetReadDeadline sets the deadline for future ReadFrom calls and any currently-blocked ReadFrom call. A zero value for t means ReadFrom will not time out.
func (*RwcConn) SetWriteDeadline ¶ added in v0.10.8
SetWriteDeadline sets the deadline for future WriteTo calls and any currently-blocked WriteTo call. Even if write times out, it may return n > 0, indicating that some of the data was successfully written. A zero value for t means WriteTo will not time out.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server handles incoming RPC streams with a mux.
func (*Server) AcceptMuxedConn ¶ added in v0.3.5
AcceptMuxedConn runs a loop which calls Accept on a muxer to handle streams.
Starts HandleStream in a separate goroutine to handle the stream. Returns context.Canceled or io.EOF when the loop is complete / closed.
func (*Server) GetInvoker ¶ added in v0.10.7
GetInvoker returns the invoker.
func (*Server) HandleStream ¶ added in v0.3.5
HandleStream handles an incoming ReadWriteCloser stream.
type ServerRPC ¶
type ServerRPC struct {
// contains filtered or unexported fields
}
ServerRPC represents the server side of an on-going RPC call message stream. Not concurrency safe: use a mutex if calling concurrently.
func NewServerRPC ¶
NewServerRPC constructs a new ServerRPC session. note: call SetWriter before handling any incoming messages.
func (*ServerRPC) Close ¶
func (r *ServerRPC) Close()
Close releases any resources held by the ServerRPC. not concurrency safe with HandlePacket.
func (*ServerRPC) HandleCallData ¶
HandleCallData handles the call data packet.
func (*ServerRPC) HandleCallStart ¶
HandleCallStart handles the call start packet.
func (*ServerRPC) HandlePacket ¶
HandlePacket handles an incoming parsed message packet. Not concurrency safe: use a mutex if calling concurrently.
func (*ServerRPC) HandleStreamClose ¶ added in v0.8.0
HandleStreamClose handles the incoming stream closing w/ optional error.
type Stream ¶
type Stream interface { // Context is canceled when the Stream is no longer valid. Context() context.Context // MsgSend sends the message to the remote. MsgSend(msg Message) error // MsgRecv receives an incoming message from the remote. // Parses the message into the object at msg. MsgRecv(msg Message) error // CloseSend signals to the remote that we will no longer send any messages. CloseSend() error // Close closes the stream. Close() error }
Stream is a handle to an on-going bi-directional or one-directional stream RPC handle.
type WebSocketConn ¶
type WebSocketConn struct {
// contains filtered or unexported fields
}
WebSocketConn implements the p2p multiplexer over a WebSocket.
func NewWebSocketConn ¶
func NewWebSocketConn(ctx context.Context, conn *websocket.Conn, isServer bool) (*WebSocketConn, error)
NewWebSocketConn constructs a new WebSocket connection.
func (*WebSocketConn) AcceptStream ¶
func (w *WebSocketConn) AcceptStream() (io.ReadWriteCloser, error)
AcceptStream accepts an incoming stream.
func (*WebSocketConn) GetOpenStreamFunc ¶
func (w *WebSocketConn) GetOpenStreamFunc() OpenStreamFunc
GetOpenStreamFunc returns the OpenStream func.
func (*WebSocketConn) GetWebSocket ¶
func (w *WebSocketConn) GetWebSocket() *websocket.Conn
GetWebSocket returns the web socket conn.
func (*WebSocketConn) OpenStream ¶
func (w *WebSocketConn) OpenStream(ctx context.Context, msgHandler PacketHandler, closeHandler CloseHandler) (Writer, error)
OpenStream tries to open a stream with the remote.