Documentation ¶
Index ¶
- Variables
- func Consume(i InputService, c Consumer)
- type ChanConnection
- func (c *ChanConnection) C() chan int
- func (c *ChanConnection) Client() *ChanConnectionClient
- func (c *ChanConnection) ClientClose() error
- func (c *ChanConnection) ClientMessagesChan() chan []byte
- func (c *ChanConnection) ClientSend(msg []byte) error
- func (c *ChanConnection) Close() error
- func (c *ChanConnection) ErrorsChan() chan error
- func (c *ChanConnection) MessagesChan() chan []byte
- func (c *ChanConnection) RaiseError(err error)
- func (c *ChanConnection) RemoteAddr() net.Addr
- func (c *ChanConnection) Send(msg []byte) error
- type ChanConnectionClient
- type Conn
- type Consumer
- type EmptyConsumer
- type Error
- type Gateway
- func (g *Gateway) C() chan bool
- func (g *Gateway) Close(id string) error
- func (g *Gateway) Conn(id string) *Conn
- func (g *Gateway) ErrorsChan() chan *Error
- func (g *Gateway) ListConn() []*Conn
- func (g *Gateway) MessagesChan() chan *Message
- func (g *Gateway) OnCloseEventsChan() chan OutputConnection
- func (g *Gateway) OnOpenEventsChan() chan OutputConnection
- func (g *Gateway) Register(conn RawConnection) (*Conn, error)
- func (g *Gateway) Send(id string, msg []byte) error
- func (g *Gateway) Stop()
- type Info
- type InputService
- type Message
- type OutputConnection
- type OutputService
- type RawConnection
Constants ¶
This section is empty.
Variables ¶
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(¤tGenerated, 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.
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) 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 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.
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 (*Gateway) ErrorsChan ¶
ErrorsChan return connection error chan.
func (*Gateway) MessagesChan ¶
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.
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 ¶
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