gobstream

package module
v1.2.1 Latest Latest
Warning

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

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

README

gob流式数据传输和文件传输

基础协议:tcp

利用go语言gob编码格式,在一个TCP连接上同时进行多路流式数据传输和文件传输。

一、文件

发送方先发送文件头,指定Session,然后顺序发送数据块;接收方根据文件头Session接收文件。

文件头
struct {
	Typ string
	Name string
	Mode uint32
	Session uint32
}
  • Typ: 字符串:file
数据块
struct {
	Typ string
	Session uint32
	Status string
	Format string
	Data []byte
}
  • Status: 空字符串表示正在传输;end表示结束,此时FormatData都应该是空。
  • Typ: filedata
  • Format: raw/gz
  • Session:程序根据数据块的 Session 判断对应的文件头

二、数据

根据指定Session,发送数据,可以单独发送数据快,也可以分段发送。

数据头
struct {
	Typ string
	Status string
	Session uint32
	Format string
	Data []byte
}
  • Typ: streamdata
  • Status: start长数据块的第一个;alone单独数据块
数据块
struct {
	Typ string
	Status string
	Session uint32
	Format string
	Data []byte
}
  • Typ: streamdata
  • Session: 和数据头一样
  • Status: 空字符串
数据尾
struct {
	Typ string
	Status string
	Session uint32
}
  • Typ: streamdata
  • Status: end
  • Session: 和数据头一样

统一数据结构

struct {
	Typ string
	Session uint32
	Status string
	Name string
	Mode uint32
	Format string
	Data []byte
	Ext uint32
}
  • Ext:供程序自定义的扩展信息

一些常量

const (
	// status
	StatusAlone = "alone"
	StatusStart = "start"
	StatusEnd   = "end"
	StatusEmpty = ""
	// type
	TypStream   = "streamdata"
	TypFileHead = "file"
	TypFileData = "filedata"
)

v1.2.0 的改进

新增方法:

func (stream *StreamConn) BeginReader() (res chan *StandaloneData, err error)

BeginReader() 返回一个管道,调用该方法后,程序开始在后台解析接收到的数据包,把数据还原为 byte[] 数据或文件,用户可以反复从返回的管道读取 StandaloneData 数据。

对应的数据结构和常量:

// StandaloneData 经过处理后的完整数据的统一数据结构
type StandaloneData struct {
	Typ      string // 可能有三种值:file/byte
	Session  uint32 // 用数字代表不同的通道
	FileName string // 文件完整路径,对应 file 类型
	Bytes    []byte // 二进制内容, 对应 byte 类型
	Ext      uint32 // 供程序自定义的扩展信息
}

// 对应 StandaloneData 的两种数据类型的常量
const (
	DataTypeFile = "file"
	DataTypeByte = "byte"
)

具体用法在新增样例 server2 中。

Documentation

Overview

@Title gob流式数据传输和文件传输 @Description 利用gob编码,在一个TCP连接中同时传输多个通道的数据或文件。 @Author 傅惠忠<[email protected]> 2024-3-10 @Update 傅惠忠<[email protected]> 2024-3-10

Index

Constants

View Source
const (
	DataTypeFile = "file"
	DataTypeByte = "byte"
)

对应 StandaloneData 的两种数据类型的常量

View Source
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

type StreamConn struct {
	Conn io.ReadWriteCloser
	Enc  *gob.Encoder
	Dec  *gob.Decoder
}

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) Close

func (p *StreamConn) Close()

Close 关闭连接

func (*StreamConn) Recv

func (p *StreamConn) Recv() (*StreamData, error)

Recv 接收一个 StreamData

func (*StreamConn) Send

func (p *StreamConn) Send(data *StreamConn) error

Send 发送一个 StreamData

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

func (p *StreamConn) SendBytesWithExt(session uint32, buf []byte, zipped bool, ext uint32) error

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

func (p *StreamConn) SendStringWithExt(session uint32, s string, zipped bool, ext uint32) error

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是不是文件头。

Directories

Path Synopsis
example
client command
server command
演示最早版本的使用方法。
演示最早版本的使用方法。
server2 command
演示 v1.2.0 版本的使用方法。
演示 v1.2.0 版本的使用方法。

Jump to

Keyboard shortcuts

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