network

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2022 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// IdleTime If no data is sent to the server within 60 seconds, the connection will be forcibly closed
	IdleTime = 60
)

Variables

View Source
var (
	// ErrServerClosed occurs when heartbeat detection the connection is not active
	ErrServerClosed = errors.New("server closed idle connection")
	// ErrClientClosed occurs when client close the connection
	ErrClientClosed = errors.New("client closed")
)

Functions

func SetLogger

func SetLogger(l Logger)

SetLogger custom logger

Types

type CMD

type CMD uint16
const (
	Heartbeat CMD = iota
	Ack
	Single
	All
)

type Codec

type Codec interface {
	// Encode encodes data into []byte
	// Returns error when error occurred
	Encode(v interface{}) ([]byte, error)

	// Decode decodes data into v
	// Returns error when error occurred
	Decode(data []byte, v interface{}) error
}

type Conn

type Conn struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Conn defines parameters for accept an client

func (*Conn) Close

func (c *Conn) Close()

Close the client connection

func (*Conn) GetClientIP

func (c *Conn) GetClientIP() net.Addr

GetClientIP get client IP

func (*Conn) GetExtraMap

func (c *Conn) GetExtraMap(k string) interface{}

GetExtraMap get the extra data

func (*Conn) GetRawConn

func (c *Conn) GetRawConn() net.Conn

GetRawConn get the raw net.Conn from the client connection

func (*Conn) SendAll

func (c *Conn) SendAll(msg *Message)

SendAll send message to all

func (*Conn) SendBytes

func (c *Conn) SendBytes(cmd CMD, b []byte)

SendBytes send bytes

func (*Conn) SendMessage

func (c *Conn) SendMessage(msg *Message)

SendMessage send message into channel

func (*Conn) SendSingle

func (c *Conn) SendSingle(sid string, msg *Message)

SendSingle send message to single

func (*Conn) SetExtraMap

func (c *Conn) SetExtraMap(k string, v interface{})

SetExtraMap set the extra data

type DefaultLogger

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

func (*DefaultLogger) Debug

func (d *DefaultLogger) Debug(v ...interface{})

Debug logs a message at level Debug on the standard logger

func (*DefaultLogger) Debugf

func (d *DefaultLogger) Debugf(format string, v ...interface{})

Debugf logs a message at level Debug on the standard logger

func (*DefaultLogger) Error

func (d *DefaultLogger) Error(v ...interface{})

Error logs a message at level Error on the standard logger

func (*DefaultLogger) Errorf

func (d *DefaultLogger) Errorf(format string, v ...interface{})

Errorf logs a message at level Error on the standard logger

func (*DefaultLogger) Fatal

func (d *DefaultLogger) Fatal(v ...interface{})

Fatal logs a message at level Fatal on the standard logger then the process will exit with status set to 1

func (*DefaultLogger) Fatalf

func (d *DefaultLogger) Fatalf(format string, v ...interface{})

Fatalf logs a message at level Fatal on the standard logger then the process will exit with status set to 1

func (*DefaultLogger) Info

func (d *DefaultLogger) Info(v ...interface{})

Info logs a message at level Info on the standard logger

func (*DefaultLogger) Infof

func (d *DefaultLogger) Infof(format string, v ...interface{})

Infof logs a message at level Info on the standard logger

func (*DefaultLogger) Panic

func (d *DefaultLogger) Panic(v ...interface{})

Panic logs a message at level Panic on the standard logger

func (*DefaultLogger) Panicf

func (d *DefaultLogger) Panicf(format string, v ...interface{})

Panicf logs a message at level Panic on the standard logger

type DefaultProtocol

type DefaultProtocol struct{}

DefaultProtocol is the default packet ╔═══════════╤════════╤═════════╗ ║ FIELD │ TYPE │ SIZE ║ ╠═══════════╪════════╪═════════╣ ║ Cmd │ uint16 │ 2 ║ ║ Size │ uint32 │ 4 ║ ║ Checksum │ uint32 │ 4 ║ ║ Data │ []byte │ dynamic ║ ╚═══════════╧════════╧═════════╝

func NewDefaultProtocol

func NewDefaultProtocol() *DefaultProtocol

NewDefaultProtocol create a *DefaultPacker with initial field value.

func (*DefaultProtocol) Pack

func (p *DefaultProtocol) Pack(msg *Message) ([]byte, error)

