pillx

package
v0.0.0-...-c14cf11 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2017 License: GPL-2.0 Imports: 28 Imported by: 0

Documentation

Overview

server

Index

Constants

View Source
const GATEWAY_VERSION uint8 = 0x01
View Source
const PILL_VERSION uint8 = 0x01
View Source
const PROTO_HEADER_FIRSTCHAR = 0x7f
View Source
const Protocal_Error_TYPE_COMMON uint8 = 1
View Source
const Protocal_Error_TYPE_DISCONNECT uint8 = 2
View Source
const SYS_BIND_UID uint16 = 0x0033
View Source
const SYS_CLIENT_DISCONNECT uint16 = 0x0006
View Source
const SYS_CLIENT_DISCONNECT_WORKER uint16 = 0x0042
View Source
const SYS_CONNECT_HANDSHAKE_ERROR uint16 = 0X0003
View Source
const SYS_CONNECT_MARK_ERROR uint16 = 0X0001
View Source
const SYS_CONNECT_SIZE_ERROR uint16 = 0X0002
View Source
const SYS_CONNECT_WORKER_ERROR uint16 = 0X0004
View Source
const SYS_GET_ALL_CLIENT_INFO uint16 = 0x0031
View Source
const SYS_GET_CLIENT_COUNT_BY_GROUP uint16 = 0x0040
View Source
const SYS_GET_CLIENT_ID_BY_UID uint16 = 0x0041
View Source
const SYS_GET_CLINET_INFO_BY_GROUP uint16 = 0x0039
View Source
const SYS_IS_ONLINE uint16 = 0x0032
View Source
const SYS_JOIN_GROUP uint16 = 0x0036
View Source
const SYS_KICK uint16 = 0x0008
View Source
const SYS_LEAVE_GROUP uint16 = 0x0037
View Source
const SYS_ON_BAN uint16 = 0X0015 //禁言
View Source
const SYS_ON_BLACK uint16 = 0X0017 //黑名单
View Source
const SYS_ON_BLOCK uint16 = 0X0014 //屏蔽
View Source
const SYS_ON_CLIENTIN uint16 = 0X0019 //广播进入
View Source
const SYS_ON_CLIENTOUT uint16 = 0X0020 //广播退出
View Source
const SYS_ON_CLOSE uint16 = 0X0012
View Source
const SYS_ON_CONNECT uint16 = 0X0010
View Source
const SYS_ON_HANDSHAKE uint16 = 0X0013
View Source
const SYS_ON_HEARTBEAT uint16 = 0X0018 //心跳
View Source
const SYS_ON_KICK uint16 = 0X0016 //踢
View Source
const SYS_ON_MESSAGE uint16 = 0X0011
View Source
const SYS_SEND_TO_ALL uint16 = 0x0009
View Source
const SYS_SEND_TO_GROUP uint16 = 0x0038
View Source
const SYS_SEND_TO_ONE uint16 = 0x0007
View Source
const SYS_SEND_TO_UID uint16 = 0x0035
View Source
const SYS_UNBIND_UID uint16 = 0x0034
View Source
const SYS_UPDATE_SESSION uint16 = 0x0030
View Source
const SYS_WORKER_DISCONNECT uint16 = 0x0005

Variables

View Source
var (

	//黑名单
	BlackIdMap = make(map[int32]bool)
	AdminIdMap = make(map[int32]bool)

	//最近聊天内容纪录
	RecentMsgChan = NewMsgList(256)
)
View Source
var (

	//GatewayPools  map[string]Pool      = make(map[string]Pool)
	WorkerClients                      = make(map[uint64]*WorkerClient)
	Gateways      map[string]*Response = make(map[string]*Response)
)
View Source
var (
	DefaultHammerTime time.Duration = 60 * time.Second
)
View Source
var (
	// ErrClosed is the error resulting if the pool is closed via pool.Close().
	ErrClosed = errors.New("pool is closed")
)

Functions

func MyLog

func MyLog() *log.Entry

func SendAllGateWay

