1
This commit is contained in:
@@ -1,4 +1,58 @@
|
||||
package entity
|
||||
|
||||
import (
|
||||
"blazing/common/socket/cmd"
|
||||
"blazing/common/socket/handler"
|
||||
"bytes"
|
||||
|
||||
"github.com/lunixbochs/struc"
|
||||
"github.com/panjf2000/gnet/v2"
|
||||
)
|
||||
|
||||
type Player struct {
|
||||
UserID uint32 //用户ID
|
||||
Conn gnet.Conn
|
||||
}
|
||||
|
||||
// PlayerOption 定义配置 Player 的函数类型
|
||||
type PlayerOption func(*Player)
|
||||
|
||||
// WithUserID 设置用户ID的选项函数
|
||||
func WithUserID(userID uint32) PlayerOption {
|
||||
return func(p *Player) {
|
||||
p.UserID = userID
|
||||
}
|
||||
}
|
||||
func WithConn(Conn gnet.Conn) PlayerOption {
|
||||
return func(p *Player) {
|
||||
p.Conn = Conn
|
||||
}
|
||||
}
|
||||
|
||||
// NewPlayer 使用 Options 模式创建 Player 实例
|
||||
func NewPlayer(opts ...PlayerOption) *Player {
|
||||
p := &Player{}
|
||||
for _, opt := range opts {
|
||||
opt(p)
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
func (p *Player) GetUserID() uint32 {
|
||||
return p.UserID
|
||||
}
|
||||
|
||||
func (p *Player) SendPackBytes(cmd cmd.EnumCommandID, data []byte) { //组包
|
||||
|
||||
head1 := handler.TomeeHeader{}
|
||||
head1.CMDID = uint32(cmd)
|
||||
head1.Len = uint32(len(data) + 17)
|
||||
head1.Version = "7"
|
||||
head1.UserID = p.UserID
|
||||
head1.Result = 0
|
||||
var data1 bytes.Buffer
|
||||
struc.Pack(&data1, head1)
|
||||
p.Conn.Write(data1.Bytes()) //写入头部
|
||||
p.Conn.Write(data) //写入数据
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user