Pack encodes the message into bytes data

func (*DefaultProtocol) Unpack

func (p *DefaultProtocol) Unpack(reader io.Reader) (msg *Message, err error)

Unpack decodes the io.Reader into Message

type JSONCodec

type JSONCodec struct{}

JSONCodec implements the Codec interface

func (*JSONCodec) Decode

func (c *JSONCodec) Decode(data []byte, v interface{}) error

Decode implements the Codec Decode method

func (*JSONCodec) Encode

func (c *JSONCodec) Encode(v interface{}) ([]byte, error)

Encode implements the Codec Encode method

type Level

type Level uint8
const (
	DEBUG Level = iota
	INFO
	ERROR
	FATAL
	PANIC
)

type Logger

type Logger interface {
	Info(v ...interface{})
	Infof(format string, v ...interface{})
	Debug(v ...interface{})
	Debugf(format string, v ...interface{})
	Error(v ...interface{})
	Errorf(format string, v ...interface{})
	Fatal(v ...interface{})
	Fatalf(format string, v ...interface{})
	Panic(v ...interface{})
	Panicf(format string, v ...interface{})
}
var Flog Logger = newLogger()

type Message

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

func NewMessage

func NewMessage(cmd CMD, data []byte) *Message

func (*Message) Checksum

func (m *Message) Checksum() bool

func (*Message) GetCmd

func (m *Message) GetCmd() CMD

func (*Message) GetData

func (m *Message) GetData() []byte

func (*Message) GetSize

func (m *Message) GetSize() uint32

func (*Message) String

func (m *Message) String() string

type Option

type Option func(o *Options)

func WithHeartbeat

func WithHeartbeat(t time.Duration) Option

func WithLogger

func WithLogger(log Logger) Option

func WithTLS

func WithTLS(tls *tls.Config) Option

type Options

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

type PBCodec

type PBCodec struct{}

PBCodec implements the Codec interface

func (*PBCodec) Decode

func (c *PBCodec) Decode(data []byte, v interface{}) error

func (*PBCodec) Encode

func (c *PBCodec) Encode(v interface{}) ([]byte, error)

type Protocol

type Protocol interface {
	// Pack packs Message into the packet to be written
	Pack(msg *Message) ([]byte, error)

	// Unpack unpacks the message packet from reader
	Unpack(reader io.Reader) (*Message, error)
}

type Server

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

Server defines parameters for running an TCP network

func NewServer

func NewServer(addr string, opts ...Option) *Server

NewServer creates a new tcp network connection using the given net connection.

func (*Server) Accept

func (s *Server) Accept(ctx context.Context, listener net.Listener)

func (*Server) Heartbeat

func (s *Server) Heartbeat()

Heartbeat heartbeat detection

func (*Server) OnClose

func (s *Server) OnClose(callback func(c *Conn, err error))

OnClose close callbacks on a connection, it will be called before closing

func (*Server) OnConnect

func (s *Server) OnConnect(callback func(c *Conn))

OnConnect connect callbacks on a connection

func (*Server) OnMessage

func (s *Server) OnMessage(callback func(c *Conn, msg *Message))

OnMessage receive callbacks on a connection

func (*Server) Start

func (s *Server) Start()

Start create a TCP network listener to accept client connection

func (*Server) Stop

func (s *Server) Stop()

Stop Close server

type Session

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

Session struct

func NewSession

func NewSession(conn *Conn) *Session

NewSession create a new session

func (*Session) BindUserID

func (s *Session) BindUserID(uid string)

BindUserID bind a user ID to session

func (*Session) GetConn

func (s *Session) GetConn() *Conn

GetConn get Conn pointer

func (*Session) GetExtraMap

func (s *Session) GetExtraMap(key string) interface{}

GetExtraMap get the extra data

func (*Session) GetSessionID

func (s *Session) GetSessionID() string

GetSessionID get a session ID

func (*Session) GetUserID

func (s *Session) GetUserID() string

GetUserID get user ID

func (*Session) SetConn

func (s *Session) SetConn(conn *Conn)

SetConn set a Conn to session

func (*Session) SetExtraMap

func (s *Session) SetExtraMap(key string, value interface{})

SetExtraMap set the extra data

func (*Session) UpdateTime

func (s *Session) UpdateTime()

UpdateTime update the message last time

Jump to

Keyboard shortcuts

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