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
- type Cmgr
- func (this *Cmgr) Close(id string)
- func (this *Cmgr) Connect(target string, uid string, data2usr chan *Sess_data) (err error)
- func (cm *Cmgr) Get_conn(id string) (conn *connection)
- func (c *Cmgr) Get_udp_writer(id string, addr string) (newcp *connection, err error)
- func (c *Cmgr) Get_writer(id string) *connection
- func (this *Cmgr) List_stats()
- func (this *Cmgr) Listen(kind string, port string, iface string, data2usr chan *Sess_data) (lid string, err error)
- func (this *Cmgr) Listen_mc(ifname string, addr string, data2usr chan *Sess_data) (sessid string, err error)
- func (this *Cmgr) Listen_tcp(port string, data2usr chan *Sess_data) (string, error)
- func (this *Cmgr) Listen_udp(port int, data2usr chan *Sess_data) (uid string, err error)
- func (this *Cmgr) String2udp_addr(astr string) (addr *net.UDPAddr, err error)
- func (this *Cmgr) TLS_listen(kind string, port string, iface string, data2usr chan *Sess_data, ...) (lid string, err error)
- func (this *Cmgr) Write(id string, buf []byte) (err error)
- func (this *Cmgr) Write_n(id string, buf []byte, n int) (err error)
- func (this *Cmgr) Write_str(id string, buf string) (err error)
- func (this *Cmgr) Write_udp(id string, to string, ibuf interface{}) (err error)
- func (this *Cmgr) Write_udp_addr(id string, addr *net.UDPAddr, buf []byte)
- type Sess_data
Constants ¶
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 )
const (
LISTENER string = "l0" // our magic session id for the default litener
)
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 ¶
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) Connect ¶
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 ¶
-------------------------------------------------------------------------------------------------------------------
Get_conn returns the connection given the connection id.
func (*Cmgr) Get_udp_writer ¶
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 ¶
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 ¶
Provides a more consistent interface with the Listen_udp name convention and is just a wrapper for Listen().
func (*Cmgr) Listen_udp ¶
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 ¶
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.
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 ¶
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 ¶
Write allows session data to be used as a Writer interface for connection oriented sessions.