network

package
v0.0.0-...-dc73762 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2024 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	WebSocketTimeout = 5 * time.Second
	ReadBufferSize   = 1024
	WriteBufferSize  = 1024
)

Variables

This section is empty.

Functions

This section is empty.

Types

type BaseChannel

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

type BaseClient

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

func NewBaseClient

func NewBaseClient() *BaseClient

func (*BaseClient) SetEncrypt

func (b *BaseClient) SetEncrypt(iEncrypt enctypthelper.IEncrypt)

func (*BaseClient) SetReadCall

func (b *BaseClient) SetReadCall(readCall func(IMessage))

func (*BaseClient) SetSocket

func (b *BaseClient) SetSocket(iSocket ISocket)

type BaseMessage

type BaseMessage struct {
	MsgId   int    // 消息id
	Data    []byte // 消息
	Headers []byte
}

func (*BaseMessage) GetHeader

func (b *BaseMessage) GetHeader() []byte

func (BaseMessage) GetMessageData

func (b BaseMessage) GetMessageData() []byte

func (BaseMessage) GetMsgId

func (b BaseMessage) GetMsgId() int

func (*BaseMessage) Reset

func (b *BaseMessage) Reset()

func (*BaseMessage) SetHeader

func (b *BaseMessage) SetHeader(data []byte)

func (*BaseMessage) SetMessageData

func (b *BaseMessage) SetMessageData(data []byte)

func (*BaseMessage) SetMsgId

func (b *BaseMessage) SetMsgId(msgId int)

type BaseServer

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

func NewBaseServer

func NewBaseServer(addr string) *BaseServer

func (*BaseServer) GetEncrypt

func (b *BaseServer) GetEncrypt() enctypthelper.IEncrypt

func (*BaseServer) GetSocketParse

func (b *BaseServer) GetSocketParse() ISocket

func (*BaseServer) NextId

func (b *BaseServer) NextId() uint64

func (*BaseServer) OnAccept

func (b *BaseServer) OnAccept(channelId uint64, addr string)

func (*BaseServer) OnClose

func (b *BaseServer) OnClose(channelId uint64)

func (*BaseServer) OnRead

func (b *BaseServer) OnRead(channelId uint64, message IMessage)

func (*BaseServer) SetOnAccept

func (b *BaseServer) SetOnAccept(onAccept func(uint64, string))

func (*BaseServer) SetOnClose

func (b *BaseServer) SetOnClose(onClose func(uint64))

func (*BaseServer) SetOnRead

func (b *BaseServer) SetOnRead(onRead func(uint64, IMessage))

type BaseSocket

type BaseSocket struct {
}

BaseSocket tcp原始解析,需要跟客户端保持一致 1、获取4字节长度 2、根据长度获取全部数据 发送时相同:1、前4字节为长度,后面为打包后的数据

func (*BaseSocket) ParseSocket

func (base *BaseSocket) ParseSocket(connection io.Reader, parseData *ParseData)

func (*BaseSocket) UnParseSocket

func (base *BaseSocket) UnParseSocket(message IMessage) (data []byte, err error)

type ConnProtocol

type ConnProtocol int
const (
	TCP ConnProtocol = iota + 1
	KCP
	WebSocket
)

type IChannel

type IChannel interface {
	Start()
	SendMsg(message IMessage)
	Close()
	GetChannelId() uint64
}

type IClient

type IClient interface {
	Connect(addr string) error
	Close() error
	SendMsg(message IMessage)
	Read()
	SetReadCall(readCall func(IMessage))
	SetEncrypt(iEncrypt enctypthelper.IEncrypt)
	SetSocket(iSocket ISocket)
}

func NewTClient

func NewTClient(addr string) (IClient, error)

func NewWClient

func NewWClient(addr string) (IClient, error)

type IMessage

type IMessage interface {
	GetMsgId() int
	SetMsgId(int)
	GetMessageData() []byte
	SetMessageData([]byte)
	SetHeader(data []byte)
	GetHeader() []byte
	Reset()
}

func NewMessage

func NewMessage() IMessage

type IServer

type IServer interface {
	Server() // 服务启动
	Close()  // 关闭连接
	SendMessage(channelId uint64, message IMessage)
	RemoveChannel(uint64)
	SetOnAccept(onAccept func(uint64, string))
	SetOnRead(onRead func(uint64, IMessage))
	SetOnClose(onClose func(uint64))
	GetSocketParse() ISocket
	GetEncrypt() enctypthelper.IEncrypt
	OnRead(channelId uint64, message IMessage)
}

type ISocket

type ISocket interface {
	ParseSocket(conn io.Reader, data *ParseData)     // 解包
	UnParseSocket(IMessage) (data []byte, err error) // 封包
}

type MessageInfo

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

type NetOperator

type NetOperator struct {
	Op        int
	ServiceId uint
	ChannelId uint64
	Message   IMessage
	Args      interface{}
}

type ParseData

type ParseData struct {
	Error error
	// contains filtered or unexported fields
}

func NewParseData

func NewParseData() *ParseData

func (*ParseData) Reset

func (p *ParseData) Reset()

func (*ParseData) ResetTmpData

func (p *ParseData) ResetTmpData(len uint32)

type TChannel

type TChannel struct {
	*BaseChannel
	// contains filtered or unexported fields
}

func NewChannel

func NewChannel(channelId uint64, conn net.Conn, server *TServer) *TChannel

NewChannel 创建连接

func (*TChannel) Close

func (conn *TChannel) Close()

func (*TChannel) GetAddr

func (conn *TChannel) GetAddr() string

func (*TChannel) GetChannelId

func (conn *TChannel) GetChannelId() uint64

func (*TChannel) GetConn

func (conn *TChannel) GetConn() net.Conn

GetConn 获取当前连接

func (*TChannel) GetConnKey

func (conn *TChannel) GetConnKey() int

func (*TChannel) SendMsg

func (conn *TChannel) SendMsg(message IMessage)

SendMsg 消息发送

func (*TChannel) Start

func (conn *TChannel) Start()

Start 开启读写业务

type TClient

type TClient struct {
	*BaseClient
	// contains filtered or unexported fields
}

func (*TClient) Close

func (n *TClient) Close() error

func (*TClient) Connect

func (n *TClient) Connect(addr string) error

func (*TClient) Read

func (n *TClient) Read()

func (*TClient) SendMsg

func (n *TClient) SendMsg(message IMessage)

type TServer

type TServer struct {
	*BaseServer
}

func NewTServer

func NewTServer(addr string) *TServer

func (*TServer) AddChannel

func (ser *TServer) AddChannel(channel *TChannel)

func (*TServer) Close

func (ser *TServer) Close()

func (*TServer) GetChannel

func (ser *TServer) GetChannel(channelId uint64) IChannel

func (*TServer) RemoveChannel

func (ser *TServer) RemoveChannel(channelId uint64)

func (*TServer) SendMessage

func (ser *TServer) SendMessage(channelId uint64, message IMessage)

func (*TServer) Server

func (ser *TServer) Server()

Server 开启服务

type WChannel

type WChannel struct {
	*BaseChannel
	// contains filtered or unexported fields
}

func NewWChannel

func NewWChannel(channelId uint64, conn *websocket.Conn, server *WServer) *WChannel

NewWChannel 创建连接

func (*WChannel) Close

func (conn *WChannel) Close()

func (*WChannel) CloseHandler

func (conn *WChannel) CloseHandler(code int, text string) error

func (*WChannel) GetAddr

func (conn *WChannel) GetAddr() string

func (*WChannel) GetChannelId

func (conn *WChannel) GetChannelId() uint64

func (*WChannel) GetConn

func (conn *WChannel) GetConn() *websocket.Conn

GetConn 获取当前连接

func (*WChannel) SendMsg

func (conn *WChannel) SendMsg(message IMessage)

SendMsg 消息发送

func (*WChannel) Start

func (conn *WChannel) Start()

Start 开启读写业务

type WClient

type WClient struct {
	*BaseClient
	// contains filtered or unexported fields
}

func (*WClient) Close

func (n *WClient) Close() error

func (*WClient) Connect

func (n *WClient) Connect(addr string) error

func (*WClient) Read

func (n *WClient) Read()

func (*WClient) SendMsg

func (n *WClient) SendMsg(message IMessage)

type WServer

type WServer struct {
	*BaseServer
	// contains filtered or unexported fields
}

func NewWServer

func NewWServer(addr string) *WServer

func (*WServer) AddChannel

func (ser *WServer) AddChannel(channel *WChannel)

func (*WServer) CheckOrigin

func (ser *WServer) CheckOrigin(r *http.Request) bool

func (*WServer) Close

func (ser *WServer) Close()

func (*WServer) GetChannel

func (ser *WServer) GetChannel(channelId uint64) IChannel

func (*WServer) RemoveChannel

func (ser *WServer) RemoveChannel(channelId uint64)

func (*WServer) SendMessage

func (ser *WServer) SendMessage(channelId uint64, message IMessage)

func (*WServer) Server

func (ser *WServer) Server()

Server 开启服务

Jump to

Keyboard shortcuts

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