net2

package
v0.0.27 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2024 License: BSD-2-Clause Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrTimeout = errors.New("time out")
	ErrParam   = errors.New("param error")
	ErrBuffer  = errors.New("buffer error")
	ErrClose   = errors.New("closed by the peer")
	ErrOOM     = errors.New("oom")
)

Functions

func CheckTimeout

func CheckTimeout(err error) bool

func GetDefaultPackageHeadLen

func GetDefaultPackageHeadLen() int

Types

type ClientBase

type ClientBase struct {
	Status Status

	//sendUse     sync.Map
	SessionIdU uint64
	UnionIdStr string
	// contains filtered or unexported fields
}

func (*ClientBase) CloseTimeout

func (c *ClientBase) CloseTimeout()

func (*ClientBase) CloseWithErr

func (c *ClientBase) CloseWithErr(err error, stack []byte, check bool)

func (*ClientBase) Error

func (c *ClientBase) Error() string

func (*ClientBase) Init

func (c *ClientBase) Init(ddb DataDecodeBase, ttl, RTtl time.Duration, onSocket OnSocket, con Conn, socket iSocket)

func (*ClientBase) IsClosedByPeer

func (c *ClientBase) IsClosedByPeer() bool

func (*ClientBase) Reactor

func (c *ClientBase) Reactor()

func (*ClientBase) SafeClose

func (c *ClientBase) SafeClose(byLocalNotRemote bool)

func (*ClientBase) Send

func (c *ClientBase) Send(buf []byte) bool

func (*ClientBase) SessionId

func (c *ClientBase) SessionId() uint64

func (*ClientBase) Stack

func (c *ClientBase) Stack() []byte

func (*ClientBase) UnionId

func (c *ClientBase) UnionId() string

type ClientSocket

type ClientSocket struct {
	ClientBase
	// contains filtered or unexported fields
}

*********ClientSocket

func Agent

func Agent(conn net.Conn, ttl time.Duration, rTtl time.Duration, OnSocket OnSocket, ddb DataDecodeBase) *ClientSocket

func (*ClientSocket) Close

func (c *ClientSocket) Close() error

func (*ClientSocket) Connect

func (c *ClientSocket) Connect(addr string, ttl time.Duration, OnSocket OnSocket, ddb DataDecodeBase) error

func (*ClientSocket) ConnectHostPort

func (c *ClientSocket) ConnectHostPort(host string, port uint16, Ttl time.Duration, OnSocket OnSocket, ddb DataDecodeBase) error

func (*ClientSocket) LocalAddr

func (c *ClientSocket) LocalAddr() net.Addr

LocalAddr returns the local network address.

func (*ClientSocket) ReConnect

func (c *ClientSocket) ReConnect(addr string) error

func (*ClientSocket) RemoteAddr

func (c *ClientSocket) RemoteAddr() net.Addr

RemoteAddr returns the remote network address.

func (*ClientSocket) SetConnect added in v0.0.19

func (c *ClientSocket) SetConnect(conn net.Conn)

支持复用

type ClientWSocket

type ClientWSocket struct {
	ClientBase
	// contains filtered or unexported fields
}

func WebAgent

func WebAgent(conn *websocket.Conn, msgType int, ttl time.Duration, rTtl time.Duration, OnSocket OnSocket, ddb DataDecodeBase) *ClientWSocket

@msgType TextMessage or BinaryMessage

func (*ClientWSocket) Close

func (c *ClientWSocket) Close() error

func (*ClientWSocket) Connect

func (c *ClientWSocket) Connect(addr string, msgType int, ttl time.Duration, OnSocket OnSocket, ddb DataDecodeBase) error

@msgType TextMessage or BinaryMessage

func (*ClientWSocket) LocalAddr

func (c *ClientWSocket) LocalAddr() net.Addr

LocalAddr returns the local network address.

func (*ClientWSocket) ReConnect

func (c *ClientWSocket) ReConnect(addr string) error

func (*ClientWSocket) RemoteAddr

func (c *ClientWSocket) RemoteAddr() net.Addr

RemoteAddr returns the remote network address.

func (*ClientWSocket) SetConnect added in v0.0.19

func (c *ClientWSocket) SetConnect(conn *websocket.Conn)

支持复用

type Conn

type Conn interface {
	//获取最近的错误
	error

	//安全关闭连接
	//@param byLocalNotRemote 是否是本地主动本地断开;true:对服务器而言就是服务器把客户端断开,对客户端而言就是客户端主动断服务器
	SafeClose(byLocalNotRemote bool)

	// 封装发送buffer
	Send(buf []byte) bool

	//是否是被对方关闭了连接
	IsClosedByPeer() bool

	//获取最近的调用堆栈,如果有的话
	Stack() []byte

	//本地地址
	LocalAddr() net.Addr

	//对方地址
	RemoteAddr() net.Addr

	//当前服务器中的唯一ID
	SessionId() uint64

	//唯一ID,多个服务器的话可能 SessionId会一样,但是UnionId肯定不一样
	UnionId() string
}

* 对外宣称对象

type Context

type Context struct {
	Con Conn //连接对象

	OnSocket OnSocket //对socket的监听
	// contains filtered or unexported fields
}

func (*Context) Deadline

func (c *Context) Deadline() (deadline time.Time, ok bool)

func (*Context) Done

func (c *Context) Done() <-chan struct{}

func (*Context) Err

func (c *Context) Err() error

func (*Context) Get

func (c *Context) Get(key string) (value any, exists bool)

func (*Context) GetBool

func (c *Context) GetBool(key string) (b bool)

GetBool returns the value associated with the key as a boolean.

func (*Context) GetFloat64

func (c *Context) GetFloat64(key string) (f64 float64)

GetFloat64 returns the value associated with the key as a float64.

