tcp

package module
v0.0.0-...-43f0627 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2018 License: Apache-2.0 Imports: 12 Imported by: 37

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrConnClosing  = errors.New("use of closed network connection")
	ErrBufferFull   = errors.New("the async send buffer is full")
	ErrWriteTimeout = errors.New("async write packet timeout")
)

Functions

This section is empty.

Types

type AsyncTCPServer

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

AsyncTCPServer 结构定义

func NewAsyncTCPServer

func NewAsyncTCPServer(tcpAddr string, callback CallBack, protocol Protocol) *AsyncTCPServer

NewAsyncTCPServer 返回一个AsyncTCPServer实例

func (*AsyncTCPServer) Close

func (srv *AsyncTCPServer) Close()

Close 首先关闭所有连接,然后关闭TCPServer

func (*AsyncTCPServer) Connect

func (srv *AsyncTCPServer) Connect(ip string, callback CallBack, protocol Protocol) (*TCPConn, error)

Connect 使用指定的callback和protocol连接其他TCPServer,返回TCPConn

func (*AsyncTCPServer) GetAllTCPConn

func (srv *AsyncTCPServer) GetAllTCPConn() []*TCPConn

func (*AsyncTCPServer) GetTCPConn

func (srv *AsyncTCPServer) GetTCPConn(key string) *TCPConn

func (*AsyncTCPServer) ListenAndServe

func (srv *AsyncTCPServer) ListenAndServe() error

ListenAndServe 使用AsyncTCPServer的tcpAddr创建TCPListner并调用Server()方法开启监听

func (*AsyncTCPServer) Serve

func (srv *AsyncTCPServer) Serve(l *net.TCPListener) error

Serve 使用指定的TCPListener开启监听

func (*AsyncTCPServer) SetReadDeadline

func (srv *AsyncTCPServer) SetReadDeadline(t time.Duration)

func (*AsyncTCPServer) SetWriteDeadline

func (srv *AsyncTCPServer) SetWriteDeadline(t time.Duration)

type CallBack

type CallBack interface {
	//链接建立回调
	OnConnected(conn *TCPConn)
	//消息处理回调
	OnMessage(conn *TCPConn, p Packet)
	//链接断开回调
	OnDisconnected(conn *TCPConn)
	//错误回调
	OnError(err error)
}

CallBack 是一个回调接口,用于连接的各种事件处理

type DefaultPacket

type DefaultPacket struct {
	Type PacketType
	Body []byte
}

func NewDefaultPacket

func NewDefaultPacket(t PacketType, body []byte) *DefaultPacket

func (*DefaultPacket) Bytes

func (m *DefaultPacket) Bytes() []byte

type DefaultProtocol

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

func (*DefaultProtocol) ReadPacket

func (p *DefaultProtocol) ReadPacket(reader io.Reader) (Packet, error)

func (*DefaultProtocol) ReadPacketLimit

func (*DefaultProtocol) ReadPacketLimit(reader io.Reader, size uint32) (Packet, error)

func (*DefaultProtocol) SetMaxPacketSize

func (p *DefaultProtocol) SetMaxPacketSize(n uint32)

func (*DefaultProtocol) WritePacket

func (*DefaultProtocol) WritePacket(writer io.Writer, p Packet) error

type Packet

type Packet interface {
	Bytes() []byte
}

type PacketType

type PacketType byte

type Protocol

type Protocol interface {
	ReadPacket(reader io.Reader) (Packet, error)
	WritePacket(writer io.Writer, msg Packet) error
}

type TCPConn

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

func NewTCPConn

func NewTCPConn(conn *net.TCPConn, callback CallBack, protocol Protocol) *TCPConn

func (*TCPConn) AsyncWritePacket

func (c *TCPConn) AsyncWritePacket(p Packet) error

func (*TCPConn) AsyncWritePacketWithTimeout

func (c *TCPConn) AsyncWritePacketWithTimeout(p Packet, sec int) error

func (*TCPConn) Close

func (c *TCPConn) Close()

func (*TCPConn) GetExtraData

func (c *TCPConn) GetExtraData(key string) interface{}

func (*TCPConn) GetLocalAddr

func (c *TCPConn) GetLocalAddr() net.Addr

func (*TCPConn) GetLocalIPAddress

func (c *TCPConn) GetLocalIPAddress() string

LocalIPAddress 返回socket连接本地的ip地址

func (*TCPConn) GetRawConn

func (c *TCPConn) GetRawConn() *net.TCPConn

func (*TCPConn) GetRemoteAddr

func (c *TCPConn) GetRemoteAddr() net.Addr

func (*TCPConn) GetRemoteIPAddress

func (c *TCPConn) GetRemoteIPAddress() string

func (*TCPConn) IsClosed

func (c *TCPConn) IsClosed() bool

func (*TCPConn) ReadPacket

func (c *TCPConn) ReadPacket() (Packet, error)

func (*TCPConn) Serve

func (c *TCPConn) Serve() error

func (*TCPConn) SetExtraData

func (c *TCPConn) SetExtraData(key string, data interface{})

type TCPConnBucket

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

TCPConnBucket 用来存放和管理TCPConn连接

func NewTCPConnBucket

func NewTCPConnBucket() *TCPConnBucket

func (*TCPConnBucket) Delete

func (b *TCPConnBucket) Delete(id string)

func (*TCPConnBucket) Get

func (b *TCPConnBucket) Get(id string) *TCPConn

func (*TCPConnBucket) GetAll

func (b *TCPConnBucket) GetAll() map[string]*TCPConn

func (*TCPConnBucket) Put

func (b *TCPConnBucket) Put(id string, c *TCPConn)

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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