package main

import (
	"mongo.games.com/game/common"
	"mongo.games.com/game/protocol/login"
	"mongo.games.com/goserver/core/logger"
	"mongo.games.com/goserver/core/netlib"
	"mongo.games.com/goserver/srvlib"
)

func init() {
	netlib.RegisterFactory(int(login.GatePacketID_PACKET_SS_DICONNECT), netlib.PacketFactoryWrapper(func() interface{} {
		return &login.SSDisconnect{}
	}))

	netlib.RegisterHandler(int(login.GatePacketID_PACKET_SS_DICONNECT), netlib.HandlerWrapper(func(s *netlib.Session, packetid int, pack interface{}) error {
		logger.Logger.Trace("receive SSDisconnect", pack)
		if ssdis, ok := pack.(*login.SSDisconnect); ok {
			client := srvlib.ClientSessionMgrSington.GetSession(ssdis.GetSessionId())
			if client != nil {
				client.SetAttribute(common.ClientSessionAttribute_State, common.ClientState_Logouted)
				if ssdis.GetType() != 2 { //非异常情况要告知客户端什么原因断线
					client.Send(int(login.GatePacketID_PACKET_SS_DICONNECT), ssdis)
				}
				client.Close()
			}
		}
		return nil
	}))
}
