connman

package
v0.0.0-...-92f3312 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2020 License: Apache-2.0 Imports: 6 Imported by: 2

Documentation

Overview

Connman implements a connection manager that listens for TCP connections, accepts them and then manages the sessions providing received buffers on a channel to the application. In addition to providing a TCP listener, the manager can also provide a UDP listener, and allow the user application to create TCP connections and to send buffers on those connections and via UDP.

The connection manager sturct is created with NewManager() which accepts a listen port and a channel on which Sess_data objects are written. The listener is invoked and when a connection is accepted a connection reader is started and a ST_NEW Sess_data object is written on the user's channel. The reader sends all data via the channel (ST_DATA) and if the session is disconnected a Sess_data object with the ST_DISC state is written and the connection is cleaned up (no need for user to invoke the Close function in a disconnect case.

When a user establishes a connection the remote IP address and a session id (name) are supplied along with a communication channel. The channel may be the same channel supplied when the connection manager object was created or it may be a different channel.

Data received from either a UDP listener, or on a connected TCP session is bundled into a Sess_data struct and placed onto the appropriate channel. The struct contains, in addition to the received buffer, the ID of the session that can be used on a generic Write command to, the current state of the session (ST_ constants), and a string indicating some useful (humanised) data about the session.

Index

Constants

