Documentation ¶
Overview ¶
Package connection implements tcp, udp and other protocols for network connection.
Index ¶
- type ConnType
- type Connection
- type EventTrigger
- type TcpConn
- func (t *TcpConn) Close() error
- func (t *TcpConn) FD() int
- func (t *TcpConn) FlushBuffer() error
- func (t *TcpConn) ID() uint64
- func (t *TcpConn) Len() int
- func (t *TcpConn) LocalAddr() string
- func (c *TcpConn) OnInterrupt() error
- func (c *TcpConn) OnRead() (err error)
- func (c *TcpConn) OnWrite() (err error)
- func (c *TcpConn) Register(eventType poll.EventType) error
- func (t *TcpConn) RemoteAddr() string
- func (t *TcpConn) SetEventTrigger(trigger EventTrigger)
- func (t *TcpConn) Type() ConnType
- func (t *TcpConn) WriteBuffer(bytes []byte) (int, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Connection ¶
type Connection interface { // ID return a uin-type value that uniquely identifies each stream connection。 ID() uint64 // FD return socket fd. FD() int // LocalAddr return the actual local connection address. LocalAddr() string // RemoteAddr return the actual remote connection address. RemoteAddr() string // WriteBuffer does not immediately write the byte stream to the network, // but rather writes it to a local output buffer. WriteBuffer(bytes []byte) (int, error) // FlushBuffer writes all data in the local output buffer to the network. // It is a blocking operation and waits until all the data has been written to the network. FlushBuffer() error // SetEventTrigger set the EventTrigger, when the network connection is readable or the connection is abnormal, // the trigger will be driven SetEventTrigger(trigger EventTrigger) // Len returns the maximum readable bytes of the current connection. Len() int // Type Return the current connection network type tcp/udp/ws. Type() ConnType // Register register conn in poller with event. Register(eventType poll.EventType) error // Close the network connection, regardless of the ongoing blocking non-blocking read and write will return an error. Close() error }
Connection is a network connection oriented towards byte streams, based on an event-driven mechanism.
type EventTrigger ¶
type EventTrigger interface { // OnConnReadable triggered when the connection gets data from the network. OnConnReadable([]byte) int // OnConnHup triggered when the connection gets an error / close event from the network. OnConnHup() }
EventTrigger define connection event notification behavior.
type TcpConn ¶
type TcpConn struct {
// contains filtered or unexported fields
}
TcpConn tcp connection implements the Connection interface.
func NewTcpConn ¶
NewTcpConn create a new tcp connection, conn implements Connection.
func (*TcpConn) FlushBuffer ¶
FlushBuffer implements Connection.
func (*TcpConn) OnInterrupt ¶
func (c *TcpConn) OnInterrupt() error
OnInterrupt executed when the network connection FD is close/hup. when the network connection needs to be closed or the exception needs to close the entire connection.
func (*TcpConn) OnRead ¶
func (c *TcpConn) OnRead() (err error)
OnRead executed when the network connection FD is readable. the network data first enters the connection buffer as much as possible, and then drives the EventTrigger OnConnReadable function of the upper layer to process the data in the buffer.
func (*TcpConn) OnWrite ¶
func (c *TcpConn) OnWrite() (err error)
OnWrite executed when the network connection FD is writeable. in some cases, there may be an `abnormality (EAGAIN)` in which data is written to the network. When the network FD becomes writable, data should be written to the network as much as possible.
func (*TcpConn) RemoteAddr ¶
RemoteAddr implements Connection.
func (*TcpConn) SetEventTrigger ¶
func (t *TcpConn) SetEventTrigger(trigger EventTrigger)
SetEventTrigger implements Connection.