加入网络层
This commit is contained in:
41
Server/common/net/grpc/grpc_conn/conn.go
Normal file
41
Server/common/net/grpc/grpc_conn/conn.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package grpc_conn
|
||||
|
||||
import (
|
||||
"common/log"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/keepalive"
|
||||
"time"
|
||||
)
|
||||
|
||||
type GrpcConnection struct {
|
||||
sid string
|
||||
conn *grpc.ClientConn
|
||||
}
|
||||
|
||||
func NewGrpcConnection(sid, address string) (*GrpcConnection, error) {
|
||||
p := &GrpcConnection{
|
||||
sid: sid,
|
||||
}
|
||||
conn, err := grpc.Dial(
|
||||
address,
|
||||
grpc.WithInsecure(),
|
||||
grpc.WithKeepaliveParams(
|
||||
keepalive.ClientParameters{
|
||||
Time: 30 * time.Second, // 保活探测包发送的时间间隔
|
||||
Timeout: 10 * time.Second, // 保活探测包的超时时间
|
||||
PermitWithoutStream: true,
|
||||
},
|
||||
),
|
||||
//grpc.WithStatsHandler(&StatsHandler{}),
|
||||
)
|
||||
if err != nil {
|
||||
log.Errorf("create grpc err: %v, sid: %v, addr: %v", err, sid, address)
|
||||
return nil, err
|
||||
}
|
||||
p.conn = conn
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func (g *GrpcConnection) GetConnection() *grpc.ClientConn {
|
||||
return g.conn
|
||||
}
|
||||
62
Server/common/net/grpc/grpc_conn/conn_mgr.go
Normal file
62
Server/common/net/grpc/grpc_conn/conn_mgr.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package grpc_conn
|
||||
|
||||
import (
|
||||
"common/log"
|
||||
"fmt"
|
||||
"google.golang.org/grpc"
|
||||
"math/rand"
|
||||
)
|
||||
|
||||
type GrpcConnectionMgr struct {
|
||||
poolM map[string]*GrpcConnection
|
||||
poolS []*GrpcConnection
|
||||
}
|
||||
|
||||
func NewGrpcConnectionMgr() *GrpcConnectionMgr {
|
||||
return &GrpcConnectionMgr{
|
||||
poolM: make(map[string]*GrpcConnection),
|
||||
poolS: make([]*GrpcConnection, 0),
|
||||
}
|
||||
}
|
||||
|
||||
func (p *GrpcConnectionMgr) Store(sid, addr string) {
|
||||
pool, err := NewGrpcConnection(sid, addr)
|
||||
if err != nil {
|
||||
log.Errorf("create grpc err: %v, sid: %v, addr: %v", err, sid, addr)
|
||||
return
|
||||
}
|
||||
p.poolM[sid] = pool
|
||||
p.poolS = append(p.poolS, pool)
|
||||
}
|
||||
|
||||
func (p *GrpcConnectionMgr) Delete(sid string) int {
|
||||
delete(p.poolM, sid)
|
||||
for i, pool := range p.poolS {
|
||||
if pool.sid == sid {
|
||||
p.poolS = append(p.poolS[:i], p.poolS[i+1:]...)
|
||||
break
|
||||
}
|
||||
}
|
||||
return len(p.poolS)
|
||||
}
|
||||
|
||||
func (p *GrpcConnectionMgr) Load(sid ...string) (*grpc.ClientConn, error) {
|
||||
var pool *GrpcConnection
|
||||
if len(sid) > 0 && len(sid[0]) > 0 {
|
||||
pool = p.poolM[sid[0]]
|
||||
} else {
|
||||
pool = p.poolS[rand.Intn(len(p.poolS))]
|
||||
}
|
||||
if pool == nil {
|
||||
return nil, fmt.Errorf("cannot find connection")
|
||||
}
|
||||
return pool.GetConnection(), nil
|
||||
}
|
||||
|
||||
func (p *GrpcConnectionMgr) LoadAll() map[string]*grpc.ClientConn {
|
||||
sidM := make(map[string]*grpc.ClientConn)
|
||||
for sid, pool := range p.poolM {
|
||||
sidM[sid] = pool.GetConnection()
|
||||
}
|
||||
return sidM
|
||||
}
|
||||
45
Server/common/net/grpc/grpc_conn/stats.go
Normal file
45
Server/common/net/grpc/grpc_conn/stats.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package grpc_conn
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"google.golang.org/grpc/stats"
|
||||
)
|
||||
|
||||
// 1. 实现 stats.Handler 接口
|
||||
type StatsHandler struct{}
|
||||
|
||||
func (h *StatsHandler) TagRPC(ctx context.Context, info *stats.RPCTagInfo) context.Context {
|
||||
// 给 RPC 调用打标签(例如记录方法名)
|
||||
return context.WithValue(ctx, "rpc_method", info.FullMethodName)
|
||||
}
|
||||
|
||||
func (h *StatsHandler) HandleRPC(ctx context.Context, s stats.RPCStats) {
|
||||
// 处理 RPC 统计信息
|
||||
switch t := s.(type) {
|
||||
case *stats.Begin:
|
||||
fmt.Printf("RPC started: %s\n", ctx.Value("rpc_method"))
|
||||
case *stats.End:
|
||||
fmt.Printf("RPC finished: %s (duration: %v)\n",
|
||||
ctx.Value("rpc_method"), t.EndTime.Sub(t.BeginTime))
|
||||
case *stats.InPayload:
|
||||
fmt.Printf("Received %d bytes\n", t.WireLength)
|
||||
case *stats.OutPayload:
|
||||
fmt.Printf("Sent %d bytes\n", t.WireLength)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *StatsHandler) TagConn(ctx context.Context, info *stats.ConnTagInfo) context.Context {
|
||||
// 给连接打标签
|
||||
return ctx
|
||||
}
|
||||
|
||||
func (h *StatsHandler) HandleConn(ctx context.Context, s stats.ConnStats) {
|
||||
// 处理连接事件
|
||||
switch s.(type) {
|
||||
case *stats.ConnBegin:
|
||||
fmt.Println("Connection established")
|
||||
case *stats.ConnEnd:
|
||||
fmt.Println("Connection closed")
|
||||
}
|
||||
}
|
||||
24
Server/common/net/grpc/service/client_gateway.go
Normal file
24
Server/common/net/grpc/service/client_gateway.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"common/discover"
|
||||
"common/discover/common"
|
||||
"common/proto/gen/grpc_pb"
|
||||
)
|
||||
|
||||
func GatewayNewClient(sid ...string) (grpc_pb.GatewayClient, error) {
|
||||
c, err := discover.FindServer(common.KeyDiscoverGateway, sid...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return grpc_pb.NewGatewayClient(c), nil
|
||||
}
|
||||
|
||||
func GatewayNewBroadcastClient() map[string]grpc_pb.GatewayClient {
|
||||
clientM := make(map[string]grpc_pb.GatewayClient)
|
||||
connM := discover.FindServerAll(common.KeyDiscoverGateway)
|
||||
for sid, conn := range connM {
|
||||
clientM[sid] = grpc_pb.NewGatewayClient(conn)
|
||||
}
|
||||
return clientM
|
||||
}
|
||||
85
Server/common/net/grpc/service/service.go
Normal file
85
Server/common/net/grpc/service/service.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"common/discover"
|
||||
"common/log"
|
||||
"common/utils"
|
||||
"context"
|
||||
"fmt"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/keepalive"
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type IService interface {
|
||||
Init(addr string, port int)
|
||||
Close()
|
||||
}
|
||||
|
||||
type Base struct {
|
||||
Target string
|
||||
SID string
|
||||
Serve *grpc.Server
|
||||
EtcdTTL int64
|
||||
OnInit func(serve *grpc.Server)
|
||||
OnClose func()
|
||||
|
||||
wg *sync.WaitGroup
|
||||
}
|
||||
|
||||
func (s *Base) Init(addr string, port int) {
|
||||
s.wg = &sync.WaitGroup{}
|
||||
s.wg.Add(1)
|
||||
s.SID = utils.SnowflakeInstance().Generate().String()
|
||||
go func() {
|
||||
defer s.wg.Done()
|
||||
defer s.OnClose()
|
||||
defer discover.UnRegisterGrpcServer(s.SID)
|
||||
|
||||
// 监听端口
|
||||
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
|
||||
if err != nil {
|
||||
log.Errorf("%v ListenPort err: %v", s.Target, err)
|
||||
return
|
||||
}
|
||||
|
||||
s.Serve = grpc.NewServer(
|
||||
grpc.UnaryInterceptor(
|
||||
func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
log.Errorf("server Panic: %v", r)
|
||||
}
|
||||
}()
|
||||
resp, err = handler(ctx, req)
|
||||
return
|
||||
},
|
||||
),
|
||||
grpc.KeepaliveEnforcementPolicy(keepalive.EnforcementPolicy{
|
||||
MinTime: 20 * time.Second,
|
||||
PermitWithoutStream: true,
|
||||
}),
|
||||
)
|
||||
s.OnInit(s.Serve)
|
||||
|
||||
// 服务注册
|
||||
if err = discover.RegisterGrpcServer(s.Target, s.SID, fmt.Sprintf("%v:%d", addr, port), s.EtcdTTL); err != nil {
|
||||
log.Errorf("%v RegisterGrpcServer err: %v", s.Target, err)
|
||||
return
|
||||
}
|
||||
if err = s.Serve.Serve(lis); err != nil {
|
||||
log.Errorf("%v Serve err: %v", s.Target, err)
|
||||
return
|
||||
}
|
||||
log.Infof("%v server stop.", s.Target)
|
||||
}()
|
||||
}
|
||||
|
||||
func (s *Base) Close() {
|
||||
if s.Serve != nil {
|
||||
s.Serve.Stop()
|
||||
s.wg.Wait()
|
||||
}
|
||||
}
|
||||
33
Server/common/net/socket/server.go
Normal file
33
Server/common/net/socket/server.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package socket
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type Action int
|
||||
|
||||
const (
|
||||
// None indicates that no action should occur following an event.
|
||||
None Action = iota
|
||||
// Close closes the connection.
|
||||
Close
|
||||
// Shutdown shutdowns the engine.
|
||||
Shutdown
|
||||
)
|
||||
|
||||
// ISocketServer 由应用层实现
|
||||
type ISocketServer interface {
|
||||
OnOpen(ISocketConn) ([]byte, Action) // 开启连接
|
||||
OnHandShake(ISocketConn) // 开始握手
|
||||
OnMessage(ISocketConn, []byte) Action // 收到消息
|
||||
OnClose(ISocketConn, error) Action // 关闭连接
|
||||
OnTick() (time.Duration, Action)
|
||||
}
|
||||
|
||||
// ISocketConn 由网络层实现
|
||||
type ISocketConn interface {
|
||||
GetParam(key string) interface{}
|
||||
SetParam(key string, values interface{})
|
||||
Write(data []byte) error
|
||||
Close() error
|
||||
}
|
||||
196
Server/common/net/socket/websocket/websocket.go
Normal file
196
Server/common/net/socket/websocket/websocket.go
Normal file
@@ -0,0 +1,196 @@
|
||||
package websocket
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"common/net/socket"
|
||||
"context"
|
||||
"github.com/panjf2000/gnet/v2"
|
||||
"github.com/panjf2000/gnet/v2/pkg/logging"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// WSServer 实现GNet库接口
|
||||
type WSServer struct {
|
||||
gnet.BuiltinEventEngine
|
||||
eng gnet.Engine
|
||||
i socket.ISocketServer
|
||||
logger logging.Logger // 日志
|
||||
upgradeTimeout time.Duration // 升级超时时间
|
||||
unUpgradeConn sync.Map
|
||||
}
|
||||
|
||||
func NewWSServer(i socket.ISocketServer, logger logging.Logger, timeout time.Duration) *WSServer {
|
||||
if i == nil {
|
||||
return nil
|
||||
}
|
||||
return &WSServer{
|
||||
i: i,
|
||||
logger: logger,
|
||||
upgradeTimeout: timeout,
|
||||
unUpgradeConn: sync.Map{},
|
||||
}
|
||||
}
|
||||
|
||||
func (s *WSServer) Run(logger logging.Logger, addr string, multiCore, reusePort, tick, lockOSThread, reuseAddr bool, processNum int) error {
|
||||
return gnet.Run(
|
||||
s,
|
||||
addr,
|
||||
gnet.WithMulticore(multiCore),
|
||||
gnet.WithNumEventLoop(processNum),
|
||||
gnet.WithReusePort(reusePort),
|
||||
gnet.WithTicker(tick),
|
||||
gnet.WithLogger(logger),
|
||||
gnet.WithLockOSThread(lockOSThread),
|
||||
gnet.WithReuseAddr(reuseAddr),
|
||||
)
|
||||
}
|
||||
|
||||
func (s *WSServer) Stop() error {
|
||||
return s.eng.Stop(context.Background())
|
||||
}
|
||||
|
||||
func (s *WSServer) OnBoot(eng gnet.Engine) gnet.Action {
|
||||
s.eng = eng
|
||||
return gnet.None
|
||||
}
|
||||
|
||||
func (s *WSServer) OnOpen(c gnet.Conn) ([]byte, gnet.Action) {
|
||||
ws := &WSConn{
|
||||
Conn: c,
|
||||
isUpgrade: false,
|
||||
openTime: time.Now().Unix(),
|
||||
buf: bytes.Buffer{},
|
||||
logger: s.logger,
|
||||
wsMessageBuf: wsMessageBuf{
|
||||
curHeader: nil,
|
||||
cachedBuf: bytes.Buffer{},
|
||||
},
|
||||
param: make(map[string]interface{}),
|
||||
}
|
||||
c.SetContext(ws)
|
||||
s.unUpgradeConn.Store(c.RemoteAddr().String(), ws)
|
||||
d, a := s.i.OnOpen(ws)
|
||||
return d, (gnet.Action)(a)
|
||||
}
|
||||
|
||||
// OnClose fires when a connection has been closed.
|
||||
// The parameter err is the last known connection error.
|
||||
func (s *WSServer) OnClose(c gnet.Conn, err error) (action gnet.Action) {
|
||||
s.unUpgradeConn.Delete(c.RemoteAddr().String())
|
||||
tmp := c.Context()
|
||||
ws, ok := tmp.(*WSConn)
|
||||
if ok {
|
||||
s.logger.Warnf("connection close")
|
||||
return gnet.Action(s.i.OnClose(ws, err))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// OnTraffic fires when a local socket receives data from the peer.
|
||||
func (s *WSServer) OnTraffic(c gnet.Conn) (action gnet.Action) {
|
||||
tmp := c.Context()
|
||||
if tmp == nil {
|
||||
if s.logger != nil {
|
||||
s.logger.Errorf("OnTraffic context nil: %v", c)
|
||||
}
|
||||
action = gnet.Close
|
||||
return
|
||||
}
|
||||
|
||||
ws, ok := tmp.(*WSConn)
|
||||
if !ok {
|
||||
if s.logger != nil {
|
||||
s.logger.Errorf("OnTraffic convert ws error: %v", tmp)
|
||||
}
|
||||
action = gnet.Close
|
||||
return
|
||||
}
|
||||
|
||||
action = ws.readBytesBuf(c)
|
||||
if action != gnet.None {
|
||||
return
|
||||
}
|
||||
|
||||
if !ws.isUpgrade {
|
||||
var data []byte
|
||||
data, ok, action = ws.upgrade()
|
||||
if ok {
|
||||
s.unUpgradeConn.Delete(c.RemoteAddr().String())
|
||||
s.i.OnHandShake(ws)
|
||||
if data != nil {
|
||||
err := ws.Conn.AsyncWrite(data, nil)
|
||||
if err != nil {
|
||||
if ws.logger != nil {
|
||||
ws.logger.Errorf("update ws write upgrade protocol error", err)
|
||||
}
|
||||
action = gnet.Close
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
msg, err := ws.readWsMessages()
|
||||
if err != nil {
|
||||
if ws.logger != nil {
|
||||
ws.logger.Errorf("read ws messages errors", err)
|
||||
}
|
||||
return gnet.Close
|
||||
}
|
||||
|
||||
if msg != nil {
|
||||
for _, m := range msg {
|
||||
a := s.i.OnMessage(ws, m.Payload)
|
||||
if gnet.Action(a) != gnet.None {
|
||||
action = gnet.Action(a)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// OnTick fires immediately after the engine starts and will fire again
|
||||
// following the duration specified by the delay return value.
|
||||
func (s *WSServer) OnTick() (delay time.Duration, action gnet.Action) {
|
||||
now := time.Now().Unix()
|
||||
|
||||
delConn := make([]string, 0)
|
||||
s.unUpgradeConn.Range(func(key, value interface{}) bool {
|
||||
k, ok := key.(string)
|
||||
if !ok {
|
||||
return true
|
||||
}
|
||||
|
||||
v, ok := value.(*WSConn)
|
||||
if !ok {
|
||||
return true
|
||||
}
|
||||
if now-v.openTime >= int64(s.upgradeTimeout.Seconds()) {
|
||||
delConn = append(delConn, k)
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
for _, k := range delConn {
|
||||
wsConn, _ := s.unUpgradeConn.LoadAndDelete(k)
|
||||
if wsConn == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
v, ok := wsConn.(*WSConn)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if err := v.Close(); err != nil {
|
||||
if s.logger != nil {
|
||||
s.logger.Errorf("upgrade ws time out close socket error: %v", err)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
d, a := s.i.OnTick()
|
||||
delay = d
|
||||
action = gnet.Action(a)
|
||||
|
||||
return
|
||||
}
|
||||
263
Server/common/net/socket/websocket/wsconn.go
Normal file
263
Server/common/net/socket/websocket/wsconn.go
Normal file
@@ -0,0 +1,263 @@
|
||||
package websocket
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"errors"
|
||||
"github.com/gobwas/ws"
|
||||
"github.com/gobwas/ws/wsutil"
|
||||
"github.com/panjf2000/gnet/v2"
|
||||
"github.com/panjf2000/gnet/v2/pkg/logging"
|
||||
"io"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
// WSConn 实现ISocketConn接口
|
||||
type WSConn struct {
|
||||
gnet.Conn
|
||||
buf bytes.Buffer
|
||||
logger logging.Logger
|
||||
isUpgrade bool
|
||||
isClose bool
|
||||
param map[string]interface{}
|
||||
openTime int64
|
||||
wsMessageBuf
|
||||
}
|
||||
|
||||
type wsMessageBuf struct {
|
||||
curHeader *ws.Header
|
||||
cachedBuf bytes.Buffer
|
||||
}
|
||||
|
||||
type readWrite struct {
|
||||
io.Reader
|
||||
io.Writer
|
||||
}
|
||||
|
||||
func (w *WSConn) readBytesBuf(c gnet.Conn) gnet.Action {
|
||||
size := c.InboundBuffered()
|
||||
if size <= 0 {
|
||||
return gnet.None
|
||||
}
|
||||
buf := make([]byte, size)
|
||||
|
||||
read, err := c.Read(buf)
|
||||
if err != nil {
|
||||
if w.logger != nil {
|
||||
w.logger.Errorf("ws read bytes buf error", err)
|
||||
}
|
||||
return gnet.Close
|
||||
}
|
||||
|
||||
if read < size {
|
||||
if w.logger != nil {
|
||||
w.logger.Errorf("read bytes len err! size: %d read: %d", size, read)
|
||||
}
|
||||
return gnet.Close
|
||||
}
|
||||
w.buf.Write(buf)
|
||||
return gnet.None
|
||||
}
|
||||
|
||||
func (w *WSConn) upgrade() (data []byte, ok bool, action gnet.Action) {
|
||||
if w.isUpgrade {
|
||||
ok = true
|
||||
return
|
||||
}
|
||||
buf := &w.buf
|
||||
tmpReader := bytes.NewReader(buf.Bytes())
|
||||
oldLen := tmpReader.Len()
|
||||
result := &bytes.Buffer{}
|
||||
tempWriter := bufio.NewWriter(result)
|
||||
|
||||
var err error = nil
|
||||
up := ws.Upgrader{
|
||||
OnRequest: w.OnRequest,
|
||||
}
|
||||
_, err = up.Upgrade(readWrite{tmpReader, tempWriter})
|
||||
skipN := oldLen - tmpReader.Len()
|
||||
if err != nil {
|
||||
if errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF) { //数据不完整
|
||||
return
|
||||
}
|
||||
buf.Next(skipN)
|
||||
if w.logger != nil {
|
||||
w.logger.Errorf("ws upgrade error", err.Error())
|
||||
}
|
||||
|
||||
action = gnet.Close
|
||||
return
|
||||
}
|
||||
buf.Next(skipN)
|
||||
if w.logger != nil {
|
||||
w.logger.Infof("ws upgrade success conn upgrade websocket protocol!")
|
||||
}
|
||||
_ = tempWriter.Flush()
|
||||
data = result.Bytes()
|
||||
ok = true
|
||||
w.isUpgrade = true
|
||||
return
|
||||
}
|
||||
|
||||
func (w *WSConn) readWsMessages() (messages []wsutil.Message, err error) {
|
||||
in := &w.buf
|
||||
//messages, err = wsutil.ReadClientMessage(in, messages)
|
||||
//return
|
||||
for {
|
||||
if w.curHeader == nil {
|
||||
if in.Len() < ws.MinHeaderSize { //头长度至少是2
|
||||
return
|
||||
}
|
||||
var head ws.Header
|
||||
//有可能不完整,构建新的 reader 读取 head 读取成功才实际对 in 进行读操作
|
||||
tmpReader := bytes.NewReader(in.Bytes())
|
||||
oldLen := tmpReader.Len()
|
||||
head, err = ws.ReadHeader(tmpReader)
|
||||
skipN := oldLen - tmpReader.Len()
|
||||
if err != nil {
|
||||
if errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF) { //数据不完整
|
||||
return messages, nil
|
||||
}
|
||||
in.Next(skipN)
|
||||
return nil, err
|
||||
}
|
||||
_, err = io.CopyN(&w.cachedBuf, in, int64(skipN))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
//in.Next(skipN)
|
||||
|
||||
w.curHeader = &head
|
||||
//err = ws.WriteHeader(&msgBuf.cachedBuf, head)
|
||||
//if err != nil {
|
||||
// return nil, err
|
||||
//}
|
||||
}
|
||||
dataLen := (int)(w.curHeader.Length)
|
||||
if dataLen > 0 {
|
||||
if in.Len() >= dataLen {
|
||||
_, err = io.CopyN(&w.cachedBuf, in, int64(dataLen))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
} else { //数据不完整
|
||||
if w.logger != nil {
|
||||
w.logger.Debugf("ws read ws message incomplete data", in.Len(), dataLen)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
if w.curHeader.Fin { //当前 header 已经是一个完整消息
|
||||
messages, err = wsutil.ReadClientMessage(&w.cachedBuf, messages)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
w.cachedBuf.Reset()
|
||||
}
|
||||
w.curHeader = nil
|
||||
}
|
||||
}
|
||||
|
||||
func (w *WSConn) OnRequest(u []byte) error {
|
||||
parsedURL, err := url.Parse(string(u))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
w.SetParam("query", parsedURL.Query())
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *WSConn) GetParam(key string) interface{} {
|
||||
return w.param[key]
|
||||
}
|
||||
|
||||
func (w *WSConn) SetParam(key string, values interface{}) {
|
||||
w.param[key] = values
|
||||
}
|
||||
|
||||
func (w *WSConn) Write(data []byte) error {
|
||||
if w.isClose {
|
||||
return errors.New("connection has close")
|
||||
}
|
||||
return w.write(data, ws.OpBinary)
|
||||
}
|
||||
|
||||
func (w *WSConn) Close() (err error) {
|
||||
defer func(Conn gnet.Conn) {
|
||||
err = Conn.Close()
|
||||
}(w.Conn)
|
||||
return w.write(make([]byte, 0), ws.OpClose)
|
||||
}
|
||||
|
||||
func (w *WSConn) write(data []byte, opCode ws.OpCode) error {
|
||||
buf := bytes.Buffer{}
|
||||
err := wsutil.WriteServerMessage(&buf, opCode, data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return w.Conn.AsyncWrite(buf.Bytes(), nil)
|
||||
}
|
||||
|
||||
//func (w *WSConn) Pong(data []byte) (err error) {
|
||||
// buf := bytes.Buffer{}
|
||||
// if data == nil {
|
||||
// err = wsutil.WriteServerMessage(&buf, ws.OpPong, emptyPayload)
|
||||
// } else {
|
||||
// err = wsutil.WriteServerMessage(&buf, ws.OpPong, data)
|
||||
// }
|
||||
// if w.isClose {
|
||||
// return errors.New("connection has close")
|
||||
// }
|
||||
// if err != nil {
|
||||
// return
|
||||
// }
|
||||
// return w.Conn.AsyncWrite(buf.Bytes(), nil)
|
||||
//}
|
||||
|
||||
//func (w *WSConn) Ping(data []byte) (err error) {
|
||||
// buf := bytes.Buffer{}
|
||||
// if data == nil {
|
||||
// err = wsutil.WriteServerMessage(&buf, ws.OpPing, emptyPayload)
|
||||
// } else {
|
||||
// err = wsutil.WriteServerMessage(&buf, ws.OpPing, data)
|
||||
// }
|
||||
// if w.isClose {
|
||||
// return errors.New("connection has close")
|
||||
// }
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
//
|
||||
// return w.Conn.AsyncWrite(buf.Bytes(), nil)
|
||||
//}
|
||||
|
||||
//func (w *WSConn) GetProperty(key string) (interface{}, error) {
|
||||
// if w.context[key] == nil {
|
||||
// return nil, errors.New("not found this key")
|
||||
// }
|
||||
// return w.context[key], nil
|
||||
//}
|
||||
//
|
||||
//func (w *WSConn) SetProperty(key string, ctx interface{}) {
|
||||
// w.context[key] = ctx
|
||||
//}
|
||||
//
|
||||
//func (w *WSConn) RemoveProperty(key string) {
|
||||
// delete(w.context, key)
|
||||
//}
|
||||
//
|
||||
//func (w *WSConn) GetConnID() uint64 {
|
||||
// return w.connID
|
||||
//}
|
||||
//
|
||||
//func (w *WSConn) Clear() {
|
||||
// w.context = make(map[string]interface{})
|
||||
//}
|
||||
//
|
||||
//func (w *WSConn) GetUrlParam() map[string][]string {
|
||||
// return w.urlParma
|
||||
//}
|
||||
//
|
||||
//func (w *WSConn) SetUrlParam(key string, values []string) {
|
||||
// w.urlParma[key] = values
|
||||
//}
|
||||
Reference in New Issue
Block a user