netx

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2023 License: MIT Imports: 15 Imported by: 14

Documentation

Overview

Package netx Created by xuzhuoxi on 2019-03-13. @author xuzhuoxi

Package netx Created by xuzhuoxi on 2019-02-14. @author xuzhuoxi

Package netx Created by xuzhuoxi on 2019-03-10. @author xuzhuoxi

Package netx Created by xuzhuoxi on 2019-02-14. @author xuzhuoxi

Index

Constants

View Source
const (
	RpcNetworkTCP  = "tcp"
	RpcNetworkHttp = "http"
)
View Source
const (
	ServerEventStart = "netx.ServerEventStart"
	ServerEventStop  = "netx.ServerEventStop"

	ServerEventConnOpened = "netx.ServerEventConnOpened"
	ServerEventConnClosed = "netx.ServerEventConnClosed"
)
View Source
const (
	EventAddressRemoved = "EventAddressRemoved"
)
View Source
const ReceiverBuffLen = 2048

Variables

View Source
var ErrHttpServerStarted = errors.New("http: Server started")
View Source
var ErrSockServerStarted = errors.New("socket: Server started")

Functions

func DefaultPackHandler

func DefaultPackHandler(data []byte, senderAddress string, other interface{}) bool

func HttpGet

func HttpGet(url string, cb ReqCallBack) error

func HttpPost

func HttpPost(url, contentType string, body io.Reader, cb ReqCallBack) error

func HttpPostForm

func HttpPostForm(url string, data url.Values, cb ReqCallBack) error

func HttpPostString

func HttpPostString(url, body string, cb ReqCallBack) error

func RegisterNetwork

func RegisterNetwork(network SockNetwork, newServer func() ISockServer, newClient func() ISockClient, newClient2 func() ISockClient)

Types

type AddrRemovedInfo added in v1.0.4

type AddrRemovedInfo struct {
	Id   string
	Addr string
}

type AddressProxy

type AddressProxy struct {
	eventx.EventDispatcher
	// contains filtered or unexported fields
}

func NewAddressProxy

func NewAddressProxy() *AddressProxy

func NewAddressProxyWithName added in v1.0.4

func NewAddressProxyWithName(name string) *AddressProxy

func (*AddressProxy) GetAddress

func (p *AddressProxy) GetAddress(id string) (address string, ok bool)

func (*AddressProxy) GetId

func (p *AddressProxy) GetId(address string) (id string, ok bool)

func (*AddressProxy) MapIdAddress

func (p *AddressProxy) MapIdAddress(id string, address string)

func (*AddressProxy) RemoveByAddress

func (p *AddressProxy) RemoveByAddress(address string)

func (*AddressProxy) RemoveById

func (p *AddressProxy) RemoveById(id string)

func (*AddressProxy) Reset

func (p *AddressProxy) Reset()

type ConnNilError

type ConnNilError string

func (ConnNilError) Error

func (e ConnNilError) Error() string

func (ConnNilError) Temporary

func (e ConnNilError) Temporary() bool

func (ConnNilError) Timeout

func (e ConnNilError) Timeout() bool

type EmptyAddrError

type EmptyAddrError string

func (EmptyAddrError) Error

func (e EmptyAddrError) Error() string

func (EmptyAddrError) Temporary

func (e EmptyAddrError) Temporary() bool

func (EmptyAddrError) Timeout

func (e EmptyAddrError) Timeout() bool

type FuncPackHandler

type FuncPackHandler func(data []byte, senderAddress string, other interface{}) (catch bool)

FuncPackHandler 消息处理函数,用于处理Sock消息 @param data: 消息数据 @param senderAddress: 发送者连接地址 @param other: 其它信息 @return catch; 当前是否已经把消息处理

type HttpServer

type HttpServer struct {
	Network  string
	ServeMux *http.ServeMux
	Server   *http.Server
}

func (*HttpServer) ClearMaps

func (s *HttpServer) ClearMaps()