func SendAllGateWay(resMap map[string]*Response, msg interface{})

func SendAllGateWayPool

func SendAllGateWayPool(gatewayPools map[string]Pool, msg interface{})

func SetLogFormat

func SetLogFormat(format string)

Types

type Channel

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

func NewChannel

func NewChannel(name string) *Channel

func (*Channel) Block

func (channel *Channel) Block(client *Response, blockClientId uint64)

func (*Channel) BlockUid

func (channel *Channel) BlockUid(uid int32, blockUid int32)

func (*Channel) Publish

func (channel *Channel) Publish(client *Response, message interface{})

func (*Channel) Subscribe

func (channel *Channel) Subscribe(client *Response)

func (*Channel) UnSubscribe

func (channel *Channel) UnSubscribe(client *Response)

type Client

type Client struct {
	Addr string
}

func (*Client) Dail

func (client *Client) Dail() (p Pool, err error)

type ClientData

type ClientData struct {
	Uid      int32
	RoomId   int32
	Channel  *Channel
	Platform int32
	Ip       string
	Uname    string
}

type ClientDataStruct

type ClientDataStruct struct {
	Uid      int
	RoomId   int
	Channel  *Channel
	Platform int
	Ip       string
	Uname    string
}

type Conn

type Conn struct {
	Id uint64

	HandshakeFlg bool //是否已经通过握手验证
	// contains filtered or unexported fields
}

A conn represents the server side of connection.

type Factory

type Factory func() (*PoolConn, error)

Factory is a function to create new connections.

type GateWayProtocol

type GateWayProtocol struct {
	Header *GatewayHeader
	//IPHeader *GatewayParamsHeader
	//IP       []byte
	//DataHeader *GatewayParamsHeader
	Content []byte
}

func NewGatewayProtocol

func NewGatewayProtocol() (protocol *GateWayProtocol)

func (*GateWayProtocol) Analyze

func (gateway *GateWayProtocol) Analyze(client *Response) (err error)

func (*GateWayProtocol) Decode

func (gateway *GateWayProtocol) Decode(buf []byte) (err error)

func (*GateWayProtocol) Encode

func (gateway *GateWayProtocol) Encode(msg interface{}) (buf []byte, err error)

func (*GateWayProtocol) GetCmd

func (gateway *GateWayProtocol) GetCmd() (cmd uint16)

func (*GateWayProtocol) New

func (req *GateWayProtocol) New() (protocol IProtocol)

func (*GateWayProtocol) SetCmd

func (gateway *GateWayProtocol) SetCmd(cmd uint16)

type Gateway

type Gateway struct {
	InnerAddr     string
	OuterAddr     string
	OuterProtocol IProtocol
	InnerServer   *Server
	OuterServer   *Server
	EtcdClient    *etcd.Client
	GatewayName   string
	WatchName     string
}

func NewGateway

func NewGateway(innerAddr string, outProtocol IProtocol) *Gateway

func (*Gateway) ConnectWorkers

func (gateway *Gateway) ConnectWorkers()

func (*Gateway) Init

func (gateway *Gateway) Init()

type GatewayHeader

type GatewayHeader struct {
	Mark     uint8
	Version  uint8
	Cmd      uint16
	Sid      uint32
	ClientId uint64
	Error    uint16
	Size     uint16
}

type GatewayParamsHeader

type GatewayParamsHeader struct {
	Type uint8 //1.开始2.中断3.结束3.参数8.data
	Size uint16
}

将来以此为核心,讲gatewayprams作为一个数组,将获取到的值放入一个map中

type GatewayWebsocket

type GatewayWebsocket struct {
	InnerAddr     string
	OuterAddr     string
	OuterProtocol IProtocol
	InnerServer   *Server
	OuterServer   *Server
	EtcdClient    *etcd.Client
	GatewayName   string
	WatchName     string
}

func (*GatewayWebsocket) Init

func (gateway *GatewayWebsocket) Init()

type Handler

type Handler interface {
	Serve(*Response, IProtocol)
}

handler接口,ServeRouter类实现了此serve方法

