Documentation
¶
Overview ¶
@Title gob流式数据传输和文件传输 @Description 利用gob编码,在一个TCP连接中同时传输多个通道的数据或文件。 @Author 傅惠忠<[email protected]> 2024-3-10 @Update 傅惠忠<[email protected]> 2024-3-10
Index ¶
- Constants
- type StandaloneData
- type StreamConn
- func (stream *StreamConn) BeginReader() (res chan *StandaloneData, err error)
- func (p *StreamConn) Close()
- func (p *StreamConn) Recv() (*StreamData, error)
- func (p *StreamConn) Send(data *StreamConn) error
- func (p *StreamConn) SendBytes(session uint32, buf []byte, zipped bool) error
- func (p *StreamConn) SendBytesBody(session uint32, buf []byte, zipped bool) error
- func (p *StreamConn) SendBytesBodyWithExt(session uint32, buf []byte, zipped bool, ext uint32) error
- func (p *StreamConn) SendBytesEnd(session uint32) error
- func (p *StreamConn) SendBytesEndWithExt(session uint32, ext uint32) error
- func (p *StreamConn) SendBytesHead(session uint32, buf []byte, zipped bool) error
- func (p *StreamConn) SendBytesHeadWithExt(session uint32, buf []byte, zipped bool, ext uint32) error
- func (p *StreamConn) SendBytesWithExt(session uint32, buf []byte, zipped bool, ext uint32) error
- func (p *StreamConn) SendFile(session uint32, pathname string) error
- func (p *StreamConn) SendFileWithExt(session uint32, pathname string, ext uint32) error
- func (p *StreamConn) SendString(session uint32, s string, zipped bool) error
- func (p *StreamConn) SendStringWithExt(session uint32, s string, zipped bool, ext uint32) error
- type StreamData
Constants ¶
const ( DataTypeFile = "file" DataTypeByte = "byte" )
对应 StandaloneData 的两种数据类型的常量
const ( // status StatusAlone = "alone" StatusStart = "start" StatusEnd = "end" StatusEmpty = "" // type TypStream = "streamdata" TypFileHead = "file" TypFileData = "filedata" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type StandaloneData ¶
type StandaloneData struct {
Typ string // 可能有三种值:file/byte
Session uint32 // 用数字代表不同的通道
FileName string // 文件完整路径,对应 file 类型
Bytes []byte // 二进制内容, 对应 byte 类型
Ext uint32 // 供程序自定义的扩展信息
}
StandaloneData 经过处理后的完整数据的统一数据结构
type StreamConn ¶
StreamConn 代表一个连接的结构体
func NewStreamConn ¶
func NewStreamConn(conn io.ReadWriteCloser) *StreamConn
NewStreamConn 从一个TCP连接创建新的StreamConn对象
func (*StreamConn) BeginReader ¶
func (stream *StreamConn) BeginReader() (res chan *StandaloneData, err error)
BeginReader() 返回一个管道, 调用该方法后,程序开始在后台解析接收到的数据包, 把数据还原为 byte[] 数据或文件, 用户可以反复从返回的管道读取 StandaloneData 数据。
func (*StreamConn) SendBytes ¶
func (p *StreamConn) SendBytes(session uint32, buf []byte, zipped bool) error
SendBytes 用指定的session发送二进制数据buf,zipped参数的代表是否压缩。
func (*StreamConn) SendBytesBody ¶
func (p *StreamConn) SendBytesBody(session uint32, buf []byte, zipped bool) error
SendBytesBody 用指定的session发送分段二进制数据的数据块buf,zipped参数的代表是否压缩。
func (*StreamConn) SendBytesBodyWithExt ¶
func (p *StreamConn) SendBytesBodyWithExt(session uint32, buf []byte, zipped bool, ext uint32) error
SendBytesBodyWithExt 用指定的session发送分段二进制数据的数据块buf,zipped参数的代表是否压缩,ext代表程序自定义信息。
func (*StreamConn) SendBytesEnd ¶
func (p *StreamConn) SendBytesEnd(session uint32) error
SendBytesEnd 用指定的session发送分段二进制数据的尾部,代表该数据已经传输结束。
func (*StreamConn) SendBytesEndWithExt ¶
func (p *StreamConn) SendBytesEndWithExt(session uint32, ext uint32) error
SendBytesEndWithExt 用指定的session发送分段二进制数据的尾部,代表该数据已经传输结束,ext代表程序自定义信息。
func (*StreamConn) SendBytesHead ¶
func (p *StreamConn) SendBytesHead(session uint32, buf []byte, zipped bool) error
SendBytesHead 用指定的session发送分段二进制数据的头部数据buf,zipped参数的代表是否压缩。
func (*StreamConn) SendBytesHeadWithExt ¶
func (p *StreamConn) SendBytesHeadWithExt(session uint32, buf []byte, zipped bool, ext uint32) error
SendBytesHeadWithExt 用指定的session发送分段二进制数据的头部数据buf,zipped参数的代表是否压缩,ext代表程序自定义信息。
func (*StreamConn) SendBytesWithExt ¶
SendBytesWithExt 用指定的session发送二进制数据buf,zipped参数的代表是否压缩,ext代表程序自定义信息。
func (*StreamConn) SendFile ¶
func (p *StreamConn) SendFile(session uint32, pathname string) error
SendFile 用指定的session发送文件pathname,数据将会用gz算法压缩
func (*StreamConn) SendFileWithExt ¶
func (p *StreamConn) SendFileWithExt(session uint32, pathname string, ext uint32) error
SendFileWithExt 用指定的session发送文件pathname,数据将会用gz算法压缩,ext代表程序自定义信息
func (*StreamConn) SendString ¶
func (p *StreamConn) SendString(session uint32, s string, zipped bool) error
SendString 用指定的session发送单独的字符串s,zipped参数的代表是否压缩。
func (*StreamConn) SendStringWithExt ¶
SendStringWithExt 用指定的session发送单独的字符串s,zipped参数的代表是否压缩,ext代表程序自定义信息。
type StreamData ¶
type StreamData struct {
Typ string //可能有三种值:file/filedata/steamdata
Session uint32 //用数字代表不同的通道
Status string //控制当前通道的状态
Name string //文件名,仅用于传输文件
Mode uint32 //文件属性,等同于fs.FileMode,仅用于传输文件
Format string //代表该数据是否压缩,仅可能有两种值:raw/gz
Data []byte //数据本身
Ext uint32 //供程序自定义的扩展信息
}
StreamData 用于网络传输的统一数据结构
func (*StreamData) GetBytes ¶
func (p *StreamData) GetBytes() ([]byte, error)
GetBytes 将数据快解码为[]byte类型返回
func (*StreamData) GetFileData ¶
func (p *StreamData) GetFileData() (yes bool, end bool, data []byte, err error)
GetFileData 读取一个文件数据快类型的StreamData中的数据, 返回值yes-表示是不是一个文件数据段,end-表示传输是否到达尾部(尾部块不包含文件数据),data-表示数据,er-r如果没有错误返回nil
func (*StreamData) GetString ¶
func (p *StreamData) GetString() (string, error)
GetString 将数据快解码为字符串返回
func (*StreamData) IsFileHeader ¶
func (p *StreamData) IsFileHeader() bool
IsFileHeader 判断一个StreamData是不是文件头。