transport

package
v3.0.8 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2022 License: AGPL-3.0 Imports: 9 Imported by: 0

README

介绍

transport 包括客户端和服务器端,仅实现tcp和udp的传输层,不负责具体消息的解析和处理。不负责粘包、半包、消息解析、心跳处理、状态管理等工作。

比如设备关闭或者离线,要修改缓存状态、数据库状态、发送离线通知、执行离线回调等等,都在上层处理。tcp server 和 udp server、tcp client 和 udp client , 消息的接收和发送都在外面处理。

tcp是流传输,需要注意粘包和半包的处理。在上层处理tcp包的时候,可以尝试使用 ring buffer

usage

参考 example.go

TODO

  • sip协议的传输层,TCP和UDP有所不同,比如重传以及超时的错误信息等。所以需要在 transaction上面,处理消息重传、错误上报等。具体参考RFC3261。

Documentation

Index

Constants

View Source
const SipHost string = "127.0.0.1"

默认端口:TCP/UDP是5060,5061是在TCP上的TLS 对于服务器监听UDP的任何端口和界面,都必须在TCP上也进行同样的监听。这是因为可能消息还需要通过TCP进行传输,比如消息过大的情况。

View Source
const SipPort uint16 = 5060

Variables

This section is empty.

Functions

func PacketHandler

func PacketHandler(s ITransport)

func RunClientTCP

func RunClientTCP()

测试通讯,客户端先发一条消息

func RunClientUDP

func RunClientUDP()

func RunServerTCP

func RunServerTCP()

func RunServerUDP

func RunServerUDP()

Types

type Connection

type Connection interface {
	net.Conn
	Network() string
	// String() string
	ReadFrom(buf []byte) (num int, raddr net.Addr, err error)
	WriteTo(buf []byte, raddr net.Addr) (num int, err error)
}

Connection Wrapper around net.Conn.

type IClient

type IClient interface {
	ITransport
	LocalAddr() net.Addr      //本地地址
	RemoteAddr() net.Addr     //远程地址
	Heartbeat(packet *Packet) //客户端需要定期发送心跳包到服务器端
}

transport 需要实现的接口如下

func NewTCPClient

func NewTCPClient(host string, port uint16) IClient

func NewUDPClient

func NewUDPClient(host string, port uint16) IClient

type IServer

type IServer interface {
	ITransport
	CloseOne(addr string) //对于关闭某个客户端连接,比如没有鉴权的非法链接,心跳超时等
	IsKeepalive() bool    //persistent connection or not
}

func NewTCPServer

func NewTCPServer(port uint16, keepalive bool) IServer

func NewUDPServer

func NewUDPServer(port uint16) IServer

type ITransport

type ITransport interface {
	Name() string
	ReadPacketChan() <-chan *Packet //读消息,消息处理器需在循环中阻塞读取
	WritePacket(packet *Packet)     //写消息
	StartAndWait() error            //开启连接,阻塞接收消息
	Close() error                   //关闭连接
	IsReliable() bool               //是否可靠传输
	Conn() *Connection
}

type Packet

type Packet struct {
	Type string //消息类型,预留字段,对于客户端主动关闭等消息的上报、心跳超时等。如果为空,则仅透传消息。
	Addr net.Addr
	Data []byte
}

type Statistic

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

通讯统计

type TCPClient

type TCPClient struct {
	Statistic
	// contains filtered or unexported fields
}

func (*TCPClient) Close

func (c *TCPClient) Close() error

func (*TCPClient) Conn added in v3.0.4

func (s *TCPClient) Conn() *Connection

func (*TCPClient) Heartbeat

func (c *TCPClient) Heartbeat(p *Packet)

外部定期调用此接口,实现心跳

func (*TCPClient) IsReliable

func (c *TCPClient) IsReliable() bool

func (*TCPClient) LocalAddr

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

func (*TCPClient) Name

func (c *TCPClient) Name() string

func (*TCPClient) ReadPacketChan

func (c *TCPClient) ReadPacketChan() <-chan *Packet

func (*TCPClient) RemoteAddr

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

func (*TCPClient) StartAndWait added in v3.0.4

func (c *TCPClient) StartAndWait() error

func (*TCPClient) WritePacket

func (c *TCPClient) WritePacket(packet *Packet)

type TCPServer

type TCPServer struct {
	Statistic

	Keepalive bool
	// contains filtered or unexported fields
}

func (*TCPServer) Close

func (s *TCPServer) Close() error

func (*TCPServer) CloseOne

func (s *TCPServer) CloseOne(addr string)

func (*TCPServer) Conn added in v3.0.4

func (s *TCPServer) Conn() *Connection

func (*TCPServer) IsKeepalive

func (s *TCPServer) IsKeepalive() bool

func (*TCPServer) IsReliable

func (s *TCPServer) IsReliable() bool

func (*TCPServer) Name

func (s *TCPServer) Name() string

func (*TCPServer) ReadPacketChan

func (s *TCPServer) ReadPacketChan() <-chan *Packet

func (*TCPServer) StartAndWait added in v3.0.4

func (s *TCPServer) StartAndWait() error

func (*TCPServer) WritePacket

func (s *TCPServer) WritePacket(packet *Packet)

type UDPClient

type UDPClient struct {
	Statistic
	// contains filtered or unexported fields
}

func (*UDPClient) Close

func (c *UDPClient) Close() error

func (*UDPClient) Conn added in v3.0.4

func (c *UDPClient) Conn() *Connection

func (*UDPClient) Heartbeat

func (c *UDPClient) Heartbeat(p *Packet)

外部定期调用此接口,实现心跳

func (*UDPClient) IsReliable

func (c *UDPClient) IsReliable() bool

func (*UDPClient) LocalAddr

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

func (*UDPClient) Name

func (c *UDPClient) Name() string

func (*UDPClient) ReadPacketChan

func (c *UDPClient) ReadPacketChan() <-chan *Packet

func (*UDPClient) RemoteAddr

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

func (*UDPClient) StartAndWait added in v3.0.4

func (c *UDPClient) StartAndWait() error

func (*UDPClient) WritePacket

func (c *UDPClient) WritePacket(packet *Packet)

type UDPServer

type UDPServer struct {
	Statistic

	Keepalive bool
	// contains filtered or unexported fields
}

func (*UDPServer) Close

func (s *UDPServer) Close() error

func (*UDPServer) CloseOne

func (s *UDPServer) CloseOne(addr string)

func (*UDPServer) Conn

func (s *UDPServer) Conn() *Connection

func (*UDPServer) IsKeepalive

func (s *UDPServer) IsKeepalive() bool

func (*UDPServer) IsReliable

func (s *UDPServer) IsReliable() bool

func (*UDPServer) Name

func (s *UDPServer) Name() string

func (*UDPServer) ReadPacketChan

func (s *UDPServer) ReadPacketChan() <-chan *Packet

func (*UDPServer) StartAndWait added in v3.0.4

func (s *UDPServer) StartAndWait() error

func (*UDPServer) WritePacket

func (s *UDPServer) WritePacket(packet *Packet)

Jump to

Keyboard shortcuts

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