func (*HttpServer) MapFunc

func (s *HttpServer) MapFunc(pattern string, f func(w http.ResponseWriter, r *http.Request))

func (*HttpServer) MapHandle

func (s *HttpServer) MapHandle(pattern string, handler http.Handler)

func (*HttpServer) Running

func (s *HttpServer) Running() bool

func (*HttpServer) StartServer

func (s *HttpServer) StartServer(addr string) error

func (*HttpServer) StopServer

func (s *HttpServer) StopServer() error

type IAddressProxy

type IAddressProxy interface {
	eventx.IEventDispatcher
	// GetId
	// 能过地址找id
	GetId(address string) (id string, ok bool)
	// GetAddress
	// 能过id找地址
	GetAddress(id string) (address string, ok bool)
	// MapIdAddress
	// 把id和地址加入映射表
	MapIdAddress(id string, address string)
	// RemoveById
	// 移除id相关映射
	RemoveById(id string)
	// RemoveByAddress
	// 移除地址相关映射
	RemoveByAddress(address string)
	// Reset
	// 重置
	Reset()
}

IAddressProxy 难住地址与id的双向映射

func NewIAddressProxy

func NewIAddressProxy() IAddressProxy

func NewIAddressProxyWithName added in v1.0.4

func NewIAddressProxyWithName(name string) IAddressProxy

type IAddressProxyGetter

type IAddressProxyGetter interface {
	GetAddressProxy() IAddressProxy
}

type IAddressProxySetter

type IAddressProxySetter interface {
	SetAddressProxy(proxy IAddressProxy)
}

type IClient

type IClient interface {
	// IsOpening
	// 连接是否打开中
	IsOpening() bool
	// OpenClient
	// 打开
	OpenClient(params SockParams) error
	// CloseClient
	// 关闭
	CloseClient() error
}

type IConnReadWriterAdapter

type IConnReadWriterAdapter interface {
	IRemoteAddress
	// contains filtered or unexported methods
}

type IConnReaderAdapter

type IConnReaderAdapter interface {
	IRemoteAddress
	// contains filtered or unexported methods
}

type IConnWriterAdapter

type IConnWriterAdapter interface {
	IRemoteAddress
	// contains filtered or unexported methods
}

type IHttpServer

type IHttpServer interface {
	// Running
	// 启动中
	Running() bool
	// StartServer
	// 启动Http服务
	StartServer(addr string) error
	// StopServer
	// 停止Http服务
	StopServer() error
	// MapHandle
	// 映射请求响应处理器
	MapHandle(pattern string, handler http.Handler)
	// MapFunc
	// 映射请求响应函数
	MapFunc(pattern string, f func(w http.ResponseWriter, r *http.Request))
	// ClearMaps
	// 清除映射
	ClearMaps()
}

func NewHttpServer

func NewHttpServer() IHttpServer

type IPackHandlerContainer

type IPackHandlerContainer interface {
	// Size Handler size
	// 处理函数数量
	Size() int
	// FirstHandler
	// 由第一个处理
	FirstHandler(first func(handler FuncPackHandler) bool)
	// LastHandler
	// 由最后一个处理
	LastHandler(last func(handler FuncPackHandler) bool)
	// ForEachHandler
	// 依顺序遍历处理
	// each返回true,则中断遍历
	ForEachHandler(each func(handler FuncPackHandler) bool)

	// AppendPackHandler
	// 追加消息处理函数
	AppendPackHandler(handler FuncPackHandler) error
	// ClearHandler
	// 清除消息处理函数
	ClearHandler(handler FuncPackHandler) error
	// ClearHandlers
	// 清除全部消息处理函数
	ClearHandlers() error
	// SetPackHandlers
	// 设置消息处理函数列表
	SetPackHandlers(handlers []FuncPackHandler) error
}

func NewDefaultIPackHandler

func NewDefaultIPackHandler() IPackHandlerContainer

func NewIPackHandler

