dcomm

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2022 License: MIT Imports: 27 Imported by: 0

Documentation

Index

Constants

View Source
const BufferSize uint16 = 200

BufferSize 服务器数据读写buffer大小

View Source
const HeaderSize int = 8
View Source
const Index string = "^"

Index 包头识别符,格式(^)(size)(data), `^`后跟数据包长度,占位长度为PKG_SIZE 例如^6 abc123

Variables

View Source
var (
	ErrNoKey    = errors.New("no key provided")
	ErrNoValue  = errors.New("no value provided")
	ErrNoPrefix = errors.New("no prefix provided")
	ErrNoFunc   = errors.New("no function of watch  provided")
)
View Source
var ErrorDisonnected = fmt.Errorf("tcp connction is not good")

Functions

func FormatOutput

func FormatOutput(in string) string

func GRPCClient

func GRPCClient(address string) (*grpc.ClientConn, error)

func GRPCClientNoTrace

func GRPCClientNoTrace(address string) (*grpc.ClientConn, error)

func GRPCServer

func GRPCServer(port string) (*grpc.Server, error)

func GRPCServerNoTrace

func GRPCServerNoTrace(port string) (*grpc.Server, error)

func GoDiscoveryBroadcast

func GoDiscoveryBroadcast(ctx context.Context, multicastAddress, port string, ipVersion IPVersion, payload []byte, duration time.Duration)

func HttpFileASyncDownloadWithProcess

func HttpFileASyncDownloadWithProcess(url, path string) chan struct {
	Size uint64
	Err  error
}

HttpFileASyncDownloadWithProcess http下载,并实时返回下载的大小

func HttpFileDownload

func HttpFileDownload(url, path string) error

func NewPkgTcpClient

func NewPkgTcpClient(address string, fun TcpHandler) (*pkgTCPclient, error)

func NewPkgTcpServer

func NewPkgTcpServer(port string, fun TcpHandler) (*pkgAsyncTCPServer, error)

func NewTcpServer

func NewTcpServer(port string, fun TcpHandler) (*asyncTCPServer, error)

func ProcessInput

func ProcessInput(buffer string) (left string, output string)

func PublicIP

func PublicIP() string

Types

type Client

type Client interface {
	Put(ctx context.Context, key, value string) error
	Get(ctx context.Context, key string) (string, error)
	//GetWithPrefix 按前缀获取数据
	GetWithPrefix(ctx context.Context, prefix string) ([]KV, error)
	Delete(ctx context.Context, key string) error
	Watch(ctx context.Context, key string) (<-chan struct{}, error)
	WatchPrefix(ctx context.Context, prefix string) (<-chan struct{}, error)
	WatchFunc(ctx context.Context, key string, f func()) error
	WatchFuncPrefix(ctx context.Context, prefix string, f func()) error
	//WatchOnce 针对某个key有且只能watch一次
	WatchOnce(ctx context.Context, key string) (<-chan struct{}, error)
	//WatchOncePrefix 针对某个前缀有且只能watch一次
	// <-chan struct{}触发时,可以用GetWithPrefix获取最新数据
	WatchOncePrefix(ctx context.Context, prefix string) (<-chan struct{}, error)
	Close()
}

func NewETCDClientV3

func NewETCDClientV3(endpoints []string, options ClientOptions) (Client, error)

type ClientOptions

type ClientOptions struct {
	Cert          string
	Key           string
	CACert        string
	DialTimeout   time.Duration
	DialKeepAlive time.Duration
}

type DiscoverSettings

type DiscoverSettings struct {
	// Limit is the number of peers to discover, use < 1 for unlimited.
	Limit int
	// Port is the port to broadcast on (the peers must also broadcast using the same port).
	// The default port is 9999.
	Port string
	// MulticastAddress specifies the multicast address.
	// You should be able to use any of 224.0.0.0/4 or ff00::/8.
	// By default it uses the Simple Service Discovery Protocol
	// address (239.255.255.250 for IPv4 or ff02::c for IPv6).
	MulticastAddress string
	// Payload is the bytes that are sent out with each broadcast. Must be short.
	Payload []byte
	// PayloadFunc is the function that will be called to dynamically generate payload
	// before every broadcast. If this pointer is nil `Payload` field will be broadcasted instead.
	PayloadFunc func() []byte
	// Delay is the amount of time between broadcasts. The default delay is 1 second.
	Delay time.Duration
	// TimeLimit is the amount of time to spend discovering, if the limit is not reached.
	// A negative limit indiciates scanning until the limit was reached or, if an
	// unlimited scanning was requested, no timeout.
	// The default time limit is 10 seconds.
	TimeLimit time.Duration
	// StopChan is a channel to stop the peer discvoery immediatley after reception.
	StopChan chan struct{}
	// AllowSelf will allow discovery the local machine (default false)
	AllowSelf bool
	// DisableBroadcast will not allow sending out a broadcast
	DisableBroadcast bool
	// IPVersion specifies the version of the Internet Protocol (default IPv4)
	IPVersion IPVersion
	// Notify will be called each time a new peer was discovered.
	// The default is nil, which means no notification whatsoever.
	Notify func(Discovered)
	// contains filtered or unexported fields
}

