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 ¶
- type Packet
- type Session
- func (sess *Session) Close()
- func (sess *Session) ID() uint64
- func (sess *Session) LocalAddr() net.Addr
- func (sess *Session) Recv() (packet *Packet, err error)
- func (sess *Session) RemoteAddr() net.Addr
- func (sess *Session) Send(b []byte) (int, error)
- func (sess *Session) SetRecvTimeout(timeout time.Duration)
- type SimpleMux
- type SimpleMuxHeader
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) Recv ¶
Recv reads data from the session. Returns net.Error at timeout, use err.(net.Error).Timeout() to determine if timeout occurs.
func (*Session) RemoteAddr ¶
RemoteAddr returns the remote address of the underlying connection.
func (*Session) Send ¶
Send is used to write to the session. For some good reasons, Send doesn't support timeout.
func (*Session) SetRecvTimeout ¶
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:
- Can only open a few connections (probably only 1 connection) to a remote server, but want to program like there can be unlimited connections.
- The remote server has its own protocol format which could not be changed.
- 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) NewSession ¶
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 ¶
RemoteAddr returns the remote address of the underlying connection.
type SimpleMuxHeader ¶
SimpleMuxHeader defines the interface that a real PacketHeader should satisfy