tcplib

package
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2019 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultConcurrency     = 8 * 1024
	DefaultRequestTimeout  = 20 * time.Second
	DefaultPendingMessages = 32 * 1024
	DefaultFlushDelay      = -1
	DefaultBufferSize      = 64 * 1024
	DefaultDialRetryTime   = 0
	DefaultConnectNumbers  = 1
)
View Source
const (
	HeaderLen = 24
	Version   = 1
	Padding   = 0
	SOH       = 0x10
	EOH       = 0x24
)
View Source
const NoPort = 0

Variables

View Source
var (
	TimeOutError        = error.SetCodeType(10001, "timeout error.")
	OverflowError       = error.SetCodeType(10002, "overflow error.")
	InternalServerError = error.SetCodeType(10003, "interval server error.")
	InvalidParam        = error.SetCodeType(10004, "invalid param")
)
View Source
var (
	NoTcpPort = errors.New("no tcp serve port")
)

Functions

This section is empty.

Types

type AsyncResult

type AsyncResult struct {
	Response Packet
	Error    error
	Done     chan struct{}
	Request  Packet
	// contains filtered or unexported fields
}

func (*AsyncResult) Cancel

func (m *AsyncResult) Cancel()

type Client

type Client struct {
	Addr            string
	Conns           int
	Dial            DialFunc
	DialRetryTime   int
	PendingRequests int
	FlushDelay      time.Duration
	RequestTimeout  time.Duration
	SendBufferSize  int
	RecvBufferSize  int

	Encoder MessageEncoderFunc
	Decoder MessageDecoderFunc
	// contains filtered or unexported fields
}

func (*Client) Call

func (c *Client) Call(req Packet) (rsp Packet, err *dogError.CodeError)

func (*Client) CallAsync

func (c *Client) CallAsync(req Packet, skipResponse bool) (*AsyncResult, *dogError.CodeError)

func (*Client) CallRetry

func (c *Client) CallRetry(req Packet, retryNum uint32) (rsp Packet, err *dogError.CodeError)

func (*Client) CallTimeout

func (c *Client) CallTimeout(req Packet, timeout time.Duration, retryNum uint32) (rsp Packet, err *dogError.CodeError)

func (*Client) SendUDP

func (c *Client) SendUDP(req Packet) (err *dogError.CodeError)

skip response

func (*Client) Start

func (c *Client) Start()

func (*Client) Stop

func (c *Client) Stop()

type DialFunc

type DialFunc func(addr string) (conn io.ReadWriteCloser, err error)

type DogPacket

type DogPacket struct {
	Header
	Body []byte
}

func NewDogPacket

func NewDogPacket(cmd uint32, body []byte) *DogPacket

func NewDogPacketWithRet

func NewDogPacketWithRet(cmd uint32, body []byte, seq uint32, ret uint32) *DogPacket

func NewDogTcpPacketWithSeq

func NewDogTcpPacketWithSeq(cmd uint32, body []byte, seq uint32) *DogPacket

func (*DogPacket) ID

func (p *DogPacket) ID() uint32

func (*DogPacket) SetErrCode

func (p *DogPacket) SetErrCode(code uint32)

type DogPacketDecoder

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

func (*DogPacketDecoder) Decode

func (d *DogPacketDecoder) Decode() (Packet, error)

type DogPacketEncoder

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

func (*DogPacketEncoder) Encode

func (e *DogPacketEncoder) Encode(p Packet) error

func (*DogPacketEncoder) Flush

func (e *DogPacketEncoder) Flush() error

type Handler

type Handler func([]byte) (uint32, []byte)

type HandlerFunc

type HandlerFunc func(req Packet) (rsp Packet)
type Header struct {
	PacketLen uint32
	Seq       uint32
	Cmd       uint32
	CheckSum  uint32
	ErrCode   uint32
	Version   uint8
	Padding   uint8
	SOH       uint8
	EOH       uint8
}

type Listener

type Listener interface {
	Init(addr string) error
	Accept() (conn io.ReadWriteCloser, clientAddr string, err error)
	Close() error
	ListenAddr() net.Addr
}