type HandlerFunc

type HandlerFunc func(*Response, IProtocol)

这里将HandlerFunc定义为一个函数类型,因此以后当调用a = HandlerFunc(f)之后, 调用a的serve实际上就是调用f的对应方法, 拥有相同参数和相同返回值的函数属于同一种类型。

func (HandlerFunc) Serve

func (f HandlerFunc) Serve(w *Response, r IProtocol)

Serve calls f(w, r).

type IProtocol

type IProtocol interface {
	Analyze(client *Response) (err error)
	Encode(msg interface{}) (buf []byte, err error)
	Decode(buf []byte) (err error)
	GetCmd() (cmd uint16)
	New() (protocol IProtocol)
}

type MsgList

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

func NewMsgList

func NewMsgList(max int) *MsgList

func (*MsgList) GetList

func (msglist *MsgList) GetList() *list.List

func (*MsgList) GetSlice

func (msglist *MsgList) GetSlice() (slice []interface{})

func (*MsgList) Push

func (msglist *MsgList) Push(msg interface{})

type OpcodeHandler

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

type PillProtocol

type PillProtocol struct {
	Header  *PillProtocolHeader
	Content []byte
}

func NewPillProtocol

func NewPillProtocol() (protocol *PillProtocol)

func (*PillProtocol) Analyze

func (req *PillProtocol) Analyze(client *Response) (err error)

func (*PillProtocol) Decode

func (req *PillProtocol) Decode(buf []byte) (err error)

func (*PillProtocol) Encode

func (req *PillProtocol) Encode(msg interface{}) (buf []byte, err error)

func (*PillProtocol) GetCmd

func (req *PillProtocol) GetCmd() (cmd uint16)

func (*PillProtocol) New

func (req *PillProtocol) New() (protocol IProtocol)

func (*PillProtocol) SetCmd

func (req *PillProtocol) SetCmd(cmd uint16)

type PillProtocolHeader

type PillProtocolHeader struct {
	Mark    uint8
	Version uint8
	Cmd     uint16
	Sid     uint32
	Error   uint16
	Size    uint16
}

type Pool

type Pool interface {
	// Get returns a new connection from the pool. Closing the connections puts
	// it back to the Pool. Closing it when the pool is destroyed or full will
	// be counted as an error.
	Get() (*PoolConn, error)

	// Close closes the pool and all its connections. After Close() the pool is
	// no longer usable.
	Close()

	// Len returns the current number of connections of the pool.
	Len() int
}

Pool interface describes a pool implementation. A pool should have maximum capacity. An ideal pool is threadsafe and easy to use.

func GetPool

func GetPool(workerPools map[string]Pool) (wp Pool, key string)

func NewChannelPool

func NewChannelPool(initialCap, maxCap int, factory Factory) (Pool, error)

NewChannelPool returns a new pool based on buffered channels with an initial capacity and maximum capacity. Factory is used when initial capacity is greater than zero to fill the pool. A zero initialCap doesn't fill the Pool until a new Get() is called. During a Get(), If there is no new connection available in the pool, a new connection will be created via the Factory() method.

type PoolConn

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

PoolConn is a wrapper around net.Conn to modify the the behavior of net.Conn's Close() method.

func (*PoolConn) Close

func (p *PoolConn) Close() error

Close() puts the given connects back to the pool instead of closing it.

func (*PoolConn) MarkUnusable

func (p *PoolConn) MarkUnusable()

MarkUnusable() marks the connection not usable any more, to let the pool close it instead of returning it to pool.

type ProtocalError

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

func (*ProtocalError) Error

func (e *ProtocalError) Error() string

type Response

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

A response represents the server side of aresponse.

func GetResponse

func GetResponse(clientId uint64, resMap map[string]*Response) (ip string, res *Response)

func (*Response) Close

func (response *Response) Close()

func (*Response) GetConn

func (response *Response) GetConn() *Conn

func (*Response) Send

func (response *Response) Send(msg interface{}) (n int, err error)

func (*Response) SendContent