func NewIPackHandler(handlers []FuncPackHandler) IPackHandlerContainer

type IPackHandlerContainerGetter

type IPackHandlerContainerGetter interface {
	GetPackHandlerContainer() IPackHandlerContainer
}

type IPackHandlerContainerSetter

type IPackHandlerContainerSetter interface {
	SetPackHandlerContainer(packHandlerContainer IPackHandlerContainer)
}

type IPackReceiver

type IPackReceiver interface {
	logx.ILoggerSupport
	// contains filtered or unexported methods
}

func NewPackMultiReceiver

func NewPackMultiReceiver(reader IConnReaderAdapter, packHandlerContainer IPackHandlerContainer, dataBlockHandler bytex.IDataBlockHandler, logger logx.ILogger) IPackReceiver

func NewPackReceiver

func NewPackReceiver(reader IConnReaderAdapter, packHandlerContainer IPackHandlerContainer, dataBlockHandler bytex.IDataBlockHandler, logger logx.ILogger) IPackReceiver

type IPackReceiverGetter

type IPackReceiverGetter interface {
	GetPackReceiver() IPackReceiver
}

type IPackReceiverSetter

type IPackReceiverSetter interface {
	SetPackReceiver(receiver IPackReceiver)
}

type IPackSendReceiver

type IPackSendReceiver interface {
	logx.ILoggerSupport
	// contains filtered or unexported methods
}

func NewPackSendReceiver

func NewPackSendReceiver(reader IConnReaderAdapter, writer IConnWriterAdapter, packHandlerContainer IPackHandlerContainer, dataBlockHandler bytex.IDataBlockHandler, logger logx.ILogger, multiReceive bool) IPackSendReceiver

type IPackSender

type IPackSender interface {
	logx.ILoggerSupport
	// contains filtered or unexported methods
}

func NewPackSender

func NewPackSender(writer IConnWriterAdapter, dataBlockHandler bytex.IDataBlockHandler, logger logx.ILogger) IPackSender

type IPackSenderGetter

type IPackSenderGetter interface {
	GetPackSender() IPackSender
}

type IPackSenderSetter

type IPackSenderSetter interface {
	SetPackSender(sender IPackSender)
}

type IRPCClient

type IRPCClient interface {
	// Dial
	// 连接远程RPC服务
	Dial(address string) error
	// IsConnected
	// RPC是否处理连接状态
	IsConnected() bool
	// Call
	// 调用RPC服务
	Call(serviceMethod string, args interface{}, reply interface{}) error
	// Close
	// 关闭远程RPC服务
	Close()
}

func NewRPCClient

func NewRPCClient(Network string) IRPCClient

type IRPCServer

type IRPCServer interface {
	// Register
	// publishes in the server the set of methods of the receiver value that satisfy the following conditions:
	Register(rcvr interface{}) error
	// RegisterName
	// is like Register but uses the provided name for the type instead of the receiver's concrete type.
	RegisterName(name string, rcvr interface{}) error
	// StartServer
	// 启动RPC服务,会阻塞
	StartServer(addr string)
	// StopServer
	// 停止RPC服务
	StopServer()
}

func NewRPCServer

func NewRPCServer() IRPCServer

type IRemoteAddress

type IRemoteAddress interface {
	RemoteAddress() string
}

type IServer

type IServer interface {
	// IsRunning
	// 服务是否启动中
	IsRunning() bool
	// StartServer
	// 启动服务,会阻塞
	StartServer(params SockParams) error
	// StopServer
	// 停止服务,会阻塞
	StopServer() error
}

type IServerConn added in v1.0.4

type IServerConn interface {
	// ClientAddress
	// 客户端连接地址
	ClientAddress() string
	// SendBytes
	// 发送二进制数据, 不会重新打包
	SendBytes(bytes []byte) error
	// SendPack
	// 发送二进制消息包(把数据打包,补充长度信息)
	SendPack(data []byte) error
	// CloseConn
	// 关闭连接
	CloseConn() error
}