View Source
const (
	// state describing the session data returned on the user's channel
	ST_NEW      = iota // new connection
	ST_DATA            // data received
	ST_DISC            // disconnected connection
	ST_ACCEPTED        // session has been accepted
)
View Source
const (
	LISTENER string = "l0" // our magic session id for the default litener
)
View Source
const (
	ST_CLOSING int = 1 // close already in progress
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Cmgr

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

func NewManager

func NewManager(port string, data2usr chan *Sess_data) *Cmgr

NewManager creates a new connection manager object. Normally the establishment of a TCP listener is a two step process (create the manager, and then allocate a TCP listener), however this can be reduced to a single call if the port is greater than zero. In this case a TCP listener will be started on the port and "attached" to the manager with any data received by sessions connected to the port sent to the user application using the channel provided. If the listener cannot be established a nil object is returned. To create a UDP listener, the two step process is needed and this function is invoked with a port of zero and a nill data2user pointer.

func (*Cmgr) Close

func (this *Cmgr) Close(id string)

Close closess the named connection.

func (*Cmgr) Connect

func (this *Cmgr) Connect(target string, uid string, data2usr chan *Sess_data) (err error)

Connect establishes a connection to the target process (ip:port) and starts a reader listening for data on the session. Any received data will be forwarded to the user application via the channel provided.

func (*Cmgr) Get_conn

func (cm *Cmgr) Get_conn(id string) (conn *connection)

-------------------------------------------------------------------------------------------------------------------

Get_conn returns the connection given the connection id.

func (*Cmgr) Get_udp_writer

func (c *Cmgr) Get_udp_writer(id string, addr string) (newcp *connection, err error)

Get_udp_writer returns a writer that can be used to write directly to the address. Faster than the generic id oriented writes because there is no need for id lookup to find the writer, and the address is already constructed.

func (*Cmgr) Get_writer

func (c *Cmgr) Get_writer(id string) *connection

Get_writer accepts a connection name, or UDP listener name, return the struct that can be used as the direct writer.

func (*Cmgr) List_stats

func (this *Cmgr) List_stats()

List_stats lists the current statistics about connections to the standard output device.

func (*Cmgr) Listen

func (this *Cmgr) Listen(kind string, port string, iface string, data2usr chan *Sess_data) (lid string, err error)

Listen starts a TCP listener, allowing the caller to supply type (tcp, tcp4, tcp6) and interface (0.0.0.0 for any) then opens and binds to the socket. A goroutine is started to actually do the listening and will accept sessions that connect. Generally the listen method will be driven during the construction of a cmgr object, though I user can use this if more than one listen port is required.

Returns an ID which identifies the listener, and a boolean set to true if the listener was established successfully.

func (*Cmgr) Listen_mc

func (this *Cmgr) Listen_mc(ifname string, addr string, data2usr chan *Sess_data) (sessid string, err error)

Listen_mc joins a multicast group as a listener on the named interface

func (*Cmgr) Listen_tcp

func (this *Cmgr) Listen_tcp(port string, data2usr chan *Sess_data) (string, error)

Provides a more consistent interface with the Listen_udp name convention and is just a wrapper for Listen().

func (*Cmgr) Listen_udp

func (this *Cmgr) Listen_udp(port int, data2usr chan *Sess_data) (uid string, err error)

Listen_udp starts a UDP listener which will forward received data back to the application using the supplied channel. The listener ID is returned along with a boolean indication of success (true) or failure. The uid is the user created session id string that is used to send buffers that are not related to a session_data struct.

func (*Cmgr) String2udp_addr

func (this *Cmgr) String2udp_addr(astr string) (addr *net.UDPAddr, err error)

String2udp_addr builds an address from the IP address:port string passed in; for use with Write_udp_addr().

func (*Cmgr) TLS_listen

func (this *Cmgr) TLS_listen(kind string, port string, iface string, data2usr chan *Sess_data, cert_base string) (lid string, err error)

Starts a listener (TCP or UDP) for tls connections on the indicated port and interface. The data2usr channel is used to pass back connections when accepted in the same manner as is done by the Listen() function in this package. The actual listener is started in a parallel goroutine which passes back accept information on the channel; this function returns and the caller does not need to invoke as a goroutine.

The cert_base parameter is the filename path (e.g. /usr2/foo/bar/progx) to which '.key' and '.cert' will be appended to create the cert and key filenames. If these files do not exist, then a self-signed certificate and key will be created and written to them.

The listener ID string is returned (allowing the listener to be cancelled) along with an error object if there was a failure.

func (*Cmgr) Write

func (this *Cmgr) Write(id string, buf []byte) (err error)

Write writes the byte array to the named connection.

func (*Cmgr) Write_n

func (this *Cmgr) Write_n(id string, buf []byte, n int) (err error)

Write_n writes n bytes from the byte array to the named session.

func (*Cmgr) Write_str

func (this *Cmgr) Write_str(id string, buf string) (err error)

Write_str writes the string to the named session.

func (*Cmgr) Write_udp

func (this *Cmgr) Write_udp(id string, to string, ibuf interface{}) (err error)

Write_udp writes the byte array to the udp address given in 'to'. The address is expected to be host:port format. Ibuf may be either an array of bytes, string or pointer to string.

func (*Cmgr) Write_udp_addr

func (this *Cmgr) Write_udp_addr(id string, addr *net.UDPAddr, buf []byte)

Write_udp_addr writes the buffer to the address associated with id.

type Sess_data

type Sess_data struct {
	Buf   []byte // actual data
	Id    string // identify src for user that receives data from multiple connections
	From  string // message source address
	State int    // ST_ constants indicating the session state
	Data  string // maybe useful (humanised) data about the session or message; generally empty for data.
	// contains filtered or unexported fields
}

Data that is returned on the channel to the user.

func (*Sess_data) Bind2sender

func (s *Sess_data) Bind2sender() (err error)

Bind2sender allow udp from address to be captured for fast replies -- users should avoid doing this as all writes will go to the bound address rather than to the sender if sess_data.Write() is used after this.

func (*Sess_data) Unbind_sender

func (s *Sess_data) Unbind_sender()

Unbind_sender allows a previously bound sender to be dropped.

func (*Sess_data) Write

func (s *Sess_data) Write(buf []byte) (nw int, err error)

Write allows session data to be used as a Writer interface for connection oriented sessions.

func (*Sess_data) Write_n

func (this *Sess_data) Write_n(buf []byte, n int) (err error)

Write_n writes n bytes to the process that sent the data represented by Sess_data

func (*Sess_data) Write_str

func (this *Sess_data) Write_str(buf string) (n int, err error)

Write_str writes the string to the process that sent the data represented by Sess_data

Jump to

Keyboard shortcuts

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