connections

package module
v0.0.0-...-0db0fbe Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2023 License: MIT Imports: 8 Imported by: 0

README

Connections

类socket连接接口框架。接口尚处于不稳定状态

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultIDGenerator = func() (string, error) {
	buf := bytes.NewBuffer(nil)
	err := binary.Write(buf, binary.BigEndian, time.Now().UnixNano())
	if err != nil {
		return "", err
	}
	c := atomic.AddUint32(&currentGenerated, 1)
	err = binary.Write(buf, binary.BigEndian, c)
	return base64.StdEncoding.EncodeToString(buf.Bytes()), nil
}

DefaultIDGenerator default generator. Return id string and any error if raised.

View Source
var ErrConnIDDuplicated = errors.New("generated connection id duplicated")

ErrConnIDDuplicated errors reaised when generated connection id duplicated

Functions

func Consume

func Consume(i InputService, c Consumer)

Consume consume input service with given consumer.

Types

type ChanConnection

type ChanConnection struct {
	Addr net.Addr

	Closed bool
	Lock   sync.Mutex
	// contains filtered or unexported fields
}

ChanConnection chan connection

func NewChanConnection

func NewChanConnection() *ChanConnection

NewChanConnection create new chan connection. Return chan connection created.

func (*ChanConnection) C

func (c *ChanConnection) C() chan int

C connection close signal chan.

func (*ChanConnection) Client

func (c *ChanConnection) Client() *ChanConnectionClient

Client create chan connection client.

func (*ChanConnection) ClientClose

func (c *ChanConnection) ClientClose() error

ClientClose close connection. Return any error if raised.

func (*ChanConnection) ClientMessagesChan

func (c *ChanConnection) ClientMessagesChan() chan []byte

ClientMessagesChan connection client message chan

func (*ChanConnection) ClientSend

func (c *ChanConnection) ClientSend(msg []byte) error

ClientSend Send message to connction from client. return any error if raised.

func (*ChanConnection) Close

func (c *ChanConnection) Close() error

Close close connection. Return any error if raised.

func (*ChanConnection) ErrorsChan

func (c *ChanConnection) ErrorsChan() chan error

ErrorsChan connection error chan.

func (*ChanConnection) MessagesChan

func (c *ChanConnection) MessagesChan() chan []byte

MessagesChan connection message chan

func (*ChanConnection) RaiseError

func (c *ChanConnection) RaiseError(err error)

RaiseError raise an error to connection

func (*ChanConnection) RemoteAddr

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

RemoteAddr return connection rempte address.

func (*ChanConnection) Send

func (c *ChanConnection) Send(msg []byte) error

Send send message to connction. return any error if raised.

type ChanConnectionClient

type ChanConnectionClient struct {
	Conn *ChanConnection
}

ChanConnectionClient client for chan connection.

func (*ChanConnectionClient) Close

func (c *ChanConnectionClient) Close() error

Close close connection. Return any error if raised.

func (*ChanConnectionClient) MessagesChan

func (c *ChanConnectionClient) MessagesChan() chan []byte

MessagesChan connection client message chan

func (*ChanConnectionClient) Send

func (c *ChanConnectionClient) Send(msg []byte) error

Send send message to connction from client. return any error if raised.

type Conn

type Conn struct {
	RawConnection
	Info *Info
}

Conn connection struct

func New

func New() *Conn

New create new connection. Return connection created.

func (*Conn) ID

func (c *Conn) ID() string

ID get connection id

type Consumer

type Consumer interface {
	//OnMessage called when connection message received.
	OnMessage(*Message)
	//OnError called when onconnection error raised.
	OnError(*Error)
	//OnClose called when connection closed.
	OnClose(OutputConnection)
	//OnOpen called when connection open.
	OnOpen(OutputConnection)
	// Stop stop consumer
	Stop()
}

Consumer Consumer interface which can consume connections message.

type EmptyConsumer

type EmptyConsumer struct {
}

EmptyConsumer empty consumer implemented all required method. Your consumer should extends to keep compatibility in future.

func (EmptyConsumer) OnClose

func (e EmptyConsumer) OnClose(OutputConnection)

