lossyconn

package module
v0.0.0-...-984944b Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2021 License: MIT Imports: 10 Imported by: 0

README

Lossy connection simulator

GoDoc MIT licensed Build Status Go Report Card Coverage Statusd

Package lossyconn is a lossy connection simulator for Golang.

lossyconn provides packet oriented lossy connection for testing purpose

	left, err := NewLossyPacketConn(0.3, 200)
	if err != nil {
		t.Fatal(err)
	}

	right, err := NewLossyPacketConn(0.2, 180)
	if err != nil {
		t.Fatal(err)
	}

	p := make([]byte, 1024)
	left.WriteTo(p, right.LocalAddr())
	right.ReadFrom(p)

	left.Close()
	right.Close()
	t.Logf("left:%v\n", left)
	t.Logf("right:%v\n", right)

Documentation

Overview

Package lossyconn is a lossy connection simulator for Golang.

lossyconn provides packet oriented lossy connection for testing purpose

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Address

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

Address defines a simulated lossy conn address

func NewAddress

func NewAddress() *Address

NewAddress creates a simulated address by filling with random numbers

func (*Address) Network

func (addr *Address) Network() string

func (*Address) String

func (addr *Address) String() string

type ConnectionManager

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

ConnectionManager contains all lossy connections

func NewConnectionManager

func NewConnectionManager() *ConnectionManager

NewConnectionManager create a connection manager for all LossyConn

func (*ConnectionManager) Delete

func (mgr *ConnectionManager) Delete(conn *LossyConn)

delete a connection from the pool

func (*ConnectionManager) Get

func (mgr *ConnectionManager) Get(addr string) *LossyConn

return a connection from the pool

func (*ConnectionManager) Set

func (mgr *ConnectionManager) Set(conn *LossyConn)

add a connection to the pool

type LossyConn

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

LossyConn implements a net.LossyConn with a given loss rate for sending

func NewLossyConn

func NewLossyConn(loss float64, delay int) (*LossyConn, error)

NewLossyConn create a loss connection with loss rate and latency

loss must be between [0,1]

delay is time in millisecond

func (*LossyConn) Close

func (conn *LossyConn) Close() error

Close closes the connection. Any blocked ReadFrom or WriteTo operations will be unblocked and return errors.

func (*LossyConn) LocalAddr

func (conn *LossyConn) LocalAddr() net.Addr

LocalAddr returns the local network address.

func (*LossyConn) Read

func (conn *LossyConn) Read(b []byte) (int, error)

Read implements the Conn Read method.

func (*LossyConn) ReadFrom

func (conn *LossyConn) ReadFrom(p []byte) (n int, addr net.Addr, err error)

ReadFrom reads a packet from the connection, copying the payload into p. It returns the number of bytes copied into p and the return address that was on the packet. It returns the number of bytes read (0 <= n <= len(p)) and any error encountered. Callers should always process the n > 0 bytes returned before considering the error err. ReadFrom can be made to time out and return an Error with Timeout() == true after a fixed time limit; see SetDeadline and SetReadDeadline.

func (*LossyConn) RemoteAddr

func (conn *LossyConn) RemoteAddr() net.Addr

RemoteAddr returns the remote network address.

func (*LossyConn) SetDeadline

func (conn *LossyConn) SetDeadline(t time.Time) error

SetDeadline sets the read and write deadlines associated with the connection. It is equivalent to calling both SetReadDeadline and SetWriteDeadline.

A deadline is an absolute time after which I/O operations fail with a timeout (see type Error) instead of blocking. The deadline applies to all future and pending I/O, not just the immediately following call to ReadFrom or WriteTo. After a deadline has been exceeded, the connection can be refreshed by setting a deadline in the future.

An idle timeout can be implemented by repeatedly extending the deadline after successful ReadFrom or WriteTo calls.

A zero value for t means I/O operations will not time out.

func (*LossyConn) SetDelayDeviation

func (conn *LossyConn) SetDelayDeviation(deviation float64)

SetDelayDeviation sets the deviation for delays, delay is the median value

Default : 1.0

func (*LossyConn) SetReadDeadline

func (conn *LossyConn) SetReadDeadline(t time.Time) error

SetReadDeadline sets the deadline for future ReadFrom calls and any currently-blocked ReadFrom call. A zero value for t means ReadFrom will not time out.

func (*LossyConn) SetWriteDeadline

func (conn *LossyConn) SetWriteDeadline(t time.Time) error

SetWriteDeadline sets the deadline for future WriteTo calls and any currently-blocked WriteTo call. Even if write times out, it may return n > 0, indicating that some of the data was successfully written. A zero value for t means WriteTo will not time out.

func (*LossyConn) String

func (conn *LossyConn) String() string

func (*LossyConn) Write

func (conn *LossyConn) Write(p []byte) (n int, err error)

Write implements the Conn Write method.

func (*LossyConn) WriteTo

func (conn *LossyConn) WriteTo(p []byte, addr net.Addr) (n int, err error)

WriteTo writes a packet with payload p to addr. WriteTo can be made to time out and return an Error with Timeout() == true after a fixed time limit; see SetDeadline and SetWriteDeadline. On packet-oriented connections, write timeouts are rare.

type Packet

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

Packet defines a data to be sent at some specific time

type TimedSender

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

TimedSender sends Packet to a connection at given time

func NewTimedSender

func NewTimedSender() *TimedSender

func (*TimedSender) Len

func (h *TimedSender) Len() int

func (*TimedSender) Less

func (h *TimedSender) Less(i, j int) bool

func (*TimedSender) Pop

func (h *TimedSender) Pop() interface{}

func (*TimedSender) Push

func (h *TimedSender) Push(x interface{})

func (*TimedSender) Send

func (h *TimedSender) Send(conn *LossyConn, packet Packet, delay time.Duration)

Send with a delay

func (*TimedSender) Swap

func (h *TimedSender) Swap(i, j int)

Jump to

Keyboard shortcuts

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