nets

package
v1.1.5 Latest Latest
Warning

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

Go to latest
Published: May 6, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	HeaderSize  = 2               // 標頭長度
	PacketSize  = int(^uint16(0)) // 封包長度
	ChannelSize = 1000            // 訊息通道大小設為1000, 避免因為爆滿而卡住
)
View Source
const (
	EventStart = "start" // 啟動會話事件, 當會話啟動後觸發, 參數是會話物件
	EventStop  = "stop"  // 停止會話事件, 當會話停止後觸發, 參數是會話物件
	EventRecv  = "recv"  // 接收訊息事件, 當接收訊息後觸發, 參數是訊息物件
	EventSend  = "send"  // 傳送訊息事件, 當傳送訊息後觸發, 參數是訊息物件
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Bind

type Bind func(session Sessioner) bool

Bind 綁定處理函式類型

func (Bind) Do

func (this Bind) Do(session Sessioner) bool

Do 執行處理

type Codec added in v1.1.0

type Codec interface {
	// Encode 編碼處理
	Encode(input any) (output any, err error)

	// Decode 解碼處理
	Decode(input any) (output any, err error)
}

Codec 編碼/解碼介面

type ConnectID

type ConnectID = int64

ConnectID 連接編號

type Connecter

type Connecter interface {
	// Connect 啟動連接
	Connect(bind Bind, unbind Unbind, wrong Wrong)

	// Address 取得位址
	Address() string
}

Connecter 連接介面

type ListenID

type ListenID = int64

ListenID 接聽編號

type Listener

type Listener interface {
	// Listen 啟動接聽
	Listen(bind Bind, unbind Unbind, wrong Wrong)

	// Stop 停止接聽
	Stop() error

	// Address 取得位址
	Address() string
}

Listener 接聽介面

type Netmgr

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

Netmgr 網路管理器, 用於管理接聽或是連接, 以及使用中的會話, 但是會話不開放給外部使用

當新增連接時, 使用者須提供以下參數

  • timeout: 連接超時時間, 連接若超過此時間會連接失敗
  • bind: 連接成功時要執行的初始化流程
  • unbind: 中斷連接時要執行的釋放流程
  • wrong: 錯誤處理函式

Bind 通常要做的流程如下

  • 建立實體
  • 實體設置
  • 模組設置
  • 處理設置
  • 會話設置(設定發布事件處理, 錯誤處理, 編碼/解碼, 擁有者)
  • 實體初始化
  • 標籤設置
  • 錯誤處理

Unbind 通常要做的流程如下

  • 釋放實體
  • 刪除實體
  • 刪除標籤

func NewNetmgr

func NewNetmgr() *Netmgr

NewNetmgr 建立網路管理器

func (*Netmgr) AddConnectTCP

func (this *Netmgr) AddConnectTCP(ip, port string, timeout time.Duration, bind Bind, unbind Unbind, wrong Wrong) ConnectID

AddConnectTCP 新增連接(TCP)

func (*Netmgr) AddListenTCP

func (this *Netmgr) AddListenTCP(ip, port string, bind Bind, unbind Unbind, wrong Wrong) ListenID

AddListenTCP 新增接聽(TCP)

func (*Netmgr) DelConnect

func (this *Netmgr) DelConnect(connectID ConnectID)

DelConnect 刪除連接

func (*Netmgr) DelListen

func (this *Netmgr) DelListen(listenID ListenID)

DelListen 刪除連接

func (*Netmgr) GetConnect

func (this *Netmgr) GetConnect(connectID ConnectID) Connecter

GetConnect 取得連接

func (*Netmgr) GetListen

func (this *Netmgr) GetListen(listenID ListenID) Listener

GetListen 取得連接

func (*Netmgr) Status

func (this *Netmgr) Status() *Status

Status 取得狀態資料

func (*Netmgr) Stop

func (this *Netmgr) Stop()

Stop 停止網路

type Publish

type Publish func(name string, param any)

Publish 發布事件處理函式類型

func (Publish) Do

func (this Publish) Do(name string, param any)

Do 執行處理

type Sessioner

type Sessioner interface {
	// Start 啟動會話, 若不是使用多執行緒啟動, 則一定被阻塞在這裡直到停止會話; 當由連接器/接聽器獲得會話器之後, 需要啟動會話才可以傳送或接收封包
	Start(bind Bind, unbind Unbind)

	// Stop 停止會話, 不會等待會話內部循環結束
	Stop()

	// StopWait 停止會話, 會等待會話內部循環結束
	StopWait()

	// SetPublish 設定發布事件處理
	SetPublish(publish ...Publish)

	// SetWrong 設定錯誤處理
	SetWrong(wrong ...Wrong)

	// SetCodec 設定編碼/解碼
	SetCodec(codec ...Codec)

	// SetOwner 設定擁有者
	SetOwner(owner any)

	// Send 傳送封包
	Send(message any)

	// RemoteAddr 取得遠端位址
	RemoteAddr() net.Addr

	// LocalAddr 取得本地位址
	LocalAddr() net.Addr

	// GetOwner 取得擁有者
	GetOwner() any
}

Sessioner 會話介面

type Status

type Status struct {
	Connect int // 連接數量
	Listen  int // 接聽數量
	Session int // 會話數量
}

Status 狀態資料

type TCPConnect

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

TCPConnect TCP連接器, 負責用TCP協議建立連接以取得會話物件

func NewTCPConnect

func NewTCPConnect(ip, port string, timeout time.Duration) *TCPConnect

NewTCPConnect 建立TCP連接器

func (*TCPConnect) Address

func (this *TCPConnect) Address() string

Address 取得位址

func (*TCPConnect) Connect

func (this *TCPConnect) Connect(bind Bind, unbind Unbind, wrong Wrong)

Connect 啟動連接

type TCPListen

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

TCPListen TCP接聽器, 負責用TCP協議建立接聽, 並等待客戶端連接以取得會話物件

func NewTCPListen

func NewTCPListen(ip, port string) *TCPListen

NewTCPListen 建立TCP接聽器

func (*TCPListen) Address

func (this *TCPListen) Address() string

Address 取得位址

func (*TCPListen) Listen

func (this *TCPListen) Listen(bind Bind, unbind Unbind, wrong Wrong)

Listen 啟動接聽

func (*TCPListen) Stop

func (this *TCPListen) Stop() error

Stop 停止接聽

type TCPSession

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

TCPSession TCP會話器, 負責傳送/接收訊息等相關的功能

func NewTCPSession

func NewTCPSession(conn net.Conn) *TCPSession

NewTCPSession 建立TCP會話器

func (*TCPSession) GetOwner

func (this *TCPSession) GetOwner() any

GetOwner 取得擁有者

func (*TCPSession) LocalAddr

func (this *TCPSession) LocalAddr() net.Addr

LocalAddr 取得本地位址

func (*TCPSession) RemoteAddr

func (this *TCPSession) RemoteAddr() net.Addr

RemoteAddr 取得遠端位址

func (*TCPSession) Send

func (this *TCPSession) Send(message any)

Send 傳送訊息

func (*TCPSession) SetCodec added in v1.1.0

func (this *TCPSession) SetCodec(codec ...Codec)

SetCodec 設定編碼/解碼

func (*TCPSession) SetOwner

func (this *TCPSession) SetOwner(owner any)

SetOwner 設定擁有者

func (*TCPSession) SetPublish added in v1.1.0

func (this *TCPSession) SetPublish(publish ...Publish)

SetPublish 設定發布事件處理

func (*TCPSession) SetWrong added in v1.1.0

func (this *TCPSession) SetWrong(wrong ...Wrong)

SetWrong 設定錯誤處理

func (*TCPSession) Start

func (this *TCPSession) Start(bind Bind, unbind Unbind)

Start 啟動會話

func (*TCPSession) Stop

func (this *TCPSession) Stop()

Stop 停止會話, 不會等待會話內部循環結束

func (*TCPSession) StopWait

func (this *TCPSession) StopWait()

StopWait 停止會話, 會等待會話內部循環結束

type Unbind

type Unbind func(session Sessioner)

Unbind 解綁處理函式類型

func (Unbind) Do

func (this Unbind) Do(session Sessioner)

Do 執行處理

type Wrong

type Wrong func(err error)

Wrong 錯誤處理函式類型

func (Wrong) Do

func (this Wrong) Do(err error)

Do 執行處理

Jump to

Keyboard shortcuts

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