udp

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

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

Go to latest
Published: Mar 19, 2019 License: MIT Imports: 7 Imported by: 2

README

udp

a simple udp c/s use kcp for go & C# for mobile game

Documentation

Overview

Package kcp - A Fast and Reliable ARQ Protocol

Index

Constants

View Source
const (
	IKCP_RTO_NDL     = 30  // no delay min rto
	IKCP_RTO_MIN     = 100 // normal min rto
	IKCP_RTO_DEF     = 200
	IKCP_RTO_MAX     = 60000
	IKCP_CMD_PUSH    = 81 // cmd: push data
	IKCP_CMD_ACK     = 82 // cmd: ack
	IKCP_CMD_WASK    = 83 // cmd: window probe (ask)
	IKCP_CMD_WINS    = 84 // cmd: window size (tell)
	IKCP_ASK_SEND    = 1  // need to send IKCP_CMD_WASK
	IKCP_ASK_TELL    = 2  // need to send IKCP_CMD_WINS
	IKCP_WND_SND     = 32
	IKCP_WND_RCV     = 32
	IKCP_MTU_DEF     = 1400
	IKCP_ACK_FAST    = 3
	IKCP_INTERVAL    = 100
	IKCP_OVERHEAD    = 24
	IKCP_DEADLINK    = 20
	IKCP_THRESH_INIT = 2
	IKCP_THRESH_MIN  = 2
	IKCP_PROBE_INIT  = 7000   // 7 secs to probe window size
	IKCP_PROBE_LIMIT = 120000 // up to 120 secs to probe window
)

Variables

View Source
var (
	Lis_sess_capacity  int = 1024 // 监听器的初始容量
	Channel_Pack_count int = 256  // 监听器的解包分发协程数量
	Channel_Pack_size  int = 1024 // 监听器的收到通道数量
)
View Source
var (
	Pack_max_len int = 1024 // 单个udp包的最大包长,用于做缓存( 必须 >= kcp_mtu + fec_head_Len)

	Fec_len      int    = 3   // fec 冗余册数 3 => p1(x1, x2, x3)
	Fec_cacheLen uint32 = 256 // fec 序号收到的记录长度 需要大于 kcp的wnd_size

	Kcp_nodelay  int           = 1   // kcp的延迟发送
	Kcp_interval int           = 33  // kcp的刷新间隔
	Kcp_resend   int           = 2   // kcP的重传次数
	Kcp_nc       int           = 1   // kcp的流控
	Kcp_mtu      int           = 256 // kcp输出的单个包的长度
	Kcp_wnd_size int           = 32  // kcp的窗口的大小
	Kcp_update   time.Duration = 10  // kcp的update调用时间间隔

	Sess_ch_socket_size int = 64 // 会话接收socket数据的通道大小
	Sess_ch_send_size   int = 64 // 会话发送数据的通道大小
	Sess_ch_logic_size  int = 16 // 会话投递数据给logic的通道大小
)

Functions

This section is empty.

Types

type Client

type Client struct {
	Closed bool

	Err     chan error  // 错误通道
	ChLogic chan []byte // 抛出数据给逻辑层的通道
	// contains filtered or unexported fields
}

func NewClient

func NewClient(conv uint32, raddr string) (*Client, error)

func (*Client) Close

func (c *Client) Close()

关闭

func (*Client) Send

func (c *Client) Send(buf []byte)

发送数据

type KCP

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

KCP defines a single KCP connection

func NewKCP

func NewKCP(conv uint32, output output_callback) *KCP

NewKCP create a new kcp control object, 'conv' must equal in two endpoint from the same connection.

func (*KCP) Check

func (kcp *KCP) Check() uint32

Check determines when should you invoke ikcp_update: returns when you should invoke ikcp_update in millisec, if there is no ikcp_input/_send calling. you can call ikcp_update in that time, instead of call update repeatly. Important to reduce unnacessary ikcp_update invoking. use it to schedule ikcp_update (eg. implementing an epoll-like mechanism, or optimize ikcp_update when handling massive kcp connections)

func (*KCP) Input

func (kcp *KCP) Input(data []byte, regular, ackNoDelay bool) int

Input when you received a low level packet (eg. UDP packet), call it regular indicates a regular packet has received(not from FEC)

func (*KCP) NoDelay

func (kcp *KCP) NoDelay(nodelay, interval, resend, nc int) int

NoDelay options fastest: ikcp_nodelay(kcp, 1, 20, 2, 1) nodelay: 0:disable(default), 1:enable interval: internal update timer interval in millisec, default is 100ms resend: 0:disable fast resend(default), 1:enable fast resend nc: 0:normal congestion control(default), 1:disable congestion control

func (*KCP) PeekSize

func (kcp *KCP) PeekSize() (length int)

PeekSize checks the size of next message in the recv queue

func (*KCP) Recv

func (kcp *KCP) Recv(buffer []byte) (n int)

Recv is user/upper level recv: returns size, returns below zero for EAGAIN

func (*KCP) Send

func (kcp *KCP) Send(buffer []byte) int

Send is user/upper level send, returns below zero for error

func (*KCP) SetMtu

func (kcp *KCP) SetMtu(mtu int) int

SetMtu changes MTU size, default is 1400

func (*KCP) Update

func (kcp *KCP) Update(current uint32)

Update updates state (call it repeatedly, every 10ms-100ms), or you can ask ikcp_check when to call it again (without ikcp_input/_send calling). 'current' - current timestamp in millisec.

func (*KCP) WaitSnd

func (kcp *KCP) WaitSnd() int

WaitSnd gets how many packet is waiting to be sent

func (*KCP) WndSize

func (kcp *KCP) WndSize(sndwnd, rcvwnd int) int

WndSize sets maximum window size: sndwnd=32, rcvwnd=32 by default

type Listener

type Listener struct {
	Err chan error // 错误通道 具体错误处理交给逻辑层
	// contains filtered or unexported fields
}

func Listen

func Listen(laddr string) (*Listener, error)

监听指定地址

func (*Listener) AddSession

func (l *Listener) AddSession(conv uint32) (*Session, error)

添加会话

func (*Listener) Close

func (l *Listener) Close()

关闭

func (*Listener) RemoveSession

func (l *Listener) RemoveSession(conv uint32)

移除会话

type Session

type Session struct {
	Err     chan error  // 错误信息通道
	ChLogic chan []byte // 抛出数据给逻辑层的通道
	// contains filtered or unexported fields
}

数据流 send2kcp: kcp=>fec=>socket recv2fec: socket=>fec=>kcp 缓存池流 recv: pop=>chSocket=>fec=>kcp=>push seg: pop=>new=>delete=>push unpack: pop=>unpack=>logic=>push

func (*Session) GetConv

func (s *Session) GetConv() uint32

GetConv get conv id

func (*Session) Send

func (s *Session) Send(b []byte)

Send data to kcp

Directories

Path Synopsis
test

Jump to

Keyboard shortcuts

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