func (*Context) GetInt

func (c *Context) GetInt(key string) (i int)

GetInt returns the value associated with the key as an integer.

func (*Context) GetInt64

func (c *Context) GetInt64(key string) (i64 int64)

GetInt64 returns the value associated with the key as an integer.

func (*Context) GetString

func (c *Context) GetString(key string) (s string)

GetString returns the value associated with the key as a string.

func (*Context) GetUint

func (c *Context) GetUint(key string) (ui uint)

GetUint returns the value associated with the key as an unsigned integer.

func (*Context) GetUint64

func (c *Context) GetUint64(key string) (ui64 uint64)

GetUint64 returns the value associated with the key as an unsigned integer.

func (*Context) SessionId

func (c *Context) SessionId() uint64

func (*Context) Set

func (c *Context) Set(key string, value any)

func (*Context) String

func (c *Context) String() string

func (*Context) Value

func (c *Context) Value(key any) any

type DataDecodeBase

type DataDecodeBase interface {
	//包解析 =》 包长+包内容  =》整个包的长度 = 包长+包内容长度
	//返回包长的长度
	GetPackageHeadLen() int
	//返回包内容的长度
	GetPackageLen([]byte) int
}

* Socket 数据解析

type DataDecodeBinaryBigEnd

type DataDecodeBinaryBigEnd struct {
}

二进制大端包 => 2字节包长+包内容

func (*DataDecodeBinaryBigEnd) GetPackageHeadLen

func (d *DataDecodeBinaryBigEnd) GetPackageHeadLen() int

func (*DataDecodeBinaryBigEnd) GetPackageLen

func (d *DataDecodeBinaryBigEnd) GetPackageLen(buf []byte) int

type DataDecodeText

type DataDecodeText struct {
}

文本解析 => 没有长度的概念+包内容

func (*DataDecodeText) GetPackageHeadLen

func (d *DataDecodeText) GetPackageHeadLen() int

func (*DataDecodeText) GetPackageLen

func (d *DataDecodeText) GetPackageLen(buf []byte) int

type OnSocket

type OnSocket interface {
	/**
	连接上服务器回调,或者服务器accept某个客户端连接
	*/
	OnConnect(conn Conn)
	/**
	只要我们曾经连接上服务器过,OnClose必定会回调。代表一个当前的socket已经关闭
	@param conn 连接
	@param byLocalNotRemote 是否是本地主动本地断开;true:对服务器而言就是服务器把客户端断开,对客户端而言就是客户端主动断服务器
	*/
	OnClose(conn Conn, byLocalNotRemote bool)
	/**
	连接超时,写入超时,读取超时回调
	*/
	OnTimeout(conn Conn)
	/**
	网络错误回调,之后直接close
	*/
	OnNetErr(conn Conn)
	/**
	接受到信息
	@return 返回true表示可以继续热恋,false表示要分手了。
	*/
	OnRecvMsg(conn Conn, buf []byte) bool
}

* Socket 事件分发

type OnSocketServer

type OnSocketServer interface {
	OnSocket
	OnServerListen()
	OnServerErr(StackError)
	//这里OnServerClose始终会回调
	OnServerClose()
}

type ServerSocket

type ServerSocket struct {
	StatusConnServer
	// contains filtered or unexported fields
}

func NewServer

func NewServer(address string, ttl time.Duration, rTtl time.Duration, onSocket OnSocketServer,
	clientDDB DataDecodeBase) *ServerSocket

@ttl 客户端发送超时 @rTtl 客户端读取超时 0表示永远等待读取。

func NewServerIp

func NewServerIp(ip string, port uint16, onSocket OnSocketServer, clientDDB DataDecodeBase) *ServerSocket

func (*ServerSocket) Listen

func (s *ServerSocket) Listen() error

func (*ServerSocket) OnClose

func (s *ServerSocket) OnClose(conn Conn, byLocalNotRemote bool)

只要我们曾经连接上服务器过,OnClose必定会回调。代表一个当前的socket已经关闭

func (*ServerSocket) OnConnect

func (s *ServerSocket) OnConnect(conn Conn)

连接上服务器回调

func (*ServerSocket) OnNetErr

func (s *ServerSocket) OnNetErr(conn Conn)

网络错误回调,之后会调用OnClose

func (*ServerSocket) OnRecvMsg

func (s *ServerSocket) OnRecvMsg(conn Conn, buf []byte) bool

接受到信息 @return 返回true表示可以继续热恋,false表示要分手了。

func (*ServerSocket) OnTimeout

func (s *ServerSocket) OnTimeout(conn Conn)

连接超时,写入超时,读取超时回调,之后会调用OnClose

func (*ServerSocket) Shutdown

func (s *ServerSocket) Shutdown()

type StackError

type StackError interface {
	error
	Stack() []byte
}

type Status

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

func (*Status) ChangeStatus

func (s *Status) ChangeStatus(status StatusNO, err error) bool

func (*Status) ChangeStatusAll

func (s *Status) ChangeStatusAll(status StatusNO, err error, stack []byte) bool

func (*Status) Error

func (s *Status) Error() string

func (*Status) GetStatus

func (s *Status) GetStatus() StatusNO

func (*Status) Reset

func (s *Status) Reset()

func (*Status) Stack

func (s *Status) Stack() []byte

type StatusConnServer

type StatusConnServer struct {
	Status
	// contains filtered or unexported fields
}

type StatusNO

type StatusNO int32
const (
	StatusUnknown  StatusNO = iota //0 尚未初始化
	StatusNormal                   //1 正常
	StatusShutdown                 //2 自己关闭的
	StatusTimeout                  //3 超时连接断开
	StatusError                    //4 其他异常断开,服务器主动断开 err为ErrClose
)

Jump to

Keyboard shortcuts

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