base

package
v1.5.2 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2023 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// max int32 = 2^31,這裡取小於 max int32 的最大二次冪數
	LIMIT_SIZE int32 = 1073741824
)
View Source
const (
	// 最大傳輸單元(Maximum Transmission Unit): 最大封包大小
	MTU int32 = 1500
)

Variables

This section is empty.

Functions

func BytesToInt32

func BytesToInt32(b []byte, order binary.ByteOrder) int32

byte 陣列 轉 int32

func BytesToInt64

func BytesToInt64(b []byte, order binary.ByteOrder) int64

byte 陣列 轉 int64

func BytesToNumber

func BytesToNumber[T int8 | int16 | int32 | int64 | uint16 | uint32 | uint64 | float32 | float64](b []byte, order binary.ByteOrder) T

func BytesToUInt16

func BytesToUInt16(b []byte, order binary.ByteOrder) uint16

byte 陣列 轉 uint16

func CheckConns

func CheckConns(root *Conn)

func CheckWorks

func CheckWorks(works *Work)

func NumberToBytes

func NumberToBytes[T int8 | int16 | int32 | int64 | uint16 | uint32 | uint64 | float32 | float64](v T, order binary.ByteOrder) []byte

===== 轉 byte 陣列 ===== 數字 轉 byte 陣列

func SliceString

func SliceString(array []byte) string

Types

type Conn

type Conn struct {

	// ==================================================
	// 連線結構
	// ==================================================
	// 連線物件
	NetConn net.Conn
	// 連線狀態
	State define.ConnectState
	// 連線模式(當 client 和 server 通信時對於長鏈接如何進行處理。)
	Mode ConnMode
	// 斷線時間戳(數秒後才切斷連線,預留時間給對方讀取數據)
	DisconnectTime time.Time
	// 下一個連線結構的指標
	Next *Conn

	// ==================================================
	// 讀寫結構
	// ==================================================
	// 緩衝長度
	BufferLength int32

	// 可讀取長度(readInput ~ readOutput 之間的數據量)
	ReadableLength int32
	// 下次讀取長度
	ReadLength int32

	// 讀取封包通道
	ReadCh chan *Packet

	// 可寫出長度(writeInput ~ writeOutput 之間的數據量)
	WritableLength int32
	// contains filtered or unexported fields
}

func NewConn

func NewConn(id int32, size int32) *Conn

func (*Conn) Add

func (c *Conn) Add(conn *Conn)

func (*Conn) CheckReadable

func (c *Conn) CheckReadable(checker func(buffer *[]byte, i int32, o int32, length int32) bool) bool

根據 checker 函式,檢查是否已讀取到所需的數據(條件可能是 長度 或 換行符 等)

func (*Conn) GetId

func (c *Conn) GetId() int32

取得連線物件編號

func (*Conn) Handler

func (c *Conn) Handler()

func (*Conn) Read

func (c *Conn) Read(data *[]byte, length int32)

從 readBuffer 讀取指定長度的數據

func (*Conn) Reconnect

func (c *Conn) Reconnect()

當有需要重新連線的情況下,首先就會發生 Socket 讀取異常,並導致 Handler 的 goroutine 結束,因此無須再利用 c.stopCh 將 Handler 結束

func (*Conn) Release

func (c *Conn) Release()

func (*Conn) SetDisconnectTime

func (c *Conn) SetDisconnectTime(second time.Duration)

func (*Conn) SetReadBuffer

func (c *Conn) SetReadBuffer(packet *Packet)

讀取封包數據,並寫入 readBuffer

func (*Conn) SetWriteBuffer

func (c *Conn) SetWriteBuffer(data *[]byte, length int32)

將寫出數據加入緩存 TODO: 檢查 c.writeInput 是否反超 c.writeOutput,若反超,表示緩衝大小不足

func (*Conn) String

func (c *Conn) String() string

func (*Conn) Write

func (c *Conn) Write() error

type ConnBuffer

type ConnBuffer struct {
	net.Conn
	Index int32
}

type ConnMode

type ConnMode byte
const (
	CLOSE     ConnMode = 0
	KEEPALIVE ConnMode = 1
)

type LoopState

type LoopState byte
const (
	// 迴圈繼續往下執行
	KEEPGOING LoopState = 0
	// 呼叫 continue
	CONTINUE LoopState = 1
	// 呼叫 break
	BREAK LoopState = 2
)

type OnEventsFunc added in v1.2.2

type OnEventsFunc map[define.EventType]func(any)

type Packet

type Packet struct {
	// 數據緩存
	Data []byte
	// 起始位置(若讀寫一次成功,則基本會是 0)
	Index int32
	// 數據結束位置(若讀寫一次成功,則基本上是數據長度)
	Length int32
	// 讀寫錯誤
	Error error
}

func NewPacket

func NewPacket() *Packet

func (*Packet) Release

func (p *Packet) Release()

type Tcp0

type Tcp0 struct {
	// 讀取狀態值 | 0: 讀取數據長度, 1: 根據前一階段取得的長度,讀取數據
	State      int8
	HeaderSize int32
	ReadLength int32
}

func NewTcp0

func NewTcp0() *Tcp0

func (*Tcp0) ReadableChecker

