udp

package module
v0.0.0-...-d0cf567 Latest Latest
Warning

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

Go to latest
Published: May 25, 2021 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const Guar_NO = uint8(0x55) //01010101
View Source
const Guar_YES = uint8(0xAA) //10101010
View Source
const MSG_HEADER_LEN = 8 //unsafe.Sizeof(p)

************ 消息头的相关处理 ************

   0    1    2    3    4    5    6    7
-----------------------------------------
|Guar|Ver | MsgLen  | MsgType |  MsgId  |
-----------------------------------------
|      Body...                          |
-----------------------------------------

Variables

This section is empty.

Functions

func Connect

func Connect(ctx context.Context, addr string) (recvCh, sendCh chan Pkg, err error)

Connect udp接口启动的时候执行,inCh, outCh是输入、输出chan,为了和后端处理模块解耦

func KcpOutput

func KcpOutput(conn *net.UDPConn, addr *net.UDPAddr) (f func([]byte, int))

KcpOutput kcp send()之后,会在flush()的时候调用这里的函数,将数据包通过udp 发送出去

func Listen

func Listen(ctx context.Context, addr string) (recvCh, sendCh chan Pkg, err error)

Listen udp接口启动的时候执行,inCh, outCh是输入、输出chan,为了和后端处理模块解耦

Types

type Conn

type Conn struct {
	*sync.Mutex //kcp操作锁,因为有可能并发创建kcp,所以必须在Conn创建之初创建锁
	// contains filtered or unexported fields
}

Conn 每条连接建立一个Conn

func (*Conn) Send

func (c *Conn) Send(pkg Pkg) (err error)

func (*Conn) SendTo

func (c *Conn) SendTo(pkg Pkg) (err error)
type Header struct {
	Guar uint8  // 是否保证到达,即是否通过KCP发送,guarantee缩写;01010101不可靠传输,10101010可靠传输
	Ver  uint8  // 版本号
	Len  uint16 // 消息体的长度
	Type uint16 // 消息类型
	ID   uint16 // 消息ID
}

func (*Header) Deserialize

func (p *Header) Deserialize(data []byte) ([]byte, error)

func (*Header) Serialize

func (p *Header) Serialize(data []byte) ([]byte, error)

Serialize 在传入的切片上序列化本对象,返回未使用的切片

type Pkg

type Pkg struct {
	MsgType uint16
	Guar    bool   //是否保证到达,false表示不保证到达
	ConnID  uint64 //UserID uint64 // 用于获取UDPAddr;
	Addr    string
	Data    []byte
}

Pkg 是用于在IO层(消息收发层)和路由层(消息分发层)之间交互的消息

func Decode

func Decode(buf []byte, connID uint64) (pkg Pkg, err error)

func (Pkg) Encode

func (p Pkg) Encode() (bufSend []byte, err error)

Directories

Path Synopsis
test

Jump to

Keyboard shortcuts

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