Settings are the settings that can be specified for doing peer discovery.

type Discovered

type Discovered struct {
	// Address is the local address of a discovered peer.
	Address string
	// Payload is the associated payload from discovered peer.
	Payload []byte
}

Discovered is the structure of the discovered peers, which holds their local address (port removed) and a payload if there is one.

func Discover

func Discover(s DiscoverSettings) (discoveries []Discovered, err error)

Discover will use the created settings to scan for LAN peers. It will return an array of the discovered peers and their associate payloads. It will not return broadcasts sent to itself.

func (Discovered) String

func (d Discovered) String() string

type HttpResponse

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

func HttpDelete

func HttpDelete(url string) (*HttpResponse, error)

func HttpGet

func HttpGet(url string) (*HttpResponse, error)

func HttpPost

func HttpPost(url string, data []byte) (*HttpResponse, error)

func HttpPostWithReader

func HttpPostWithReader(url string, data io.Reader) (*HttpResponse, error)

func HttpPutJson

func HttpPutJson(url string, data interface{}) (*HttpResponse, error)

func ParseHttpResponse

func ParseHttpResponse(r *http.Response) (*HttpResponse, error)

func (*HttpResponse) GetBody

func (res *HttpResponse) GetBody() []byte

func (*HttpResponse) GetBodyString

func (res *HttpResponse) GetBodyString() string

func (*HttpResponse) GetCode

func (res *HttpResponse) GetCode() int

func (*HttpResponse) GetHeader

func (res *HttpResponse) GetHeader(key string) ([]string, bool)

type IPVersion

type IPVersion uint

IPVersion specifies the version of the Internet Protocol to be used.

const (
	IPv4 IPVersion = 4
	IPv6 IPVersion = 6
)

type KV

type KV interface {
	GetKey() string
	GetValue() string
}

type NetPacketConn

type NetPacketConn interface {
	JoinGroup(ifi *net.Interface, group net.Addr) error
	SetMulticastInterface(ini *net.Interface) error
	SetMulticastTTL(int) error
	ReadFrom(buf []byte) (int, net.Addr, error)
	WriteTo(buf []byte, dst net.Addr) (int, error)
}

type PacketConn4

type PacketConn4 struct {
	*ipv4.PacketConn
}

func (PacketConn4) ReadFrom

func (pc4 PacketConn4) ReadFrom(buf []byte) (int, net.Addr, error)

ReadFrom wraps the ipv4 ReadFrom without a control message

func (PacketConn4) WriteTo

func (pc4 PacketConn4) WriteTo(buf []byte, dst net.Addr) (int, error)

WriteTo wraps the ipv4 WriteTo without a control message

type PacketConn6

type PacketConn6 struct {
	*ipv6.PacketConn
}

func (PacketConn6) ReadFrom

func (pc6 PacketConn6) ReadFrom(buf []byte) (int, net.Addr, error)

ReadFrom wraps the ipv6 ReadFrom without a control message

func (PacketConn6) SetMulticastTTL

func (pc6 PacketConn6) SetMulticastTTL(i int) error

SetMulticastTTL wraps the hop limit of ipv6

func (PacketConn6) WriteTo

func (pc6 PacketConn6) WriteTo(buf []byte, dst net.Addr) (int, error)

WriteTo wraps the ipv6 WriteTo without a control message

type TcpClient

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

func NewTcpClient

func NewTcpClient(address string, handler TcpHandler) (*TcpClient, error)

func (*TcpClient) Close

func (c *TcpClient) Close() error

io.Closer

func (*TcpClient) Start

func (c *TcpClient) Start()

func (*TcpClient) Write

func (c *TcpClient) Write(out []byte) (int, error)

io.Writer

type TcpHandler

type TcpHandler func([]byte)

TcpHandler 定义回调函数

Jump to

Keyboard shortcuts

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