type IServerConnSet added in v1.0.4

type IServerConnSet interface {
	// SetMaxConn
	// 设置最大连接数量
	SetMaxConn(max int)
	// Connections
	// 连接数
	Connections() int
	// CloseConnection
	// 关闭指定连接
	CloseConnection(address string) (err error, ok bool)
	// FindConnection
	// 查找连接
	FindConnection(address string) (conn IServerConn, ok bool)
}

type ISockClient

type ISockClient interface {
	ISockName
	IClient
	IPackReceiver
	ISockSender

	LocalAddress() string
}

type ISockClientGetter

type ISockClientGetter interface {
	GetClient() ISockClient
}

type ISockClientSetter

type ISockClientSetter interface {
	SetClient(client ISockClient)
}

type ISockConn

type ISockConn interface {
	// Close
	// 关闭
	Close() error
	// LocalAddr
	// 本地连接地址
	LocalAddr() net.Addr
	// RemoteAddr
	// 远程连接地址
	RemoteAddr() net.Addr
}

ISockConn 这个是裁剪接口,函数签名不能改 针对net.Conn、quic.Session、*net.UDPConn、*websocket.Conn

type ISockEventClient added in v1.0.4

type ISockEventClient interface {
	eventx.IEventDispatcher
	ISockClient
}

type ISockEventServer added in v1.0.4

type ISockEventServer interface {
	eventx.IEventDispatcher
	ISockServer
}

type ISockName

type ISockName interface {
	// SetName
	// 设置标识名称
	SetName(name string)
	// GetName
	// 获取标识名称
	GetName() string
}

type ISockSender

type ISockSender interface {
	// SendBytesTo
	// 发送二进制数据, 不会重新打包
	SendBytesTo(bytes []byte, rAddress ...string) error
	// SendPackTo
	// 发送二进制消息包(把数据打包,补充长度信息)
	SendPackTo(data []byte, rAddress ...string) error
}

type ISockSenderGetter

type ISockSenderGetter interface {
	GetSockSender() ISockSender
}

type ISockSenderSetter

type ISockSenderSetter interface {
	SetSockSender(sockSender ISockSender)
}

type ISockServerGetter

type ISockServerGetter interface {
	GetSockServer() ISockServer
}

type ISockServerSetter

type ISockServerSetter interface {
	SetSockServer(server ISockServer)
}

type NoAddrError

type NoAddrError string

func (NoAddrError) Error

func (e NoAddrError) Error() string

func (NoAddrError) Temporary

func (e NoAddrError) Temporary() bool

func (NoAddrError) Timeout

func (e NoAddrError) Timeout() bool

type PackHandlerContainer

type PackHandlerContainer struct {
	RWMutex sync.RWMutex
	// contains filtered or unexported fields
}

func NewDefaultPackHandler

func NewDefaultPackHandler() *PackHandlerContainer

func NewPackHandler

func NewPackHandler(handlers []FuncPackHandler) *PackHandlerContainer

func (*PackHandlerContainer) AppendPackHandler

func (ph *PackHandlerContainer) AppendPackHandler(handler FuncPackHandler) error

func (*PackHandlerContainer) ClearHandler

func (ph *PackHandlerContainer) ClearHandler(handler FuncPackHandler) error

func (*PackHandlerContainer) ClearHandlers

func (ph *PackHandlerContainer) ClearHandlers() error

func (*PackHandlerContainer) FirstHandler

func (ph *PackHandlerContainer) FirstHandler(first func(handler FuncPackHandler) bool)

func (*PackHandlerContainer) ForEachHandler

func (ph *PackHandlerContainer) ForEachHandler(each func(handler FuncPackHandler) bool)

func (*PackHandlerContainer) LastHandler

func (ph *PackHandlerContainer) LastHandler(first func(handler FuncPackHandler) bool)

func (*PackHandlerContainer) SetPackHandlers

