net

package
v0.0.0-...-71d5ba4 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2018 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MsgTypeReq int32 = 1
	MsgTypeAck int32 = 2
	MsgTypeNtf int32 = 3
	MsgTypeCmd int32 = 4
)
View Source
const (
	MsgIdConnDisconnect = -1
	MsgIdConnConnect    = -2
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client interface {
	Send(msgId MessageId, body MessageBody) error
	Close()
	Reconnect()
	// contains filtered or unexported methods
}

客户端接口

func Connect

func Connect(net Protocol, serverAddr string, callback MessageCallback) (Client, error)

同步连接服务器

func GoConnect

func GoConnect(net Protocol, serverAddr string, callback MessageCallback) (Client, error)

异步连接服务器

type Conn

type Conn interface {
	Send(msgId MessageId, body MessageBody) error
	Close()
	Address() string

	NetProtocol() Protocol
	Identity() uint32
	State() ConnState
	// contains filtered or unexported methods
}

网络连接接口

type ConnState

type ConnState int
const (
	ConnStateClosed ConnState = iota
	ConnStateConnecting
	ConnStateConnected
)

type MessageBody

type MessageBody []byte

消息体

func (MessageBody) Len

func (i MessageBody) Len() int

type MessageCallback

type MessageCallback func(conn Conn, msgId MessageId, body MessageBody)

消息回调

type MessageId

type MessageId int32

消息ID *

消息ID组成:
123456
1 表示消息类别REQ、ACK、NTF、CMD
2 表示消息组,例如gate, hall, game
34 在game消息组中表示游戏ID,其他消息组暂时为0
56 表示具体消息

func PackId

func PackId(idType, group, extra, id int32) MessageId

封装消息ID

func (MessageId) Extra

func (i MessageId) Extra() int32

func (MessageId) Group

func (i MessageId) Group() int32

func (MessageId) Int

func (i MessageId) Int() int

func (MessageId) Int32

func (i MessageId) Int32() int32

func (MessageId) MsgId

func (i MessageId) MsgId() int32

func (MessageId) Type

func (i MessageId) Type() int32

type MessagePacker

type MessagePacker interface {
	// 封包,传入消息ID和包体,返回字节集
	Pack(MessageId, MessageBody) []byte
	// 解包,传入符合包结构的字节集,返回消息ID,包体,剩余内容
	Unpack([]byte) (MessageId, MessageBody, []byte)
}

消息封装器接口

type Protocol

type Protocol int

网络协议定义

const (
	Tcp Protocol = iota
	WebSocket
	Kcp
)

type Server

type Server interface {
	GetConnection(uint32) (Conn, bool)
	Close()
	// contains filtered or unexported methods
}

服务器接口

func GoListen

func GoListen(net Protocol, port int, callback MessageCallback) (Server, error)

异步执行网络监听

func Listen

func Listen(net Protocol, port int, callback MessageCallback) (Server, error)

同步执行网络监听

Jump to

Keyboard shortcuts

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