https://github.com/agentcoop/net-dataframe
A Go library for sending data over TCP networks in frames
https://github.com/agentcoop/net-dataframe
frames go golang network-programming networking tcp-networks
Last synced: 10 months ago
JSON representation
A Go library for sending data over TCP networks in frames
- Host: GitHub
- URL: https://github.com/agentcoop/net-dataframe
- Owner: AgentCoop
- License: mit
- Created: 2020-12-31T19:06:50.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2021-01-13T11:19:01.000Z (about 5 years ago)
- Last Synced: 2025-04-12T20:53:38.158Z (10 months ago)
- Topics: frames, go, golang, network-programming, networking, tcp-networks
- Language: Go
- Homepage:
- Size: 17.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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
```go
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
```go
//
// 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
````