mux

package
v1.19.3 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2023 License: LGPL-3.0 Imports: 7 Imported by: 0

README

Overview

Package mux (short for connection multiplexer) is a multiplexing package for Golang.

In some rare cases, we can only open a few connections to a remote server, but we want to program like we can open unlimited connections. Should you encounter this rare cases, then this package is exactly what you need.

SimpleMux

SimpleMux is a connection multiplexer. It is very useful when under the following constraints:

  1. Can only open a few connections (probably only 1 connection) to a remote server, but want to program like there can be unlimited connections.
  2. The remote server has its own protocol format which could not be changed.
  3. Fortunately, we can set 8 bytes of information to the protocol header which will remain the same in the server's response.

Basic example

Seek to simple_mux_test.go for detailed usage.

Documentation

Overview

Package mux (short for connection multiplexer) is a multiplexing package for Golang.

In some rare cases, we can only open a few connections to a remote server, but we want to program like we can open unlimited connections. Should you encounter this rare cases, then this package is exactly what you need.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Packet

type Packet struct {
	Header SimpleMuxHeader // protocol header
	Body   []byte          // protocol body
}

Packet holds the protocol data receive from the remote server.

type Session

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

Session is created from a SimpleMux. You can create as many sessions as you want. All sessions are base on the single connection of the SimpleMux, but they act like they are separate connections.

Session supports bidirectional communication and server-side push.

Note: Methods of Session are not goroutine-safe.
      One session is intended to be used within one goroutine.

func (*Session) Close

func (sess *Session) Close()

Close is used to close the session. After finish using a Session, Close() must be called to release resources.

func (*Session) ID

func (sess *Session) ID() uint64

ID returns the ID of this session.

func (*Session) LocalAddr

func (sess *Session) LocalAddr() net.Addr

LocalAddr returns the local address of the underlying connection.

func (*Session) Recv

func (sess *Session) Recv() (packet *Packet, err error)

Recv reads data from the session. Returns net.Error at timeout, use err.(net.Error).Timeout() to determine if timeout occurs.

func (*Session) RemoteAddr

func (sess *Session) RemoteAddr() net.Addr

RemoteAddr returns the remote address of the underlying connection.

func (*Session) Send

func (sess *Session) Send(b []byte) (int, error)

Send is used to write to the session. For some good reasons, Send doesn't support timeout.

func (*Session) SetRecvTimeout

func (sess *Session) SetRecvTimeout(timeout time.Duration)

SetRecvTimeout sets timeout to the session. After calling this method, all subsequent calls to Recv() will time out after the specified `timeout`.

Should you want to cancel the timeout setting, just call SetRecvTimeout(0)

Example:
    sess.SetRecvTimeout(5 * time.Millisecond)

type SimpleMux

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

SimpleMux is a connection multiplexer. It is very useful when under the following constraints:

  1. Can only open a few connections (probably only 1 connection) to a remote server, but want to program like there can be unlimited connections.
  2. The remote server has its own protocol format which could not be changed.
  3. Fortunately, we can set 8 bytes of information to the protocol header which will remain the same in the server's response.

All methods of SimpleMux are goroutine-safe.

Seek to simple_mux_test.go for detailed usage.

func NewSimpleMux

func NewSimpleMux(conn net.Conn, hdrSz int,
	hdrParser func(hdr []byte) (SimpleMuxHeader, error),
	defHandler func(defSess *Session, packet *Packet)) (*SimpleMux, error)

NewSimpleMux is the only way to get a new, ready-to-use SimpleMux.

conn: Connection to the remote server. Once a connection has been assigned to a SimpleMux,
      you should never use it elsewhere, otherwise it might cause the SimpleMux to malfunction.
hdrSz: Size (in bytes) of protocol header for communicating with the remote server.
hdrParser: Function to parser the header. Returns (hdr, nil) on success, or (nil, err) on error.
defHandler: Handler for handling packets without an associated session. Could be nil.
            `defSess` is the default session for sending information back to the remote server if necessary.
                     Do not close this `defSess`, otherwise you can't use it later.
            `packet` is the current packet received whose associated session could not be found.

func (*SimpleMux) Close

func (mux *SimpleMux) Close()

Close is used to close the SimpleMux (including its underlying connection) and all sessions.

Note: After finish using a SimpleMux, Close must be called to release resources.

func (*SimpleMux) LocalAddr

func (mux *SimpleMux) LocalAddr() net.Addr

LocalAddr returns the local address of the underlying connection.

func (*SimpleMux) NewSession

func (mux *SimpleMux) NewSession() (sess *Session, err error)

NewSession is used to create a new session. You can create as many sessions as you want. All sessions are base on the single connection of the SimpleMux, but they act like they are separate connections.

Note: Methods of Session are not goroutine-safe.
      One session is intended to be used within one goroutine.

func (*SimpleMux) RemoteAddr

func (mux *SimpleMux) RemoteAddr() net.Addr

RemoteAddr returns the remote address of the underlying connection.

type SimpleMuxHeader

type SimpleMuxHeader interface {
	SessionID() uint64
	BodyLen() int64
}

SimpleMuxHeader defines the interface that a real PacketHeader should satisfy

Jump to

Keyboard shortcuts

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