func (ph *PackHandlerContainer) SetPackHandlers(handlers []FuncPackHandler) error

func (*PackHandlerContainer) Size added in v1.0.4

func (ph *PackHandlerContainer) Size() int

type RPCClient

type RPCClient struct {
	Network string
	// contains filtered or unexported fields
}

func (*RPCClient) Call

func (c *RPCClient) Call(serviceMethod string, args interface{}, reply interface{}) error

func (*RPCClient) Close

func (c *RPCClient) Close()

func (*RPCClient) Dial

func (c *RPCClient) Dial(address string) error

func (*RPCClient) IsConnected

func (c *RPCClient) IsConnected() bool

type RPCServer

type RPCServer struct {
	logx.LoggerSupport

	Network  string
	Server   *rpc.Server
	Listener net.Listener
}

func (*RPCServer) Register

func (s *RPCServer) Register(rcvr interface{}) error

func (*RPCServer) RegisterName

func (s *RPCServer) RegisterName(name string, rcvr interface{}) error

func (*RPCServer) StartServer

func (s *RPCServer) StartServer(addr string)

func (*RPCServer) StopServer

func (s *RPCServer) StopServer()

type ReadWriterAdapter

type ReadWriterAdapter struct {
	Reader     io.Reader
	Writer     io.Writer
	RemoteAddr net.Addr
}

func (*ReadWriterAdapter) ReadBytes

func (rw *ReadWriterAdapter) ReadBytes(bytes []byte) (n int, address string, err error)

func (*ReadWriterAdapter) RemoteAddress

func (rw *ReadWriterAdapter) RemoteAddress() string

func (*ReadWriterAdapter) WriteBytes

func (rw *ReadWriterAdapter) WriteBytes(bytes []byte, rAddress ...string) (n int, err error)

type ReaderUnSupportError

type ReaderUnSupportError string

func (ReaderUnSupportError) Error

func (e ReaderUnSupportError) Error() string

func (ReaderUnSupportError) Temporary

func (e ReaderUnSupportError) Temporary() bool

func (ReaderUnSupportError) Timeout

func (e ReaderUnSupportError) Timeout() bool

type ReqCallBack

type ReqCallBack func(res *http.Response, body *[]byte)

type SockClientBase

type SockClientBase struct {
	Name     string
	Network  SockNetwork
	ClientMu sync.RWMutex
	Opening  bool

	LocalAddr string
	Conn      ISockConn

	PackProxy IPackSendReceiver
	Logger    logx.ILogger

	PackHandler IPackHandlerContainer
}

func (*SockClientBase) GetLogger

func (o *SockClientBase) GetLogger() logx.ILogger

func (*SockClientBase) GetName

func (o *SockClientBase) GetName() string

func (*SockClientBase) GetPackHandlerContainer

func (o *SockClientBase) GetPackHandlerContainer() IPackHandlerContainer

func (*SockClientBase) IsOpening

func (o *SockClientBase) IsOpening() bool

func (*SockClientBase) IsReceiving

func (o *SockClientBase) IsReceiving() bool

func (*SockClientBase) LocalAddress

func (o *SockClientBase) LocalAddress() string

func (*SockClientBase) SendBytesTo

func (o *SockClientBase) SendBytesTo(bytes []byte, rAddress ...string) error

func (*SockClientBase) SendPackTo

func (o *SockClientBase) SendPackTo(msg []byte, rAddress ...string) error

func (*SockClientBase) SetLogger

func (o *SockClientBase) SetLogger(logger logx.ILogger)

func (*SockClientBase) SetName

func (o *SockClientBase) SetName(name string)

func (*SockClientBase) SetPackHandlerContainer

func (o *SockClientBase) SetPackHandlerContainer(packHandler IPackHandlerContainer)

func (*SockClientBase) StartReceiving

func (o *SockClientBase) StartReceiving() error

func (*SockClientBase) StopReceiving