func (response *Response) SendContent(content []byte)

func (*Response) Write

func (response *Response) Write(data []byte) (n int, err error)

type ServeRouter

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

func NewServeRouter

func NewServeRouter() *ServeRouter

NewServeRouter allocates and returns a new ServeRouter.

func (*ServeRouter) Handle

func (router *ServeRouter) Handle(name uint16, handler Handler)

将router对应的opcode,方法存储

func (*ServeRouter) Serve

func (router *ServeRouter) Serve(w *Response, r IProtocol)

取出opcode对应的操作方法,然后回调

type Server

type Server struct {
	Addr     string  // TCP address to listen on, ":http" if empty
	Handler  Handler // handler to invoke, http.DefaultServeMux if nil
	Protocol IProtocol

	// ErrorLog specifies an optional logger for errors accepting
	// connections and unexpected behavior from handlers.
	// If nil, logging goes to os.Stderr via the log package's
	// standard logger.
	ErrorLog *log.Logger
}

func NewGatewayClient

func NewGatewayClient(addr string) *Server

func (*Server) Dial

func (client *Server) Dial() (response *Response)

func (*Server) HandleFunc

func (srv *Server) HandleFunc(name uint16, handler func(*Response, IProtocol))

注册路由相应处理方法

func (*Server) ListenAndServe

func (srv *Server) ListenAndServe() error

ListenAndServe listens on the TCP network address srv.Addr and then calls Serve to handle requests on incoming connections. If srv.Addr is blank, ":5917" is used.

func (*Server) ListenAndServeUdp

func (srv *Server) ListenAndServeUdp() error

func (*Server) Serve

func (srv *Server) Serve(l net.Listener) error

Serve accepts incoming connections on the Listener l, creating a new service goroutine for each. The service goroutines read requests and then call srv.Handler to reply to them.

type ServerHandler

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

用来获得当前的router并回调相应的处理方法

type TextProtocol

type TextProtocol struct {
	Content []byte
}

func (*TextProtocol) Analyze

func (req *TextProtocol) Analyze(client *Response) (err error)

func (*TextProtocol) Decode

func (req *TextProtocol) Decode(buf []byte) (err error)

func (*TextProtocol) Encode

func (req *TextProtocol) Encode(msg interface{}) (buf []byte, err error)

func (*TextProtocol) GetCmd

func (req *TextProtocol) GetCmd() (cmd uint16)

func (*TextProtocol) New

func (req *TextProtocol) New() (protocol IProtocol)

type WebSocketHeader

type WebSocketHeader struct {
	OpcodeByte  byte
	PayloadByte byte
}

type WebSocketProtocol

type WebSocketProtocol struct {
	Header  *WebSocketHeader
	Content []byte
}

func (*WebSocketProtocol) Analyze

func (websocket *WebSocketProtocol) Analyze(client *Response) (err error)

func (*WebSocketProtocol) Decode

func (req *WebSocketProtocol) Decode(buf []byte) (err error)

func (*WebSocketProtocol) Encode

func (gateway *WebSocketProtocol) Encode(msg interface{}) (buf []byte, err error)

func (*WebSocketProtocol) GetCmd

func (req *WebSocketProtocol) GetCmd() (cmd uint16)

func (*WebSocketProtocol) Handshake

func (this *WebSocketProtocol) Handshake(client *Response) (bool, []byte)

func (*WebSocketProtocol) New

func (this *WebSocketProtocol) New() (protocol IProtocol)

func (*WebSocketProtocol) SetCmd

func (req *WebSocketProtocol) SetCmd(cmd uint16)

type Worker

type Worker struct {
	InnerAddr   string
	InnerServer *Server
	EtcdClient  *etcd.Client
	WorkerName  string
	WatchName   string
}

func NewWorker

func NewWorker(name string) *Worker

func (*Worker) Init

func (worker *Worker) Init()

func (*Worker) Watch

func (worker *Worker) Watch()

type WorkerClient

type WorkerClient struct {
	IP string
}

Jump to

Keyboard shortcuts

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