func (t *Tcp0) ReadableChecker(buffer *[]byte, i int32, o int32, length int32) bool

檢查是否滿足:可讀長度 大於 欲讀取長度

func (*Tcp0) ResetReadLength

func (t *Tcp0) ResetReadLength()

type TransData

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

func LoadTransData

func LoadTransData(data []byte) *TransData

func NewTransData

func NewTransData() *TransData

func (*TransData) AddBoolean

func (t *TransData) AddBoolean(v bool)

func (*TransData) AddByte

func (t *TransData) AddByte(v byte)

func (*TransData) AddByteArray

func (t *TransData) AddByteArray(v []byte)

func (*TransData) AddFloat32

func (t *TransData) AddFloat32(v float32)

func (*TransData) AddFloat64

func (t *TransData) AddFloat64(v float64)

func (*TransData) AddInt16

func (t *TransData) AddInt16(v int16)

func (*TransData) AddInt32

func (t *TransData) AddInt32(v int32)

func (*TransData) AddInt64

func (t *TransData) AddInt64(v int64)

func (*TransData) AddInt8

func (t *TransData) AddInt8(v int8)

func (*TransData) AddJson added in v1.2.3

func (t *TransData) AddJson(v map[string]string)

func (*TransData) AddRawData

func (t *TransData) AddRawData(v []byte)

func (*TransData) AddString

func (t *TransData) AddString(v string)

func (*TransData) AddUInt16

func (t *TransData) AddUInt16(v uint16)

func (*TransData) AddUInt32

func (t *TransData) AddUInt32(v uint32)

func (*TransData) AddUInt64

func (t *TransData) AddUInt64(v uint64)

func (*TransData) Clear

func (t *TransData) Clear()

func (TransData) FormData

func (t TransData) FormData() []byte

取得格式化數據

func (*TransData) GetCapacity

func (t *TransData) GetCapacity() int32

func (TransData) GetData

func (t TransData) GetData() []byte

取出全部的數據

func (*TransData) GetLength

func (t *TransData) GetLength() int32

func (*TransData) InsertInt32

func (t *TransData) InsertInt32(v int32)

func (*TransData) PopBoolean

func (t *TransData) PopBoolean() bool

func (*TransData) PopByte

func (t *TransData) PopByte() byte

func (*TransData) PopByteArray

func (t *TransData) PopByteArray() []byte

func (*TransData) PopFloat32

func (t *TransData) PopFloat32() float32

func (*TransData) PopFloat64

func (t *TransData) PopFloat64() float64

func (*TransData) PopInt16

func (t *TransData) PopInt16() int16

func (*TransData) PopInt32

func (t *TransData) PopInt32() int32

func (*TransData) PopInt64

func (t *TransData) PopInt64() int64

func (*TransData) PopInt8

func (t *TransData) PopInt8() int8

func (*TransData) PopJson added in v1.2.3

func (t *TransData) PopJson() map[string]string

func (*TransData) PopString

func (t *TransData) PopString() string

func (*TransData) PopUInt16

func (t *TransData) PopUInt16() uint16

func (*TransData) PopUInt32

func (t *TransData) PopUInt32() uint32

func (*TransData) PopUInt64

func (t *TransData) PopUInt64() uint64

func (*TransData) ResetIndex

func (t *TransData) ResetIndex()

func (*TransData) SetCapacity

func (t *TransData) SetCapacity(capacity int32)

func (*TransData) SetOrder

func (t *TransData) SetOrder(order binary.ByteOrder)

type Work

type Work struct {

	// 對應的 Conn id 的 Index(空閒 Context id = -1,因此 Index 預設值為 -2,要和前者做區分)
	Index int32
	// 請求發起的時間(若距離實際處理的時間過長,則不處理)
	RequestTime time.Time
	// 下一個工作
	Next *Work

	// 工作狀態 -1: 空閒; 0: 完成; 1: 尚未完成; 2: 需寫出數據
	State WorkState
	// 數據緩衝
	Data []byte
	// 數據長度
	Length int32
	// 數據封裝容器
	Body *TransData
	// contains filtered or unexported fields
}

封裝基本連線物件,提供給外部存取

func NewWork

func NewWork(id int32) *Work

func (*Work) Add

func (w *Work) Add(work *Work)

func (*Work) Equals

func (w *Work) Equals(other *Work) bool

func (*Work) Finish

func (w *Work) Finish()

func (*Work) GetId

func (w *Work) GetId() int32

func (*Work) Read

func (w *Work) Read() []byte

func (*Work) Release

func (w *Work) Release()

func (*Work) Send

func (w *Work) Send()

原始數據寫入緩存

func (*Work) SendTransData

func (w *Work) SendTransData()

格式化數據寫入緩存

func (*Work) String

func (w *Work) String() string

type WorkState added in v1.5.0

type WorkState int32
const (
	// 空閒
	WORK_FREE WorkState = iota
	// 完成
	WORK_DONE
	// 尚未完成
	WORK_NEED_PROCESS
	// 需寫出數據
	WORK_OUTPUT
)

工作狀態 -1: 空閒; 0: 完成; 1: 尚未完成; 2: 需寫出數據

func (WorkState) String added in v1.5.0

func (ws WorkState) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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