network

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 30, 2022 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CloseFrom_Self           = "close from self"           // 本端主动关闭
	CloseFrom_Remote         = "close from remote"         // 对端正常关闭
	CloseFrom_RemoteForcibly = "forcibly closed by remote" // 对端强制关闭
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Communicator

type Communicator[T any] struct {
	// contains filtered or unexported fields
}

网络通信对象

func NewCommunicator

func NewCommunicator[T any](ctx context.Context, host ICommHost[T], logger *logutil.Logger) *Communicator[T]

NewCommunicator Make a communicator

func (*Communicator[T]) BordcastMsg

func (comm *Communicator[T]) BordcastMsg(msg T)

BordcastMsg Send message to all of already exist session's connection.

func (*Communicator[T]) Close

func (comm *Communicator[T]) Close()

Close Stop network communication, and close all of sessions.

func (*Communicator[T]) Disconnect

func (comm *Communicator[T]) Disconnect(guid uint64)

Disconnect Activitily disconnect the session's connection by session id.

func (*Communicator[T]) EstablishTCP

func (comm *Communicator[T]) EstablishTCP(connType, address string) (sessionID uint64, err error)

EstablishTCP Dial to TCP networks by type and address.

func (*Communicator[T]) ListenTCP

func (comm *Communicator[T]) ListenTCP(connType, address string) (err error)

ListenTCP Listen TCP networks by type and address.

func (*Communicator[T]) SendMsg

func (comm *Communicator[T]) SendMsg(guid uint64, msg T) (success bool)

SendMsg Send message to session's connection by session id.

type ICommHost

type ICommHost[T any] interface {
	OnConnect(sessionID uint64, addr string)        // 新连接处理函数
	OnDisconnect(sessionID uint64, err error)       // 连接断开处理函数
	OnMessage(sessionID uint64, msg T)              // 新消息处理函数
	Pack(payload T, buf *bytes.Buffer)              // 消息编码函数 编码后的数据直接写入连接
	Unpack(reader io.Reader) (payload T, err error) // 消息解码函数 从连接中解码数据
}

通信对象所属宿主

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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