rnet

package
v0.0.0-...-9fbb145 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 13, 2021 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NilServiceID = ServiceID(uuid.Nil)

NilServiceID is the empty ServiceID

View Source
var ServiceFactory = serviceFactory{
	// contains filtered or unexported fields
}

ServiceFactory is the global service factory to instantiate Services

Functions

func UnregisterService

func UnregisterService(name string) error

UnregisterService removes a service from the global pool.

Types

type Context

type Context struct {
	// contains filtered or unexported fields
}

Context represents the methods that are available to a service.

func (*Context) GetNetBlocks

func (c *Context) GetNetBlocks(si *network.ServerIdentity) int

func (*Context) RegisterProcessor

func (c *Context) RegisterProcessor(p network.Processor, msgType network.MessageTypeID)

RegisterProcessor overrides the RegisterProcessor methods of the Dispatcher. It delegates the dispatching to the serviceManager.

func (*Context) RegisterProcessorFunc

func (c *Context) RegisterProcessorFunc(msgType network.MessageTypeID, fn func(*network.Envelope))

RegisterProcessorFunc takes a message-type and a function that will be called if this message-type is received.

func (*Context) SendRaw

func (c *Context) SendRaw(si *network.ServerIdentity, msg interface{}, bForeConnect bool) error

SendRaw sends a message to the ServerIdentity.

func (*Context) ServerIdentity

func (c *Context) ServerIdentity() *network.ServerIdentity

ServerIdentity returns this server's identity.

func (*Context) Service

func (c *Context) Service(name string) Service

Service returns the corresponding service.

func (*Context) ServiceID

func (c *Context) ServiceID() ServiceID

ServiceID returns the service-id.

func (*Context) String

func (c *Context) String() string

String returns the host it's running on.

type GenericConfig

type GenericConfig struct {
	Data []byte
}

GenericConfig is a config that can hold any type of specific configs for protocols. It is passed down to the service NewProtocol function.

type NewServiceFunc

type NewServiceFunc func(c *Context) (Service, error)

NewServiceFunc is the type of a function that is used to instantiate a given Service A service is initialized with a Server (to send messages to someone).

type Server

type Server struct {
	*network.Router

	IsStarted bool
	// contains filtered or unexported fields
}

Server connects the Router and the Services together. It sets up everything and returns once a working network has been set up.

func NewKcpServer

func NewKcpServer(addr string) *Server

func NewServerKCPWithListenAddr

func NewServerKCPWithListenAddr(e *network.ServerIdentity, listenAddr string) *Server

NewServerKCPWithListenAddr returns a new Server out of a private-key and its related address within the ServerIdentity. The server will use a KcpRouter listening on the given address as Router.

func (*Server) Address

func (c *Server) Address() network.Address

Address returns the address used by the Router.

func (*Server) AdjustConnect

func (c *Server) AdjustConnect(list []*common.Cnode)

CloseConnect close remote connection

func (*Server) Close

func (c *Server) Close() error

Close closes the Router

func (*Server) GetService

func (c *Server) GetService(name string) Service

GetService is kept for backward-compatibility.

func (*Server) Service

func (c *Server) Service(name string) Service

Service returns the service with the given name.

func (*Server) Start

func (c *Server) Start()

Start makes the router listen on their respective ports. It returns once all servers are started.

type Service

type Service interface {
	// NewProtocol is called upon a ProtocolInstance's first message when Rnet needs
	// to instantiate the protocol. A Service is expected to manually create
	// the ProtocolInstance it is using. If a Service returns (nil,nil), that
	// means this Service lets Rnet handle the protocol instance.
	//NewProtocol(*TreeNodeInstance, *GenericConfig) (ProtocolInstance, error)
	// ProcessClientRequest is called when a message from an
	// external client is received by the websocket for this
	// service. It returns a message that will be sent back to the
	// client. The returned error will be formatted as a websocket
	// error code 4000, using the string form of the error as the message.
	ProcessClientRequest(req *http.Request, handler string, msg []byte) (reply []byte, err error)
	// Processor makes a Service being able to handle any kind of packets
	// directly from the network. It is used for inter service communications,
	// which are mostly single packets with no or little interactions needed. If
	// a complex logic is used for these messages, it's best to put that logic
	// into a ProtocolInstance that the Service will launch, since there's nicer
	// utilities for ProtocolInstance.
	network.Processor
}

Service is a generic interface to define any type of services. A Service has multiple roles:

  • Processing websocket client requests with ProcessClientRequests
  • Handling rnet information to ProtocolInstances created with NewProtocol
  • Handling any kind of messages between Services between different hosts with the Processor interface

type ServiceID

type ServiceID uuid.UUID

ServiceID is a type to represent a uuid for a Service

func RegisterNewService

func RegisterNewService(name string, fn NewServiceFunc) (ServiceID, error)

RegisterNewService is a wrapper around service factory

func (ServiceID) Equal

func (s ServiceID) Equal(s2 ServiceID) bool

Equal returns true if and only if s2 equals this ServiceID.

func (ServiceID) IsNil

func (s ServiceID) IsNil() bool

IsNil returns true iff the ServiceID is Nil

func (ServiceID) String

func (s ServiceID) String() string

String returns the string representation of this ServiceID

type ServiceProcessor

type ServiceProcessor struct {
	*Context
	// contains filtered or unexported fields
}

ServiceProcessor allows for an easy integration of external messages into the Services. You have to embed it into your Service-struct as a pointer. It will process client requests that have been registered with RegisterMessage.

func NewServiceProcessor

func NewServiceProcessor(c *Context) *ServiceProcessor

NewServiceProcessor initializes your ServiceProcessor.

func (*ServiceProcessor) Process

func (p *ServiceProcessor) Process(env *network.Envelope)

Process implements the Processor interface and dispatches ClientRequest messages.

func (*ServiceProcessor) ProcessClientRequest

func (p *ServiceProcessor) ProcessClientRequest(req *http.Request, path string, buf []byte) ([]byte, error)

ProcessClientRequest takes a request from a websocket client, calculates the reply and sends it back. It uses the path to find the appropriate handler- function. It implements the Server interface.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL