network

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	NEW_CONNECTION = iota // 新连接状态
	WORKING               // 工作中状态
	CLOSING               // 关闭中状态
	CLOSED                // 已关闭状态
)

连接状态常量

View Source
const (
	PKG_TYPE      = iota // 数据包类型
	HEARTBEAT            // 心跳包类型
	HEARTBEAT_RET        // 心跳包响应类型
	DATA                 // 数据类型
)

数据包类型常量

Variables

This section is empty.

Functions

This section is empty.

Types

type Data

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

Data 结构体表示一个通用的数据包

type Handler

type Handler interface {
	Connect(uint16, *Session)
	Message(uint16, uint16, []byte)
	Heartbeat(uint16, uint16)
	Close(uint16)
}

Handler 定义了RPC网络处理器的接口,包括连接、消息、心跳和关闭事件的处理方法。

type Session

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

Session 结构体表示一个网络会话

func CreateSession

func CreateSession(conn io.ReadWriteCloser, msgHandle func(uint16, uint16, []byte)) *Session

CreateSession 创建一个新的会话

func (*Session) Close

func (this *Session) Close()

Close 标记会话状态为 CLOSING

func (*Session) Forward

func (this *Session) Forward(msgHandle func(uint16, uint16, []byte))

Forward 更改会话的消息处理函数

func (*Session) Reader

func (this *Session) Reader() (err error)

Reader 从连接中读取数据并解析成消息

func (*Session) Release

func (this *Session) Release()

Release 释放会话资源

func (*Session) Start

func (this *Session) Start()

Start 用于启动会话的工作。一旦启动,会话将进入 WORKING 状态, 并创建独立的 goroutine 来处理读取和写入操作。

type TcpClient

type TcpClient struct {
	TcpConn // 嵌入TcpConn以复用网络连接和关闭方法
	// contains filtered or unexported fields
}

TcpClient 表示RPC客户端,用于建立与服务器的连接并处理网络通信。

func NewTcpClient

func NewTcpClient(handle Handler) *TcpClient

NewTcpClient 创建一个新的TCP客户端实例。 参数 handle 是一个实现了Handler接口的对象,用于处理网络连接事件和消息。 返回一个新的TcpClient实例,用于建立与服务器的连接和处理通信。

func (*TcpClient) Close

func (this *TcpClient) Close()

Close 关闭TCP客户端连接,停止心跳定时器和清理资源。

func (*TcpClient) Dial

func (this *TcpClient) Dial(addr string)

Dial 建立与指定地址的TCP连接并初始化客户端会话。 参数 addr 是服务器的网络地址,如"host:port"。

func (*TcpClient) GetSessionID

func (this *TcpClient) GetSessionID() uint16

GetSessionID 获取当前客户端会话的唯一标识符(会话ID)。 返回一个uint16类型的值,表示当前会话的唯一标识符。

func (*TcpClient) WriteData

func (this *TcpClient) WriteData(s *Session, buff []byte) uint16

WriteData 向服务器发送自定义数据消息,并返回分配的会话ID。 参数 s 是会话实例,buff 是要发送的数据内容。 返回一个uint16类型的值,表示分配的会话ID。

type TcpConn

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

TcpConn 包含TCP连接相关的通用操作和处理器接口。

func (*TcpConn) Close

func (this *TcpConn) Close(s *Session)

Close 关闭指定会话并释放相关资源。 参数 s 是会话的唯一标识符。

func (*TcpConn) NewSession

func (this *TcpConn) NewSession(conn net.Conn) *Session

NewSession 创建一个新的会话实例,关联到指定的TCP连接。 参数 conn 是网络连接实例,msgHandler 是消息处理器接口。 返回一个新的会话实例。

func (*TcpConn) Write

func (this *TcpConn) Write(s *Session, sID uint16, buff []byte)

Write 向指定会话发送数据消息。 参数 s 是会话实例,sID 是会话的唯一标识符,buff 是要发送的数据内容。

type TcpServer

type TcpServer struct {
	TcpConn
	// contains filtered or unexported fields
}

TcpServer 表示RPC服务器,处理网络连接和消息传递。

func NewTcpServer

func NewTcpServer(handle Handler, addr string) *TcpServer

NewTcpServer 创建一个新的TCP服务器实例,监听指定地址。

func (*TcpServer) Start

func (this *TcpServer) Start()

Start 启动TCP服务器,监听指定地址并接受客户端连接。

Jump to

Keyboard shortcuts

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