98 lines
2.3 KiB
Go
98 lines
2.3 KiB
Go
package main
|
|
|
|
import (
|
|
"crypto/md5"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"math/rand"
|
|
"mongo.games.com/game/common"
|
|
"mongo.games.com/game/model"
|
|
loginproto "mongo.games.com/game/protocol/login"
|
|
"mongo.games.com/goserver/core/logger"
|
|
"mongo.games.com/goserver/core/netlib"
|
|
"strconv"
|
|
"sync/atomic"
|
|
"time"
|
|
)
|
|
|
|
/*
|
|
添加到客户端管理器,管理器负责登录
|
|
当连接断开时,从管理器中移除,判断是否需要重连
|
|
*/
|
|
|
|
const (
|
|
GateSessionHandlerName = "handler-gate-session"
|
|
)
|
|
|
|
type GateSessionHandler struct {
|
|
netlib.BasicSessionHandler
|
|
}
|
|
|
|
func (g *GateSessionHandler) GetName() string {
|
|
return GateSessionHandlerName
|
|
}
|
|
|
|
func (g *GateSessionHandler) GetInterestOps() uint {
|
|
return 1<<netlib.InterestOps_Opened |
|
|
1<<netlib.InterestOps_Closed
|
|
}
|
|
|
|
func (g *GateSessionHandler) OnSessionOpened(s *netlib.Session) {
|
|
// 登录账号
|
|
StartLogin(s)
|
|
return
|
|
}
|
|
|
|
func (g *GateSessionHandler) OnSessionClosed(s *netlib.Session) {
|
|
|
|
}
|
|
|
|
func init() {
|
|
netlib.RegisteSessionHandlerCreator(GateSessionHandlerName, func() netlib.SessionHandler {
|
|
return &GateSessionHandler{}
|
|
})
|
|
}
|
|
|
|
var UserNameIndex int64
|
|
|
|
func StartLogin(s *netlib.Session) {
|
|
ts := time.Now().UnixNano()
|
|
username := fmt.Sprintf("benchmark-%v", atomic.AddInt64(&UserNameIndex, 1))
|
|
s.SetAttribute(SessionAttributeClientAccountId, username)
|
|
csLogin := &loginproto.CSLogin{
|
|
Username: username,
|
|
TimeStamp: ts,
|
|
Platform: "1",
|
|
Channel: "",
|
|
PlatformTag: "test.win88.yy_android",
|
|
}
|
|
|
|
params := &model.PlayerParams{
|
|
Ip: fmt.Sprintf("%v.%v.%v.%v", 1+rand.Int31n(255), 1+rand.Int31n(255), 1+rand.Int31n(255), 1+rand.Int31n(255)),
|
|
City: "北京",
|
|
Platform: 1,
|
|
Logininmodel: "app",
|
|
}
|
|
data, err := json.Marshal(params)
|
|
if err == nil {
|
|
csLogin.Params = string(data[:])
|
|
}
|
|
|
|
h := md5.New()
|
|
io.WriteString(h, fmt.Sprintf("%v%v", username, Config.AppId))
|
|
pwd := hex.EncodeToString(h.Sum(nil))
|
|
h.Reset()
|
|
io.WriteString(h, fmt.Sprintf("%v%v%v", pwd, Config.AppId, ts))
|
|
pwd = hex.EncodeToString(h.Sum(nil))
|
|
csLogin.Password = pwd
|
|
|
|
csLogin.LoginType = 0
|
|
csLogin.Sign = common.MakeMd5String(csLogin.GetUsername(), csLogin.GetPassword(),
|
|
strconv.Itoa(int(csLogin.GetTimeStamp())), csLogin.GetParams(), Config.AppId)
|
|
|
|
s.Send(int(loginproto.LoginPacketID_PACKET_CS_LOGIN), csLogin)
|
|
logger.Logger.Infof("账号 [%v] 开始登录", username)
|
|
}
|