socketgo

package module
v0.0.0-...-a0b333f Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2023 License: GPL-3.0 Imports: 10 Imported by: 0

README

socketgo

socket with golang

本仓库大部分的代码来自 wentby

最开始是直接goimport的方式使用, 因本人只需要原始socket方面的代码, 其它大部分代码用不到, 所以并没有fork完整项目。 只是简单的goimport的方式使用 netmodelprotocol 包中的内容。

后因为个人需要修改了部分内容,之后每次要使用相关代码都要copy文件很不方便。

所以提交上来, 方便使用。

使用方法

  1. 项目goimport的方式加载项目中。
  2. 参考example中的样式定义 自己的msgdispatcherprotocol 即可。

样例的运行方法

Example的消息结构定义依赖 google protobuf, 因此需要提前安装好 protocgofaster插件。 然后按如下步骤执行:

  1. 进入 example/proto目录。
  2. 编译消息结构 protoc --go_out=. *.proto
  3. 分别进入 client 和 server 目录,go build
  4. 执行编译出来的 serverclient 即可。

相关链接

Documentation

Index

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) Close

func (c *AsyncClient) Close()

Close 关闭连接

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) GetSession

func (c *AsyncClient) GetSession() ISession

GetSession 获取会话信息

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

func (*Client) Conn

func (c *Client) Conn(network, address string, readBufferSize, writeBufferSize int) error

func (*Client) Recv

func (c *Client) Recv() (interface{}, error)

func (*Client) Send

func (c *Client) Send(packet interface{}) error

type Dispatcher

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

func NewDispatcher

func NewDispatcher() *Dispatcher

NewDispatcher 事件分发器

func (*Dispatcher) AddHandler

func (p *Dispatcher) AddHandler(id uint32, handler PacketHandler)

AddHandler 添加新的事件处理器

func (*Dispatcher) DelHandler

func (p *Dispatcher) DelHandler(id uint32)

DelHandler 卸载新的事件处理器

func (*Dispatcher) GetHandler

func (p *Dispatcher) GetHandler(id uint32) PacketHandler

func (*Dispatcher) HandleProc

func (p *Dispatcher) HandleProc(session ISession, packet interface{})

HandleProc 事件处理过程

type FnCallbackClosed

type FnCallbackClosed func(net.Conn)

FnCallbackClosed 链接本身的处理句柄

type FnCallbackSended

type FnCallbackSended func(net.Conn, interface{})

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 PacketHandler

type PacketHandler func(ISession, interface{})

PacketHandler 事件处理句柄,用于解析相应的封包

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()

func (*Server) Close

func (s *Server) Close()

func (*Server) GetDispatcher

func (s *Server) GetDispatcher() IDispatcher

GetDispatcher 获取事件分发器

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) Close

func (s *Session) Close() error

Close 关闭连接并释放相关资源.

func (*Session) RawConn

func (s *Session) RawConn() net.Conn

RawConn return net.Conn

func (*Session) Send

func (s *Session) Send(packet interface{}) error

Send 异步发送方法, 仅将 packet 写入 sendChan 中等待sendLoop处理, 如果 sendChan 满了, 则return ErrSendChanBlocking。 如果 sendChan 已关闭, 则return ErrSessionClosed。

func (*Session) SetCloseCallback

func (s *Session) SetCloseCallback(callback FnCallbackClosed)

func (*Session) SetReadDeadline

func (s *Session) SetReadDeadline(delt time.Duration)

SetReadDeadline 设置读取的超时时间 goroutine safe, 如果不需要设置,则不要调用

func (*Session) SetSendCallback

func (s *Session) SetSendCallback(callback FnCallbackSended)

func (*Session) Start

func (s *Session) Start()

Start 开始会话,循环监听发送与接收

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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