Skip to content

A Go library for sending data over TCP networks in frames

License

Notifications You must be signed in to change notification settings

AgentCoop/net-dataframe

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Preface

It's quite often in network programming to send large amount of structured data over TCP networks that do not fit into a single TCP packet, hence there is a strong need to re-assemble received packets into a single data frame taking into account packet fragmentation. This Go library is meant to facilitate that.

API

type Receiver interface {
	// Captures all available frames in buffer
	Capture(buf []byte) ([]*dataFrame, error)
	// Resets frame capturing returning captured before data
	Flush() []byte
	// Returns true if no data were captured
	IsEmpty() bool
}

type Sender interface {
	// Converts data into a frame using gob encoding
	ToFrame(data interface{}) (*dataFrame, error)
}

type DataFrame interface {
	// Decodes captured data frame using gob decoder
	Decode(receiver interface{}) error

	GetBytes() []byte
}

How to use

    //
    // Client
    //
    req := &Request{}
    req.msg = "Hello"
    
    // Send your data over wire
    frame, err := netdataframe.ToFrame(p)
    if err != nil {
        panic(err)
    }

    var n int
    n, err = conn.Write(frame.GetBytes())
    ...

    //
    // Server
    //
    // Read network data
    n, err := conn.Read(readbuf)

    // Capture data frames
    recv := netdataframe.NewReceiver()
    frames, err := recv.Capture(readbuf[0:n])
    // Say hello
    req := &Request{}
    err := frames[0].Decode(req)
    if err != nil { panic(err) }
    fmt.Printf(req.msg) // hello    

About

A Go library for sending data over TCP networks in frames

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages