Documentation ¶
Index ¶
- func ExcludeConn(list []net.Conn, omit net.Conn) []net.Conn
- func ExcludeString(list []string, omit string) []string
- type Builder
- type Capsule
- type Client
- type Request
- type Responder
- type Server
- func (server *Server) Accept(socket_tag string, struct_type reflect.Type, ...)
- func (server *Server) AcceptRequest(socket_tag string, struct_type reflect.Type, ...)
- func (server *Server) Delete(socket net.Conn)
- func (server *Server) Insert(socket net.Conn)
- func (server *Server) TagSocket(socket net.Conn, tag string)
- func (server *Server) UntagSocket(socket net.Conn, tag string)
- type StreamWriter
- type TLJContext
- type TypeStore
- func (store *TypeStore) AddType(inst_type reflect.Type, ptr_type reflect.Type, builder Builder) error
- func (store *TypeStore) BuildType(struct_code uint16, data []byte, context TLJContext) interface{}
- func (store *TypeStore) Format(instance interface{}) ([]byte, error)
- func (store *TypeStore) FormatCapsule(instance interface{}, request_id uint16) ([]byte, error)
- func (store *TypeStore) LookupCode(struct_type reflect.Type) (uint16, bool)
- func (store *TypeStore) NextStruct(socket net.Conn, context TLJContext) (interface{}, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ExcludeConn ¶
Given a slice of net.Conn interfaces, return all interfaces that are not equal to omit.
func ExcludeString ¶
Given a slice of strings, return all strings that are not equal to omit.
Types ¶
type Builder ¶
type Builder func([]byte, TLJContext) interface{}
Builders are functions that take the raw payload in the TLV protocol and parse the JSON and run any other validations that may be nessicary based on the context before returning the struct.
type Capsule ¶
A capsule is used to maintain a session between a client and a server when using sever.AcceptRequest, client.Request, or request.OnResponse.
type Client ¶
type Client struct { Socket net.Conn TypeStore TypeStore Requests map[uint16]map[uint16][]func(interface{}) NextID uint16 Writing *sync.Mutex RequestsManipulation *sync.Mutex Dead chan error }
A Client is used to wrap a net.Conn interface and send TLJ formatted structs through the interface.
func NewClient ¶
Create a new Client with a net.Conn interface and a TypeStore containing all types that will be seen on the network.
type Request ¶
Requests are returned by Clients when stateful requests are made, and can be used to handle server responses with request.OnResponse.
func (*Request) OnResponse ¶
OnResponse is used to define the behaviors used to handle responses to client.Request.
type Server ¶
type Server struct { Listener net.Listener TypeStore TypeStore Tag func(net.Conn, *Server) Tags map[net.Conn][]string Sockets map[string][]net.Conn Events map[string]map[uint16][]func(interface{}, TLJContext) Requests map[string]map[uint16][]func(interface{}, TLJContext) FailedServer chan error FailedSockets chan net.Conn TagManipulation *sync.Mutex InsertRequests *sync.Mutex InsertEvents *sync.Mutex }
A Server wraps a net.Listener and accepts incoming connections, tagging them and running any relevant callbacks on valid TLJ structs received on them.
func NewServer ¶
Create a new server from a net.Listener, a TypeStore, and a tagging function that will assign tags to all accepted sockets.
func (*Server) Accept ¶
func (server *Server) Accept(socket_tag string, struct_type reflect.Type, function func(interface{}, TLJContext))
Create a new callback to be ran when a socket with a certain tag receives a specific type of struct. The server will have no ability to respond statefully to this event.
func (*Server) AcceptRequest ¶
func (server *Server) AcceptRequest(socket_tag string, struct_type reflect.Type, function func(interface{}, TLJContext))
Create a new callback to be ran when a socket with a certain tag receives a capsule containing a specific type of struct. The callback accepts a responder which can be used to respond to the client statefully.
func (*Server) Insert ¶
Tag the socket then read an structs from this socket until the socket is closed.
type StreamWriter ¶
A StreamWriter is a Client that can only be used to send one type. Because of this restriction it takes the reflect.Type directly and avoids a reflect call on every write.
func NewStreamWriter ¶
func NewStreamWriter(conn net.Conn, type_store TypeStore, struct_type reflect.Type) (StreamWriter, error)
Create a new StreamWriter using a net.Conn, a TypeStore, and the reflect.Type of the type this StreamWriter should be capable of sending.
func (*StreamWriter) Write ¶
func (writer *StreamWriter) Write(obj interface{}) error
Write a struct using the StreamWriter.
type TLJContext ¶
Context about TLJ events so Server callbacks can respond statefully and Builders can conditionally validate data and verify signatures.
func (*TLJContext) Respond ¶
func (context *TLJContext) Respond(object interface{}) error
Respond is used to send a struct down the socket the sent a request with client.Request
type TypeStore ¶
type TypeStore struct { Types map[uint16]Builder TypeCodes map[reflect.Type]uint16 NextID uint16 InsertType *sync.Mutex }
A TypeStore stores the information needed to marshal, unmsrahsl, and recognize all types passed between all other instances of TLJ that are communicated with.
func NewTypeStore ¶
func NewTypeStore() TypeStore
Create a new TypeStore with type 0 being a Capsule.
func (*TypeStore) AddType ¶
func (store *TypeStore) AddType(inst_type reflect.Type, ptr_type reflect.Type, builder Builder) error
Insert a new type into the TypeStore by providing a reflect.Type of the struct and a pointer to the struct, as well as the Builder that will be used to construct the type. Types must be JSON serializable.
func (*TypeStore) BuildType ¶
func (store *TypeStore) BuildType(struct_code uint16, data []byte, context TLJContext) interface{}
Call the Builder function for a given type on some data if the type exists in the type store, return nil if the type does not exist.
func (*TypeStore) Format ¶
Take any JSON serializable struct that is in the TypeStore and return the byte sequence to send on the network to deliver the struct to the other instance of TLJ, as well as any errors.
func (*TypeStore) FormatCapsule ¶
Take a struct and format it inside of a Capsule so it can be sent statefully to another TLJ instance.
func (*TypeStore) LookupCode ¶
Given the reflect.Type of a type in the TypeStore, return the uint16 that is used to identify this type over the network, and a boolean to indicate if the type was present in the TypeStore.
func (*TypeStore) NextStruct ¶
func (store *TypeStore) NextStruct(socket net.Conn, context TLJContext) (interface{}, error)
Read a struct from a net.Conn interface using the types contained in a TypeStore.