func (o *SockClientBase) StopReceiving() error

type SockNetwork

type SockNetwork string
const (
	Undefined SockNetwork = ""

	QuicNetwork SockNetwork = "quic"

	TcpNetwork  SockNetwork = "tcp"
	TcpNetwork4 SockNetwork = "tcp4"
	TcpNetwork6 SockNetwork = "tcp6"

	UDPNetwork  SockNetwork = "udp"
	UDPNetwork4 SockNetwork = "udp4"
	UDPNetwork6 SockNetwork = "udp6"

	WSNetwork SockNetwork = "websocket"
)

func ParseSockNetwork

func ParseSockNetwork(str string) SockNetwork

ParseSockNetwork 字符串转 SockNetwork

func (SockNetwork) NewClient

func (n SockNetwork) NewClient() (server ISockClient, err error)

func (SockNetwork) NewClient2

func (n SockNetwork) NewClient2() (server ISockClient, err error)

func (SockNetwork) NewServer

func (n SockNetwork) NewServer() (server ISockServer, err error)

func (SockNetwork) String

func (n SockNetwork) String() string

type SockParams

type SockParams struct {
	Network SockNetwork
	// E.g
	// tcp,udp,quic:	127.0.0.1:9999
	LocalAddress string
	// E.g
	// websocket:	ws://127.0.0.1:9999
	// tcp,udp,quic:	127.0.0.1:9999
	RemoteAddress string

	// E.g: /,/echo
	WSPattern string
	// E.g: http://127.0.0.1/,最后的"/"必须
	WSOrigin string
	// E.g: ""
	WSProtocol string
}

type SockServerBase

type SockServerBase struct {
	Name     string
	Network  SockNetwork
	ServerMu sync.RWMutex
	Running  bool

	Logger logx.ILogger

	PackHandlerContainer IPackHandlerContainer
}

func (*SockServerBase) DispatchServerConnCloseEvent

func (s *SockServerBase) DispatchServerConnCloseEvent(dispatcher eventx.IEventDispatcher, address string)

func (*SockServerBase) DispatchServerConnOpenEvent

func (s *SockServerBase) DispatchServerConnOpenEvent(dispatcher eventx.IEventDispatcher, address string)

func (*SockServerBase) DispatchServerStartedEvent

func (s *SockServerBase) DispatchServerStartedEvent(dispatcher eventx.IEventDispatcher)

func (*SockServerBase) DispatchServerStoppedEvent

func (s *SockServerBase) DispatchServerStoppedEvent(dispatcher eventx.IEventDispatcher)

func (*SockServerBase) GetLogger

func (s *SockServerBase) GetLogger() logx.ILogger

func (*SockServerBase) GetName

func (s *SockServerBase) GetName() string

func (*SockServerBase) GetPackHandlerContainer

func (s *SockServerBase) GetPackHandlerContainer() IPackHandlerContainer

func (*SockServerBase) IsRunning

func (s *SockServerBase) IsRunning() bool

func (*SockServerBase) SetLogger

func (s *SockServerBase) SetLogger(logger logx.ILogger)

func (*SockServerBase) SetName

func (s *SockServerBase) SetName(name string)

func (*SockServerBase) SetPackHandlerContainer

func (s *SockServerBase) SetPackHandlerContainer(packHandlerContainer IPackHandlerContainer)

Directories

Path Synopsis
Package quicx Create on 2023/8/5 @author xuzhuoxi
Package quicx Create on 2023/8/5 @author xuzhuoxi
Package tcpx Create on 2023/8/5 @author xuzhuoxi
Package tcpx Create on 2023/8/5 @author xuzhuoxi
Package udpx Create on 2023/8/5 @author xuzhuoxi
Package udpx Create on 2023/8/5 @author xuzhuoxi
Package wsx Create on 2023/8/5 @author xuzhuoxi
Package wsx Create on 2023/8/5 @author xuzhuoxi

Jump to

Keyboard shortcuts

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