connection

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2023 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package connection implements tcp, udp and other protocols for network connection.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConnType

type ConnType int
const (
	// TCPCONNECTION tcp conn
	TCPCONNECTION ConnType = iota
)

define tcp、upd、webSocket connType

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

func NewTcpConn(fd int, lsr, rsr net.Addr) *TcpConn

NewTcpConn create a new tcp connection, conn implements Connection.

func (*TcpConn) Close

func (t *TcpConn) Close() error

Close implements Connection.

func (*TcpConn) FD

func (t *TcpConn) FD() int

FD implements Connection.

func (*TcpConn) FlushBuffer

func (t *TcpConn) FlushBuffer() error

FlushBuffer implements Connection.

func (*TcpConn) ID

func (t *TcpConn) ID() uint64

ID implements Connection.

func (*TcpConn) Len

func (t *TcpConn) Len() int

Len implements Connection.

func (*TcpConn) LocalAddr

func (t *TcpConn) LocalAddr() string

LocalAddr 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) Register

func (c *TcpConn) Register(eventType poll.EventType) error

Register the network connection to poll.

func (*TcpConn) RemoteAddr

func (t *TcpConn) RemoteAddr() string

RemoteAddr implements Connection.

func (*TcpConn) SetEventTrigger

func (t *TcpConn) SetEventTrigger(trigger EventTrigger)

SetEventTrigger implements Connection.

func (*TcpConn) Type

func (t *TcpConn) Type() ConnType

Type implements Connection.

func (*TcpConn) WriteBuffer

func (t *TcpConn) WriteBuffer(bytes []byte) (int, error)

WriteBuffer implements Connection.

Jump to

Keyboard shortcuts

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