sharpshooter

package module
v0.1.9 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2026 License: MIT Imports: 16 Imported by: 0

README

Sharpshooter

Reliable UDP transport protocol for Go

中文文档


Sharpshooter is a reliable transport protocol built on UDP, implemented in Go. It provides TCP-like connection-oriented semantics without TCP's protocol fingerprint, making it suitable for bypassing protocol-based traffic detection and serving as a transport layer for P2P applications.

Features:

  • TCP-like 3-way handshake
  • ACK-based retransmission
  • Adaptive sliding window congestion control
  • Optional FEC (Forward Error Correction) via Reed-Solomon
  • RTT/RTO auto-calibration
  • Health check & timeout detection
  • Implements net.Conn interface
  • Zero external dependencies besides Reed-Solomon

Quick Start

go get github.com/soyum2222/sharpshooter

Server

l, _ := sharpshooter.Listen(":8858")
conn, _ := l.Accept()
// conn implements net.Conn — use Read/Write directly

Client

conn, _ := sharpshooter.Dial("127.0.0.1:8858")
// Enable FEC (optional)
conn.(*sharpshooter.Sniper).OpenFec(10, 3)
conn.Write([]byte("hello"))

More examples in the example/ directory.


API

Method Description
Dial(addr) (net.Conn, error) Connect to a remote listener
Listen(addr) (*headquarters, error) Start a UDP listener
Accept() (net.Conn, error) Accept a new connection
OpenFec(data, par) Enable FEC (e.g. OpenFec(10, 3) tolerates 30% loss)
SetPackageSize(size) Set packet payload size
SetSendWin(size) / SetRecWin(size) Set send/receive window
OpenStaTraffic() Enable traffic statistics
TrafficStatistics() Get traffic stats snapshot

All standard net.Conn methods (Read, Write, Close, SetDeadline, etc.) are supported.

For protocol specification and detailed documentation, see docs/.


File Transfer Tool

# Server (receive)
go run example/sharp_transfer.go -l 8858 -o output.dat

# Client (send)
go run example/sharp_transfer.go -addr 127.0.0.1:8858 -i input.dat

# Resume transfer
go run example/sharp_transfer.go -addr 127.0.0.1:8858 -i input.dat -o output.dat -c

# Compare with TCP
go run example/sharp_transfer.go -addr 127.0.0.1:8858 -i input.dat -t

License

MIT

Documentation

Index

Constants

View Source
const (
	DEFAULT_HEAD_SIZE                          = 20
	DEFAULT_INIT_SENDWIND                      = 64
	DEFAULT_INIT_RECEWIND                      = 1 << 10
	DEFAULT_INIT_PACKSIZE                      = 800
	DEFAULT_INIT_HEALTHTICKER                  = 1
	DEFAULT_INIT_HEALTHCHECK_TIMEOUT_TRY_COUNT = 10
	DEFAULT_INIT_HANDSHACK_TIMEOUT             = 6
	DEFAULT_INIT_RTO_UNIT                      = float64(200 * time.Millisecond)
	DEFAULT_INIT_DELAY_ACK                     = float64(200 * time.Millisecond)
	DEFAULT_INIT_INTERVAL                      = 500
)
View Source
const (
	STATUS_NONE = iota
	STATUS_SECONDHANDSHACK
	STATUS_THIRDHANDSHACK
	STATUS_NORMAL
	STATUS_CLOSEING1
	STATUS_CLOSEING2
	STATUS_CLOSEING3
)

Variables

View Source
var (
	CLOSEERROR         = errors.New("the connection is closed")
	HEALTHTIMEOUTERROR = errors.New("health monitor timeout ")
	TIMEOUERROR        = timeout(0)
	RCVAMMOBAGEMPTY    = make([]*protocol.Ammo, DEFAULT_INIT_RECEWIND)
)

Functions

func Dial

func Dial(addr string) (net.Conn, error)

func Listen

func Listen(addr string) (*headquarters, error)

func NewHeadquarters

func NewHeadquarters() *headquarters

Types

type Sniper

type Sniper struct {
	Statistics
	// contains filtered or unexported fields
}

func NewSniper

func NewSniper(conn *net.UDPConn, aim *net.UDPAddr) *Sniper

func (*Sniper) CleanStatistics added in v0.1.6

func (s *Sniper) CleanStatistics()

func (*Sniper) Close

func (s *Sniper) Close() error

func (*Sniper) Debug added in v0.1.6

func (s *Sniper) Debug()

func (*Sniper) LocalAddr

func (s *Sniper) LocalAddr() net.Addr

func (*Sniper) OpenFec

func (s *Sniper) OpenFec(dataShards, parShards int)

OpenFec use FEC algorithm in communication this will waste some traffic , but when the packet is lost there is a certain probability that the lost packet can be recovered

func (*Sniper) OpenStaTraffic added in v0.1.6

func (s *Sniper) OpenStaTraffic()

func (*Sniper) Read

func (s *Sniper) Read(b []byte) (n int, err error)

func (*Sniper) RemoteAddr

func (s *Sniper) RemoteAddr() net.Addr

func (*Sniper) SetDeadline

func (s *Sniper) SetDeadline(t time.Time) error

func (*Sniper) SetInterval

func (s *Sniper) SetInterval(interval int64)

func (*Sniper) SetPackageSize added in v0.1.6

func (s *Sniper) SetPackageSize(size int64)

func (*Sniper) SetReadDeadline

func (s *Sniper) SetReadDeadline(t time.Time) error

func (*Sniper) SetRecWin

func (s *Sniper) SetRecWin(size int64)

func (*Sniper) SetSendWin

func (s *Sniper) SetSendWin(size int32)

func (*Sniper) SetWriteDeadline

func (s *Sniper) SetWriteDeadline(t time.Time) error

func (*Sniper) TrafficStatistics added in v0.1.6

func (s *Sniper) TrafficStatistics() Statistics

func (*Sniper) Write

func (s *Sniper) Write(b []byte) (n int, err error)

type Statistics added in v0.1.6

type Statistics struct {
	TotalTraffic     int64
	EffectiveTraffic int64
	TotalPacket      int64
	EffectivePacket  int64
	RTT              int64
	RTO              int64
	SendWin          int64
	ReceiveWin       int64
}

type TimedSched added in v0.1.2

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

TimedSched represents the control struct for timed parallel scheduler

var SystemTimedSched *TimedSched = NewTimedSched(runtime.NumCPU())

SystemTimedSched is the library level timed-scheduler

func NewTimedSched added in v0.1.2

func NewTimedSched(parallel int) *TimedSched

NewTimedSched creates a parallel-scheduler with given parallelization

func (*TimedSched) Close added in v0.1.2

func (ts *TimedSched) Close()

Close terminates this scheduler

func (*TimedSched) Put added in v0.1.2

func (ts *TimedSched) Put(f func(), deadline time.Time)

Put a function 'f' awaiting to be executed at 'deadline'

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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