OnClose called when connection closed.

func (EmptyConsumer) OnError

func (e EmptyConsumer) OnError(*Error)

OnError called when onconnection error raised.

func (EmptyConsumer) OnMessage

func (e EmptyConsumer) OnMessage(*Message)

OnMessage called when connection message received.

func (EmptyConsumer) OnOpen

func (e EmptyConsumer) OnOpen(OutputConnection)

OnOpen called when connection open.

func (EmptyConsumer) Stop

func (e EmptyConsumer) Stop()

Stop stop consumer

type Error

type Error struct {
	//Error raw error
	Error error
	//Conn connection which raised error.
	Conn OutputConnection
}

Error connection error struct

type Gateway

type Gateway struct {
	//ID gateway id
	ID string
	//IDGenerator connection id generator
	IDGenerator func() (string, error)
	//Connections connections managerd by gateway.
	Connections sync.Map
	// contains filtered or unexported fields
}

Gateway connection gateway struct

func NewGateway

func NewGateway() *Gateway

NewGateway create new gateway

func (*Gateway) C

func (g *Gateway) C() chan bool

C return close chan.

func (*Gateway) Close

func (g *Gateway) Close(id string) error

Close connection by given id. Return any error if raised.

func (*Gateway) Conn

func (g *Gateway) Conn(id string) *Conn

Conn get connection by id. Return nil if connection not registered.

func (*Gateway) ErrorsChan

func (g *Gateway) ErrorsChan() chan *Error

ErrorsChan return connection error chan.

func (*Gateway) ListConn

func (g *Gateway) ListConn() []*Conn

ListConn list all connections

func (*Gateway) MessagesChan

func (g *Gateway) MessagesChan() chan *Message

MessagesChan return connection message chan.

func (*Gateway) OnCloseEventsChan

func (g *Gateway) OnCloseEventsChan() chan OutputConnection

OnCloseEventsChan return closed event chan.

func (*Gateway) OnOpenEventsChan

func (g *Gateway) OnOpenEventsChan() chan OutputConnection

OnOpenEventsChan return open event chan.

func (*Gateway) Register

func (g *Gateway) Register(conn RawConnection) (*Conn, error)

Register register raw connection to gateway. Return connection and any error if raised.

func (*Gateway) Send

func (g *Gateway) Send(id string, msg []byte) error

Send send message to connection by given id. Return and error if raised.

func (*Gateway) Stop

func (g *Gateway) Stop()

Stop close gate way and close gate close chan.

type Info

type Info struct {
	//ID connection registered id
	ID string
	//Timestamp connection registered timestamp.
	Timestamp int64
}

Info connection info struct

type InputService

type InputService interface {
	//MessagesChan return connection message chan.
	MessagesChan() chan *Message
	//ErrorsChan return connection error chan.
	ErrorsChan() chan *Error
	//OnCloseEventsChan return connection on close events chan.
	OnCloseEventsChan() chan OutputConnection
	//OnOpenEventsChan return connection on open events chan.
	OnOpenEventsChan() chan OutputConnection
	//C close chan
	C() chan bool
}

InputService input service interface

type Message

type Message struct {
	//Message raw message
	Message []byte
	//Conn connection which received message
	Conn OutputConnection
}

Message connection message struct

type OutputConnection

type OutputConnection interface {
	Close() error
	Send([]byte) error
	ID() string
}

OutputConnection connection interface which can output messages.

type OutputService

type OutputService interface {
	//Send message to connection by given id.
	Send(id string, msg []byte) error
	//Close close connection by given id.
	Close(id string) error
}

OutputService output service interface

type RawConnection

type RawConnection interface {
	//Close close connection.
	//Return any error if raised.
	Close() error
	//Send send message to connction.
	//return any error if raised.
	Send([]byte) error
	//MessagesChan connection message chan
	MessagesChan() chan []byte
	//ErrorsChan connection error chan.
	ErrorsChan() chan error
	//RemoteAddr return connection rempte address.
	RemoteAddr() net.Addr
	//C connection close signal chan.
	C() chan int
}

RawConnection raw connection interface

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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