Documentation ¶
Index ¶
- Variables
- type AsyncClient
- func (c *AsyncClient) Close()
- func (c *AsyncClient) Conn(network, address string, readBufferSize, writeBufferSize int, ...) error
- func (c *AsyncClient) GetDispatcher() IDispatcher
- func (c *AsyncClient) GetSession() ISession
- func (c *AsyncClient) Recv() (interface{}, error)
- func (c *AsyncClient) Send(packet interface{}) error
- type Client
- type Dispatcher
- type FnCallbackClosed
- type FnCallbackSended
- type IDispatcher
- type IPacketProtocol
- type ISession
- type PacketHandler
- type Server
- type Session
- func (s *Session) Close() error
- func (s *Session) RawConn() net.Conn
- func (s *Session) Send(packet interface{}) error
- func (s *Session) SetCloseCallback(callback FnCallbackClosed)
- func (s *Session) SetReadDeadline(delt time.Duration)
- func (s *Session) SetSendCallback(callback FnCallbackSended)
- func (s *Session) Start()
Constants ¶
This section is empty.
Variables ¶
View Source
var ( ErrWritePacketFailed = errors.New("socket: Write packet failed") ErrReadPacketFailed = errors.New("socket: read packet failed") ErrSignalStopped = errors.New("socket: Signal Stopped") ErrListenFailed = errors.New("socket: listen Failed Error") ErrAcceptFailed = errors.New("socket: accept Failed Error") ErrSessionClosed = errors.New("socket: Session was closed") ErrSendChanBlocking = errors.New("socket: buff Length is not enough") )
Functions ¶
This section is empty.
Types ¶
type AsyncClient ¶
type AsyncClient struct { Client // contains filtered or unexported fields }
AsyncClient 异步通讯客户端
func NewAsyncClient ¶
func NewAsyncClient(protocol IPacketProtocol, dispatcher IDispatcher, bufferSize int) *AsyncClient
func (*AsyncClient) Conn ¶
func (c *AsyncClient) Conn(network, address string, readBufferSize, writeBufferSize int, callbackSend FnCallbackSended, callbackClosed FnCallbackClosed) error
Conn 与服务器建立TCP连接 :Param network: 网络类型: "tcp"、"udp" :Param address: 链接的地址: "IP:PORT"
func (*AsyncClient) GetDispatcher ¶
func (c *AsyncClient) GetDispatcher() IDispatcher
GetDispatcher 获取事件分发器
func (*AsyncClient) Recv ¶
func (c *AsyncClient) Recv() (interface{}, error)
Recv 异步通讯的客户端只能注册接收消息的句柄,不能直接收取封包内容
func (*AsyncClient) Send ¶
func (c *AsyncClient) Send(packet interface{}) error
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
func NewClient ¶
func NewClient(protocol IPacketProtocol) *Client
type Dispatcher ¶
type Dispatcher struct {
// contains filtered or unexported fields
}
func (*Dispatcher) AddHandler ¶
func (p *Dispatcher) AddHandler(id uint32, handler PacketHandler)
AddHandler 添加新的事件处理器
func (*Dispatcher) GetHandler ¶
func (p *Dispatcher) GetHandler(id uint32) PacketHandler
func (*Dispatcher) HandleProc ¶
func (p *Dispatcher) HandleProc(session ISession, packet interface{})
HandleProc 事件处理过程
type FnCallbackSended ¶
type IDispatcher ¶
type IDispatcher interface { AddHandler(id uint32, handler PacketHandler) DelHandler(id uint32) GetHandler(id uint32) PacketHandler HandleProc(ISession, interface{}) }
type IPacketProtocol ¶
type IPacketProtocol interface { // ReadPacket 读取封包 ReadPacket(net.Conn) (interface{}, error) // BuildPacket 组包 BuildPacket(interface{}) []byte // SendPacket 发送数据 SendPacket(net.Conn, []byte) error }
IPacketProtocol 此接口定义了如何组包,解包,发送 TODO: 如果要正常发送需要实现此接口
type ISession ¶
type ISession interface { RawConn() net.Conn Start() Send(packet interface{}) error Close() error SetCloseCallback(callback FnCallbackClosed) SetSendCallback(callback FnCallbackSended) }
type Server ¶
type Server struct { SessionMng []ISession // contains filtered or unexported fields }
func NewServer ¶
func NewServer(network, address string, protocol IPacketProtocol, dispatcher IDispatcher) (*Server, error)
NewServer 新建服务器
func (*Server) AcceptLoop ¶
func (s *Server) AcceptLoop()
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
Session 异步会话管理
func NewSession ¶
func NewSession(conn net.Conn, protocol IPacketProtocol, handler PacketHandler, sendChanSize int) *Session
func (*Session) Send ¶
Send 异步发送方法, 仅将 packet 写入 sendChan 中等待sendLoop处理, 如果 sendChan 满了, 则return ErrSendChanBlocking。 如果 sendChan 已关闭, 则return ErrSessionClosed。
func (*Session) SetCloseCallback ¶
func (s *Session) SetCloseCallback(callback FnCallbackClosed)
func (*Session) SetReadDeadline ¶
SetReadDeadline 设置读取的超时时间 goroutine safe, 如果不需要设置,则不要调用
func (*Session) SetSendCallback ¶
func (s *Session) SetSendCallback(callback FnCallbackSended)
Click to show internal directories.
Click to hide internal directories.