type MessageDecoder

type MessageDecoder interface {
	Decode() (Packet, error)
}

type MessageDecoderFunc

type MessageDecoderFunc func(r io.Reader, bufferSize int) (decoder MessageDecoder, err error)

type MessageEncoder

type MessageEncoder interface {
	Encode(msg Packet) error
	Flush() error
}

type MessageEncoderFunc

type MessageEncoderFunc func(w io.Writer, bufferSize int) (encoder MessageEncoder, err error)

type Packet

type Packet interface {
	ID() uint32
	SetErrCode(code uint32)
}

type Server

type Server struct {
	Addr             string
	Handler          HandlerFunc
	Concurrency      int
	FlushDelay       time.Duration
	PendingResponses int
	SendBufferSize   int
	RecvBufferSize   int
	Listener         Listener

	Encoder MessageEncoderFunc
	Decoder MessageDecoderFunc
	// contains filtered or unexported fields
}

func (*Server) Serve

func (s *Server) Serve() *dogError.CodeError

func (*Server) Start

func (s *Server) Start() *dogError.CodeError

func (*Server) Stop

func (s *Server) Stop()

type TcpClient

type TcpClient struct {
	Cm map[string]*Client

	Timeout  time.Duration
	RetryNum uint32
	// contains filtered or unexported fields
}

func NewClient

func NewClient(timeout time.Duration, retryNum uint32) *TcpClient

func (*TcpClient) AddAddr

func (c *TcpClient) AddAddr(addr string)

add server address

func (*TcpClient) Connect

func (c *TcpClient) Connect() (*Client, error)

connect

func (*TcpClient) DogConnect

func (c *TcpClient) DogConnect() (*Client, error)

dog packet establish connection

func (*TcpClient) DogInvoke

func (c *TcpClient) DogInvoke(cmd uint32, req []byte, client ...*Client) (rsp []byte, err *dogError.CodeError)

dog packet. Invoke rpc call

func (*TcpClient) Invoke

func (c *TcpClient) Invoke(cmd uint32, req []byte, client ...*Client) (rsp []byte, err *dogError.CodeError)

Invoke rpc call

func (*TcpClient) Stop

func (c *TcpClient) Stop()

Stop stop client

type TcpPacket

type TcpPacket struct {
	Seq       uint32
	ErrCode   uint32
	Cmd       uint32 // also be a string, for dispatch.
	PacketLen uint32
	Body      []byte
}

func NewTcpPacket

func NewTcpPacket(cmd uint32, body []byte) *TcpPacket

func NewTcpPacketWithRet

func NewTcpPacketWithRet(cmd uint32, body []byte, seq uint32, ret uint32) *TcpPacket

func NewTcpPacketWithSeq

func NewTcpPacketWithSeq(cmd uint32, body []byte, seq uint32) *TcpPacket

func (*TcpPacket) ID

func (p *TcpPacket) ID() uint32

func (*TcpPacket) SetErrCode

func (p *TcpPacket) SetErrCode(code uint32)

type TcpPacketDecoder

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

func (*TcpPacketDecoder) Decode

func (d *TcpPacketDecoder) Decode() (Packet, error)

type TcpPacketEncoder

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

func (*TcpPacketEncoder) Encode

func (e *TcpPacketEncoder) Encode(p Packet) error

func (*TcpPacketEncoder) Flush

func (e *TcpPacketEncoder) Flush() error

type TcpServer

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

func NewDogTcpServer

func NewDogTcpServer() *TcpServer

func NewTcpServer

func NewTcpServer() *TcpServer

func (*TcpServer) AddTcpHandler

func (s *TcpServer) AddTcpHandler(headCmd uint32, f Handler)

func (*TcpServer) GetAddr

func (s *TcpServer) GetAddr() string

func (*TcpServer) Run

func (s *TcpServer) Run(port int) error

func (*TcpServer) SetAddr

func (s *TcpServer) SetAddr(addr string)

func (*TcpServer) Stop

func (s *TcpServer) Stop()

Jump to

Keyboard shortcuts

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