network

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2022 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrConnClosing   = errors.New("use of closed network connection")
	ErrWriteBlocking = errors.New("write packet was blocking")
	ErrReadBlocking  = errors.New("read packet was blocking")
)
View Source
var (
	ErrDataLengthOutOfLimit = errors.New("the size of packet is larger than the limit")
)

Functions

This section is empty.

Types

type Config

type Config struct {
	PacketSendChanLimit    uint32 // the limit of packet send channel
	PacketReceiveChanLimit uint32 // the limit of packet receive channel
	ConnReadTimeout        time.Duration
	ConnWriteTimeout       time.Duration
}

type Conn

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

Conn exposes a set of callbacks for the various events that occur on a connection

func NewConn

func NewConn(conn net.Conn, srv *Server) *Conn

NewConn creates a new connection

func (*Conn) AsyncWritePacket

func (c *Conn) AsyncWritePacket(p Packet, timeout time.Duration) (err error)

AsyncWritePacket async writes a packet, this method woule never be blocked

func (*Conn) Close

func (c *Conn) Close()

Close closes the connection

func (*Conn) Do

func (c *Conn) Do()

Do is to run loops

func (*Conn) GetExtraData

func (c *Conn) GetExtraData() interface{}

GetExtraData returns the extra data from the Conn

func (*Conn) GetRawConn

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

GetRawConn returns the raw net.TCPConn from the Conn

func (*Conn) IsClosed

func (c *Conn) IsClosed() bool

IsClosed indicates whether the connection is closed or not

func (*Conn) PutExtraData

func (c *Conn) PutExtraData(data interface{})

PutExtraData puts the extra data with the Conn

func (*Conn) SetCallback

func (c *Conn) SetCallback(callback ConnCallback)

SetCallback sets the connection callback

type ConnCallback

type ConnCallback interface {
	// OnConnect is called when the connection was accepted,
	// If the return value of false is closed
	OnConnect(*Conn) bool

	// OnMessage is called when the connection receives a packet,
	// If the return value of false is closed
	OnMessage(*Conn, Packet) bool

	// OnClose is called when the connection closed
	OnClose(*Conn)
}

ConnCallback is an interface of methods that are used as callbacks on a connection

type ConnectionCreator

type ConnectionCreator func(net.Conn, *Server) *Conn

ConnectionCreator is a creator to create connection

type DefaultPacket

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

func NewDefaultPacket

func NewDefaultPacket(buff []byte) *DefaultPacket

func (*DefaultPacket) Serialize

func (dp *DefaultPacket) Serialize() []byte

type DefaultProtocol

type DefaultProtocol struct {
}

func (*DefaultProtocol) ReadPacket

func (dp *DefaultProtocol) ReadPacket(r io.Reader) (Packet, error)

type Packet

type Packet interface {
	Serialize() []byte
}

type Protocol

type Protocol interface {
	ReadPacket(conn io.Reader) (Packet, error)
}

type Server

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

func NewServer

func NewServer(config *Config, callback ConnCallback, protocol Protocol) *Server

NewServer creates a new server

func (*Server) Start

func (s *Server) Start(listener net.Listener, creator ConnectionCreator)

Start starts service

func (*Server) Stop

func (s *Server) Stop()

Stop stops service

Jump to

Keyboard shortcuts

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